Library /sys$common/syshlp/helplib.hlb  —  CRTL  closedir
    Closes directories.

    Format

      #include  <dirent.h>

      int closedir  (DIR *dir_pointer);

1  –  Argument

 dir_pointer

    Pointer to the dir structure of an open directory.

2  –  Description

    The closedir function closes a directory stream and frees
    the structure associated with the dir_pointer argument. Upon
    return, the value of dir_pointer does not necessarily point to an
    accessible object of the type DIR.

    The type DIR, which is defined in the <dirent.h> header file,
    represents a directory stream that is an ordered sequence of
    all the directory entries in a particular directory. Directory
    entries represent files. You can remove files from or add files
    to a directory asynchronously to the operation of the readdir
    function.

                                   NOTE

       An open directory must always be closed with the closedir
       function to ensure that the next attempt to open the
       directory is successful.

3  –  Example

      The following example shows how to search a directory for the
      entry name, using the opendir, readdir, and closedir functions:

        #include <dirent.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>

        #define FOUND     1
        #define NOT_FOUND 0

        static int dir_example(const char *name, unsigned int unix_style)
        {
            DIR *dir_pointer;
            struct dirent *dp;

            if ( unix_style )
                dir_pointer = opendir(".");
            else
                dir_pointer = opendir(getenv("PATH"));

            if ( !dir_pointer ) {
                perror("opendir");
                return NOT_FOUND;
            }

            /* Note, that if opendir() was called with UNIX style file  */
            /* spec like ".", readdir() will return only a single       */
            /* version of each file in the directory. In this case the  */
            /* name returned in d_name member of the dirent structure   */
            /* will contain only file name and file extension fields,   */
            /* both lowercased like "foo.bar".                          */

            /* If opendir() was called with OpenVMS style file spec,    */
            /* readdir() will return every version of each file in the  */
            /* directory. In this case the name returned in d_name      */
            /* member of the dirent structure will contain file name,   */
            /* file extension and file version fields. All in upper     */
            /* case, like "FOO.BAR;1".                                  */

            for ( dp = readdir(dir_pointer);
                  dp && strcmp(dp->d_name, name);
                  dp = readdir(dir_pointer) )
                ;

            closedir(dir_pointer);

            if ( dp != NULL )
                return FOUND;
            else
                return NOT_FOUND;
        }

        int main(void)
        {
           char *filename = "foo.bar";
           FILE *fp;

           remove(filename);

           if ( !(fp = fopen(filename, "w")) ) {
                perror("fopen");
                return (EXIT_FAILURE);
           }

           if ( dir_example( "FOO.BAR;1", 0 ) == FOUND )
                puts("OpenVMS style: found");
           else
                puts("OpenVMS style: not found");

           if ( dir_example( "foo.bar", 1 ) == FOUND )
                puts("UNIX style: found");
           else
                puts("UNIX style: not found");

           fclose(fp);
           remove(filename);
           return( EXIT_SUCCESS );
        }

4  –  Return Values

    0                  Indicates success.
    -1                 Indicates an error and is further specified in
                       the global errno.
Close Help