实现Linux下的ls -l命令
基本实现了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 ;
实现Linux下的ls -l命令的更多相关文章
- 高仿linux下的ls -l命令——C语言实现
主要用到的函数可以参考头文件,仅仅支持ls -l这功能,扩展就交给大家了0.0 相关测试图片: 话不多说,直接上码 #include <stdio.h> #include < ...
- 模拟linux下的ls -l命令
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...
- 编程实现Linux下的ls -l
头文件 #ifndef __FUNC_H__ #define __FUNC_H__ #include <stdio.h> #include <stdlib.h> #includ ...
- linux下ls -l命令(即ll命令)查看文件的显示结果分析
在linux下使用“ls -l”或者“ls -al”或者“ll”命令查看文件及目录详情时,shell中会显示出好几列的信息.平时也没怎么注意过,今天忽然心血来潮想了解一下,于是整理了这篇博客,以供参考 ...
- linux下如何使用sftp命令【转】
linux下如何使用sftp命令 from: http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个 ...
- Linux下的一些常用命令(一)
在Linux环境下敲各种命令是再正常不过了,尤其是现在大多少服务器均为Linux系统,但是我又记不住这么多命令,只是偶尔在项目做完发布到服务器上的时候会涉及到,所以在网上找了一些命令,在此记录一下~ ...
- linux下find和grep命令详解
在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...
- 实现linux下的ls
实现linux下的ls ls的使用 ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录). ls -l ...
- 在linux下的使用复制命令cp,不让出现“overwrite”(文件覆盖)提示的方法。(转)
在linux下的使用复制命令cp,不让出现“overwrite”(文件覆盖)提示的方法. 一般我们在使用cp命令时加上-f选项,希望不让出现“overwrite”的提示(文件覆盖的提示).如:# cp ...
随机推荐
- SVN使用方法总结
SVN使用方法 SVN版本管理模式:http://www.cnblogs.com/newstar/archive/2011/01/04/svn.html (集中式-trunk和分散式-branch ...
- nginx负载均衡 - session失效
最近迷上了Nginx,真实麻雀虽小,五脏俱全..功能实在强大.. nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态.静态页面的分离,可以 ...
- jqueryrotate 使用 帮助 笔记 学习
1.想变角度 $("imgID").rotate(45); 2.想变角度时,有运动过程 $("imgID").rotate({animateTo:45} ...
- uboot源码整体框架
源码解压以后,我们可以看到以下的文件和文件夹: cpu 与处理器相关的文件.每个子目录中都包括cpu.c和interrupt.c.start.S.u-boot.lds. cpu.c:初始化CPU.设 ...
- Hive QL 介绍
小结 本次课程学习了 Hive QL 基本语法和操作. 一.实验环境说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 2. 环境介绍 本实验环境采用带桌面的 ...
- mysql 读取硬盘数据
innodb 的最小管理单位是页 innodb的最小申请单位是区,一个区 1M,内含64个页,每个页16K ,即 64*16K=1M, 考虑到硬盘局部性,每次读取4个区,即读4M的数据加载至内存 线性 ...
- UML中常见关系详解(泛化、实现、依赖、关联、组合、聚合)
UML中类与类,已经类与接口,接口与接口的关系有:泛化(generalization),关联(association),依赖(dependency),实现(realization)这几种. 泛化( ...
- SSMS错误代码大全
0 操作成功完成. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. 5 拒绝访问. 6 句柄无效. 7 存储控制块被损坏. 8 存储空间不足,无法处理此 ...
- CSS sprites 技术
Css Sprites 技术逐渐流行,各大网站上都可以看到它的身影. 但从本质上,Css Sprites 只是 Css 技术的一个使用小窍门,初学者也能快速上手. Css Sprites 简单解释: ...
- 【 D3.js 高级系列 — 4.0 】 矩阵树图
矩阵树图(Treemap),也是层级布局的扩展,根据数据将区域划分为矩形的集合.矩形的大小和颜色,都是数据的反映.许多门户网站都能见到类似图1,将照片以不同大小的矩形排列的情形,这正是矩阵树图的应用. ...