Link to home
Start Free TrialLog in
Avatar of hehe007
hehe007

asked on

Trying to emulate ls -l using c

Hey Guys,

I am trying to emulate ls -l command using c, that lists all the files in a directory. I don't want to use system call. What I am looking for is a way to do it using <dirent.h> header. May be start with a struct or something. Any ideas or solutions will be appreciated.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hehe007
hehe007

ASKER

anyone else?
If you want to keep your program portable, you can use ftell and fseek to determine the filesize (in bytes). You would have to fopen the filename using binary.
    http://www.cplusplus.com/reference/clibrary/cstdio/fseek/
    http://www.cplusplus.com/reference/clibrary/cstdio/ftell/
    http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

But, if want the permissions, group, owner, and date as well, then you will have to use non-standard C functions.
From the link in the first post:
What's in a struct dirent?
The POSIX 1003.1 standard defines only one required entry for struct dirent, an array of char named d_name. This is the entry's name as a standard NUL-terminated string. Anything else found in this structure is specific to your UNIX system.

That's right, everything else found in struct dirent is not portable. Strictly conforming systems might not have anything else in there at all. If you write code that uses any extra structure members, you'll have to flag it as not portable and, if you're feeling particularly friendly, include an alternate code path that does the same thing.

For example, many UNIXes include a d_type member and some additional constants that let you check a directory entry's type without making an additional stat() call. Besides saving you another system call, this non-portable extension saves you an expensive trip back to the file system for more metadata. The stat() function is notoriously slow on most UNIXes.

In this struct dirent definition is the ino_t d_fileno field:
This is the file serial number. For BSD compatibility, you can also refer to this member as d_ino. In the GNU system and most POSIX systems, for most files this the same as the st_ino member that stat will return for the file. See section 14.9 File Attributes.
Section 14.9.1 The meaning of the File Attributes, describes the attributes in structs stat and stat64, offering more information and links than the link in the first post.

This man page, stat, has source code which you can complete to emulate 'ls -l'. But, you may need the function defined in https://www.experts-exchange.com/questions/21742284/how-to-get-file-permissions.html (see Q&A for reason).
From all the above links, I had written the code and it works fine. If you are having any problems doing it, then you can post your attempt, explain what problem you are having, and we can help you through it.

FYI - It is expected that you provide feedback on your progress to get further assistance.
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.