自己编写的linux ls命令

 

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


int aflag = 0;
int lflag = 0;

typedef char * datatype;
typedef struct _node_
{
    char data[256];
    struct _node_ *next;
}linknode,*linklist;

void Display_file(char *path,char *name);   //打印文件的信息
void Mode(struct stat buf);                               //查看文件的各种属性
void SeleFi(struct stat buf);                             //打印文件的类型
void Max(struct stat buf);                                 //
void Time(struct stat buf);                               //打印修改文件的时间
void GetUID(struct stat buf);                           //获得UID并打印
void GetGID(struct stat buf);                           //获得GID并打印
void Display_Dir(linklist list,char *name);   //打印目录
void ifDir(linklist list,char *path);                   //判断是否是目录
void getflag(int argc,char **argv,int *aflag,int *lflag);       //获得-a或-l的参数
void GetName(linklist list,char *name);       //获得目录中所有文件名

linklist CreateEmptyLinklist()
{
    linklist h=(linklist)malloc(sizeof(linknode));
    h->next=NULL;
    return h;
}

int EmptyLinklist(linklist h)
{
    return (NULL==h->next);
}

int LengthLinklist(linklist h)
{
    int i=-1;
    while(h->next!=NULL) {
        i++;
        h=h->next;
    }
    return i;
}

void VisitLinklist(linklist h)
{
    h=h->next;
    while(h!=NULL) {
//        printf("%d\n",h->data);
        Display_file(h->data,h->data);
        h=h->next;
    }
    return ;
}

void InsertLinklist_2(linklist h,datatype x)
{
    while((h->next!=NULL)&&(strcmp(h->next->data,x) < 0)) {
        h=h->next;
    }
    linklist new=(linklist)malloc(sizeof(linknode));
    strcpy(new->data,x);
    new->next=h->next;
    h->next=new;
    return ;
}

void DeleteLinklist_2(linklist h,datatype x)
{
    linklist p;
    p=h->next;
    while(p!=NULL) {
        if(p->data==x) {
            h->next=p->next;
            free(p);
        }
        h=h->next;
        p=p->next;
    }
}


int main(int argc,char **argv)
{
    struct stat buf;
    int ch;
    int i;

    linklist list = CreateEmptyLinklist();
    getflag(argc,argv,&aflag,&lflag);

    for ( i = optind ; i < argc ; i++ ) {
        ifDir(list,argv[i]);
    }
    if (optind == argc) {
        Display_Dir(list,".");
    }

    VisitLinklist(list);
   
    printf("\n");
    return 0;
}

void SeleFi(struct stat buf)
{

    if ( S_ISREG(buf.st_mode) ) {
        printf("-");
    }
    if ( S_ISDIR(buf.st_mode) ) {
        printf("d");
    }
    if ( S_ISCHR(buf.st_mode) ) {
        printf("c");
    }
    if ( S_ISBLK(buf.st_mode) ) {
        printf("b");
    }
    if ( S_ISFIFO(buf.st_mode) ) {
        printf("p");
    }
    if ( S_ISLNK(buf.st_mode) ) {
        printf("l");
    }
    if ( S_ISSOCK(buf.st_mode) ) {
        printf("s");
    }   
}


void Mode(struct stat buf)
{
    if ( (buf.st_mode)&S_IRUSR ) {
        printf("r");
    }else {
        printf("-");
    }

    if ( (buf.st_mode)&S_IWUSR ) {
        printf("w");
    }else {
        printf("-");
    }

    if ( (buf.st_mode)&S_IXUSR ) {
        printf("x");
    }else {
        printf("-");
    }

    if ( (buf.st_mode)&S_IRGRP ) {
        printf("r");
    }else {
        printf("-");
    }

    if ( (buf.st_mode)&S_IWGRP ) {
        printf("w");
    }else {
        printf("-");
    }

    if ( (buf.st_mode)&S_IXGRP ) {
        printf("x");
    }else {
        printf("-");
    }

    if ( (buf.st_mode)&S_IROTH ) {
        printf("r");
    }else {
        printf("-");
    }

    if ( (buf.st_mode)&S_IWOTH ) {
        printf("w");
    }else {
        printf("-");
    }

    if ( (buf.st_mode)&S_IXOTH ) {
        printf("x");
    }else {
        printf("-");
    }
    printf(" ");
}


void Max(struct stat buf)
{
    printf("%8ld",buf.st_size);
    printf(" ");
}


void Time(struct stat buf)
{
    struct tm *tp;
    tp = localtime(&(buf.st_ctime));
    printf("%2d-%2d-%2d %2d:%2d",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday,tp->tm_hour,tp->tm_min);
    printf(" ");
}

void GetUID(struct stat buf)
{
    struct passwd *p;
    p = getpwuid(buf.st_uid);
    printf("%3s ",p->pw_name);
}

void GetGID(struct stat buf)
{
    struct group *g;
    g = getgrgid(buf.st_gid);
    printf("%3s ",g->gr_name);

}

void Display_file(char *path,char *name)
{
    struct stat buf;

    if ( stat(path,&buf) == -1 ) {
        perror("ls:");
        return ;
    }

    if ( lflag == 1 ) {
        SeleFi(buf);
        Mode(buf);
        printf("%3d ",buf.st_nlink);
        GetUID(buf);
        GetGID(buf);
        Max(buf);
        Time(buf);
        printf("%s\n",name);
        return ;
    }
    printf("%s  ",name);
}

void Display_Dir(linklist list,char * dirname)
{
    DIR *mydir;
    struct dirent *dir;
    char path[256];
    if ( (mydir = opendir(dirname)) == NULL ) {
        perror("DIR:");
        return ;
    }

    printf("%s :\n",dirname);
    while ( (dir = readdir(mydir)) != NULL ) {
        if ( dir->d_name[0]== '.' ) {
            if ( aflag == 0 ) {
                continue;
            }
        }
        sprintf(path,"%s/%s",dirname,dir->d_name);
//        Display_file(path,dir->d_name);
        GetName(list,dir->d_name);
    }
    closedir(mydir);
}

void ifDir(linklist list,char *path)
{
    struct stat buf;

    if ( stat(path,&buf) < 0 ) {
        perror("path:");
    }

    if ( S_ISDIR(buf.st_mode) ) {
        Display_Dir(list,path);
    }else {
//        Display_file(path,path);
        GetName(list,path);
    }
}

void getflag(int argc,char **argv,int *aflag,int *lflag)
{
    int ch;

    while ( (ch = getopt(argc,argv,"la")) != EOF ) {
        switch (ch) {
            case 'a':
                *aflag = 1;
                break;
            case 'l':
                *lflag = 1;
                break;
            default:
                printf("Wrong option !\n");
                break;
        }
    }

}
         
void GetName(linklist list,char *name)
{
    InsertLinklist_2(list,name);
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
myls命令包含9个选项: (1) myls : 在缺省选项的情况下,列出当前文件夹下的普通文件(不包含隐藏文件)。 例如:当前目录包含文件home1.c, home2.c, .home3.c,输入myls后,列出的文件名为home1.c, home2.c. (2) myls –a: 列出当前文件夹下的所有文件(包含隐藏文件)。 例如:当前目录包含文件home1.c、home2.c、.home3.c,输入myls -a后,列出所有的文件名为home1.c, home2.c, .home3.c. (3) myls –l: 列出当前文件夹下普通文件的详细信息,包括文件模式,文件链接数,文件所属用户,文件所属用户组,文件大小,文件最后修改时间,文件名。并且在最后一行显示该目录下所显示的文件的文件块数。 例如:显示drwxr-xr-x 3 eli eli 4096 Nov 16 23:07 Desktop (4) myls -R 递归列出文件及其子文件。 例如:当前目录为home, 其中包含文件home1, home2, home3. 其中home1是目录文件,包含文件home11, home12, home2不是目录文件,home3是目录文件,包含文件home31, home32。 输入myls –R后,列出的文件名为 ./home: home1 home2 home3 ./home/home1: home11 home12 ./home/home3: home31 home32. (5) myls –u: 列出当前文件夹下用户x的普通文件,若输入myls -u bb,则显示所属bb的普通文件。 例如:文件home1, home2, home3属于aa,文件tmp1, tmp2, tmp3属于bb, 则若输入myls –u aa, 则显示home1,home2,home3,若输入myls -u bb, 则显示tmp1,tmp2,tmp3。 (6) myls –S: 对文件进行排序,需要输入比较参数。 myls –S 的参数包括: time——按最近修改时间排序 name——按文件名的字典序排序 size——按文件的大小从小到大排序 link——按文件链接数从少到多排序 (7) myls -1: 将当前文件夹下的文件按照一行一个的方式显示。 (8) myls –s: 在各个文件开头显示这个文件的文件块大小。 (9) myls /dirname: 显示/dirname下的文件。 编译 gcc main.c -o myls 执行 ./myls 可加若干参数,具体见上描述 程序并不完整,可能会有BUG,希望广大网友指点,交流~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值