0

I am assigning a different value to *fd each time I iterate through the while loop, yet it seems that the stat information is not dynamic. The objective is to take a directory that is passed as a command line argument, loop through the files in that directory, and print relevant stat info. The file name that prints is dynamic, but the stat info is not.

Can anyone offer any insight into this?

 while ((pDirent = readdir(pDir)) != NULL)
 {

     char *fd = pDirent->d_name;
     struct stat *buf;

     buf = malloc(sizeof(struct stat));

     stat(fd, buf);

     if (pDirent->d_name[0] != '.' && (lSwitch || noSwitch))
     {
         int i=0;
         for (i = 0; i<4; i++)
         {
             printf("%s", " ");
         }
         printf("%s", pDirent->d_name);
         if (lSwitch)
         {
             //  print   the file    type
             if (S_ISLNK(buf->st_mode))
                printf(" L ");
             else if (S_ISFIFO(buf->st_mode))
                printf(" P ");
             else if (S_ISREG(buf->st_mode))
                printf(" F ");
             else if (S_ISDIR(buf->st_mode))
                printf(" D ");
             else
                printf(" L ");
             printf("number  of  512 blocks  is  %ld\n", buf->st_blocks);

         }
         printf("\n");
     }

     free(buf);
 }
ClarksonDev
  • 75
  • 1
  • 9
  • Unrelated to your problem, but what is the reason you allocate the `stat` structure dynamically each time inside the loop? You do know about the address-of operator `&`? Like in `struct stat buf /* No pointer */; ...; stat(fd, &buf);`? – Some programmer dude Jul 31 '18 at 01:04
  • 2
    Much more related to your problem, why don't you check what [`stat`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html) *returns*? It *can* fail, and I'm guessing that's what is happening here for you (since you don't provide the full path to the file, so it will return `-1` with `errno` set to `ENOENT`). – Some programmer dude Jul 31 '18 at 01:06
  • @Someprogrammerdude, I am allocating stat dynamically bc I need it to be dynamic for each file within the directory. I am learning low level I/O and perhaps I don't know the best way to do this. Can you suggest something that would work in lieu of this? – ClarksonDev Jul 31 '18 at 01:08
  • @Someprogrammerdude, also, thanks for the suggestion. I will check what stat returns and see if I can narrow down what is going wrong. – ClarksonDev Jul 31 '18 at 01:09
  • 2
    It seems you could learn a little bit about *scope* and *lifetime* of variables. If you declare the `buf` variable inside the loop, its lifetime ends as soon as the loop iterates (or ends). It will (in essence) be recreated anew each iteration, even if it's not a pointer. So please stop using pointers and dynamic allocation, unless you actually need to *save* the structure in a list or something similar, to be used later after the loop. – Some programmer dude Jul 31 '18 at 01:30
  • @Someprogrammerdude the reason I used pointers is because when researching the usage for stat, I found the following: `int stat(const char *path, struct stat *buf);` – ClarksonDev Jul 31 '18 at 01:33
  • 2
    Then please read my first comment again. You don't need a pointer variable to create a pointer. If you have `struct stat buf;` (no pointer) then the type of the expression `&buf` will be `struct stat *` (i.e. a pointer). In fact, almost all examples you will see using the `stat` function will use the address-of operator `&` (including the one in the "official" POSIX reference I linked to above). – Some programmer dude Jul 31 '18 at 01:35
  • @ClarksonDev there just happens to be a complete example provide in [man 2 stat](http://man7.org/linux/man-pages/man2/stat.2.html) that can help illustrate the use of `buf` declared as a `struct stat` with *automatic* storage. While you can use `malloc`, on large file trees, the dynamic allocation will be costly compared to using simple automatic storage and providing the pointer by using the *address of* operator. – David C. Rankin Jul 31 '18 at 04:20
  • You need to review the content of [Why is `stat()` not working after `readdir()`?](https://stackoverflow.com/questions/34168266/). I'm moderately sure this is a duplicate of that — given that you've not mentioned `chdir()` anywhere and you have mentioned working with directories specified on the command line. – Jonathan Leffler Jul 31 '18 at 05:09

0 Answers0