linux系統編程之檔案與IO(六):實現ls

來源:互聯網
上載者:User

本文利用以下系統調用實現ls -l命令的功能:

 

1,lstat:獲得檔案狀態,

2,getpwuid:

#include <pwd.h>

struct passwd *getpwuid(uid_t uid);

描述:

The getpwuid() function returns a pointer to a structure containing the broken-out fields of the record in the password database  that  matches the user ID uid.

傳回值:
The passwd structure is defined in <pwd.h> as follows:

    struct passwd {
        char   *pw_name;       /* username */
        char   *pw_passwd;     /* user password */
        uid_t   pw_uid;        /* user ID */
        gid_t   pw_gid;        /* group ID */
        char   *pw_gecos;      /* real name */
        char   *pw_dir;        /* home directory */
        char   *pw_shell;      /* shell program */
    };

樣本:

#include <unistd.h><stdlib.h><stdio.h><pwd.h> ERR_EXIT(m) \    ( main( passwd *=((pw = getpwuid(uid)) ==,pw->,pw->,pw->,pw->    printf(,pw->,pw-> 

運行結果:

<stdlib.h><stdio.h> ERR_EXIT(m) \ ( main( argc, **(argc != , argv[ buf[(readlink(argv[],buf,) ==-,argv[

運行結果:

<unistd.h><stdlib.h><dirent.h><sys/types.h><sys/stat.h><.h><time.h><pwd.h><grp.h><libgen.h> ERR_EXIT(m) \ ( lsdir( * lsfile( * lsfile( * getFileType( stat * getFilePerm( stat *st, * main( argc, **(argc != ,argv[(lstat(argv[],&fstat) == - lsdir( ** filename[] = {=(dir == dirent *((dentry = readdir(dir)) != *= dentry->(strncmp(fname,,) == lsfile( *(lstat(filename,&tmpstat) == - buf[]= {= getFileType(& *bname =] =(type == content[(readlink(filename,content,) == -& tm *= localtime(&->->->tm_mon+->->-> getFileType( stat * type = (st->st_mode &= = = = = = = getFilePerm( stat *st, *= st-> (mode &] = (mode &] = (mode &] = (mode &] = (mode &] = (mode &] = (mode &] = (mode &] = (mode &] =

運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.