myls.h
#ifndef __MYLS_H
#define __MYLS_H#define BUFSIZE 1024static int sum_blocks(const char *path);static int mydu(const char *path);static int is_dot_dir(const char *path);int myls_h(const char *path);int myls_a(char *path);int myls_i(char *path);int myls_l(char *path);int allstat(char *path);int myls(int argc,char **argv);#endifmyls.c
#include "myls.h"
#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<sys/stat.h>#include<string.h>#include<glob.h>#include<errno.h>#include<dirent.h>#include<time.h>#include<grp.h>#include<pwd.h>/*实现ls -h功能*/int myls_h(const char *path){ glob_t globres; struct stat cur_stat; char buf[BUFSIZE] = {}; if (lstat(path, &cur_stat) < 0) { perror("lstat()"); return 1; } if (!S_ISDIR(cur_stat.st_mode)) { printf("%s",path); return 1; } strcpy(buf, path); strcat(buf, "/*"); if (glob(buf, GLOB_NOSORT, NULL, &globres) != 0) { fprintf(stderr, "glob error\n"); return 1; } for (int i = 0; i < globres.gl_pathc; i++) { printf("%-15s ",(globres.gl_pathv)[i]); } puts("\n"); globfree(&globres); return 0;}int myls_i(char *path){ struct stat cur_stat; int a; glob_t globre; a = lstat(path, &cur_stat); char buf[BUFSIZE] = {}; if(a<0) { perror("lstat()"); return 1; } if(!S_ISDIR(cur_stat.st_mode)) { printf("%ld %s\n ",cur_stat.st_ino,path); return 1; } strcpy(buf,path); strcat(buf,"/*"); glob(buf,0,NULL,&globre); for(int i = 0;i<globre.gl_pathc;i++) { if(lstat(((globre.gl_pathv)[i]),&cur_stat)<0) { perror("lstat()"); return 1; } printf("%5ld %5s ",cur_stat.st_ino,globre.gl_pathv[i]); } puts("\n"); globfree(&globre); return 0;}/*实现ls -a功能*/int myls_a(char *path){ DIR *dp = NULL; struct dirent *entry = NULL; struct stat cur_stat; if (lstat(path, &cur_stat) < 0) { perror("lstat()"); return 1; } if (!S_ISDIR(cur_stat.st_mode)) { printf("%s",path); } dp = opendir(path); if (NULL == dp) { perror("opendir()"); return 1; } // read while (1) { entry = readdir(dp); if (NULL == entry) { if (errno) { perror("readdir()"); goto ERROR; } break; } printf("%-10s ", entry->d_name); } closedir(dp); puts("\n"); return 0;ERROR: closedir(dp); return 1;}/*文件的状态信息*/int allstat(char *path){ struct stat mystat; struct passwd *paswdname = NULL; struct group *grname = NULL; struct tm *tmp = NULL; char buf[BUFSIZE] = {}; int a; a = stat(path,&mystat); if(a == -1) { 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("%5ld ",mystat.st_nlink); paswdname = getpwuid(mystat.st_uid); printf("%5s ", paswdname->pw_name); grname = getgrgid(mystat.st_gid); printf("%5s ", grname->gr_name); printf("%5ld ",mystat.st_size); tmp = localtime(&mystat.st_mtime); if(tmp == NULL) return 1; strftime(buf, BUFSIZE, "%m月 %d %H:%M", tmp); printf("%5s ", buf); printf("%5s ", path); putchar('\n'); return 0;}/*实现ls -l功能*/int myls_l(char *path){ glob_t globres; struct stat cur_stat; char buf[BUFSIZE] = {}; if (lstat(path, &cur_stat) < 0) { perror("lstat()"); return 1; } if (!S_ISDIR(cur_stat.st_mode)) { allstat(path); return 1; } strcpy(buf, path); strcat(buf, "/*"); if (glob(buf, GLOB_NOSORT, NULL, &globres) != 0) { fprintf(stderr, "glob error\n"); return 1; } for (int i = 0; i < globres.gl_pathc; i++) { allstat((globres.gl_pathv)[i]); } globfree(&globres); return 0;}int myls(int argc ,char **argv){ int c; char *opt = "-l:a:i:h:"; while(1) { if(c == -1) break; c = getopt(argc,argv,opt); switch(c) { case 'l': myls_l(optarg); break; case 'a': myls_a(optarg); break; case 'i': myls_i(optarg); break; case 'h': myls_h(optarg); break; case '?': printf("不认识此选项%s\n", argv[optind-1]); break; case 1: printf("非选项参数%s\n", argv[optind-1]); break; default: break; } } return 0;}main.c
#include "myls.h"
#include<stdio.h>/*主函数*/int main(int argc,char **argv){ if(argc < 3) return 1; myls(argc,argv); return 0;}makefile
main :main.o myls.o
gcc main.o myls.o -o main clean: rm -rf *.o main