linux 下myls实现
来源:互联网 发布:有人做淘宝秒杀群 编辑:程序博客网 时间:2024/06/05 23:07
==============================================================================================================================================================================
作者:心梦无痕
完成时间2013 .7 .26
程序主体思路来自,linuxc 编程实战 自己实现ls
find(); 除外
分析原代碼的一些感悟,该程序将命令行中的参数转化为宏(以数字替换)
--1.#define PARAM_NONE 0在main函数中,int flag=PARAM_NONE ;得到flag时,
通过判断,参数赋值给flag
flag|=(定义过该代表命令的宏)
有 0|x==x;
获得命令参数标志
如有多个选项直接讨论他们相加的情况,避免了直接对,选项名的讨论。
--2.定义了每一行容纳长度的宏
当前行的占用长度,以此作为衡量尺度判断,该文件名应该放到该行还是下一行。
再问件名间打印固定的空格数,(也可定义为宏);
如果使用固定个数打印,如果某行文件名较长的话,就不能显示全文件名;
然而该方法会自动将其放到下一行打印
=============================================================================================================================================================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <string.h>
#define PARAM_NONE 0 //无参数
#definePARAM_A 1//-a
#definePARAM_L 2//-l
#definePARAM_R 10
#define MAXROWLEN 80//一行显示的最多字数
int sys_leave=MAXROWLEN;
int sys_maxlen;
void show_attribute(struct stat buf,char* filename);
void display(int flag,char* pathname);
void show_ruler(char *);
void find_all(char* dirnamex)
{
DIR *dir=NULL;
chardirname[1024];
chardirlist[256][1204];
structdirent * ptr;
struct statst;
inti=0,j=0;
charpath1[512];
puts(dirnamex);
chdir(dirnamex);
getwd(dirname);
printf("dir:%s\n",dirname);
if((dir=opendir("./"))==NULL)
{
printf("null");
return;
}
i=0;
while((ptr=readdir(dir))!=NULL)
{
if(stat(ptr->d_name,&st)==-1)
{
perror("error name");
}
if(strcmp(ptr->d_name,".")&&strcmp(ptr->d_name,".."))
{
//puts(ptr->d_name);
show_ruler(ptr->d_name);
if(S_ISDIR(st.st_mode))
{
strcpy(dirlist[i++],ptr->d_name);
}
}
}
for(j=0;j<i;j++)
{
find_all(dirlist[j]);
}
//回到上层目录,将其设置为工作目录
chdir("..");
}
void display_dir(int flag,char* path)
{
DIR*dir;
structdirent* ptr;
intcount;
作者:心梦无痕
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <string.h>
#define PARAM_NONE 0 //无参数
#definePARAM_A
#definePARAM_L
#definePARAM_R
#define MAXROWLEN
int sys_leave=MAXROWLEN;
int sys_maxlen;
void show_attribute(struct stat buf,char* filename);
void display(int flag,char* pathname);
void show_ruler(char *);
void find_all(char* dirnamex)
{
}
void display_dir(int flag,char* path)
{