#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中使用watch监听对象或数组

    最近发现在vue中使用watch监听对象或者数组时,当数组或者对象只是单一的值改变时,并不会出发watch中的事件. 在找问题过程中,发现当数组使用push一类的方法时,会触发watch,如果只是单一 ...

  2. django+nginx+gunicorn+pipenv微信小程序实践笔记

    一.我采用pipenv来管理虚拟环境,在本地新建虚拟环境: mkdir wxProject #进入环境目录,创建虚拟环境 pipenv install #激活虚拟环境 pipenv shell #然后 ...

  3. HTL里面使用sling model的时候传参问题

    Question: Can we pass in parameters? <div data-sly-use.myClass=”${ ‘mysite.myproject.HeaderCompon ...

  4. 区间DP 洛谷P2858牛奶零食

    题目链接 题意:你有n个货物从1-n依次排列,每天可以从两侧选一个出来卖,卖的价格是当天的天数乘该货物的初始价格,问这批货物卖完的最大价格 输入:第一行n,之后是n个货物的初始价值 这道题不能用贪心做 ...

  5. 数模美赛准备——我的第一个LaTex文档

    软件下载地址:清华大学镜像网站(点击下载) https://mirrors.tuna.tsinghua.edu.cn/ctex/legacy/2.9/ 上述网站页面 根据个人需求下载不同的版本安装完成 ...

  6. matlab 三维激光雷达点云的地面与障碍物检测

    基于激光雷达的地面与障碍物检测 这个例子告诉我们如何去检测地平面并且找到三维LIDAR数据中与车相近的障碍物. 这个过程能够方便我们对汽车导航的可行驶区域规划. 注:每一帧的雷达属于都被存储为三维的雷 ...

  7. mongodb使用问题记录

    findOneAndUpdate(条件,更新,function(err,data){....}) 更新后返回的data值一直都是没有更新的数据状态, 原因:需要设置{new:true},这时返回的才是 ...

  8. mysql8.0修改密码无效的问题

    今天安装了mysql8,但是在修改默认密码的时候发现一直无法成功,下面给出解决的办法. 一直报ERROR 1064 (42000): You have an error in your SQL syn ...

  9. vue-计算属性和侦听器

    1.计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="example"> { ...

  10. js简单封装样式

    class Foo{ constructor(name) { this.name = name } greet() { console.log('hello this is',this.name) } ...