|
|
|||||||
| Home | Register | Downloads | FAQ | Members List | Calendar | Arcade | Mark Forums Read |
» Less advertising throughout
» Post and participate in discussions
» Network with other forum members
» Free private messaging
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Implementing ls...
Ok, as my next assignment our teacher told us to implement a subversion of ls... so far I've had no problems in implementing the basic ls functionality along with some basic flags like the -a one, and some custom ones out teacher is asking for. For reading the directories I'm using the dirent.h library functions, however when analysing each file these functions will return to me a dirent structure that will give me the name of the file at max. This becomes a problem when I try to implement the 'ls -l' functionality, since I will be missing a lot of data. My question is that if there is a set of C function that will analize a file and return me the owner, creator, permissions, and those kind of things that the -l flag include... Found the answer myself: stat fstat and lstat are your friends ... now the question is how the heck do you parse the st_mode field (i found an example here but i don't know what the sperm function is or what is it supposed to do.... (well it does some parsing but...)
__________________
Last edited by Proto; April 7th, 2006 at 07:47.. |
|
|
|
| Advertisement | [Remove Advertisement] | ||
|
|
|
#2 |
|
A1C
![]() ![]() Join Date: Jan 2004
Location: California
Posts: 230
|
Check "man 2 stat" for information specific to your system. Code:
The sb argument is a pointer to a stat() structure as defined by
<sys/stat.h> (shown below) and into which information is placed concern-
ing the file.
struct stat {
dev_t st_dev; /* device inode resides on */
ino_t st_ino; /* inode's number */
mode_t st_mode; /* inode protection mode */
nlink_t st_nlink; /* number or hard links to the file */
uid_t st_uid; /* user-id of owner */
gid_t st_gid; /* group-id of owner */
dev_t st_rdev; /* device type, for special file inode */
struct timespec st_atimespec; /* time of last access */
struct timespec st_mtimespec; /* time of last data modification */
struct timespec st_ctimespec; /* time of last file status change */
off_t st_size; /* file size, in bytes */
quad_t st_blocks; /* blocks allocated for file */
u_long st_blksize;/* optimal file sys I/O ops blocksize */
u_long st_flags; /* user defined flags for file */
u_long st_gen; /* file generation number */
};
Code:
The status information word st_mode has the following bits:
#define S_IFMT 0170000 /* type of file */
#define S_IFIFO 0010000 /* named pipe (fifo) */
#define S_IFCHR 0020000 /* character special */
#define S_IFDIR 0040000 /* directory */
#define S_IFBLK 0060000 /* block special */
#define S_IFREG 0100000 /* regular */
#define S_IFLNK 0120000 /* symbolic link */
#define S_IFSOCK 0140000 /* socket */
#define S_IFWHT 0160000 /* whiteout */
#define S_ISUID 0004000 /* set user id on execution */
#define S_ISGID 0002000 /* set group id on execution */
#define S_ISVTX 0001000 /* save swapped text even after use */
#define S_IRUSR 0000400 /* read permission, owner */
#define S_IWUSR 0000200 /* write permission, owner */
#define S_IXUSR 0000100 /* execute/search permission, owner */
|
|
|
|
|
|
#3 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Oh yeah, indeed that helps. I had consulted my man previously but mine didn't have the implementation for the st_mode pre-defined bits so I though they were just some kind of setters, but if they are just the number that saves a lot of trouble ![]() EDIT: Ok the ls is done, yay (though it would be better if readdir returned you pointers to alphabetically ordered dirents but aw wel... ill have to do it by hand i guess....) Now I have to implement the ps... does c has functions to make this task easier or you have to do all the parsing by hand?
__________________
Last edited by Proto; April 7th, 2006 at 18:57.. |
|
|
|
|
|
#4 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Ok well... I already found everything I needed... the /proc/<number>/stat file... however I have a problem here. When I want to implement the basic ps functionality, i only have to print the processes thay were initiated by the current terminal. That implies that I must compare the tty process id of the terminal my program is running on to the tty the process i am analysing on that moment. I know I could just do a call to ps | grep bash and from there get the pid, but i wanted to know if there is a less dirty solution to get the tty pid...
__________________
Last edited by Proto; April 7th, 2006 at 20:20.. |
|
|
|
|
|
#5 |
|
A1C
![]() ![]() Join Date: Jan 2004
Location: California
Posts: 230
|
Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pid;
pid = getpid(); /* current process id */
printf("%d\n", pid);
pid = getppid(); /* parent process id */
printf("%d\n", pid);
return 0;
}
|
|
|
|
|
|
#6 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Yeah, I realized the ppid thingie a little after I made the post... (but forgot to update) I must appear like an idiot now ![]() Now my only remaining doubt is how to map the tty_nr that you obtain from the stat file to the actual terminal name... aw well, let's see of google can help me. EDIT: On the quick an dirty side I guess I could stat() all the /dev/pts devices
__________________
Last edited by Proto; April 8th, 2006 at 02:52.. |
|
|
|
|
|
#7 |
|
Transcended
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2001
Location: Moonlight Spire
Posts: 1,409
|
|
|
|
|
|
|
#8 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
How so? Isn't it one of the golden rules of programming: "If it works don't move it" ![]() My new assignment now asks you to implement a distributed brute-force attack for obtaining the key a file is encrypted with... I just love this subject's projects
Last edited by Proto; May 7th, 2006 at 03:45.. |
|
|
|
|
|
#9 |
|
Transcended
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2001
Location: Moonlight Spire
Posts: 1,409
|
Fine. ![]() Odd, though. I think implementing ls is easier than the other projects you've had. Whatever happened to increasing levels of difficulty? Bah, your prof just decided to spice up a typical distributed job. As always, it's not the actual task that's hard, but the job management. *Sour graping as to why MY profs never gave a a project like that*
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|