tags:

views:

1277

answers:

5

Hi,

I am implementing unix ls command in c with all option.But i am not getting any idea about ls -l command.

please can some body give me direction of this or give one sample program for it.

Thanks

+4  A: 

See the stat functions family.

Edit: these functions will give you a stat structure. The st_mode member of this structure contains the permissions.

Bastien Léonard
A: 

If you're reinventing the wheel, you have to read its source code. If you just want to get some output from it, you can use system("ls -l");

Joset
I think the OP needs to demonstrate his understanding of how stat() works ... -1.
Tim Post
+2  A: 

This is tagged homework, I'm not going to write it for the OP.

The source code to the core utilities did not lend any guidance? Granted, you may not want some of the portability hacks, you also might want to cater to 'fluid width' terminals. And, GNU delights in making their own mini libs in object form to make those utilities work.

However, they are worth studying.

For a great grade, there is also the source to the midnight commander.

Turn in an app that automatically detects curses and does something spectacular, I guarantee a great grade if you do :) You'll also learn quite a bit while doing it.

Not sure if you have time for the above, just a suggestion :)

Remember, free/open software means your ability to study the code, to figure out how it works, especially largefile support :)

Tim Post
I just love fly by night down voters, really. If you click -1, leave a comment.
Tim Post
A: 

You probably write it a bit like this

time4tea
+2  A: 

you will have to use those 2 structures : dirent and stat

as far as I can remember ... you will probably have to start by opening the directory with opendir, which can be done this way

DIR *dirp;
dirp = opendir("path_to_a_directory");

then you can use the dirent structure while using the directory stream you just opened

struct dirent *dp;
while ((dp = readdir(dirp)) != NULL)

then you for every file inside the directory, you can use the stat structure with the stat function, to get info such as time of modification, etc ...

struct stat statbuf;
stat(dp->d_name, &statbuf);

see man 2 stat, and look closely at the structures involved to see what informations you can get from them :)

and don't forget to close what you open (closedir) :-)

hope it helped.

Sylvain
Yep. That's the way I implemented it when I had to.
Geo
Yeah I had to do it for school too like 3 years ago :-)
Sylvain