linux下實現簡易ll命令

來源:互聯網
上載者:User

標籤:

/*    實現ll命令,輸出指定目錄下詳細資料    格式:ll -l dirname    目錄採用快速排序,按字典順序排序*/#include <stdio.h>#include <dirent.h>#include <stdlib.h>#include <sys/stat.h>#include <string.h>#include <time.h>#include <grp.h>#include <pwd.h>#define NUMLINE 40#define NUMCHAR 20/*    產生屏蔽字*/void mode_to_letters(int mode, char str[]){    strcpy(str, "----------");    if (S_ISDIR(mode)) str[0] = ‘d‘;    if (S_ISCHR(mode)) str[0] = ‘c‘;    if (S_ISBLK(mode)) str[0] = ‘b‘;        if (mode & S_IRUSR) str[1] = ‘r‘;    if (mode & S_IWUSR) str[2] = ‘w‘;    if (mode & S_IXUSR) str[3] = ‘x‘;        if (mode & S_IRGRP) str[4] = ‘r‘;    if (mode & S_IWGRP) str[5] = ‘w‘;    if (mode & S_IXGRP) str[6] = ‘x‘;        if (mode & S_IROTH) str[7] = ‘r‘;    if (mode & S_IWOTH) str[8] = ‘w‘;    if (mode & S_IXOTH) str[9] = ‘x‘;        str[10] = ‘\0‘;}/*    字串交換*/void exchange(char *s1, char *s2){    char buf[20];    strcpy(buf, s1);        strcpy(s1, s2);    strcpy(s2, buf);}/*    字串排序*/void sort(char filename[][NUMCHAR], int beg, int end){    int n = beg + 1;    int m = end;    if (beg >= end)        return;    while ( n < m)        if (strcmp(filename[n], filename[beg]) > 0)             while (n < m)                if (filename[m] < filename[beg]) {                    exchange(filename[m--], filename[n++]);                    break;                }                else                    m--;        else             n++;    if (strcmp(filename[n], filename[beg]) > 0)        exchange(filename[--n], filename[beg]);    else        exchange(filename[n], filename[beg]);    sort(filename, 0, n-1);    sort(filename, n+1, end);}/*    通過getpwuid函數,由使用者id獲得使用者名稱*/char* get_username(uid_t uid){    struct passwd *ptr;    if ((ptr=getpwuid(uid)) == NULL){        fprintf(stderr, "get username error");        exit(1);    }    return ptr->pw_name;}/*    由組id獲得組名*/char* get_groupname(gid_t gid){    struct group *ptr;    if ((ptr = getgrgid(gid)) == NULL) {        fprintf(stderr, "get groupname error");        exit(1);    }    return ptr->gr_name;}/*    時間格式轉換*/void timetrans(time_t *tim, char* s){    struct tm *temp;    temp = localtime(tim);    strftime(s, 20, "%m %e %R", temp);}int main(int argc, char* argv[]){    DIR* dr;    struct dirent* dir;    char filename[NUMLINE][NUMCHAR];    struct stat buf;    int n, m, p;    char s1[11];    char tim[20];    struct tm *temp;    if (argc < 3) {        fprintf(stderr, "ls02:missing operand");        exit(1);    }    if (strcmp(argv[1], "-l")) {        fprintf(stderr, "ls02: operand:%s is wrong", argv[1]);        exit(1);    }    dr = opendir(argv[2]);    if (dr == NULL) {        perror(argv[2]);        exit(1);    }    m = 0;    while ((dir = readdir(dr)) != NULL) {         if (strcpy(filename[m], argv[2]) == NULL) {            perror("strcpy error");            exit(1);        }        strcpy(filename[m], argv[2]);        p = 0;        while(filename[m][p++] != ‘\0‘) ;        strcpy(&filename[m][p-1], dir->d_name);        m++;    }    sort(filename, 0, --m);    for (n = 0;n <= m;n++)         if (stat(filename[n], &buf) == -1)            printf("stat error\n");        else {            mode_to_letters(buf.st_mode, s1);            timetrans(&buf.st_mtime, tim);            printf("%s %4d %s ", s1, buf.st_nlink, get_username(buf.st_uid));            printf("%s %7d %s %s\n", get_groupname(buf.st_gid), (int)buf.st_size, tim, &filename[n][p-1]);        }        closedir(dr);}

總結:

在快速排序處耽誤了時間

二維數組做函數形參,第2維不可為空

/*    ls命令    格式: ls dirname*/#include <stdio.h>#include <dirent.h>#include <stdlib.h>int main(int argc, char* argv[]){    DIR* dr;    struct dirent* dir;    if (argc == 1) {        fprintf(stderr, "ls01:missing operand");        exit(1);    }    dr = opendir(argv[1]);    if (dr == NULL) {        perror(argv[1]);        exit(1);    }    while ((dir = readdir(dr)) != NULL)        //if (dir->d_name[0] != ‘.‘)            printf("%s ", dir->d_name);    printf("\n");    closedir(dr);}


linux下實現簡易ll命令

聯繫我們

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