码迷,mamicode.com
首页 > 其他好文 > 详细

myls

时间:2019-03-15 19:00:57      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:mod   while   dna   const   end   uname   dep   types.h   null   

/*
尝试实现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

 

 

myls

标签:mod   while   dna   const   end   uname   dep   types.h   null   

原文地址:https://www.cnblogs.com/Mr-zzzzzz/p/10538582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!