简单的unix ls编程用C语言编写 [英] Simple unix ls programming in C

查看:132
本文介绍了简单的unix ls编程用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;
    }



我正试图让这个ls程序的输出

没有显示。或..

和ls -l输出显示

- 按此顺序:目录(d)/文件( - )/ simbolic链接(l)

- 权限(rwxrwxrwx)

- 文件的大小



并且无法找到如何使其成为现实的线索。





//越简越好!!谢谢



我尝试了什么:



尝试使用st_mode但是无法找到正确的方法


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

推荐答案

请参阅 readdir(3):读取目录 - Linux手册页 [ ^ 。它提供了有关目录条目的所有信息。获得该条目后,您需要使用 fstat(2):文件状态 - Linux手册页 [ ^ ]以获取文件的详细信息。
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.


这篇关于简单的unix ls编程用C语言编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆