2019.03.15王苛震——myls
/*
1.尝试实现ls命令的功能 加选项-l -a -i -h
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h> #define MAX 1024 static int myls_l(char *optarg);
static void ls_all(char *path);
static int myls_a(char *path);
static int is_dir(char *path);
static void myls_i(char *path);
static void myls_i_notdir(char *path);
static int myls_h(char *path); int main(int argc, char *argv[])
{
int c;
char *str = "-l:a:i:h:";
if (argc < )
return -; while () {
if ((c = getopt(argc, argv, str)) == -)
break;
switch (c) {
case 'l':
is_dir(optarg) ? ls_all(optarg) : myls_l(optarg);
break;
case 'a':
is_dir(optarg) ? myls_a(optarg) : puts(optarg);
break;
case 'i':
is_dir(optarg) ? myls_i(optarg) : myls_i_notdir(optarg);
break;
case 'h':
is_dir(optarg) ? myls_h(optarg) : puts(optarg);
break;
case '?':
printf("输入选项错误\n");
break;
case :
printf("错误参数选项%s\n", argv[optind-]);
break;
default:
break;
}
} return ;
} // 单文件显示详细信息 相当于ls -l +文件名
static int myls_l(char *optarg)
{
struct stat buf; if (stat(optarg, &buf) == -) {
perror("STAT()");
return -;
}
// 文件类型
if (S_ISREG(buf.st_mode))
putchar('-');
if (S_ISDIR(buf.st_mode))
putchar('d');
if (S_ISCHR(buf.st_mode))
putchar('c');
if (S_ISBLK(buf.st_mode))
putchar('b');
if (S_ISFIFO(buf.st_mode))
putchar('p');
if ((buf.st_mode & S_IFMT) == S_IFSOCK)
putchar('s');
if ((buf.st_mode & S_IFMT) == S_IFLNK)
putchar('l');
// 权限
if (buf.st_mode & S_IRUSR)
putchar('r');
else
putchar('-');
if (buf.st_mode & S_IWUSR)
putchar('w');
else
putchar('-');
if (buf.st_mode & S_IXUSR) {
if (buf.st_mode & S_ISUID) {
putchar('s');
} else {
putchar('x');
}
}
else
putchar('-');
if (buf.st_mode & S_IRGRP)
putchar('r');
else
putchar('-');
if (buf.st_mode & S_IWGRP)
putchar('w');
else
putchar('-');
if (buf.st_mode & S_IXGRP) {
if (buf.st_mode & S_ISGID) {
putchar('s');
} else {
putchar('x');
}
}
else
putchar('-');
if (buf.st_mode & S_IROTH)
putchar('r');
else
putchar('-');
if (buf.st_mode & S_IWOTH)
putchar('w');
else
putchar('-');
if (buf.st_mode & S_IXOTH) {
if (buf.st_mode & S_ISVTX) {
putchar('t');
} else {
putchar('x');
}
}
else
putchar('-');
// 硬链接
printf(" %zu ", buf.st_nlink);
// 拥有者
struct passwd *pwd = NULL;
pwd = getpwuid(buf.st_uid);
printf("%s ", pwd->pw_name);
// 所属组
struct group *grp = NULL;
grp = getgrgid(buf.st_gid);
printf("%s ", grp->gr_name);
// 文件字节大小
printf("%zu ", buf.st_size);
// 最后更改文件时间
struct tm *tmp = NULL;
char s[MAX] = {};
tmp = localtime(&(buf.st_mtim.tv_sec));
strftime(s, MAX, "%m月 %d %H:%M ", tmp);
printf("%s ", s);
// 文件名
char *ptr = NULL;
if ((ptr = strrchr(optarg, '/')) != NULL) {
printf("%s", ptr+);
} else {
printf("%s", optarg);
} putchar('\n'); return ;
} // 判断该文件名第一个字符是否为隐藏文件和 . .. .asd
static int is_hidden(char *str)
{
if (str[] == '.') {
return ; // 是隐藏文件返回1
}
return ;
} // 相当于 ls -l +目录的路径
static void ls_all(char *path)
{
DIR *dp = NULL;
char str[MAX] = {};
struct dirent *ret_dir = NULL;
dp = opendir(path); while () {
memset(str, '\0', MAX);
strcpy(str, path);
strcat(str, "/");
if ((ret_dir = readdir(dp)) != NULL) {
if (is_hidden(ret_dir->d_name)) //排除隐藏文件
continue;
strcat(str, ret_dir->d_name); //完整路径
myls_l(str); //调用函数
} else { // 目录读取完毕
break;
}
}
closedir(dp);
} // 判断是文件还是目录 是目录返回1 不是返回0
static int is_dir(char *path)
{
struct stat buf;
lstat(path, &buf);
if (!S_ISDIR(buf.st_mode)) {
return ;
} else
return ;
} // 相当于 ls -a +目录的路径 (当前目录下所以文件包含隐藏文件)
static int myls_a(char *path)
{
DIR *dp = NULL;
struct dirent *ptr = NULL; dp = opendir(path);
if (dp == NULL) {
perror("OPENDIR()");
return -;
}
while () {
ptr = readdir(dp); //返回值为结构体类型指针
if (ptr == NULL) {
if (errno) {
perror("READDIR()");
closedir(dp);
return -;
}
break;
}
printf("%s\n", ptr->d_name);
} closedir(dp);
return ;
} // 相当于 ls -i +目录 显示当前目录下所有文件的inode号 (不包含隐藏)
static void myls_i(char *path)
{
DIR *dp = NULL;
struct dirent *entry = NULL;
dp = opendir(path);
while () {
if ((entry = readdir(dp)) == NULL)
break;
if (is_hidden(entry->d_name)) { //隐藏文件
continue;
}
printf("%zu %s\n", entry->d_ino, entry->d_name);
}
} // 相当于 ls -i +文件
static void myls_i_notdir(char *path)
{
struct stat buf;
stat(path, &buf);
printf("%zu %s\n", buf.st_ino, path);
} // 相当于 ls -h +目录 本目录下所有文件(不包含隐藏)
static int myls_h(char *path)
{
DIR *dp = NULL;
struct dirent *ptr = NULL; dp = opendir(path);
if (dp == NULL) {
perror("OPENDIR()");
return -;
}
while () {
ptr = readdir(dp); //返回值为结构体类型指针
if (ptr == NULL) {
if (errno) {
perror("READDIR()");
closedir(dp);
return -;
}
break;
}
if (is_hidden(ptr->d_name)) {
continue;
}
printf("%s\n", ptr->d_name);
} closedir(dp);
return ;
}
2019.03.15王苛震——myls的更多相关文章
- 2019.03.15 ZJOI2019模拟赛 解题报告
得分: \(20+45+15=80\)(三题暴力全写挂...) \(T1\):Lyk Love painting 首先,不难想到二分答案然后\(DP\)验证. 设当前需验证的答案为\(x\),则一个暴 ...
- 2019.03.03 - Linux搭建go语言交叉环境
编译GO 1.6版本以上的需要依赖GO 1.4版本的二进制,并且需要把GOROOT_BOOTSTRAP的路径设置为1.4版本GO的根目录,这样它的bin目录就可以直接使用到1.4版本的GO 搭建go语 ...
- PSP(16/03/14-16/03/15)
//整理了自己过去的时间记录文件,最早用电子版记录是15/04/21,依旧断断续续记录到15/11/21,每月至少三次记录,然而自己并没有对数据进行整理,只是纯粹记录,真是浪费了花在上面的时间.期间八 ...
- [2019.03.25]Linux中的查找
TMUX天下第一 全世界所有用CLI Linux的人都应该用TMUX,我爱它! ======================== 以下是正文 ======================== Linu ...
- Data truncation: Incorrect datetime value: 'May 15, 2019 4:15:37 PM
因为系统在windows下测试过是正常的 windows下的jdk+ windows下安装的mysql 全部cases通过 linux下的jdk + windows下安装的mysql 新增和更新,影响 ...
- MyBatis 配置/注解 SQL CRUD 经典解决方案(2019.08.15持续更新)
本文旨在记录使用各位大神的经典解决方案. 2019.08.14 更新 Mybatis saveOrUpdate SelectKey非主键的使用 MyBatis实现SaveOrUpdate mybati ...
- 2019.03.14 ZJOI2019模拟赛 解题报告
得分: \(100+100+0=200\)(\(T1\)在最后\(2\)分钟写了出来,\(T2\)在最后\(10\)分钟写了出来,反而\(T3\)写了\(4\)个小时爆\(0\)) \(T1\):风王 ...
- [2019.03.16]使用DOM操作函数和CSS选择器来针对已有的HTML进行只凭JS的改动
刚入职的时候看到公司用的HTML日志生成工具附带的Panel,工具不够用,找个Fail还要找半天,于是自己琢磨着添砖加瓦.以前也是个半吊子前端工程师,现在可倒好,想要改页面却连页面生成的模板在哪里都不 ...
- 2019.03.25 bzoj4568: [Scoi2016]幸运数字(倍增+线性基)
传送门 题意:给你一棵带点权的树,多次询问路径的最大异或和. 思路: 线性基上树?? 倍增维护一下就完了. 时间复杂度O(nlog3n)O(nlog^3n)O(nlog3n) 代码: #include ...
随机推荐
- MySQL Tips
MySQL中的一些Tips,个人总结或者整理自网络 不明白为什么MySQL的很多材料中总是喜欢把联合(复合)索引和覆盖索引放在一块说事? 1,联合索引是一种索引的类型,指创建索引的时候包含了多个字段. ...
- 虚拟机中安装完Lunix系统后,开机黑屏,只显示一个-,解决方法
1,查看设置->硬盘是不是SCSI,如果是,先关闭虚拟机,移除该硬盘(实际数据不会删除) 2,添加一个新的虚拟硬盘,最后位置选IDE设备 3,确定,重启虚拟机即可
- 排序算法(Gif动图演示)
冒牌排序(BubbleSort) 冒泡排序是一种比较简单的排序算法,它循环走过需要排序的元素,依次比较相邻的两个元素,如果顺序错误就交换,直至没有元素交换,完成排序. 若对n个人进行排序,我们需要n- ...
- css预处理器:Sass LASS Stylus
语法 Sass h1 { color: #0982C1; } h1 color: #0982c1 LESS h1 { color: #0982C1; } Stylus /* style.styl */ ...
- vue-router跳转
当使用$router.push()操作时,如果路由不加反斜杠会自动替换当前路由的最后一个反斜杠后面部分
- python day09 函数(第一篇)
2019.4.9 S21 day09笔记总结 一.三元运算 三元运算又叫三目运算.(是为了赋值的) v = 前面 if 条件 else 后面 #条件为真,v取if前面的值:条件为假,v取if后面的值 ...
- 项目(一)ftp搭建
FTP服务 FTP两种模式: 主动模式服务器向客户端敲门,然后客户端开门 被动模式客户端向服务器敲门,然后服务器开门 传输模式:可以是文本模式,也可以是二进制模式,二进制模式更适合传输图片等非文本字符 ...
- 一个简单的struts2上传图片的例子
https://www.cnblogs.com/yeqrblog/p/4398914.html 在我的大创项目中有对应的应用
- 今天遇到一个怪异的问题,maven生成项目war包中有一个Jar包不是我指定的版本,运行时会找不到符号,o(╥﹏╥)o
我要求的jar包: 这是我parent项目中pom文件的依赖管理 这是我要生成war包那个工程最后依赖的jar包,这个时候它们的版本号还是一致的 最后项目生成的: 下图是Dmaven.test.ski ...
- spring boot 常见的第三方集成
spring boot基于1.x. 一 集成redis 1.1 配置 spring.redis.host = localhost spring.redis.port = 6379 spring.red ...