-1

I have to read a directory name and print directory's index (type of each file and date of the last modification).

1      #include <stdio.h>
2      #include <sys/stat.h>
3      #include <dirent.h>
4      #include <sys/types.h>
5
6      int main() {
7          char file[100];
8          struct stat stats;
9          DIR *dr;
10         struct dirent *en;
11         char location[500];
12         dr = opendir(".");
13         if (dr) {
14             while ((en = readdir(dr)) != NULL) {
15                 printf("%s\n", en->d_name);
16                 properties(en);
17             }
18             closedit(dr);
19         }
20         else{
21             printf("File not found! Check if there is the %s\n", file);
22         }
23         return 0;
24
25         void **properties**(struct stat stats)
26     {
27         if (stats.st_mode & R_OK){
28             printf("Read ");
29         if (stats.st_mode & W_OK)
30             printf("Write ");
31         if (stats.st_mode & X_OK)
32             printf("Exe");
33     }
34         if (stats.st_size != 0)
35             printf("\nFile Size: %d\n", stats.st_size);
36         }     
37     }

So my question is, why the declaration "properties" (declared at line 25) is incompatible with previous "properties" (declared at line 16). Also why my compiler is asking me a ";" at line 26.

Thanks!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
manolex
  • 11
  • 4
  • 3
    1) `struct stat` and `struct dirent` are incompatible; 2) the compiler expected a matching `}` for the first `{` after `main`. – pmg Jan 04 '22 at 08:20
  • Second one solved. What could I do for the first? If I use stat for both structs will this be resolved? – manolex Jan 04 '22 at 08:22
  • 2
    Programming by trial-and-error is not very productive (it's fun though!). I suggest you read the documentation ([`readdir()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html)) and follow the rules. – pmg Jan 04 '22 at 08:30
  • Functions need to be declared before first use. Also there are a lot of \* around your function definition, the compiler cannot deal with that. declare the function as void properties( – lalala Jan 04 '22 at 08:33
  • 2
    This has become a "chameleon question". The revised code bears no resemblance to the original code. Once you've got an answer, you should not transform the question so completely that the answer becomes unrecognizable. I've rolled back the changes you made (and, of necessity, my edit of those changes). – Jonathan Leffler Jan 04 '22 at 10:15
  • In the original code, note that you don't complete `main()` before you declare/define `properties`. And when you do add `properties`, the compiler thinks it should be a function declaration — hence the request for the semicolon. The line numbers are undesirable — people can't copy the code and compile it without having to remove the line numbers. And the `void **properties**(…` notation is presumably meant to make the function name bold — but MarkDown doesn't work like that. – Jonathan Leffler Jan 04 '22 at 10:18

1 Answers1

1

why the declaration "properties" (declared at line 25) is incompatible with previous "properties"

You also get a warning: implicit declaration of function .

What could I do for the first?

Call a function to get the statistics of a path.

 while ((en = readdir(dr)) != NULL) {
     struct stat s;
     fstatat(dirfd(dr), en->d_name, &s, 0);
     properties(s);
 }
KamilCuk
  • 120,984
  • 8
  • 59
  • 111