#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<string.h>
#include<sys/types.h>
#include<pwd.h>
#include<grp.h>
void mode_to_letters(int mode,char str[]);
void do_ls(char dirname[]);
void show_stat_info(char *fname,struct stat *buf);
char *uid_to_name(uid_t uid);
char *gid_to_name(gid_t gid);
int main(int ac,char *av[])
{
   if(ac==1)
      do_ls(".");
   else
        while(ac--)
         {
         printf("%s:\n",*++av);
         do_ls(*av);
         }
}
void do_ls(char dirname[])
{
   DIR *drip;                //the directory
   struct dirent *direntp;   //each entry
   struct stat info;

if((drip=opendir(dirname))==NULL)
        fprintf(stderr,"ls2:cannot open %s\n",dirname);
   else
       {
        while((direntp=readdir(drip))!=NULL)
            if(stat(direntp->d_name,&info)!=-1)
         {
            show_stat_info(direntp->d_name,&info);
        }
            else
        perror(direntp->d_name);
        closedir(drip);
        }
}
void show_stat_info(char *fname,struct stat *buf)
{
   char str[11];
   mode_to_letters(buf->st_mode,str);
   printf("%10s",str);
   //printf("   mode:%o ",buf->st_mode);
   printf("%3d  ",buf->st_nlink);
   printf("%-6s",uid_to_name(buf->st_uid));
   printf("%-7s",gid_to_name(buf->st_gid));
   printf("%8ld ",buf->st_size);
   printf("%12.12s   ",4+ctime(&(buf->st_mtime)));
   printf("%s\n",fname);
}
/*
* This function takes a mode value and a char array
* and puts into the char array the file type and the
* nine letters that correspond to the bits in mode.
* NOTE:It doesn't code setuid,setgid,and sticky
* codes
*/
void mode_to_letters(int mode,char str[])
{
   strcpy(str,"----------");       /*default=no perms*/
  
   if(S_ISDIR(mode)) str[0]='d';    /*directory*/
   if(S_ISCHR(mode)) str[0]='c';    /*char device*/
   if(S_ISBLK(mode)) str[0]='b';    /*block device*/
   if(S_ISREG(mode)) str[0]='-';    /*regular device*/
   
   if(mode & S_IRUSR) str[1]='r';  /*the privilege of the owner*/
   if(mode & S_IWUSR) str[2]='w';
   if(mode & S_IXUSR) str[3]='x';

if(mode & S_IRGRP) str[4]='r';   /*the privilege of the group*/
   if(mode & S_IWGRP) str[5]='w';
   if(mode & S_IXGRP) str[6]='x';
  
   if(mode & S_IROTH) str[7]='r';   /*the privilege of others*/
   if(mode & S_IWOTH) str[8]='w';
   if(mode & S_IXOTH) str[9]='x';
}
/*transit user id to user name*/
char *uid_to_name(uid_t uid)
{
    struct passwd *getpwuid(),*ptr;
    static char numstr[10];
    if((ptr=getpwuid(uid))==NULL)
       {
    sprintf(numstr,"%d",uid);
       return numstr;
       }
       else
         return ptr->pw_name;
}
/*transit gruop id to group name*/
char *gid_to_name(gid_t gid)
{
    struct group *getgrgid(),*grd_ptr;
    static char numstr[10];
   
    if((grd_ptr=getgrgid(gid))==NULL)
       {
        sprintf(numstr,"%d",gid);
    return numstr;
       }
       else
     return grd_ptr->gr_name;
}

linux 中ls命令函数的更多相关文章

  1. linux中ls命令详解 (转)

    -a -- 全部(all).列举目录中的全部文件,包括隐藏文件(.filename).位于这个列表的起首处的 .. 和 . 依次是指父目录和你的当前目录.      -l -- 长(long).列举目 ...

  2. Linux中ls命令详解

    ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,下面我们就来一起看看ls的用法 英文全名:List即列表的意思,当我们学习某种东西的时候要做到知其所 ...

  3. Linux中ls命令用法

    ls 命令的含义是list显示当前目录中的文件名字.注意不加参数它显示除隐藏文件外的所有文件及目录的名字. 1)ls –a 显示当前目录中的所有文件,包含隐藏文件 命令: aijian.shi@U-a ...

  4. linux中ls命令使用选项

    ls:英文全名:List即列表的意思 -a 列出目录下的所有文件,包括以 . 开头的隐含文件.-b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式列出.-c 输出文件的 i 节 ...

  5. linux中ls命令

    ls跟dos下的dir命令是一样的都是用来列出目录下的文件 ls参数: -a: ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的, ...

  6. Linux的ls命令在Windows中的应用

    Linux的ls命令在Windows中的应用 注:ls是Linux中的命令.其作用是列出当前目录下的文件与文件夹.效果等同于Wndows中的dir指令. 如下图 下面是详细步骤 步骤一.在桌面新建一个 ...

  7. linux系统中ls命令的用法

    普通文件: -,f目录文件: d链接文件(符号链接): L设备文件:字符设备:c块设备:b命名管道: p套接字文件: s linux文件时间戳 时间分为三种类型:创建时间,修改时间:open访问时间: ...

  8. Linux中exec命令相关

    Linux中exec命令相关 exec和source都属于bash内部命令(builtins commands),在bash下输入man exec或man source可以查看所有的内部命令信息. b ...

  9. Linux下ls命令显示符号链接权限为777的探索

    Linux下ls命令显示符号链接权限为777的探索 --深入ls.链接.文件系统与权限 一.摘要 ls是Linux和Unix下最常使用的命令之一,主要用来列举目录下的文件信息,-l参数允许查看当前目录 ...

随机推荐

  1. SyntaxError: Non-ASCII character '\xe2' in file 编码错误

    Editing .py file in the Notepad: But when run in the PowerShell, I found the follwing error: It seem ...

  2. Mongoose的使用

    最近想做一个练手的App小项目.考虑到数据接口的问题,因为关系型数据库用的比较多,也有一定经验了,所以打算使用比较火的MongoDB作为数据库,下面就介绍一下Mongoose的使用方法吧. 概念:Mo ...

  3. T - stl 的mapⅡ

    Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language th ...

  4. Spring中注解的使用详解

    一:@Rsource注解的使用规则 1.1.案例演示 Spring的主配置文件:applicationContext.xml(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...

  5. Android 使用BroadcastReceiver的几种方法

    发送自定义广播 全局广播 发送标准广播 1.定义广播接收器.(在发送广播前,需要先定义一个广播接收器,不然发了也是白发) public class MyBroadcastReceiver extend ...

  6. ecos3.0编译 if_lancepci.c:528: 错误: 赋值运算的左操作数必须是左值

    /home/xin/ecos3/ecos-3.0/packages/devs/eth/amd/lancepci/v3_0/src/if_lancepci.c:528: 错误: 赋值运算的左操作数必须是 ...

  7. Pet--hdu4707

    Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. 计算两点间的距离,hdu-2001

    计算两点间的距离 Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离.   Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1 ...

  9. 打造阅读Linux源代码利器

    打造阅读Linux源代码利器 在Linux里阅读/编写代码一般用vi 但是碰到较大的项目时阅读源代码还是比较费力,一直用find  和 grep命令. 其实,我们自己可以打造一个阅读源代码的vim,这 ...

  10. 浅谈 qmake 之 shadow build(就是将源码路径和构建路径分开)

    shadow build shadow build 是什么东西?就是将源码路径和构建路径分开(也就是生成的makefile文件和其他产物都不放到源码路径),以此来保证源码路径的清洁. 这不是qmake ...