#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h> int llstat(char *p);
int astat(char *p);
int istat(char *p);
int hstat(char *p);
static int nums; int main(int argc, char **argv)
{
int c;
char *optstring = "-laih";
if(argc < )
return ;
while (){
c = getopt(argc, argv, optstring);
if(c == -)
break;
switch(c){
case 'l': llstat(argv[]);break;
case 'a': astat(argv[]);break;
case 'i': istat(argv[]);break;
case 'h': hstat(argv[]);break;
// case '1': printf("非选项参数\n");break;
case '?': printf("?\n");break;
default : break;
}
}
return ; } //-l
//文件名->路径
static char *buf_cat(const char *p, const char *name)
{
char *bufcat = malloc();
memset(bufcat,'\0',);
strcpy(bufcat,p);
strcat(bufcat,"/");
strcat(bufcat,name);
return bufcat;
}
// 判断是否为隐藏文件
static int is_i(const char *p)
{
if(*p == '.')
return ;
else
return ;
} int l_llstat(const char *p, const char *name)
{
struct stat mystat;
struct passwd *pwd = NULL;
struct group *grp = NULL;
struct tm *tmp = NULL;
char *buf=NULL; buf = buf_cat(p,name);
if(lstat(buf,&mystat) == -){
perror("stat()");
return ;
}
if(is_i(name) == ){
nums+=mystat.st_blocks/; //类型
switch(mystat.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(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);
//文件拥有者名
pwd = getpwuid(mystat.st_uid);
printf("%s ", pwd->pw_name);
//文件所有组
grp = getgrgid(mystat.st_gid);
printf("%s ",grp->gr_name);
//总字节个数
printf("%5ld ", mystat.st_size);
//获取文件时间
tmp = localtime(&mystat.st_mtim.tv_sec);
strftime(buf, , "%m月 %d %H:%M", tmp);
printf("%s ", buf);
//文件名
printf("%s\n", name);
}
return ;
} int llstat(char *p)
{
DIR *dp = NULL;
struct dirent *entry = NULL;
char buf[] = {};
struct stat mystat1;
if(lstat(p,&mystat1) == -){
perror("stat()");
return ;
}
if(S_ISREG(mystat1.st_mode)){
l_llstat(".", p);
}else{
dp = opendir(p);
if(dp == NULL){
perror("opendir()");
return ;
} while(){
entry = readdir(dp);
if(NULL == entry){
if(errno){
perror("readdir()");
closedir(dp);
return ;
}
break;
}
l_llstat(p, entry->d_name);
}
printf("总用量:%d\n", nums);
closedir(dp);
} return ; } // a int a_llstat(const char *p, const char *name)
{
struct stat mystat;
char *buf=NULL; buf = buf_cat(p,name);
if(lstat(buf,&mystat) == -){
perror("stat()");
return ;
}
//文件名
printf("%s ", name);
return ;
} int astat(char *p)
{
struct stat mystat2;
struct dirent *entry = NULL;
DIR *dp = NULL;
if(lstat(p,&mystat2) == -){
perror("stat()");
return ;
} if(S_ISREG(mystat2.st_mode)){
printf("%s ", p);
}
dp= opendir(p);
if(dp == NULL){
perror("opendir()");
return ;
} while(){
entry = readdir(dp);
if(NULL == entry){
if(errno){
perror("readdir()");
closedir(dp);
return ;
}
break;
}
a_llstat(p, entry->d_name);
} closedir(dp);
printf("\n");
return ; } // i int i_llstat(const char *p, const char *name)
{
struct stat mystat;
char *buf=NULL; buf = buf_cat(p,name);
if(lstat(buf,&mystat) == -){
perror("stat()");
return ;
}
printf("%ld ",mystat.st_ino);
//文件名
printf("%s ", name);
return ;
} int istat(char *p)
{
struct stat mystat2;
struct dirent *entry = NULL;
DIR *dp = NULL;
if(lstat(p,&mystat2) == -){
perror("stat()");
return ;
} if(S_ISREG(mystat2.st_mode)){
if(is_i(p))
return ;
printf("%ld ",mystat2.st_ino);
printf("%s ", p);
}
else{
dp= opendir(p);
if(dp == NULL){
perror("opendir()");
return ;
} while(){
entry = readdir(dp);
if(NULL == entry){
if(errno){
perror("readdir()");
closedir(dp);
return ;
}
break;
}
if(is_i(entry->d_name))
continue;
i_llstat(p, entry->d_name);
}
}
closedir(dp);
printf("\n");
return ;
} //h
int hstat(char *p)
{
struct stat mystat2;
struct dirent *entry = NULL;
DIR *dp = NULL;
if(lstat(p,&mystat2) == -){
perror("stat()");
return ;
} if(!((mystat2.st_mode & S_IFMT) == S_IFDIR)){
if(!is_i(p)){
printf("%s\n", p);
return ;
}
} else{ dp= opendir(p);
if(dp == NULL){
perror("opendir()");
return ;
} while(){
entry = readdir(dp);
if(NULL == entry){
if(errno){
perror("readdir()");
closedir(dp);
return ;
}
break;
}
if(is_i(entry->d_name) == )
printf("%s ", entry->d_name);
}
closedir(dp);
}
printf("\n");
return ;
}

myls的更多相关文章

  1. 【转】APUE学习1:迈出第一步,编译myls.c

    原文网址:http://blog.csdn.net/sddzycnqjn/article/details/7252444 注:以下写作风格均学习自潘云登前辈 /******************** ...

  2. UC高级编程--实现myls程序

    跟着达内视频,学习UC高级编程,完毕程序小练习. 主要练习的函数为:  int lstat(const char *path, struct stat *buf);  size_t strftime( ...

  3. 2019.03.15王苛震——myls

    /* 1.尝试实现ls命令的功能 加选项-l -a -i -h */ #include <stdio.h> #include <sys/types.h> #include &l ...

  4. APUE fig 1.10示例代码的完善--对提示符及输入回车的优化

    APUE 第3版第15页的shell2.c示例程序,运行效果如下: gcc ol.shell.c -o origin_shell ./origin_shell % date 2015年12月13日 星 ...

  5. 在ubuntu上配置apue的运行环境

    http://www.apuebook.com/code3e.html 在上面的网站下载代码包,解压得到源码 sudo apt-get install libbsd-dev 安装这个支持,在解压包的m ...

  6. shell之变量与read

    环境变量 set 环境变量可供shell以外的程序使用 shell变量 env shell变量仅供shell内部使用 set:显示(设置)shell变量 包括的私有变量以及用户变量,不同类的shell ...

  7. [UNIX环境高级编程](第三版)中apue.h的问题

    编译 gcc -g myls.c 时,报错 ‘找不到头文件 apue.h’ apue.h是作者自己写的一个文件,系统不自带.其中包含了常用的头文件,以及出错处理函数的定义. 需要到 http://ww ...

  8. Linux---Ls命令 初级实现

    By xxx0624Done:    ls    ls -a    ls -l    ls /tmp    ls -R    ls -t    FileName color    FileName o ...

  9. Unix System Overview

    一.Unix 体系结构 由上图可以看出,内核居于最里层,Shell,Libary routines,以及Application通过系统调用(system calls)访问内核提供的功能.注意系统调用与 ...

随机推荐

  1. tomcat发布项目如何通过域名直接访问

    首先在服务器中找到tomcat安装后的文件夹,进入到conf目录下,找到server.xml文件 打开并修改,修改如下: 第一步:修改port,该值默认为8080,将其修改为80 第二步:修改defa ...

  2. WDA 新SELECT OPTION

    感谢肖总的提示,不然还不知道wda的新select option... 使用前提:SE24类:CL_WDR_SELECT_OPTIONS_20 核对了多个版本,ERP系统版本要S4 1701往上,CR ...

  3. visual studio 找不到模板信息

    问题: 创建项目提示“ 找不到visual studio模板信息” 解决方案: 打开Visual Studio  在菜单->工具->选项->项目和解决方案-> "Vi ...

  4. 《网络是怎样连接的》PDF电子版书籍分享

    资料下载地址: 链接:https://pan.baidu.com/s/15tN9klTEsu-mQLayxI979g 提取码:ptu1 封面如下所示:

  5. lumion物体系统,导入模型6.3

    选择物体后可以改变它的颜色 “导入”“导入新模型.”su中模型分正面反面.在lumion中显示是不一样的.正面是可以显示出来的,反面就是透明的,lumion只能识别不同的材质,所以在su中要用不同的颜 ...

  6. 程序员必会算法-KMP算法

    KMP算法是一种优秀的字符串匹配算法,字符串匹配的常规算法是一步一步进行移位和比较操作,直至找到完全相匹配的字符串. 下面通过一个例子,为大家仔细说明KMP算法的使用和思路: 问题: 在字符串“DEA ...

  7. java8中optional和.stream().map()

    使用optional的好处:是一个可以包含或不可以包含非空值的容器对象,更加友好的处理程序中的空对象. Optional<T>有方法 isPresent() 和 get() 是用来检查其包 ...

  8. ES6中var/let/const的区别

    let的含义及let与var的区别: let 声明的变量只在它所在的代码块有效: 如下: for (let i = 0; i < 10; i++) { console.log(i); } con ...

  9. Python之路【目录】

    https://www.cnblogs.com/wupeiqi/articles/4938499.html

  10. dva,清除模块数据

    前言: 在项目中,模块过多,dva使用namespace分离模块后,若没有在模块卸载后清除对应的数据,下次进入时,有可能会有上一次数据的残留. 比如详情页,从A商品的详情页离开后,返回选择B商品进入, ...