2019-3-15

makefile:

  1 myls : myls.o ls_l.o ls_i.o ls_h.o ls_a.o
  2     gcc myls.o ls_l.o ls_i.o ls_h.o ls_a.o -o myls
  3 clean :
  4     rm -rf *.o myls

主函数:

#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include "ls_l.h"
#include "ls_i.h"
#include "ls_h.h"
#include "ls_a.h"

int main (int argc, char **argv)
{
    int c;
    const struct option opt[] = {
        {"csss", required_argument, NULL, 'c'},
        {0,0,0,0}
    };

    if(argc < 2)
        return -1;

    while(1)
    {
        c = getopt_long(argc, argv, "l:i:h:a:", opt, NULL);
        {
            if(c == -1)
                break;

            switch(c)
            {
                case 'l' :
                    ls_l(optarg);
                    break;
                case 'i' :
                    ls_i(optarg);
                    break;
                case 'h' :
                    ls_a(optarg);
                    break;
                case 'a' :
                    ls_h(optarg);
                    break;
                case '?' :
                    printf("无效选项字符\n");
                    break;
                case 1 :
                    printf("不识别\n");
                    break;
                default :
                    break;
            }
        }
    }
    return 0;
}

LS_L.H: 实现ls  -l

#ifndef __LS_L_H
#define __LS_L_H

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

void ls_l(const char *path);

#endif

LS_L.C:

#include "ls_l.h"
#include <errno.h>
#include <string.h>

#define ARRSIZE 1024

void ls_l(const char *path)
{
    struct stat buf;
    struct passwd *pwd = NULL;
    struct group *get = NULL;
    struct tm *lo = NULL;
    char arr[ARRSIZE] = {};
    char *p = NULL;
    DIR *op = NULL;
    struct dirent *re = NULL;
    char brr[ARRSIZE] = {};

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

    if(S_ISDIR(buf.st_mode))
    {
        op = opendir(path);
        while(1)
        {
            re = readdir(op);
            if(re == NULL)
            {
                if(errno)
                {
                    perror("readdir");
                    closedir(op);
                    return ;
                }
                break;
            }

            memset(brr, '\0', ARRSIZE);
            strcpy(brr, path);
            strcat(brr, "/");
            strcat(brr, re->d_name);
//            strcpy(path, brr);

leap:    if(stat(brr, &buf) == -1)
        {
            perror("stat()");
            return ;
        }
        switch(buf.st_mode & S_IFMT)
        {
            case S_IFSOCK :
                putchar('s');
                break;
            case S_IFLNK :
                putchar('l');
                break;
            case S_IFREG :
                putchar('-');
                break;
            case S_IFBLK :
                putchar('b');
                break;
            case S_IFDIR :
                putchar('d');
                break;
            case S_IFCHR :
                putchar('c');
                break;
            case S_IFIFO :
                putchar('p');
                break;
            default :
                break;
        }

        if(buf.st_mode & S_IRUSR)
            putchar('r');
        else
            putchar('-');
        if(buf.st_mode & S_IWUSR)
            putchar('w');
        else
            putchar('-');
        if(buf.st_mode & S_IXUSR)
            if(buf.st_mode & S_ISUID)
                putchar('s');
            else
                putchar('x');
        else
            putchar('-');


        if(buf.st_mode & S_IRGRP)
            putchar('r');
        else
            putchar('-');
        if(buf.st_mode & S_IWGRP)
            putchar('w');
        else
            putchar('-');
        if(buf.st_mode & S_IXGRP)
            if(buf.st_mode & S_ISGID)
                putchar('s');
            else
                putchar('x');
        else
            putchar('-');

        if(buf.st_mode & S_IROTH)
            putchar('r');
        else
            putchar('-');
        if(buf.st_mode & S_IWOTH)
            putchar('w');
        else
            putchar('-');
        if(buf.st_mode & S_IXOTH)
            if(buf.st_mode & S_ISVTX)
                putchar('t');
            else
                putchar('x');
        else
            putchar('-');

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

        pwd = getpwuid(buf.st_uid);
        if(pwd == NULL)
        {
            if(errno)
            {
                perror("getpwuid()");
                return ;
            }
        }

        printf("%s ", pwd->pw_name);

        get = getgrgid(buf.st_gid);
        if(get == NULL)
        {
            if(errno)
            {
                perror("getgrnam()");
                return ;
            }
        }

        printf("%s ", get->gr_name);
        printf("%ld ", buf.st_size);

        lo = localtime(&(buf.st_mtim.tv_sec));
        if(lo == NULL)
        {
            fprintf(stderr, "localtime error");
            return ;
        }

        strftime(arr, ARRSIZE, "%m月  %d %H:%M", lo);
        printf("%s ", arr);

        p = strrchr(brr, '/');
        printf("%s\n", ++p);

        }
    }
        else
            goto leap;
    }

LS_i.H:实现 ls -i

#ifndef __LS_I_H
#define __LS_I_H

#include "ls_l.h"
#include <glob.h>
#include <string.h>

void ls_i(const char *path);

#endif

LS_i.C:

#include "ls_i.h"

#define BUFSIZE 1024

void ls_i(const char *path)
{
    glob_t pglob;

    struct stat buf;
    char *p = NULL;
    char arr[BUFSIZE] = {};
    char brr[BUFSIZE] = {};

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

    if(S_ISDIR(buf.st_mode))
    {
//        strcpy(brr, "\"");
        strcpy(brr, path);
        strcat(brr, "/*");
        if(glob(brr, 0, NULL, &pglob) != 0)
        {
            fprintf(stderr, "glob error\n");
            return ;
        }

        for(int i = 0; i < pglob.gl_pathc; i++)
        {
            if(stat((pglob.gl_pathv)[i], &buf) == -1)
            {
                perror("stat()");
                return ;
            }
            printf("%ld  ", buf.st_ino);
            memset(arr, '\0', BUFSIZE);
            strcpy(arr, (pglob.gl_pathv)[i]);
            p = strrchr(arr, '/');
            printf("%s\n", ++p);


        }
    }
    else
    {
        printf("%ld ",buf.st_ino);
        memset(arr, '\0', BUFSIZE);
        strcpy(arr, path);
        p = strrchr(arr, '/');
        printf("%s\n", ++p);

    }


}

LS_H.H : 实现 ls -a

#ifndef __LS_H_H
#define __LS_H_H

#include "ls_i.h"
#include <errno.h>

void ls_h(const char *path);

#endif

LS_H.C:

#include "ls_h.h"

void ls_h(const char *path)
{
    DIR *op = NULL;
    struct dirent *re = NULL;

    op = opendir(path);
    if(NULL == op)
    {
        perror("opendir");
        return ;
    }
    while(1)
    {
        re = readdir(op);
        if(re == NULL)
        {
            if(errno)
            {
                perror("readdir");
                return ;
            }
            else
                break;
        }

        printf("%s\n", re->d_name);

    }
}

LS_A.H : 实现 ls -h

#ifndef __LS_A_H
#define __LS_A_H

#include "ls_h.h"
#include <sys/types.h>
#include <dirent.h>

void ls_a(const char *path);

#endif

LS_A.C

#include "ls_a.h"


void ls_a(const char *path)
{
    DIR *op = NULL;
    struct dirent *re = NULL;
    char *p = NULL;

    op = opendir(path);
    if(NULL == op)
    {
        perror("opendir");
        return ;
    }
    while(1)
    {
        re = readdir(op);
        if(re == NULL)
        {
            if(errno)
            {
                perror("readdir");
                return ;
            }
            else
                break;
        }
        p = re->d_name;
        if(p[0] == '.')
            continue;
        printf("%s\n", re->d_name);

    }
}

 

posted @ 2019-03-15 17:12  二人了一  阅读(201)  评论(0编辑  收藏  举报