资源简介
自己c实现linux下ls的两个选项 给大家提供个思路
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void show_l(char *filenameint *cnt);
int main(int argc char *argv[] )
{
int count = 0;
DIR *dp;
struct dirent *dirp;
char *filepath=“.“;
if ((dp = opendir(filepath)) == NULL)
{
perror(“open :“);
exit(1);
}
//printf(“argc = %d\targv[1] = %s\n“argcargv[1]);
while ((dirp = readdir(dp)) != NULL)
{
if (argc == 1)
{
if (dirp->d_name[0] ==‘.‘)
continue;
printf(“%s\t“dirp->d_name);
continue;
}
if (strcmp(argv[1] “-a“) == 0)
{
printf(“%s\t“dirp->d_name);
}
else
if (strcmp(argv[1] “-l“) == 0)
{
if (dirp->d_name[0] ==‘.‘)
continue;
show_l(dirp->d_name&count);
}
else
{
printf(“参数出错.\n“);
break;
}
}
printf(“\n“);
closedir(dp);
return 0;
}
void show_l(char *filenameint *cnt)
{
/*drwxr-xr-x 2 root root 4096 10-17 19:58 cs
-rwxr-xr-x 1 root root 5520 10-17 20:03 list
*/
struct stat buf;
if (stat(filename&buf) < 0)
{
printf(“%s\n“filename);
exit(1);
}
//printf(“%s\n“filename);
//*cnt += buf.st_blocks;
if (S_ISREG(buf.st_mode))
printf(“-“);
else if (S_ISDIR(buf.st_mode))
printf(“d“);
else if (S_ISCHR(buf.st_mode))
printf(“c“);
else if (S_ISBLK(buf.st_mode))
printf(“b“);
else if (S_ISFIFO(buf.st_mode))
printf(“f“);
else if (S_ISLNK(buf.st_mode))
printf(“l“);
else if (S_ISSOCK(buf.st_mode))
printf(“s“);
else
printf(“*“);
if (((buf.st_mode) &S_IRUSR) == S_IRUSR)
printf(“r“);
else
printf(“-“);
if (S_IWUSR == (buf.st_mode & S_IWUSR))
printf(“w“);
else
printf(“-“);
if ((S_IXUSR == (buf.st_mode & S_IXUSR)) && (S_ISUID == (buf.st_mode & S_ISUID)))
printf(“s“);
else if (S_IXUSR == (buf.st_mode & S_IXUSR))
printf(“x“);
else if (S_ISUID == (buf.st_mode & S_ISUID))
printf(“S“);
else
printf(“-“);
if (S_IRGRP == (buf.st_mode & S_IRGRP))
printf(“r“);
else
printf(“-“);
if (S_IWGRP == (buf.st_mode & S_IWGRP))
printf(“w“);
else
printf(“-“);
if ((S_IXGRP == (buf.st_mode & S_IXGRP)) && (S_ISGID == (buf.st_mode & S_ISGID)))
printf(“s“);
else if (S_IXGRP == (buf.st_mode & S_IXGRP))
printf(“x“);
else if (S_ISGID == (buf.st_mode & S_ISGID))
printf(“S“);
else
printf(“-“);
if (S_IROTH == (buf.st_mode & S_IROTH))
printf(“r“);
el
- 上一篇:2fsk的调制与解调仿真
- 下一篇:流水灯Protues图和程序
相关资源
- linux题
- windows7下Lua的编译,使用Visual Studio
- Linux环境下的网络抓包工具pcap,界面
-
LSCommon
helper和LSGISHelper - 最新版本linux libc库实现源码
- gtk写的贪吃蛇游戏
- grub引导文件
- linux为服务器,Windows为客户端的简单
- VmwareHorizonView6ForLinuxVDI全套解决方案所
- 学生信息管理系统项 Linux
- 操作系统接口:兼容Unix/Linux命令接口
- OpenVPN在Linux教程.pdf
- Linux 常用C函数中文版
- linux c下acc打包成RTP并在VLC上播放
- keil 中 TOOLS.INI 文件
- 用net-snmp的api实现trap接收,windows+li
- Datawindow导出Excel控件(dw2xls.pbl)4.1
- ModelSimSE_10.2c的破解文件,绝对可用
- 基于Linux嵌入式网络视频监控系统
- OCX接口查看工具
- linux网络编程源码
- 把Linux中的文本文件格式转为Windows的
- iNEMO-V2LSM303DLHC(3轴加速度+3轴地磁)
- 数字时钟,采用555构成的多协振荡器
- 2018年中国城市分级完整名单包含338个
- Live555基于h264嵌入式linux下rtsp项目的裁
- OK6410 linux混杂设备驱动LED
- Linux RS485 通信代码
- Linux系统下dhcp源码
- arm开发板上摄像头抓图源代码(v4l通
评论
共有 条评论