/************************************************************************ * * * Copyright Digital Equipment Corporation 1998. All rights reserved. * * * * Restricted Rights: Use, duplication, or disclosure by the U.S. * * Government is subject to restrictions as set forth in subparagraph * * (c) (1) (ii) of DFARS 252.227-7013, or in FAR 52.227-19, or in FAR * * 52.227-14 Alt. III, as applicable. * * * * This software is proprietary to and embodies the confidential * * technology of Digital Equipment Corporation. Possession, use, or * * copying of this software and media is authorized only pursuant to a * * valid written license from Digital or an authorized sublicensor. * * * ************************************************************************/ /* closedir_example.c */ #include #include #include #include #define FOUND 1 #define NOT_FOUND 0 static int dirent_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 VMS-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 ( dirent_example( "FOO.BAR;1", 0 ) == FOUND ) puts("VMS style: found"); else puts("VMS style: not found"); if ( dirent_example( "foo.bar", 1 ) == FOUND ) puts("Unix style: found"); else puts("Unix style: not found"); fclose(fp); remove(filename); return( EXIT_SUCCESS ); }