1 /* 2 * list.c 3 * 4 * Listet Verzeichnisse auf. 5 * Verwendet Funktionen nach POSIX-Standard. 6 * 7 * Author H.Drachenfels 8 * Erstellt am: 15.6.2015 / 8.3.2018 (C11) 9 */
10
11 #define _POSIX_C_SOURCE 1
12
13 #include <stdio.h> // fprintf, printf
14 #include <string.h> // strerror
15
16 #include <sys/stat.h> // struct stat, S_ISDIR
17 #include <dirent.h> // DIR, struct dirent, opendir, readdir
18 #include <errno.h> // errno
19
20 int main(int argc, char *argv[])
21 {
22 for (int i = 1; i < argc; ++i)
23 {
24 struct stat s; // Dateistatus
25 if (stat(argv[i], &s) == -1)
26 {
27 fprintf(stderr, "%s existiert nicht (errno %d: %s)\n",
28 argv[i], errno, strerror(errno));
29 continue;
30 }
31
32 if (!S_ISDIR(s.st_mode))
33 {
34 fprintf(stderr, "%s ist kein Verzeichnis\n", argv[i]);
35 continue;
36 }
37
38 DIR *d = opendir(argv[i]); // geoeffnetes Verzeichnis
39 if (d == NULL)
40 {
41 fprintf(stderr, "%s kann nicht geoeffnet werden (errno %d: %s)\n",
42 argv[i], errno, strerror(errno));
43 continue;
44 }
45
46 errno = 0;
47
48 struct dirent *e; // gelesener Verzeichniseintrag
49 while ((e = readdir(d)) != NULL)
50 {
51 printf("%s/%s\n", argv[i], e->d_name);
52 }
53
54 if (errno)
55 {
56 fprintf(stderr, "Lesefehler in %s (errno %d: %s)\n",
57 argv[i], errno, strerror(errno));
58 }
59
60 closedir(d);
61 }
62
63 return 0;
64 }
65