Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <sys/types.h>
    #include <dirent.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char** argv)
        DIR* dp;
        struct dirent* dentry;

        if(argc != 2) {
            printf("usage: my_ls dirname\n");
            exit(1);
        }

        if(! (dp=opendir(argv[1])) ) {
            printf("opendir error\n");
            exit(2);
        }
    
        while(1) {
            dentry=readdir(dp);
            if(!dentry)
                break;

            printf("%s\n", dentry->d_name);
        }
        return 0;
    }

I'm trying to make this ls program's output
doesn't show . or ..
and ls -l output shows
- in this order : directory(d)/file(-)/simbolic link(l)
- permission (rwxrwxrwx)
- the size of the file

and can't find a clue how to make it so.


//The simpler the better!! Thank you

What I have tried:

tried using st_mode but can't find a way to put it properly
Posted
Updated 13-Nov-16 1:07am
v4

1 solution

See readdir(3): read directory - Linux man page[^]. It gives all the information you need about the directory entry. Once you have that entry you need to use fstat(2): file status - Linux man page[^] to get the details of the file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900