LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2006, 08:48 PM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Using sys/stat.h and directory listing in C...


Currently, I have a program that lists the contents of a directory. What I need to do (as I've sort of started) is to use the stat structure to print such things as date modified, etc. like the "ls" program does. My code is as follows:

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

int main()
{
   char complete_filename[512];
   struct dirent **namelist;
   struct stat buf;
   int n = scandir("./", &namelist, 0, alphasort);
   int i;

   for ( i = 0; i < n; i++ )
   { 
      char *file_name = namelist[i]->d_name;

      strcpy(complete_filename, "./");
      strcat(complete_filename, "/");
      strcat(complete_filename, file_name);
 
      printf("%s\n", file_name);
   }
   
   return 0;
}
Basically, what I'm wondering is how I can use the stat structure to print such information. Can anyone help me out or at least point me in the right direction? (And the man pages are not much help for me).

If someone could just touch up my code so that it would print the date modified or something, so I could follow by example, that would be great.
 
Old 03-13-2006, 09:10 PM   #2
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
You must call lstat(2) and you can print the contents of the inode and use the macros available. See the manpage and the header file for information. On symbolic links, you may use readlink(2) and stat(2) to get at the info of the file it points to.
 
Old 03-14-2006, 06:10 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
See these thread for some stat() and lstat() examples:
http://www.linuxquestions.org/questi...d.php?t=274533
http://www.linuxquestions.org/questi...d.php?t=237402
http://www.linuxquestions.org/questi...d.php?t=165250
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot stat /etc/X11/X (no such file or directory) theabyyss Linux - Software 2 02-12-2008 07:15 PM
Apache Directory Listing Of NFS Mount, cannot view directory list via apache luqmana Linux - Networking 2 12-19-2005 06:03 AM
stat directory minil Programming 5 04-22-2005 12:40 AM
lsattr: no such file or directory while trying to stat... acampbell Linux - General 6 12-01-2003 02:41 PM
Directory Listing mattbeekler Linux - Software 8 07-22-2003 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration