实现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 ...
随机推荐
- 动态改变QSS
通常,一旦设置使用setObjectName来初始设置QSS: list_widget = new QListWidget(); list_widget->setObjectName(" ...
- 转:tar 常用命令
tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...
- C#中保留2位小数
public static void Method() { double a = 1.991; a = Math.Round(a); Console.WriteLine("a = {0}&q ...
- Hashtable与HashMap区别(2)
提到hashtable,先要澄清两个问题hashCode与equals().Hashtable有容量和加载因子,容量相当于桶,因子相当于桶里的对象.而hashCode我们可以把它理解为桶的序号,所以H ...
- 什么是WebService
举个例子:现在有5个项目,项目彼此独立,甚至都不是同一类语言进行开发的.这5个项目是:百度知道,百度贴吧,百度新闻,百度视频,百度百科.突然有一天,老板说:把这几个系统揉称一个大项目,起名直接叫做百度 ...
- 对于requirejs AMD模块加载的理解
个人人为使用模块化加载的优点有三: 1,以我的项目为例:90%为图表展示,使用的是echarts,此文件较大,requirejs可以在同一个版本号(urlArgs)之下缓存文件,那么我就可以在访问登陆 ...
- UVa 11572 (滑动窗口) Unique Snowflakes
滑动窗口挺有意思的,如果符合条件右端点一直向前走,不符合的话,左端点向前走. #include <bits/stdc++.h> using namespace std; set<in ...
- Java泛型类与类型擦除
转载自:http://blog.csdn.net/lonelyroamer/article/details/7868820 一.Java泛型的实现方法:类型擦除 前面已经说了,Java的泛型是伪泛型. ...
- [转]VPN服务器配置详解
借助VPN,企业外出人员可随时连到企业的VPN服务器,进而连接到企业内部网络.借助windows2003的“路由和远程访问”服务,可以实现基于软件的VPN. VPN(Virtual Private N ...
- DELL R710服务器可以安装的VMWare ESX Server 4.1 全套下载带注册码
随着R710的停产,R720随之面世,但DELL R720服务器只支持vmware esxi5.0以上,DELL客户经理给了一套系统安装后序列号无法解决,还是用4.1好了,却又发现怎么都无法安装.按网 ...