#include
#include
#include
#include
#include
#include
#include
#include
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命令详解 (转)
-a -- 全部(all).列举目录中的全部文件,包括隐藏文件(.filename).位于这个列表的起首处的 .. 和 . 依次是指父目录和你的当前目录. -l -- 长(long).列举目 ...
Linux中ls命令详解
ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,下面我们就来一起看看ls的用法 英文全名:List即列表的意思,当我们学习某种东西的时候要做到知其所 ...
Linux中ls命令用法
ls 命令的含义是list显示当前目录中的文件名字.注意不加参数它显示除隐藏文件外的所有文件及目录的名字. 1)ls –a 显示当前目录中的所有文件,包含隐藏文件 命令: aijian.shi@U-a ...
linux中ls命令使用选项
ls:英文全名:List即列表的意思 -a 列出目录下的所有文件,包括以 . 开头的隐含文件.-b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式列出.-c 输出文件的 i 节 ...
linux中ls命令
ls跟dos下的dir命令是一样的都是用来列出目录下的文件 ls参数: -a: ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的, ...
Linux的ls命令在Windows中的应用
Linux的ls命令在Windows中的应用 注:ls是Linux中的命令.其作用是列出当前目录下的文件与文件夹.效果等同于Wndows中的dir指令. 如下图 下面是详细步骤 步骤一.在桌面新建一个 ...
linux系统中ls命令的用法
普通文件: -,f目录文件: d链接文件(符号链接): L设备文件:字符设备:c块设备:b命名管道: p套接字文件: s linux文件时间戳 时间分为三种类型:创建时间,修改时间:open访问时间: ...
Linux中exec命令相关
Linux中exec命令相关 exec和source都属于bash内部命令(builtins commands),在bash下输入man exec或man source可以查看所有的内部命令信息. b ...
Linux下ls命令显示符号链接权限为777的探索
Linux下ls命令显示符号链接权限为777的探索 --深入ls.链接.文件系统与权限 一.摘要 ls是Linux和Unix下最常使用的命令之一,主要用来列举目录下的文件信息,-l参数允许查看当前目录 ...
随机推荐
MFC下OpenGL入门(可以用)
MFC下OpenGL入门 源文件 1, 建一工程文件,我这里命名为first,现在first工程里面我们没有添加任何东西,所有的东西都是MFC自动帮我们创建的. 2, 添加链接库.这一步很关键.打开菜 ...
Tomcat启动时项目重复加载,导致资源初始化两次的问题
http://blog.csdn.net/testcs_dn/article/details/38855641
Sqlite3 设置插入触发器
需求: 数据库中表t_VerifyCsmDetail需要最多保存10W条记录,超出时删除最旧的那一条. 思路:设置插入触发器.插入前先判断表中记录总数,如果大于99999条,则删除最旧的一条记录. 代 ...
键盘上各键对应的ASCII码与扫描码
键盘上各键对应的ASCII码与扫描码 vbKeyLButton 0x1 鼠标左键vbKeyRButton 0x2 鼠标右键vbKeyCancel 0x3 CANCEL 键vbKeyMButton 0x ...
Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include #include ...
HDOJ(HDU) 2136 Largest prime factor(素数筛选)
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
HUD2087
#include #include #include #define maxn 1010 using name ...
java随机数生成器
一.java.lang.Math.Random 调用这个Math.Random()函数能够返回带正号的double值,取值范围是[0.0,1.0)的左闭右开区间,并在该范围内(近似)均匀分布. 二.j ...
HDU 4970 Killing Monsters
开始以为是线段树,算了一下复杂度也觉得能过...但是这题貌似卡了线段树... 具体做法: 对每一个塔,记录attack[l]+=d,attack[r+1]-=d;这样对于每个block,受到的伤害就是 ...
Linux进程间通信——使用消息队列
下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信——使用命名管道 一.什么是消息队列 消息队列提 ...