![]() |
|
#1
|
|||
|
|||
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:
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
|
|||
|
|||
Re: Using stat.h and listing directory contents...Quote:
Try man 2 stat and man 3 asctime. Hopefully this will get you going with stat-ing your file information... CPP / C++ / C Code:
...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:
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
|
|||
|
|||
Re: Using stat.h and listing directory contents...This is all very helpful, thanks a lot!
|
|
#4
|
|||
|
|||
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
|
||||
|
||||
Re: Using stat.h and listing directory contents...Thank you so much davis
|
|
#6
|
|||
|
|||
Re: Using stat.h and listing directory contents...Quote:
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
|
|||
|
|||
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
|
|||
|
|||
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:
Code: CPP / C++ / C Code:
I also already tried: CPP / C++ / C Code:
But I obviously encountered pointer problems with that. Note: All "if" statements, "for" loops and the "dir" counter work correctly. |
|
#9
|
|||
|
|||
Re: Using stat.h and listing directory contents...Quote:
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
|
|||
|
|||
Re: Using stat.h and listing directory contents...Nevermind the last post.
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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