#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. 常见的eclipse和真机出现的问题

    1.eclipse和手机连接时间过断导致运行时报错(时间,,,) 2.adk中文件夹中文件遗失错乱: tools下的zipalign丢失(打包时出现提示the zipalign tool was no ...

  2. Confluence 6 针对 'unmigrated-wiki-markup' 宏重新尝试合并

    在签名的章节中,我们主要是针对没有完全合并完成余下的为合并内容的异常处理.最常见的情况是内容以及被合并了,但是页面使用 wiki 标记的内容没有被合并,通常这些 wiki 标记的内容使用了 'unmi ...

  3. js的call和apply区别

    call和apply都是改变了this的指针. obj.call(newObj, arg1, arg2, ...); obj.apply(newObj, [arg1, arg2, ...]); 上面的 ...

  4. 【Java】【10】后台处理Emoji表情

    问题:存到数据库的emoji表情,取出来后,在前端显示为乱码 环境:SpringBoot + Oracle(MySQL据说是支持表情的) 解决方案: 引入emoji相关的jar包,使用很方便,不过表情 ...

  5. WEB UI 上传URL附件(使用方法备份)

    FUNCTION zcrm_update_atta. *"------------------------------------------------------------------ ...

  6. macaca 初试

    看到macaca的介绍,在移动端和PC端的自动化UI测试都同时支持, 联想到最近的行业都是同时对移动端和PC端(较少,几乎不维护)追求UI自动化测试,打算拿来试一下. 首先上macaca的地址:htt ...

  7. rsyslog及loganalyzer

    日志:历史日志 历史事件: 时间记录,事件的关键性程度,loglevel 系统日志服务: syslogd,守护进程 syslogd:system klogd:kernel   rsyslog: sys ...

  8. 老男孩Python九期全栈学习笔记4

    ---恢复内容开始--- day4 1.作业回顾 1.有变量name = 'aleX leNb',完成如下操作: 1)移除 name 变量对应的值两边的空格,并输出处理结果 2)移除 name 变量左 ...

  9. Python logging系统

    我们都知道python在2.x之后自带了一个模块import logging. 但是每次都要写log很麻烦,同时我想把info,debug之类的指令通过颜色分离开来. 于是写了一个简单的类似glog的 ...

  10. java前的部分了解(计算机小白)

    一.加密 对称加密: des 3des AES rc4 (数据加密) 会话密钥 非对称加密(成对:公钥/私钥(一个加密一个解密)):RSA DSA 密钥交换 / 数字签名(用私钥加密摘要算法出的一串数 ...