myls

/*
尝试实现ls命令的功能 加选项-l -a -i -h
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mystat.h"
#include "dir_reg.h"
#include "readblocks.h"
#include "checkdir.h"

int inodeprint(char *optarg)
{
    struct stat mystat1;
    int a;

    a = stat(optarg, &mystat1);
    if(a == -1)
    {
        perror("stat()");
        return 1;
    }
    printf("%ld ",mystat1.st_ino);
    return 0;
}

int main(int argc, char **argv)
{
    char *optstring = "-l:i:a:h:";
    int c;

    while(1)
    {
        c = getopt(argc, argv, optstring);
        if(c == -1)
            break;
        switch(c)
        {
            case 'l':
                long_print(optarg);
                break;
            case 'i':
                inodeprint(optarg);
                printf("%s\n", optarg);
                break;
            case 'a':
                check_dir(optarg);
                break;
            case 'h':
                blocksprint(optarg);
                break;
            case '?':
                printf("没有此选项: %s\n", argv[optind-1]);
                break;
            case 1:
                printf(" %s\n", argv[optind-1]);
                break;
            default:
                break;
        }
    }

    return 0;
}
#include <stdio.h>
#include <string.h>
#include <glob.h>
#include "mystat.h"
#include "dir_reg.h"

int long_print(char *optarg)
{
    char *p = NULL;
    glob_t pglob;
    int i = 0;
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include "mystat.h"

#define BUFSIZE 1024

int mystat(char *optarg)
{
    struct stat mystat;
    struct passwd *paswdname = NULL;
    struct group *gruname = NULL;
    struct tm *tmp = NULL;
    char buf[BUFSIZE] = {};
    int a;

    a = stat(optarg, &mystat);
    if(a == -1)
    {
        printf("----------error---------");
        perror("stat()");
        return 1;
    }
    //文件类型
    switch(mystat.st_mode & S_IFMT)
    {
        case S_IFREG : printf("-"); break;
        case S_IFDIR : printf("d"); break;
        case S_IFCHR : printf("c"); break;
        case S_IFBLK : printf("b"); break;
        case S_IFSOCK : printf("s"); break;
        case S_IFLNK : printf("l"); break;
        case S_IFIFO : printf("p"); break;
        default :
            break;
    }
    //文件的权限
    if(mystat.st_mode & S_IRUSR)
        putchar('r');
    else
        putchar('-');
    if(mystat.st_mode & S_IWUSR)
        putchar('w');
    else
        putchar('-');
    if(mystat.st_mode & S_IXUSR)
    {
        if(mystat.st_mode & S_ISUID)
            putchar('s');
        else
            putchar('x');
    }
    else
        putchar('-');
    if(mystat.st_mode & S_IRGRP)
        putchar('r');
    else
        putchar('-');
    if(mystat.st_mode & S_IWGRP)
        putchar('w');
    else
        putchar('-');
    if(mystat.st_mode & S_IXGRP)
    {
        if(mystat.st_mode & S_ISGID)
            putchar('s');
        else
            putchar('x');
    }
    else
        putchar('-');
    if(mystat.st_mode & S_IROTH)
        putchar('r');
    else
        putchar('-');
    if(mystat.st_mode & S_IWOTH)
        putchar('w');
    else
        putchar('-');
    if(mystat.st_mode & S_IXOTH)
    {
        if(mystat.st_mode & S_ISVTX)
            putchar('t');
        else
            putchar('x');
    }
    else
        putchar('-');

    printf(" %ld ",mystat.st_nlink);

    paswdname = getpwuid(mystat.st_uid);
    printf("%s ", paswdname->pw_name);

    gruname = getgrgid(mystat.st_gid);
    printf("%s ", gruname->gr_name);

    printf("%ld ",mystat.st_size);

    tmp = localtime(&mystat.st_mtime);
    if(tmp == NULL)
        return 1;

    strftime(buf, BUFSIZE, "%m月  %d %H:%M", tmp);
    printf("%s ", buf);

    printf("%s ", optarg);

    putchar('\n');
    
    return 0;
}
 
     
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glob.h>
#include <string.h>
#include "readblocks.h"

#define BUFSIZE 1024

int is_dian_dir(const char *path)
{
    char *p = NULL;
    p = strrchr(path, '/');

    if(p != NULL)
    {
        if(!strcmp(p, "/.") || !strcmp(p, "/.."))
            return 1;
    }
    return 0;
}
int sum_blocks(const char *path)
{
    int sum = 0;
    glob_t pglob;
    struct stat mystat;
    char buf[BUFSIZE] = {};

    if(lstat(path, &mystat) < 0)
    {
        perror("lstat()");
        return -1;
    }
    if(!S_ISDIR(mystat.st_mode))
    {
        return mystat.st_blocks;
    }
    //是目录
    sum += mystat.st_blocks;//目录本身

    strcpy(buf, path);
    strcat(buf, "/*");
    glob(buf, 0, NULL, &pglob);

    //隐藏文件
    memset(buf, '\0', BUFSIZE);
    strcpy(buf, path);
    strcat(buf, "/.*");
    glob(buf, GLOB_APPEND, NULL, &pglob);

    for(int i = 0; i < pglob.gl_pathc; i++)
    {
        if(is_dian_dir((pglob.gl_pathv)[i]))
            continue;
        sum += sum_blocks((pglob.gl_pathv)[i]);
    }

    globfree(&pglob);

    return sum;
    

}

int mydu(const char *path)
{
    int blks;

    blks = sum_blocks(path);

    return blks/2;
}

int blocksprint(char *optarg)
{

    printf("%d K\n", mydu(optarg));

    return 0;
}

实现结果:

.uplooking@zl-pc:~/桌面/2019-2-20_UEA/高级编程/03day/myls$ ./myls -l .
drwxrwxr-x 2 uplooking uplooking 4096 03月  15 17:30 . 
uplooking@zl-pc:~/桌面/2019-2-20_UEA/高级编程/03day/myls$ ./myls -l "./*"
-rw-rw-r-- 1 uplooking uplooking 527 03月  15 13:09 ./checkdir.c 
-rw-rw-r-- 1 uplooking uplooking 81 03月  15 13:09 ./checkdir.h 
-rw-rw-r-- 1 uplooking uplooking 2080 03月  15 17:30 ./checkdir.o 
-rw-rw-r-- 1 uplooking uplooking 375 03月  15 13:41 ./dir_reg.c 
-rw-rw-r-- 1 uplooking uplooking 82 03月  14 23:29 ./dir_reg.h 
-rw-rw-r-- 1 uplooking uplooking 1976 03月  15 17:30 ./dir_reg.o 
-rw-rw-r-- 1 uplooking uplooking 145 03月  15 17:09 ./makefile 
-rwxrwxr-x 1 uplooking uplooking 14528 03月  15 17:30 ./myls 
-rw-rw-r-- 1 uplooking uplooking 985 03月  15 17:09 ./myls.c 
-rw-rw-r-- 1 uplooking uplooking 2904 03月  15 17:30 ./myls.o 
-rw-rw-r-- 1 uplooking uplooking 2241 03月  15 09:07 ./mystat.c 
-rw-rw-r-- 1 uplooking uplooking 74 03月  14 23:34 ./mystat.h 
-rw-rw-r-- 1 uplooking uplooking 4032 03月  15 17:30 ./mystat.o 
-rw-rw-r-- 1 uplooking uplooking 1202 03月  15 17:08 ./readblocks.c 
-rw-rw-r-- 1 uplooking uplooking 185 03月  15 17:08 ./readblocks.h 
-rw-rw-r-- 1 uplooking uplooking 3296 03月  15 17:30 ./readblocks.o 
-rw-rw-r-- 1 uplooking uplooking 183 03月  15 13:18 ./readme
uplooking@zl-pc:~/桌面/2019-2-20_UEA/高级编程/03day/myls$ ./myls -i .
3277517 .
uplooking@zl-pc:~/桌面/2019-2-20_UEA/高级编程/03day/myls$ ./myls -a .
.
checkdir.h
dir_reg.o
mystat.c
mystat.o
readblocks.h
myls
readblocks.c
myls.c
dir_reg.c
mystat.h
myls.o
checkdir.c
checkdir.o
readme
makefile
dir_reg.h
..
readblocks.o
uplooking@zl-pc:~/桌面/2019-2-20_UEA/高级编程/03day/myls$ ./myls -h .
84 K

 

 

posted on 2019-03-15 17:48  赵___雷 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Mr-zzzzzz/p/10538582.html

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、付费专栏及课程。

余额充值