stat() on readdir entries returns the same size and type for every file #209447
Replies: 3 comments
|
💬 Your Product Feedback Has Been Submitted 🎉 Thank you for taking the time to share your insights with us! Your feedback is invaluable as we build a better GitHub experience for all our users. Here's what you can expect moving forward ⏩
Where to look to see what's shipping 👀
What you can do in the meantime 💻
As a member of the GitHub community, your participation is essential. While we can't promise that every suggestion will be implemented, we want to emphasize that your feedback is instrumental in guiding our decisions and priorities. Thank you once again for your contribution to making GitHub even better! We're grateful for your ongoing support and collaboration in shaping the future of our platform. ⭐ |
|
@iNeeedIt Use #include <fcntl.h>
if (fstatat(dirfd(d), e->d_name, &st, 0) == -1)
{
perror(e->d_name);
continue;
}Tested with your Docs: https://man7.org/linux/man-pages/man2/fstatat.2.html If this solves it, please mark it as the answer so others can find it. |
|
Uh oh!
There was an error while loading. Please reload this page.
🏷️ Discussion Type
Bug
Body
I hit this while writing a small ls clone and it took me a while to see it, so I'm posting it with the fix in case someone else runs into it.
The program lists a directory with readdir and prints the size and type of each entry with stat:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
const char *dir = argc > 1 ? argv[1] : ".";
DIR *d = opendir(dir);
struct dirent *e;
struct stat st;
}
With no argument it works fine. But with another directory, every entry gets the same values:
$ ./a.out demo
. 4096 dir
sub 4096 dir
.. 4096 dir
a.txt 4096 dir
big.bin 4096 dir
a.txt is 12 bytes and big.bin is 5000, so the sizes are clearly wrong. It compiles with -Wall -Wextra -Werror without any warning.
Guidelines
All reactions