#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. vue项目使用element ui的Checkbox

    最近使用到element ui的下拉多选框Checkbox Checkbox用法可参考与于 http://element.eleme.io/#/zh-CN/component/checkbox Che ...

  2. opencv使用总结

    在resize矩阵时出现报错 (-215) func != 0 in function cv::resize" 很可能是矩阵未设置为float,应该在读入后转换类型 item = item. ...

  3. 关于java类加载机制的一些理解

    关于java的类加载机制加载顺序,这个东西可以说是基础的东西,不过很遗憾这方面很多人也都不是很在意,比如我自己,最近上班闲下来了,就开始看一些博客文章了,今天恰好被一篇博文给吸引了,并且他的示例题一开 ...

  4. SWUST OJ(599)

    拉丁方阵 #include <iostream> #include <cstdlib> using namespace std; int main() { int n; cin ...

  5. 『TensorFlow』专题汇总

    TensorFlow:官方文档 TensorFlow:项目地址 本篇列出文章对于全零新手不太合适,可以尝试TensorFlow入门系列博客,搭配其他资料进行学习. Keras使用tf.Session训 ...

  6. selenium自动化定位方式

    自动化定位方式 1.String Xpath = String.format("//*[@id=\"saveFileKeyWordsBtnHand\"]/../../.. ...

  7. SAP基础:定位点运算

    先看一下下面简单的代码: REPORT zlytest003. ) VALUE '21.00'. ) . b = a. WRITE b. 运行结果是: 这时候到程序属性页面: 修改固定点算术为空. 保 ...

  8. Spring官方文档下载

    Spring框架是目前最流行的java web开发框架,很多时候,我们需要去查看spring的官方文档,这里就简单介绍下如何下载其官方文档. 1.搜索到spring 官网并进入 2.点击DOCS 3. ...

  9. Source Insight 4.0安装使用教程

    一.说明 Source Insight是什么:Source Insight是一款代码编缉.浏览.分析工具. Source Insight与文本编缉器有什么区别:Notepad++等文本编缉器也可以编缉 ...

  10. 【还是回来了】博客搬家--https://cangbean.github.io

    还是弄了个自己的地址:https://cangbean.github.io 想记录下,防止万一以后迁移不好弄 想练习markdown写作 折腾而已 以后不再博客园记录东西了,但是还是会回来看看的