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.


Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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.

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/Programming/Languages/C/Q_21742284.html (see Q&A for reason).
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

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.