/* Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) This work is licensed under the Creative Commons Attribution-NonCommercial- ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. */ #include #include /* uses POSIX functionality - not 'Standard' C */ int list_directory(char *dir_path) { DIR *dir_h; struct dirent *entry; if ((dir_h = opendir(dir_path)) == NULL) return 0; printf("Contents of \"%s\":\n", dir_path); for ( ; entry = readdir(dir_h); ) printf("\t%s\n", entry->d_name); closedir(dir_h); return 1; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage:\n\t%s (directory to list)\n", argv[0]); return -1; } if (!list_directory(argv[1])) { perror(argv[0]); return -1; } return 0; }