首页 技术 正文
技术 2022年11月14日
0 收藏 1000 点赞 3,671 浏览 3346 个字

基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现:

 /*************************************************************************
> File Name: dirwalk.c
> Author:
> Mail:
> Created Time: Tue 31 Mar 2015 11:56:38 AM CST
************************************************************************/ #include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <time.h> #define MAX_PATH 1024
#define MODE_LEN 10
#define TIME_LEN 20
#define NAME_LEN 30 unsigned long ugo_mode[] = {S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH};
char* rwx[] = {"r", "w", "x"};
char* userpath = "/etc/passwd";
char* grouppath = "/etc/group"; /*dirwalk: apply fcn to all files in dir */
void dirwalk(char* dir, void(*fcn)(char*))
{
struct dirent *dp;
DIR* dfd; char name[MAX_PATH];
if((dfd = opendir(dir)) == NULL)
{
fprintf(stderr, "dirwalk: can't open %s\n", dir);
return;
} while((dp = readdir(dfd)) != NULL)
{
if(strcmp(dp->d_name, ".") == || strcmp(dp->d_name, "..") == )
{
continue;
} if(strlen(dir) + strlen(dp->d_name) + > sizeof(name))
{
fprintf(stderr, "%s/%s too long\n", dir, dp->d_name);
}else
{
sprintf(name, "%s/%s", dir, dp->d_name);
(*fcn)(name);
}
}
closedir(dfd);
} /*getname: get the name of the user and the name of the group name */
void getname(char* path, int id, char* name)
{ int fd, save_fd;
if((fd = open(path, O_RDONLY)) < )
{
perror("open \n");
exit();
} save_fd = dup(STDIN_FILENO);
dup2(fd, STDIN_FILENO);
close(fd);
char p[MAX_PATH];
char usrid[];
sprintf(usrid, "%d", id);
char* puid;
while((scanf("%s", p)) != EOF)
{
if((puid = strstr(p, usrid)) != NULL)
{
char* pFlag;
char* pResult;
if((pFlag = strstr(p, ":")) != NULL)
{
pResult = pFlag;
pFlag = strstr(pFlag + , ":");
}
if(pFlag != NULL)
{
if((memcmp(pFlag + , puid, strlen(usrid))) == )
{
*pResult = '\0';
strcpy(name, p);
}
}
//printf("%s\n", puid);
}
} dup2(save_fd, STDIN_FILENO);
close(save_fd);
} /*getmode: get the mode of a file in string format by st_mode */
void getmode(unsigned long st_mode, char* mode)
{
switch(st_mode & S_IFMT)
{
case S_IFDIR:
mode[] = 'd';
break;
case S_IFIFO:
mode[] = 'p';
break;
case S_IFBLK:
mode[] = 'b';
break;
case S_IFCHR:
mode[] = 'c';
break;
case S_IFREG:
mode[] = '-';
break;
case S_IFLNK:
mode[] = 'l';
break;
case S_IFSOCK:
mode[] = 's';
break;
default:
mode[] = 'u';
break;
} int i;
for(i = ; i < ; ++i)
{
if(st_mode & ugo_mode[i])
{
strcat(mode, rwx[i % ]);
}else
{
strcat(mode, "-");
}
}
} /*gettime: get the time in string format */
void gettime(const time_t ct, char* time)
{
char* tmp = ctime(&ct);
time[] = tmp[];
time[] = tmp[];
time[] = tmp[];
time[] = ' '; struct tm* tm_buf = gmtime(&ct);
char last[];
sprintf(last, "%02d %02d:%02d", tm_buf->tm_mday, tm_buf->tm_hour, tm_buf->tm_min);
strcat(time, last);
} /* print the file name and the size of the "name" */
void fsize(char* name)
{
struct stat st_buf;
if(stat(name, &st_buf) < )
{
fprintf(stderr, "fsize: can't access %s\n", name);
return;
} if((st_buf.st_mode & S_IFMT) == S_IFDIR)
{
dirwalk(name, fsize);
}
char* time = (char*)malloc(sizeof(char) * TIME_LEN);
memset(time, , TIME_LEN);
gettime(st_buf.st_atime, time); char* mode = (char*)malloc(sizeof(char) * MODE_LEN);
memset(mode, , MODE_LEN);
getmode(st_buf.st_mode, mode); char* username = (char*)malloc(sizeof(char) * NAME_LEN);
memset(username, , NAME_LEN);
getname(userpath, st_buf.st_uid, username); char* groupname = (char*)malloc(sizeof(char) * NAME_LEN);
memset(groupname, , NAME_LEN);
getname(grouppath, st_buf.st_gid, groupname); printf("%s %d %s %s %4ld %s %s\n", mode, (int)st_buf.st_nlink, username, groupname, st_buf.st_size, time, name);
free(groupname);
free(username);
free(mode);
free(time);
} int main(int argc, char* argv[])
{
if(argc == )
fsize(".");
while(--argc)
fsize(*++argv);
return ;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,112
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,585
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922