VMS Help  —  CRTL  closedir  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 );
        }
Close Help