C语言实现Linux下的ls命令。

来源:互联网 发布:mac chrome json 插件 编辑:程序博客网 时间:2024/05/22 12:14

对于C语言来说,我的水平不是很高,但一下的代码都是我亲自动手在Linux环境下跑过的。希望能让大家满意!

#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>

#define  IFMT  (p.st_mode & S_IFMT)

#define  RUSR  (p.st_mode & S_IRUSR)
#define  WUSR  (p.st_mode & S_IWUSR)
#define  XUSR  (p.st_mode & S_IXUSR)

#define  RGRP  (p.st_mode & S_IRGRP)
#define  WGRP  (p.st_mode & S_IWGRP)
#define  XGRP  (p.st_mode & S_IXGRP)

#define  ROTH  (p.st_mode & S_IROTH)
#define  WOTH  (p.st_mode & S_IWOTH)
#define  XOTH  (p.st_mode & S_IXOTH)


void S_IG(struct stat p);
void S_IS(struct stat p);
void S_IO(struct stat p);
void passwd_uid(int uid);
void group_uid(int gid);
void st_tm(time_t *q);
void type(struct stat p);

int main(int argc ,char *argv[])
{
    struct stat p;
    struct dirent *s;
    DIR *m;
   
    if(argc < 2){
        m = opendir(".");
        while((s = readdir(m)) != NULL){
            if(s->d_name[0] != '.'){
                printf("%-7s",s->d_name);
            }
        }
        printf("\n");
    }

    if(argc == 2){
        m = opendir(".");
        if(strcmp(argv[1],"-a") == 0){
            while((s = readdir(m)) != NULL){
                printf("%-7s",s->d_name);
            }
            printf("\n");
        }
        else if(strcmp(argv[1],"-l") == 0){
            while((s = readdir(m)) != NULL){
                if(stat(s->d_name,&p) == -1){
                    fprintf(stderr, "%s\n", strerror(errno));
                }
                else{
                    if(s->d_name[0] != '.'){
                        type(p);    
                        S_IS(p);
                        S_IG(p);
                        S_IO(p);
                        printf("%2d",p.st_nlink);
                        passwd_uid(p.st_uid);
                        group_uid(p.st_gid);
                        printf("%6ld",p.st_size);
                        st_tm(&p.st_mtime);
                        printf("%7s\n",s->d_name);
                    }
                }
            }
        }
        else{
            m = opendir(argv[1]);
            if(opendir == NULL){
                fprintf(stderr, "open file %s:%s\n",argv[1],strerror(errno));
                exit(1);
            }
            while((s = readdir(m)) != NULL){
                if(s->d_name[0] != '.'){
                    printf("%-7s",s->d_name);
                }
            }
            printf("\n");
        }
    }
    closedir(m);
    return 0;   
}

void type(struct stat p)
{
    if(S_ISREG(IFMT)){
        printf("-");
    }
    if(S_ISDIR(IFMT)){
        printf("d");
    }
    if(S_ISCHR(IFMT)){
        printf("c");
    }
    if(S_ISBLK(IFMT)){
        printf("b");
    }
}

void S_IS(struct stat p)
{
    if(S_IRUSR == RUSR){
        printf("r");
    }
    else
        printf("-");
    if(WUSR == S_IWUSR){
        printf("w");
    }
    else
        printf("-");
    if(XUSR == S_IXUSR){
        printf("x");
    }
    else
        printf("-");
}

void S_IG(struct stat p)
{
    if(S_IRGRP == RGRP){
        printf("r");
    }
    else
        printf("-");
    if(WGRP == S_IWGRP){
        printf("w");
    }
    else
        printf("-");
    if(XGRP == S_IXGRP){
        printf("x");
    }
    else
        printf("-");
}

void S_IO(struct stat p)
{
    if(S_IROTH == ROTH){
        printf("r");
    }
    else
        printf("-");
    if(WOTH == S_IWOTH){
        printf("w");
    }
    else
        printf("-");
    if(XOTH == S_IXOTH){
        printf("x");
    }
    else
        printf("-");
}

void passwd_uid(int uid){
    struct passwd *q;
    q = getpwuid(uid);
    printf("%8s",q->pw_name);   
}


void group_uid(int gid){
    struct group *q;
    q = getgrgid(gid);
    printf("%8s",q->gr_name);   
}

void st_tm(time_t *q){
    struct tm *tm;
    char str[100];
    tm = gmtime(q);
    strftime(str,100,"%F %R ",tm);
    printf("%20s",str);
}