首页 技术 正文
技术 2022年11月11日
0 收藏 857 点赞 4,319 浏览 3982 个字
 /*
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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,962
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781