Unix Programming - Listing file stats in a directory

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > Listing file stats in a directory





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Listing file stats in a directory
Jamie

2004-01-23, 5: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."

Michael Fuhr

2004-01-23, 5: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/
Michael Fuhr

2004-01-23, 5: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/
Chris F.A. Johnson

2004-01-23, 5: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
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5: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
Chris F.A. Johnson

2004-01-23, 5: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
Felipe Cerqueira (skylazart)

2004-01-23, 5: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
>



=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5: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
Felipe Cerqueira (skylazart)

2004-01-23, 5: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
>



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com