my_ls
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
/*
* $ls -la ~
* total 460
* drwxr-xr-x 31 linux linux 4096 10月 27 17:27 .
* drwxr-xr-x 3 root root 4096 9月 22 2012 ..
* -rw------- 1 linux linux 3126 10月 5 12:56 altvimrc
* -rw-rw-r-- 1 linux linux 102 10月 27 11:31 .apport-ignore.xml
* -rw------- 1 linux linux 20110 10月 27 15:47 .bash_history
* -rw-r--r-- 1 linux linux 220 9月 22 2012 .bash_logout
*/
int main(int argc, const char *argv[])
{
//打开目录流
//循环读取其中的文件名,
//将文件名使用stat()读取属性,并做相应解析
//写入到文件
DIR* dp=opendir(".");
struct dirent* Dir;
struct stat File;
while(Dir=readdir(dp)){
stat(Dir->d_name,&File);
char prot[11]={0};
//获取文件类型并解析
switch (File.st_mode&S_IFMT){
case S_IFSOCK:
strcpy(prot,"s");
break;
case S_IFLNK:
strcpy(prot,"l");
break;
case S_IFREG:
strcpy(prot,"-");
break;
case S_IFBLK:
strcpy(prot,"b");
break;
case S_IFDIR:
strcpy(prot,"d");
break;
case S_IFCHR:
strcpy(prot,"c");
break;
case S_IFIFO:
strcpy(prot,"p");
break;
}
//获取文件权限并解析
//处理属主权限
if(S_IRUSR==((File.st_mode&S_IRWXU)&S_IRUSR))
strcat(prot,"r");
else
strcat(prot,"-");
if(S_IWUSR==((File.st_mode&S_IRWXU)&S_IWUSR))
strcat(prot,"w");
else
strcat(prot,"-");
if(S_IXUSR==((File.st_mode&S_IRWXU)&S_IXUSR))
strcat(prot,"x");
else
strcat(prot,"-");
//处理属组权限
if(S_IRGRP==((File.st_mode&S_IRWXG)&S_IRGRP))
strcat(prot,"r");
else
strcat(prot,"-");
if(S_IWGRP==((File.st_mode&S_IRWXG)&S_IWGRP))
strcat(prot,"w");
else
strcat(prot,"-");
if(S_IXGRP==((File.st_mode&S_IRWXG)&S_IXGRP))
strcat(prot,"x");
else
strcat(prot,"-");
//处理其他权限
if(S_IROTH==((File.st_mode&S_IRWXO)&S_IROTH))
strcat(prot,"r");
else
strcat(prot,"-");
if(S_IWOTH==((File.st_mode&S_IRWXO)&S_IWOTH))
strcat(prot,"w");
else
strcat(prot,"-");
if(S_IXOTH==((File.st_mode&S_IRWXO)&S_IXOTH))
strcat(prot,"x");
else
strcat(prot,"-");
//获取文件硬链接数
//获取文件属主的名
struct passwd* pwd=getpwuid(File.st_uid);
//获取文件属主的组名
struct group* grp=getgrgid(File.st_gid);
//获取文件的大小
//获取文件的mtime
struct tm* myTime;
myTime=localtime(&File.st_mtime);
printf("%s %d %s %s %5ld %d月 %d %02d:%02d %s\n",
prot,
File.st_nlink,
pwd->pw_name,
grp->gr_name,
File.st_size,
myTime->tm_mon+1,
myTime->tm_mday,
myTime->tm_hour,
myTime->tm_min,
Dir->d_name
);
}
return 0;
}
my_ls的更多相关文章
- (笨方法)利用stat函数实现ls -l filename
学习了一段时间的Linux了,但是我感觉我做不出来啥子,后头选择利用系统IO函数实现命令,先从ls走起吧.先来看看ls -l filename给我们显示了什么吧 : 可以看到,一共八项:文件类型.用户 ...
- 【操作系统】C语言编写的FAT16文件系统
[操作系统]C语言编写的FAT16文件系统 这是操作系统的期末课程设计作业之一,主要功能是在物理内存中虚拟出一个1M大小的FAT16的文件系统,然后把它读入内存中,进行具体的文件操作,具体的实用性不大 ...
- python传参
写在前面 Python唯一支持的参数传递方式是『共享传参』(call by sharing) 多数面向对象语言都采用这一模式,包括Ruby.Smalltalk和Java(Java的引用类型是这样,基本 ...
- Python传入参数的几种方法
写在前面 Python唯一支持的参数传递方式是『共享传参』(call by sharing) 多数面向对象语言都采用这一模式,包括Ruby.Smalltalk和Java(Java的引用类型是这样,基本 ...
随机推荐
- 2016 年青岛网络赛---Family View(AC自动机)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...
- Verilog学习笔记简单功能实现(五)...............序列检测设计
这里采用夏宇闻教授第十五章的序列检测为例来学习; 从以上的状态转换图可以写出状态机的程序: module seqdet(x,out,clk,rst); input x,clk,rst; output ...
- 【转】PHP计划任务:如何使用Linux的Crontab执行PHP脚本
转:https://www.centos.bz/2011/03/auto-run-task-crontab/ 我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是 ...
- Spring IoC源码解读——谈谈bean的几种状态
阅读Spring IoC部分源码有一段时间了,经过不断的单步调试和参阅资料,对Spring容器中bean管理有了一定的了解.这里从bean的几个状态的角度出发,研究下IoC容器. 一.原材料 Xml中 ...
- slid.es – 创建在线幻灯片和演示文稿的最佳途径
slid.es 提供了一种创建在线幻灯片和演示文稿的简单方法,让你通过几个简单的步骤制作效果精美的在线演示文稿.基于 HTML5 和 CSS3 实现,在现代浏览器中效果最佳. 您可能感兴趣的相关文章 ...
- asp.net保存网上图片到服务器
本文讲述的是根据一个图片的url地址,保存图片到asp.net服务器端的实现方法. 可以先了解这篇文章http://keleyi.com/a/bjac/0899dpgi.htm,以更好的理解本文. 首 ...
- Kali Linux (XFce版本)安装后的一些设置
kali Linux的主版本自带的是Gnome桌面环境,安装后使用效率太低,不知道是不是我机器配置低的原因, 在虚拟机里运行起来太慢.卡.丑啦....所以以前都一直都在用Backbox Linux,并 ...
- putty不能连接linxu,报:connection refused
用putty连接新装的centos6.5,出现了connection refused问题, 后参考http://blog.sina.com.cn/s/blog_60d2d62a0100tq2l.htm ...
- [SharePoint] SharePoint 错误集 1
1. Delete a site collection · Run command : Remove-SPSite –Identity http://ent132.sharepoint.hp.com/ ...
- 【转】扫盲 同步利器、分布式网盘--BT Sync
原文地址:http://program-think.blogspot.com/2015/01/BitTorrent-Sync.html先向大伙儿宣布个好消息——经过多位热心读者的大力支持,经过几天的努 ...