#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h> void do_ls(char[]);
void dostat(char *);
void show_file_info( char *, struct stat *);
void mode_to_letters( int , char[] );
char * uid_to_name( uid_t );
char * gid_to_name( gid_t ); main(int ac, char *av[])
{
if( ac == )
do_ls( "." ); /*显示当前目录*/
else
while( --ac ){
printf("%s:\n", *++av); /*显示参数指定的目录*/
do_ls( *av );
}
} void do_ls( char dirname[] )
{
DIR *dir_ptr;
struct dirent *direntp; if( (dir_ptr = opendir( dirname )) == NULL) /*打开目录,成功则返回 DIR 结构指针*/
fprintf(stderr, "ls1: cannot open %s\n", dirname); else
{
while( ( direntp = readdir( dir_ptr ) ) != NULL )
dostat( direntp->d_name ); closedir( dir_ptr );
}
} void dostat( char *filename )
{
struct stat info; if( stat( filename, &info ) == - )
perror( filename );
else
show_file_info( filename, &info );
} void show_file_info( char *filename, struct stat *info_p )
{ if(filename[]!='.')
{
char *uid_to_name(), *ctime(), *gid_to_name(), *filemode();
void mode_to_letters(); char modestr[]; mode_to_letters( info_p->st_mode, modestr ); /*模式到字符的转换*/ printf("%s", modestr ); /*输出模式标识符*/ printf("%4d", (int)info_p->st_nlink); printf(" %-8s", uid_to_name(info_p->st_uid)); printf("%-8s", gid_to_name(info_p->st_gid)); printf("%8ld ", (long)info_p->st_size); printf("%.12s ", + ctime(&info_p->st_mtime)); printf("%s\n",filename);
} } void mode_to_letters( int mode, char str[] )
{
strcpy(str,"----------"); if( S_ISDIR(mode) ) str[] = 'd'; /*目录*/
if( S_ISCHR(mode) ) str[] = 'c'; /*字符文件*/
if( S_ISBLK(mode) ) str[] = 'b'; /*块文件*/ if(mode & S_IRUSR) str[] = 'r';
if(mode & S_IWUSR) str[] = 'w';
if(mode & S_IXUSR) str[] = 'x'; if(mode & S_IRGRP) str[] = 'r';
if(mode & S_IWGRP) str[] = 'w';
if(mode & S_IXGRP) str[] = 'x'; if(mode & S_IXOTH) str[] = 'r';
if(mode & S_IXOTH) str[] = 'w';
if(mode & S_IXOTH) str[] = 'x';
} #include <pwd.h> char *uid_to_name( uid_t uid )
/*
*返回和 uid 相应的用户名的指针
*/
{
struct passwd *getpwuid(), *pw_ptr;
static char numstr[]; if( ( pw_ptr = getpwuid( uid ) ) == NULL ) {
sprintf(numstr, "%d", uid); /*没有对应的用户名则 uid 存入 numstr,返回后以字符串的形式打印 uid*/
return numstr;
}
else
return pw_ptr->pw_name; /*打印用户名*/
} #include <grp.h> char *gid_to_name( gid_t gid )
{
struct group *getgrgid(), *grp_ptr;
static char numstr[]; if( ( grp_ptr = getgrgid(gid) ) == NULL ){ sprintf(numstr, "%d", gid);
return numstr;
}
else
return grp_ptr->gr_name;
}

linux 下用 c 实现 ls -l 命令的更多相关文章

  1. 实现Linux下的ls -l命令

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

  2. linux 下文件重命名/移动/复制命令(转)

    linux 下文件重命名/移动/复制命令(转) linux下重命名文件:使用mv命令就可以了, 例:要把名为:abc   重命名为:123 可以这样操作: 重命名:MV命令 1.进入你的文件目录,运行 ...

  3. 构建LINUX下的入侵检测系统——LIDS 系统管理命令--vlock

    构建LINUX下的入侵检测系统——LIDS   系统管理命令--vlock http://blog.chinaunix.net/uid-306663-id-2440200.html LIDS官方网站: ...

  4. Linux下如何保持gnome-terminal窗口执行命令后停留而不立刻关闭(gnome-terminal -x)

    Linux下如何保持gnome-terminal窗口执行命令后停留而不立刻关闭(gnome-terminal -x) 转自:http://jakfruit.blog.163.com/blog/stat ...

  5. Linux下df与du两个命令的差别?

    Linux下df与du两个命令的差别? 一.df显示文件系统的使用情况,与du比較,就是更全盘化. 最经常使用的就是 df -T,显示文件系统的使用情况并显示文件系统的类型. 举比例如以下: [roo ...

  6. linux下使用script和scriptreplay对命令行操作进行录像

    转自:linux下用script和scriptreplay对命令行操作录像 在Linux中可以使用script命令来记录命令行的操作过程,并使用scriptreplay命令对命令操作进行回放,操作步骤 ...

  7. linux下查看动态链接库依赖关系的命令 x86: ldd *.so arm: arm-linux-readelf -d *.so 实际例子: 以项目中用到的库librtsp.so分析: lijun@ubuntu:~/workspace$ arm-hisiv100nptl-linux-ld -d librtsp.so arm-hisiv100nptl-linux-ld:

    linux下查看动态链接库依赖关系的命令 x86:ldd    *.so arm:arm-linux-readelf    -d    *.so 实际例子:以项目中用到的库librtsp.so分析:l ...

  8. Linux下mysql使用systemctl restart mysqld命令失败

    Linux下mysql使用systemctl restart mysqld命令失败: 解决方法:将mysql赋予root的权限 vim /etc/passwd 找到mysql开头的一行文件mysql: ...

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

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

随机推荐

  1. 二叉树及其遍历方法---python实现

    github:代码实现 本文算法均使用python3实现 1. 二叉树 1.1 二叉树的定义   二叉树是一种特殊的树,它具有以下特点:   (1)树中每个节点最多只能有两棵树,即每个节点的度最多为2 ...

  2. UVA 167 R-The Sultan's Successors

    https://vjudge.net/contest/68264#problem/R The Sultan of Nubia has no children, so she has decided t ...

  3. 最近面试js部分试题总结

    二,JavaScript面试题总结 1,首先是数组去重算法:给一个数组,去掉重复值 (function() { var arr = [1, 2, 3, 3, 4, ]; function unique ...

  4. mysql三种备份方式

    一.备份的目的 做灾难恢复:对损坏的数据进行恢复和还原需求改变:因需求改变而需要把数据还原到改变以前测试:测试新功能是否可用 二.备份需要考虑的问题 可以容忍丢失多长时间的数据:恢复数据要在多长时间内 ...

  5. 从大量的IP访问记录中找到访问次数最多的IP

    1.内存不受限 一个IP有32bit(4Byte),1GB=10亿,那么在4GB内存的情况下,可以存10亿个IP.用HashMap,边存入IP边维护一个最大次数,这样遍历一遍就可以求出,时间复杂度为O ...

  6. java.awt.AWTError: Can't connect to X11 window server using ':20' as the value of the DISPLAY variable

    1.使用pio在Linux服务器上创建window文件时,需要使用到Linux的图形界面服务,出现以下问题需确认用户权限. 参考文献:https://zhidao.baidu.com/question ...

  7. C#中的反射和扩展方法的运用

    前段时间做了一个练手的小项目,采用的是三层架构,也就是Models,IDAL,DAL,BLL 和 Web , 在DAL层中各个类中有一个方法比较常用,那就是 RowToClass ,顾名思义,也就是将 ...

  8. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

  9. matlab 中try/catch语句

    try的作用是让Matlab尝试执行一些语句,执行过程中如果出错,则执行catch部分的语句,其语法: try (command1)组命令1总被执行,错误时跳出此结构 catch (command2) ...

  10. iOS-UI控件概述

    IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @property(nonatomic, wea ...