Listing file stats in a directory
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Listing file stats in a directory




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Listing file stats in a directory  
Jamie


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM




I wrote a small piece of code, but it isn't working right. I am trying
to list the contents of a directory, and the associated inode numbers with
each of the listed files.


#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{

DIR *directory;
struct dirent *entry;
struct stat buffer;


directory = opendir("/home/jamie");
while ((entry = readdir(directory)) != NULL)
{
stat(entry->d_name, &buffer);
printf("%d   %s\n", buffer.st_ino, entry->d_name);
}
closedir(directory);
return 0;
}


For some reason, when I run the program, it gives me output like this:

163455   findthem.c
163455   books
163455   .procmailrc.bak
163455   nasa
163455   music.tar.gz
163455   mbox


..in other words, it is giving me the same inode number over and over
again. I am not sure why it would do this. When I go into my loop, I am
calling stat with a different entry->d_name each time, so different
results should go into my buffer with each loop.


This is on FreeBSD 4.5 with gcc 2.95. Thanks for any insight!


- Jamie



Greetings from Minneapolis, MN, United States

"A friend is someone who lets you have total freedom to be yourself."






[ Post a follow-up to this message ]



    Re: Listing file stats in a directory  
Michael Fuhr


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM

Jamie <jamie@gnulife.remove-this-antispam.org> writes:
quote:
> I wrote a small piece of code, but it isn't working right. I am trying > to list the contents of a directory, and the associated inode numbers with > each of the listed files.
[snip]
quote:
> For some reason, when I run the program, it gives me output like this: > > 163455 findthem.c > 163455 books > 163455 .procmailrc.bak > 163455 nasa > 163455 music.tar.gz > 163455 mbox > > ...in other words, it is giving me the same inode number over and over > again. I am not sure why it would do this. When I go into my loop, I am > calling stat with a different entry->d_name each time, so different > results should go into my buffer with each loop.
Hint: add error checking to your program. You're assuming something that isn't true; the error checking should reveal what that is. -- Michael Fuhr http://www.fuhr.org/~mfuhr/




[ Post a follow-up to this message ]



    Re: Listing file stats in a directory  
Michael Fuhr


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM

Jamie <jamie@gnulife.remove-this-antispam.org> writes:
quote:
> I wrote a small piece of code, but it isn't working right. I am trying > to list the contents of a directory, and the associated inode numbers with > each of the listed files.
[snip]
quote:
> For some reason, when I run the program, it gives me output like this: > > 163455 findthem.c > 163455 books > 163455 .procmailrc.bak > 163455 nasa > 163455 music.tar.gz > 163455 mbox > > ...in other words, it is giving me the same inode number over and over > again. I am not sure why it would do this. When I go into my loop, I am > calling stat with a different entry->d_name each time, so different > results should go into my buffer with each loop.
Hint: add error checking to your program. You're assuming something that isn't true; the error checking should reveal what that is. -- Michael Fuhr http://www.fuhr.org/~mfuhr/




[ Post a follow-up to this message ]



    Re: Listing file stats in a directory  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM

On Sat, 29 Nov 2003 at 20:34 GMT, Jamie wrote:
quote:
> > > > I wrote a small piece of code, but it isn't working right. I am trying > to list the contents of a directory, and the associated inode numbers with > each of the listed files. > > > #include <stdio.h> > #include <dirent.h> > #include <sys/types.h> > #include <sys/stat.h> > > int main() > { > > DIR *directory; > struct dirent *entry; > struct stat buffer; > > > directory = opendir("/home/jamie"); > while ((entry = readdir(directory)) != NULL) > { > stat(entry->d_name, &buffer);
entry->d_name only contains the file name; you need to strcat that on to the directory name. char path[NAME_MAX]; .... sprintf( path, "/home/jamie/%s", entry->d_name); if (!stat(path, &buffer))
quote:
> printf("%d %s\n", buffer.st_ino, entry->d_name); > } > closedir(directory); > return 0; > } > > > For some reason, when I run the program, it gives me output like this: > > 163455 findthem.c > 163455 books > 163455 .procmailrc.bak > 163455 nasa > 163455 music.tar.gz > 163455 mbox > > > ...in other words, it is giving me the same inode number over and over > again. I am not sure why it would do this. When I go into my loop, I am > calling stat with a different entry->d_name each time, so different > results should go into my buffer with each loop. > > > This is on FreeBSD 4.5 with gcc 2.95. Thanks for any insight! > > > - Jamie > > > > Greetings from Minneapolis, MN, United States > > "A friend is someone who lets you have total freedom to be yourself." >
-- Chris F.A. Johnson http://cfaj.freeshell.org ======================================== =========================== My code (if any) in this post is copyright 2003, Chris F.A. Johnson and may be copied under the terms of the GNU General Public License




[ Post a follow-up to this message ]



    Re: Listing file stats in a directory  
examnotes


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM

"Chris F.A. Johnson" <c.fa.johnson@rogers.com> writes:
quote:
> entry->d_name only contains the file name; you need to strcat that > on to the directory name. > > char path[NAME_MAX];
You'd better make that PATH_MAX. -- Måns Rullgård mru@kth.se




[ Post a follow-up to this message ]



    Re: Listing file stats in a directory  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM

On Sat, 29 Nov 2003 at 20:34 GMT, Jamie wrote:
quote:
> > > > I wrote a small piece of code, but it isn't working right. I am trying > to list the contents of a directory, and the associated inode numbers with > each of the listed files. > > > #include <stdio.h> > #include <dirent.h> > #include <sys/types.h> > #include <sys/stat.h> > > int main() > { > > DIR *directory; > struct dirent *entry; > struct stat buffer; > > > directory = opendir("/home/jamie"); > while ((entry = readdir(directory)) != NULL) > { > stat(entry->d_name, &buffer);
entry->d_name only contains the file name; you need to strcat that on to the directory name. char path[NAME_MAX]; .... sprintf( path, "/home/jamie/%s", entry->d_name); if (!stat(path, &buffer))
quote:
> printf("%d %s\n", buffer.st_ino, entry->d_name); > } > closedir(directory); > return 0; > } > > > For some reason, when I run the program, it gives me output like this: > > 163455 findthem.c > 163455 books > 163455 .procmailrc.bak > 163455 nasa > 163455 music.tar.gz > 163455 mbox > > > ...in other words, it is giving me the same inode number over and over > again. I am not sure why it would do this. When I go into my loop, I am > calling stat with a different entry->d_name each time, so different > results should go into my buffer with each loop. > > > This is on FreeBSD 4.5 with gcc 2.95. Thanks for any insight! > > > - Jamie > > > > Greetings from Minneapolis, MN, United States > > "A friend is someone who lets you have total freedom to be yourself." >
-- Chris F.A. Johnson http://cfaj.freeshell.org ======================================== =========================== My code (if any) in this post is copyright 2003, Chris F.A. Johnson and may be copied under the terms of the GNU General Public License




[ Post a follow-up to this message ]



    Re: Listing file stats in a directory  
Felipe Cerqueira (skylazart)


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM



On Sat, 29 Nov 2003, Chris F.A. Johnson wrote:
quote:
> On Sat, 29 Nov 2003 at 20:34 GMT, Jamie wrote: > > entry->d_name only contains the file name; you need to strcat that > on to the directory name. > > char path[NAME_MAX]; > .... > sprintf( path, "/home/jamie/%s", entry->d_name); > if (!stat(path, &buffer)) >
You're right. You could use chdir too... A thing I want to say is how to get NAME_MAX to prevent overflow in construction of file's complete path: char * ftw_path_alloc (int *size) { char *ptr; if (pathmax == 0) { if ((pathmax = pathconf ("/", _PC_PATH_MAX)) < 0) { if (errno == 0) { pathmax = PATH_MAX_GUESS; } else { err_sys_exit ("pathconf failed", -1); } } else { pathmax += 2; } } if ((ptr = alloc (pathmax + 8)) == NULL) { err_sys_exit ("alloc failed", -1); } if (size != NULL) *size = pathmax; return (ptr); } Its correctly running (tested) in linux, solaris and AIX.. I hope it can be completly portable.
quote:
> > > -- > Chris F.A. Johnson http://cfaj.freeshell.org > ======================================== =========================== > My code (if any) in this post is copyright 2003, Chris F.A. Johnson > and may be copied under the terms of the GNU General Public License >




[ Post a follow-up to this message ]



    Re: Listing file stats in a directory  
examnotes


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM

"Chris F.A. Johnson" <c.fa.johnson@rogers.com> writes:
quote:
> entry->d_name only contains the file name; you need to strcat that > on to the directory name. > > char path[NAME_MAX];
You'd better make that PATH_MAX. -- Måns Rullgård mru@kth.se




[ Post a follow-up to this message ]



    Re: Listing file stats in a directory  
Felipe Cerqueira (skylazart)


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:01 PM



On Sat, 29 Nov 2003, Chris F.A. Johnson wrote:
quote:
> On Sat, 29 Nov 2003 at 20:34 GMT, Jamie wrote: > > entry->d_name only contains the file name; you need to strcat that > on to the directory name. > > char path[NAME_MAX]; > .... > sprintf( path, "/home/jamie/%s", entry->d_name); > if (!stat(path, &buffer)) >
You're right. You could use chdir too... A thing I want to say is how to get NAME_MAX to prevent overflow in construction of file's complete path: char * ftw_path_alloc (int *size) { char *ptr; if (pathmax == 0) { if ((pathmax = pathconf ("/", _PC_PATH_MAX)) < 0) { if (errno == 0) { pathmax = PATH_MAX_GUESS; } else { err_sys_exit ("pathconf failed", -1); } } else { pathmax += 2; } } if ((ptr = alloc (pathmax + 8)) == NULL) { err_sys_exit ("alloc failed", -1); } if (size != NULL) *size = pathmax; return (ptr); } Its correctly running (tested) in linux, solaris and AIX.. I hope it can be completly portable.
quote:
> > > -- > Chris F.A. Johnson http://cfaj.freeshell.org > ======================================== =========================== > My code (if any) in this post is copyright 2003, Chris F.A. Johnson > and may be copied under the terms of the GNU General Public License >




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:18 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register