lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <dirent.h> |
| 7 | |
| 8 | #define _DTIFY(DT) [DT] #DT |
| 9 | const char * const types[] = { |
| 10 | _DTIFY(DT_UNKNOWN), |
| 11 | _DTIFY(DT_FIFO), |
| 12 | _DTIFY(DT_CHR), |
| 13 | _DTIFY(DT_DIR), |
| 14 | _DTIFY(DT_BLK), |
| 15 | _DTIFY(DT_REG), |
| 16 | _DTIFY(DT_LNK), |
| 17 | _DTIFY(DT_SOCK), |
| 18 | _DTIFY(DT_WHT) |
| 19 | }; |
| 20 | |
| 21 | int main(int argc, char *argv[]) |
| 22 | { |
| 23 | DIR *dirh; |
| 24 | struct dirent *de; |
| 25 | const char *mydir = (argc == 1 ? "/" : argv[1]); |
| 26 | |
| 27 | if ((dirh = opendir(mydir)) == NULL) { |
| 28 | perror("opendir"); |
| 29 | return 1; |
| 30 | } |
| 31 | |
| 32 | printf("readdir() says:\n"); |
| 33 | while ((de = readdir(dirh)) != NULL) |
| 34 | printf("\tdir entry %s: %s\n", types[de->d_type], de->d_name); |
| 35 | |
| 36 | closedir(dirh); |
| 37 | |
| 38 | return 0; |
| 39 | } |