资源简介
这是自己写的ls -R函数,实现了分栏、隐藏文件隐藏、上色等功能,没有排序。用的时候根据输入不同有两种功能 ls 和ls -R 、用的时候只要再加一个主函数就可以了。
代码片段和文件信息
#include
#include
#include
#include
#include
void do_ls(char[]int);
void dostat(char *char *);
void show_file_info( char * struct stat *);
void mode_to_letters( int char [] );
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );
lsR(int ac char *av[])
{
int R_flag = 0;
int anyfiles = 0;
while ( --ac ){
if ( strcmp(“-R“ *++av) == 0 )
R_flag = 1;
else {
do_ls( *av R_flag );
anyfiles = 1;
}
}
if ( !anyfiles )
do_ls(“.“ R_flag);
}
void do_ls( char dirname[] int subdirs )
{
DIR *dir_ptr;
struct dirent *direntp;
char *fullpath;
if ( ( dir_ptr = opendir( dirname ) ) == NULL ){
fprintf(stderr“ls2: cannot open %s\n“ dirname);
return;
}
printf(“%s:\n“ dirname);
fullpath = (char *)malloc(strlen(dirname) + 1 + MAXNAMLEN + 1);
while ( ( direntp = readdir( dir_ptr ) ) != NULL )
{
char cname[20];
strcpy(cnamedirentp->d_name);
if(cname[0]!=‘.‘)
{
sprintf(fullpath“%s/%s“dirnamedirentp->d_name);
dostat( fullpath direntp->d_name );
}
}
if ( subdirs ){
rewinddir(dir_ptr);
while ( ( direntp = readdir( dir_ptr ) ) != NULL ){
if(direntp->d_name[0]==‘.‘)
continue;
sprintf(fullpath“%s/%s“dirnamedirentp->d_name);
if ( isadir1(fullpath) ){
putchar(‘\n‘);
do_ls( fullpath subdirs );
}
}
}
closedir(dir_ptr);
free(fullpath);
}
void dostat( char *fullpath char *filename )
{
struct stat info;
if ( lstat(fullpath &info) == -1 )
perror(filename);
else
show_file_info(filename &info);
}
void show_file_info( char *filename struct stat *info_p )
{
char *uid_to_name() *ctime() *gid_to_name() *filemode();
void mode_to_letters();
char modestr[11];
mode_to_letters( info_p->st_mode modestr );
printf( “%s“ modestr );
printf( “%4d “ (int) info_p->st_nlink);
printf( “%-8s “ uid_to_name(info_p->st_uid) );
printf( “%-8s “ gid_to_name(info_p->st_gid) );
printf( “%8ld “ (long)info_p->st_size);
- 上一篇:编译原理课程设计for循环LR法三元式
- 下一篇:飞跃手册汇总.rar
相关资源
- pthreads-w32-2-9-1-release.zip
- uboot到linux logo显示不间断 补丁
- 修正Sway-Rocking土-结构相互作用模型的
- ANSYS LS-DYNA
- VisualStudioUninstaller vs卸载工具
- UNIX/LINUX编程实践教程的源码
- B/S 网上订餐系统
- Linux任务管理器
- linux应用层的华容道游戏源代码
- VMware65_SLP_DeLLSLIC2.1
- 加密cad图形的lisp文件
- YS
- ubuntu9.10 可加载内核模块和字符设备驱
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- Tone-Reservation
- 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命令大全离线版&在线版
评论
共有 条评论