UC進階編程,實現myls程式

來源:互聯網
上載者:User

標籤:style   blog   http   使用   strong   檔案   

跟著達內視頻,學習UC進階編程,完成程式小練習。

主要練習的函數為:

 int lstat(const char *path, struct stat *buf);

 size_t strftime(char *s, size_t max, const char *format,  const struct tm *tm);此函數, 第一次使用。

time_t mktime(struct tm *tm);//把分離的時間合成整數,寫項目代碼中,當時自己實現了這個函數功能。

#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <time.h>#include <grp.h>#include <pwd.h>   void show01(struct stat st)//檔案屬性-rwxr--r--{/*列印檔案類型*/    if (S_ISLNK(st.st_mode)) {        printf("l");    } else if (S_ISREG(st.st_mode)) {        printf("-");    } else if (S_ISDIR(st.st_mode)) {        printf("d");    } else if (S_ISCHR(st.st_mode)) {        printf("c");    } else if (S_ISBLK(st.st_mode)) {        printf("b");    } else if (S_ISFIFO(st.st_mode)) {        printf("f");    } else if (S_ISSOCK(st.st_mode)) {        printf("s");    }/*U檔案所有者的許可權*/    if (st.st_mode & S_IRUSR){        printf("r");    } else {        printf("-");    }    if (st.st_mode & S_IWUSR){        printf("w");    } else {        printf("-");    }    if (st.st_mode & S_IXUSR){        printf("x");    } else {        printf("-");    }/*G檔案所有組的許可權*/    if (st.st_mode & S_IRGRP){        printf("r");    } else {        printf("-");    }    if (st.st_mode & S_IWGRP){        printf("w");    } else {        printf("-");    }    if (st.st_mode & S_IXGRP){        printf("x");    } else {        printf("-");    }/*O其它使用者的許可權*/    if (st.st_mode & S_IROTH){        printf("r");    } else {        printf("-");    }    if (st.st_mode & S_IWOTH){        printf("w");    } else {        printf("-");    }    if (st.st_mode & S_IXOTH){        printf("x");    } else {        printf("-");    }printf(" ");}void show02(struct stat st)//永久連結數{printf("%lu", st.st_nlink);printf(" ");}void show03(struct stat st)//使用者名稱{struct passwd *psd ;psd = getpwuid(st.st_uid);    printf("%s", psd->pw_name);printf(" ");}void show04(struct stat st)//組名{struct group *grp = getgrgid(st.st_gid);printf("%s", grp->gr_name);printf(" ");}void show05(struct stat st)//檔案大小{printf("%lu", st.st_size);printf(" ");}void show06(struct stat st)//檔案時間{char timebuf[20];struct tm* newtime = localtime(&st.st_mtime);strftime(timebuf, 20,"%B %d %H:%M",newtime);printf("%s", timebuf);                printf(" ");}void show07(const char *fname)//檔案名稱{printf("%s", fname);                printf(" ");}int main(int argc, const char *argv[]){int ret = 0;struct stat st;if(argc<2){printf("./a.out file\n");return ;}ret = lstat(argv[1], &st);if(ret<0) perror("lstat()");show01(st);show02(st);show03(st);show04(st);show05(st);show06(st);show07(argv[1]);puts("");//換行return 0;}

函數的介面設計的方面,不是很合理,主要是練習函數的使用。 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.