There are at least three errors in your code:
1) There is no such function in the above text: "read_dir (dir)". There is a "readdir (dir)".
2) You incorrectly form the file name of the current level - there is no top part of the file path. And when processing items that are subdirectories, this error is repeated twice!
3) Well, when analyzing a subdirectory, you are doing something that is not very clear to me - apparently, read_dir (dir) is your function, which should depict repetitive processing? Well, then write it recursively!
I did not bother with the complete alteration of your code to the recursive version, and on the first two points, the code should be like this:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/stat.h> #include <dirent.h> int main(int argc, char *argv[]) { DIR *d; struct dirent *entry; struct stat st; char dir_name[256]; char wrk[256]; int rc; if (argc > 1) { strcpy(dir_name, argv[1]); } else { strcpy(dir_name, "."); } d = opendir(dir_name); if (d == NULL) { printf("ΠΡΠΈΠ±ΠΊΠ° ΠΏΡΠΈ ΠΎΡΠΊΡΡΡΠΈΠΈ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΠΈ %s\n", dir_name); exit(-1); } while ((entry = readdir(d)) != NULL) { strcpy(wrk, dir_name); strcat(wrk, "/"); strcat(wrk,entry->d_name); rc = stat(wrk, &st); if (rc != 0 ) { printf("ΠΡΠΈΠ±ΠΊΠ° ΠΏΡΠΈ Π²ΡΠ·ΠΎΠ²Π΅ stat('%s')\n", wrk); exit(-1); } if (S_ISREG(st.st_mode)) { printf("%20s\t%ld\n", entry->d_name, st.st_size); } if (S_ISDIR(st.st_mode)) { DIR* dir; dir = opendir(entry->d_name); printf("dir %s\n", entry->d_name); printf("st %x\n", st.st_mode); readdir(dir); } } }
statexecution succeed? Maybe it is not executed at all, and then you look at the same garbage inst.st_mode. - AnT".",".."add error checking to all functions)? Give a complete example of the code that shows the error. The minimum reproducible example is jfs