#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. Javascript进阶篇——(JS基础语法)笔记整理

    根据慕课网学习整理到一起的笔记,把东西整理到一起看起来比较方便 什么是变量字面意思:变量是可变的量:编程角度:变量是用于存储某种/某些数值的存储器.我们可以把变量看做一个盒子,盒子用来存放物品,物品可 ...

  2. mvc ajax请求

    @{ ViewBag.Title = "ajax"; } <script src="../../Scripts/jquery-1.4.4.js" type ...

  3. 【IOS学习基础】文件相关

    一.沙盒(SandBox) 1.沙盒机制 1> 每个应用都有属于自己的存储空间,即沙盒. 2> 应用只能访问自己的沙盒,不可访问其他区域. 3> 如果应用需要进行文件操作,则必须将文 ...

  4. What is SignalR and Why Should I Use It?

    原文地址: What is SignalR and why should I use it? When I first heard about SignalR, I was not sure what ...

  5. javascript 高级程序设计学习笔记(面向对象的程序设计) 2

    在调用构造函数时会为实例添加一个指向最初原型的指针,我们可以随时为原型添加属性和方法,并且能在实例中体现出来,但如果是重新了原型对象,那就会切断构造函数与最初原型的联系. function Dog ( ...

  6. javabean对象自动赋值给另一个javabean对象

    方法1:把JavaBean的from的值自动set给to,省略了自己从from中get然后再set给to import java.beans.BeanInfo;import java.beans.In ...

  7. Hibernate: merge方法

    在Hibernate中,有save.persist.savaOrUpdate.merge等方法有插入数据的功能.前三者理解起来较后者容易一些,merge方法从api中的介绍就看以看出它是最复杂的.下面 ...

  8. The package does not support the device architecture (x86). You can change the supported architectures in the Android Build section of the Project Opt

    The package does not support the device architecture (x86). You can change the supported architectur ...

  9. Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析

    原文:Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析 前段时间,公司同事开发了一个小工具,在工具执行过程中,UI界面一直处于卡死状态. 通过阅读代码发现,主要是 ...

  10. android 管理Bitmap内存 - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接   Managing Bitmap Memory 管理Bitmap内存 In additi ...