fileops files changed
[eleutheria.git] / fileops / listdir.c
blob8baa43bfd12a0dd15c54ae701cea6622170159d7
1 /*
2 * Compile with:
3 * gcc listdir.c -o listdir -Wall -W -Wextra -ansi -pedantic
4 */
6 #include <dirent.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 int main(int argc, char *argv[])
12 struct dirent *pdent;
13 DIR *pdir;
15 /* Check argument count */
16 if (argc != 2) {
17 fprintf(stderr, "Usage: %s directory\n", argv[0]);
18 exit(EXIT_FAILURE);
22 * Open directory named by argv[1], associate a directory stream
23 * with it and return a pointer to it
25 if ((pdir = opendir(argv[1])) == NULL) {
26 perror("opendir");
27 exit(EXIT_FAILURE);
30 /* Get all directory entries */
31 while((pdent = readdir(pdir)) != NULL)
32 printf("%s\n", pdent->d_name);
34 closedir(pdir);
35 return EXIT_SUCCESS;