GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Mar-2006, 18:58
smoothdogg00 smoothdogg00 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 31
smoothdogg00 is on a distinguished road
Question

Using stat.h and listing directory contents...


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:

CPP / C++ / C 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.
Last edited by LuciWiz : 14-Mar-2006 at 02:54. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 14-Mar-2006, 06:52
davis
 
Posts: n/a

Re: Using stat.h and listing directory contents...


Quote:
Originally Posted by smoothdogg00
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:

<snipped>

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.


Try man 2 stat and man 3 asctime. Hopefully this will get you going with stat-ing your file information...

CPP / C++ / C Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

int main()
{
    char complete_filename[2048] = {0};
    struct dirent **namelist;
    struct stat file_info;
    int n = scandir("./", &namelist, 0, alphasort);
    int i;
    struct tm last_modified;

    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);
        if( strlen( file_name ) > 0 )
        {
            stat( &complete_filename[0], &file_info );
            localtime_r( &file_info.st_mtime, &last_modified );
            printf( "Filename: %s\nModified: %s\n\n", file_name, asctime( &last_modified ) );
        }
    }

    return 0;
}

// output:

Filename: .
Modified: Tue Mar 14 04:25:11 2006


Filename: ..
Modified: Tue Mar 14 04:21:49 2006


Filename: dirstat
Modified: Tue Mar 14 04:25:11 2006


Filename: dirstat.o
Modified: Tue Mar 14 04:25:11 2006

ls -la produces:

drwxrwxr-x  2 davis davis 4096 Mar 14 04:25 .
drwxr-xr-x  3 davis davis 4096 Mar 14 04:21 ..
-rwxrwxr-x  1 davis davis 8320 Mar 14 04:25 dirstat
-rw-rw-r--  1 davis davis 4284 Mar 14 04:25 dirstat.o

...I created a simple C app called "dirstat" and compiled it. This is the output of the "Debug" directory that my Linux editor (www.slickedit.com) creates whenever I make a "GCC Project" that it manages. I ran the executable "dirstat" from that directory on the files that it contained. You can see that the output of the program corresponds to what ls tells me about the files.

If I run the program from the directory above it:

Code:
$ ./Debug/dirstat Filename: . Modified: Tue Mar 14 04:21:49 2006 Filename: .. Modified: Tue Mar 14 04:09:24 2006 Filename: Debug Modified: Tue Mar 14 04:25:11 2006 Filename: dirstat.c Modified: Tue Mar 14 04:25:10 2006 Filename: dirstat.mak Modified: Tue Mar 14 04:10:07 2006 Filename: dirstat.vpj Modified: Tue Mar 14 04:10:06 2006 Filename: dirstat.vpw Modified: Tue Mar 14 04:09:26 2006 Filename: dirstat.vpwhistu Modified: Tue Mar 14 04:10:07 2006 Filename: dirstat.vtg Modified: Tue Mar 14 04:25:10 2006 ls outputs: drwxr-xr-x 3 davis davis 4096 Mar 14 04:21 . drwxr-xr-x 12 davis davis 4096 Mar 14 04:09 .. drwxrwxr-x 2 davis davis 4096 Mar 14 04:25 Debug -rw-rw-r-- 1 davis davis 1596 Mar 14 04:25 dirstat.c -rw-rw-r-- 1 davis davis 1606 Mar 14 04:10 dirstat.mak -rw-rw-r-- 1 davis davis 4980 Mar 14 04:10 dirstat.vpj -rw-rw-r-- 1 davis davis 196 Mar 14 04:09 dirstat.vpw -rw-rw-r-- 1 davis davis 115 Mar 14 04:10 dirstat.vpwhistu -rw-r--r-- 1 davis davis 90112 Mar 14 04:25 dirstat.vtg

You can see that you'll want to modify your formatting, but this should get you a few more steps down the road to your "ls" program.

Note that I made your buffer a lot larger. I figured that eventually you were going to recurse directories. However, you should probably use strncat and strncpy to ensure that you never exceed the bounds of your char array.


:davis:
  #3  
Old 14-Mar-2006, 08:19
smoothdogg00 smoothdogg00 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 31
smoothdogg00 is on a distinguished road

Re: Using stat.h and listing directory contents...


This is all very helpful, thanks a lot!
  #4  
Old 14-Mar-2006, 16:43
smoothdogg00 smoothdogg00 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 31
smoothdogg00 is on a distinguished road
Cool

Re: Using stat.h and listing directory contents...


Ok, so I'm getting a pretty good understanding for this stuff now, but when I'm going to recurse the directories, how do I enter the next directory?

For example: Scandir is scanning the current directory and getting the files (and folders I'm assuming), but what call should be made in order to open the next directory down? Or maybe the question I need to ask, how can I recognize and open folders? Is it basically a "try scandir" and if it works, go with it, if not, try the next file?
  #5  
Old 14-Mar-2006, 17:00
mina_canada's Avatar
mina_canada mina_canada is offline
New Member
 
Join Date: Mar 2006
Posts: 1
mina_canada is on a distinguished road

Re: Using stat.h and listing directory contents...


Thank you so much davis
  #6  
Old 14-Mar-2006, 18:06
davis
 
Posts: n/a

Re: Using stat.h and listing directory contents...


Quote:
Originally Posted by smoothdogg00
Or maybe the question I need to ask, how can I recognize and open folders?

Use the st_mode member of your stat struct and the S_ISDIR(m) macro where m = your st_mode member. It will return a value if the file mode is a directory. Then scandir it and other directories under it until you've recursed the entire world.

Check out section 8.6 in K&R (2nd Edition). They have an example of walking the directories.


:davis:
  #7  
Old 14-Mar-2006, 18:57
smoothdogg00 smoothdogg00 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 31
smoothdogg00 is on a distinguished road

Re: Using stat.h and listing directory contents...


I was actually looking at that earlier, but I couldn't quite understand how to use it. Is there also some macro for determining if a file is "hidden" or not? Or should it just be determined by the "." in front?

<Edit>I got it under control, forget that question.<Edit>
  #8  
Old 14-Mar-2006, 20:32
smoothdogg00 smoothdogg00 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 31
smoothdogg00 is on a distinguished road
Question

Re: Using stat.h and listing directory contents...


I am now building an array within a while loop (of set size 500) that keeps track of all the directories within a directory. After those are added to the array, I am trying to copy the contents of that array to a new (smaller) array as follows:

Variables:
CPP / C++ / C Code:
char buf[255];
int i, dirs = 0;
int n = scandir(path, &namelist, 0, alphasort);
char * templist[n];

Code:
CPP / C++ / C Code:
for(code here)
{
    if(strncmp(file_name, ".", 1) && S_ISDIR(file_info.st_mode))
      {
          strcpy(templist[dirs], buf);
          dirs++;
      }
}

char * dirlist[dirs];

for(i = 0; i < dirs; i++)
    strcpy(dirlist[i], templist[i]);

I also already tried:
CPP / C++ / C Code:
templist[dirs] = buf;

But I obviously encountered pointer problems with that.

Note: All "if" statements, "for" loops and the "dir" counter work correctly.
  #9  
Old 14-Mar-2006, 22:39
davis
 
Posts: n/a

Re: Using stat.h and listing directory contents...


Quote:
Originally Posted by smoothdogg00
I was actually looking at that earlier, but I couldn't quite understand how to use it. Is there also some macro for determining if a file is "hidden" or not? Or should it just be determined by the "." in front?

<Edit>I got it under control, forget that question.<Edit>

You need to understand that the example in K&R is reentrant. The way to really do it is by hand coding the code into a separate program, building it and stepping through it with a debugger. All you really need to do is set a break-point at each function entry point so that you can track how the reentrancy works under any known directory "file" contents.

Try doing: "vi ." and, after it complains about "." being a directory, look at the contents of the file...or use "vi /usr/include" ...you'll get a lot of directory entries and a lot of "regular" file entries.

The more you understand about a "directory" mode file, the easier it will be to implement a solution that uses them. It helps to think about "how would I do it, if it wasn't already done." In other words, how you would implement a directory file so that when you did "dir" or "ls" that it would show the directories and regular files (and so forth) separately from each other? If you did "ls ." (which is the same as "ls"), you would use the CWD or current working directory for your input. We know that the CWD is implemented as a file named "."

If we view the contents of the file named "." we would see the subdirs and regular files under "." contained in it. When we mkdir, we create a "." dir entry named by the argument passed into mkdir. We cannot make a dir called "." as for any path that exists, it (".") will already be named.

The operating system is responsible for abstracting the IO subsystem such that it is represented as a file system to us with a C interface.

If you haven't read Tanenbaum's "Modern Operating Systems," you need to give it a quick look-through.

Let me know how it goes, if you want me to, I'll write a quick implementation to help you through it.


:davis:
  #10  
Old 14-Mar-2006, 23:58
smoothdogg00 smoothdogg00 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 31
smoothdogg00 is on a distinguished road

Re: Using stat.h and listing directory contents...


Nevermind the last post.
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Listing directory information (C) smoothdogg00 C Programming Language 1 10-Mar-2006 13:17
File listing of a directory durrson C Programming Language 2 17-Mar-2005 16:59
Denying directory listing and htaccess question meet_raman Apache Web Server Forum 11 01-Jul-2004 11:20
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 07:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:02.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.