资源简介
自己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图和程序
相关资源
- uboot到linux logo显示不间断 补丁
- ANSYS LS-DYNA
- VisualStudioUninstaller vs卸载工具
- UNIX/LINUX编程实践教程的源码
- B/S 网上订餐系统
- Linux任务管理器
- linux应用层的华容道游戏源代码
- VMware65_SLP_DeLLSLIC2.1
- 加密cad图形的lisp文件
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- linux下用多进程同步方法解决生产者
- 单片机控制74ls164程序
- ModelSim全套中文手册ModelSim SE6.0C
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- LSI RAID配置手册(图文)
- linux 网络实验 ftp程序
- 管家婆加密狗工具dogtools
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
评论
共有 条评论