实现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 ...
随机推荐
- vimrc for mac
" Configuration file for vim set modelines=0 " CVE-2007-2438 " Normally we use vim-ex ...
- clone函数
http://blog.csdn.net/caianye/article/details/5947282 http://wenku.baidu.com/link?url=qnq7laYDYm1V8tl ...
- poj 1924 Paths on a Grid(组合数学)
题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...
- C#控件背景透明的几种解决方案
已经很少做winform程序了,最新参与了一个小项目,遇到了控件背景透明的功能要求,特在此总结一下,供有需要的同行参考. 0.背景透明的概念和分类 背景透明是啥意思呢,就是背景透明.哈哈,废话了.其实 ...
- JAVA分布式事务原理及应用
JTA(Java Transaction API)允许应用程序执行分布式事务处理--在两个或多个网络计算机资源上访问并且更新数据.JDBC驱动程序的JTA支持极大地增强了数据访问能力. 本文的目的是要 ...
- Android Service即四大组件总结
原文转载自:http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html Service 服务: 一个Service 是一段长 ...
- 【转】UINavigationBar 使用总结
原文网址:http://www.jianshu.com/p/f0d3df54baa6 UINavigationBar是我们在开发过程中经常要用到的一个控件,下面我会为大家介绍一些常用的用法. 1. 设 ...
- 解决android手机sd卡安装pak后直接打开,按home键异常问题
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { finish(); return; }
- BigRender
当一个网站越来越庞大,加载速度越来越慢的时候,开发者们不得不对其进行优化,谁愿意访问一个需要等待 10 秒,20 秒才能出现的网页呢? 常见的也是相对简单易行的一个优化方案是 图片的延迟加载.一个庞大 ...
- ORACLE的sign函数和DECODE函数
比较大小函数 sign 函数语法:sign(n) 函数说明:取数字n的符号,大于0返回1,小于0返回-1,等于0返回0 示例:一.select sign( 100 ),sign(- 100 ),sig ...