基本实现了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命令的更多相关文章

  1. 高仿linux下的ls -l命令——C语言实现

    主要用到的函数可以参考头文件,仅仅支持ls -l这功能,扩展就交给大家了0.0 相关测试图片: ​ ​ 话不多说,直接上码 #include <stdio.h> #include < ...

  2. 模拟linux下的ls -l命令

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...

  3. 编程实现Linux下的ls -l

    头文件 #ifndef __FUNC_H__ #define __FUNC_H__ #include <stdio.h> #include <stdlib.h> #includ ...

  4. linux下ls -l命令(即ll命令)查看文件的显示结果分析

    在linux下使用“ls -l”或者“ls -al”或者“ll”命令查看文件及目录详情时,shell中会显示出好几列的信息.平时也没怎么注意过,今天忽然心血来潮想了解一下,于是整理了这篇博客,以供参考 ...

  5. linux下如何使用sftp命令【转】

    linux下如何使用sftp命令 from:   http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个 ...

  6. Linux下的一些常用命令(一)

    在Linux环境下敲各种命令是再正常不过了,尤其是现在大多少服务器均为Linux系统,但是我又记不住这么多命令,只是偶尔在项目做完发布到服务器上的时候会涉及到,所以在网上找了一些命令,在此记录一下~ ...

  7. linux下find和grep命令详解

    在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...

  8. 实现linux下的ls

    实现linux下的ls ls的使用 ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录). ls -l ...

  9. 在linux下的使用复制命令cp,不让出现“overwrite”(文件覆盖)提示的方法。(转)

    在linux下的使用复制命令cp,不让出现“overwrite”(文件覆盖)提示的方法. 一般我们在使用cp命令时加上-f选项,希望不让出现“overwrite”的提示(文件覆盖的提示).如:# cp ...

随机推荐

  1. VMware 11完全安装Mac OS X 10.10

    本文已迁移到我的个人网站 http://www.wshunli.com 文章地址: http://www.wshunli.com/2016/03/17/VMware-12安装Mac-OS-X-10-1 ...

  2. GNU :6.47 Function Names as Strings

    链接:http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html#Function-Names GCC provides three magic var ...

  3. java去除重复的字符串和移除不想要的字符串

    在java开发中碰到了有些字符串是重复的,如果在进行业务处理要全部遍历太对的数据就会重复,所以在进行业务处理前进行一个去重操作. 这里由于业务需要所以先将字符串转化为string数组,使用split分 ...

  4. strlen的C/C+++实现

    2013-07-05 11:36:05 小结: 本函数给出了几种strlen的实现,有ugly implementation,也有good implementation.并参考标准库中的impleme ...

  5. 如何在给快满的Linux分区"无伤"扩容

    1. 首先在虚拟机设置里面设置磁盘的扩展,前提条件是该虚拟机没有快照. 2. 在虚拟机设置好以后,需要开机在系统里扩容磁盘 3. 使用 # fdisk /dev/sda 扩展磁盘,具体操作使用 m 选 ...

  6. NFC(13)使用Android Beam技术传输文件

    注意 Android Beam技术传输文件时nfc只负责连接两个手机,而传输文件实际是用蓝牙模块.且目前接收文件功能只是系统完成,不用自写个接收程序. 传输文件相关的重要api 从Android4.1 ...

  7. COM, COM+ and .NET 的区别

    所有的优秀程序员都会尽自己的最大努力去使自己所写的程序具有更好的可重用性,因为它可以让你快速地写出更加健壮和可升级性的程序. 有两种使代码重用的选择: 1.白盒:最简单的一种,就是把你的程序片拷贝到另 ...

  8. Area of a Circle

    Area of a Circle Description: Complete the function circleArea so that it will return the area of a ...

  9. 第二部分 overlay 架构初探

    1 overlay可能支持的颜色格式/* possible overlay formats可能支持的颜色格式 */enum {    OVERLAY_FORMAT_RGBA_8888    = HAL ...

  10. Javascript 对输入框中的内容进行 “全选/反选”

    <</span>script> document.write("<</span>ul>"); for(var i=0;i<&l ...