文档库 最新最全的文档下载
当前位置:文档库 › myls

myls

myls
myls

/*

* 自己写的l s函数,能实现: 例如myls -al /home/

* 后面的参数可以随意组合,但是只实现了-a 和-l 选项

* 代码看上去比较多,其实很多是一样的

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

void myread_dir();

void myls(char *option,char *dir);

void myread_dir_a(); // ls -a

void myread_dir_l(); // ls -l

void myread_dir_al(); // ls -al

void print_file_mode(unsigned short num); // 打印文件类型符号

void print_other_mode(unsigned short num); // 打印其它权限

void print_own_mode(unsigned short num); // 打印文件拥有者权限

void print_group_mode(unsigned short num); // 打印组用户权限

char *get_user_name(uid_t uid); //通过uid获得用户名

void myread_dir_dir(char *s);

void myread_dir_al_dir(char *s); // ls -al /dir

void myread_dir_l_dir(char *s); // ls -l /dir

void myread_dir_a_dir(char *s); // ls -a /dir

void print_file_info(struct dirent *ep,struct stat st);//把打印文件的信息放在这里面

/*

* 把输入的参数放到数组里面,假定有三个参数,第二个放在a1,第三个放在a2,* 若参数不存在,在数组第一个元素放0

*/

int main(int argc,char *argv[])

{

char a1[50];

char a2[50];

switch(argc)

{

case 1:

a1[0] = 0;

a2[0] = 0;

break;

case 2:

if(argv[1][0] == '-')

{

strncpy(a1,argv[1],strlen(argv[1]));

a2[0] = 0;

}

else

{

a1[0] = 0;

strcpy(a2,argv[1]);

}

break;

case 3:

strcpy(a1,argv[1]);

strcpy(a2,argv[2]);

break;

default:

if(argc > 3)

printf("there are too many arguments!\n");

break;

}

myls(a1,a2);

return 0;

}

/*

*根据参数情况,把各种组合情况考虑进来,如ls -a, ls -l , ls /dir , ls ,等多种情况*/

void myls( char *option,char *dir)

{

char *po = option;

char *pd = dir;

if(po[0] == 0 && pd[0] == 0)//不带参数(第一个不算)

{

myread_dir();

}

else if(po[0] != 0 && pd[0] == 0) //带一个参数,我们还要考虑,到底是哪一个{

if((strlen(po) > 3) && (strlen(po) < 2))

{

printf("The opition is wrong!\n");

exit(-1);

}

else

{

if((strchr(po,'a') != NULL) && (strchr(po,'l') == NULL))/* -a */

{

myread_dir_a();

}

else if((strchr(po,'a') == NULL) && (strchr(po,'l') != NULL))/* -l */

{

myread_dir_l();

}

else if((strchr(po,'a') != NULL) && (strchr(po,'l') != NULL))/* -al */

{

myread_dir_al();

}

}

}

else if(po[0] == 0 && pd[0] != 0)

{

myread_dir_dir(pd);

}

else if(po[0] != 0 && pd[0] != 0) //带二个参数,这里也有多种情况

{

if(strchr(po,'a') != NULL && strchr(po,'l') == NULL)

{

myread_dir_a_dir(pd);

}

else if(strchr(po,'a') == NULL && strchr(po,'l') != NULL)

{

myread_dir_l_dir(pd);

}

else if(strchr(po,'a') != NULL && strchr(po,'l') != NULL)

{

myread_dir_al_dir(pd);

}

}

}

/*读当前目录*/

/* eg: ls ./ */

void myread_dir()

{

DIR *dp;

struct dirent *ep;

dp = opendir("./");

if(dp != NULL)

{

while((ep = readdir(dp)))

{

if(ep->d_name[0] !='.')

printf("%s ",ep->d_name);

}

putchar('\n');

}

closedir(dp);

}

/*读指定文件目录*/

/*eg: ls /dir */

void myread_dir_dir(char *s)

{

DIR *dp;

struct dirent *ep;

int count = 0;

dp = opendir(s);

if(dp == NULL)

{

perror("open failed");

printf("%s\n",s);

}

if(dp != NULL)

{

while((ep = readdir(dp)))

{

if(ep->d_name[0] !='.')

{

printf("%-20s",ep->d_name);

count++;

}

if(count == 5)

{

putchar('\n');

count = 0;

}

putchar('\n');

}

closedir(dp);

}

/*读目录下所有文件,包括隐藏文件*/

/*eg: ls -a ./ */

void myread_dir_a()

{

DIR *dp;

struct dirent *ep;

dp = opendir("./");

if(dp != NULL)

{

while((ep = readdir(dp)))

{

printf("%s ",ep->d_name);

}

putchar('\n');

}

closedir(dp);

}

/* myls -a /home/ */

/*显示指定目录下的全部文件*/

void myread_dir_a_dir(char *s)

{

DIR *dp;

struct dirent *ep;

int count = 0;

dp = opendir(s);

if(dp != NULL)

{

while((ep = readdir(dp)))

{

printf("%-25s",ep->d_name);

count++;

if(count == 4)

{

putchar('\n');

count = 0;

}

putchar('\n');

}

closedir(dp);

}

/* myls -al /home/ */

/*打印目录信息,可带参数*/

void myread_dir_al_dir(char *s)

{

DIR *dp;

struct dirent *ep;

struct stat st;

dp = opendir(s);

if(dp != NULL)

{

while((ep = readdir(dp)))

{

char *str = malloc(50);

strcpy(str,s);

strcat(str,ep->d_name);/*把s加到文件名的前面去*/

if(stat(str,&st) != -1)

{

print_file_info(ep,st);

}

else

{

printf("stat failed!\n");

}

free(str);

}

}

closedir(dp);

}

/*myls -l /home/ */

/*显示指定目录下的全部文件*/

void myread_dir_l_dir(char *s)

{

DIR *dp;

struct dirent *ep;

struct stat st;

dp = opendir(s);

//printf("%s\n",s);

if(dp != NULL)

{

while((ep = readdir(dp)))

{

if(ep->d_name[0] != '.')

{

char *str = malloc(50);

strcpy(str,s);

strcat(str,ep->d_name);/*把s加到文件名的前面去*/

if(stat(str,&st) != -1)

{

print_file_info(ep,st);

}

else

{

printf("stat failed!\n");

}

free(str);

}

}

}

closedir(dp);

}

/* ls -al ./ */

void myread_dir_al()

{

DIR *dp;

struct dirent *ep;

struct stat st;

dp = opendir("./");

if(dp != NULL)

{

while((ep = readdir(dp)))

{

if(stat(ep->d_name,&st) != -1)

{

print_file_info(ep,st);

}

}

}

closedir(dp);

}

/*把打印全放在这个函数中了*/

void print_file_info(struct dirent *ep,struct stat st) {

unsigned short num;

num = st.st_mode & S_IFMT;

print_file_mode(num);/*打印文件类型*/

num = st.st_mode & S_IRWXU;

print_own_mode(num);/*打印文件拥有者权限*/

num = st.st_mode & S_IRWXG;

print_group_mode(num);/*打印同组用户权限*/

num = st.st_mode & S_IRWXO;

print_other_mode(num);/*打印其它人权限*/

printf(" ");/*分隔符*/

printf("%3u",st.st_nlink);/*打印文件链接数*/

printf(" ");/*分隔符*/

printf("%s",get_user_name(st.st_uid));

printf(" ");

printf("%s",get_user_name(st.st_gid));

printf(" ");/*分隔符*/

printf("%ld",st.st_blksize);

printf(" ");/*分隔符*/

char str[50];

strcpy(str,ctime(&(st.st_mtime)));

str[strlen(str)-1] = ' ';

printf("%s",str);

printf(" ");/*分隔符*/

printf("%s",ep->d_name);

putchar('\n');

}

/*eg: ls -l ./ */

void myread_dir_l()

{

DIR *dp;

struct dirent *ep;

struct stat st;

dp = opendir("./");

if(dp != NULL)

{

while((ep = readdir(dp)))

{

if(ep->d_name[0] != '.')

{

if(stat(ep->d_name,&st) != -1)

{

print_file_info(ep,st);

}

}

}

}

closedir(dp);

}

/*打印文件类型*/

void print_file_mode(unsigned short num)

{

switch(num)

{

case S_IFSOCK:

printf("s");

break;

case S_IFLNK:

printf("l");

break;

case S_IFREG:

printf("-");

break;

case S_IFBLK:

printf("b");

break;

case S_IFDIR:

printf("d");

break;

case S_IFCHR:

printf("c");

break;

case S_IFIFO:

printf("f");

break;

default:

break;

}

}

/*打印用户权限*/

void print_own_mode(unsigned short num) {

unsigned short temp;

temp = num;

if((num & S_IWUSR) == S_IWUSR)

printf("w");

else

printf("-");

num = temp;

if((num & S_IRUSR) == S_IRUSR)

printf("r");

else

printf("-");

num =temp;

if((num & S_IXUSR) == S_IXUSR)

printf("x");

else

printf("-");

}

/*打印组用户权限*/

void print_group_mode(unsigned short num) {

unsigned short temp;

temp = num;

if((num & S_IWGRP) == S_IWGRP)

printf("w");

else

printf("-");

num = temp;

if((num & S_IRGRP) == S_IRGRP)

printf("r");

else

printf("-");

num = temp;

if((num & S_IXUSR) == S_IXUSR)

printf("x");

else

printf("-");

}

/*打印其它用户权限*/

void print_other_mode(unsigned short num)

{

unsigned short temp;

temp = num;

if((num & S_IWOTH) == S_IWOTH)

printf("w");

else

printf("-");

num =temp;

if((num & S_IROTH) == S_IROTH)

printf("r");

else

printf("-");

num = temp;

if((num & S_IXOTH) == S_IXOTH)

printf("x");

else

printf("-");

}

/*这里放了一个很重要的错误,关于指针的,后来改过来了*/ char * get_user_name(uid_t uid)/*通过uid得到用户名*/

{

struct passwd *pn;// = &pw;

pn = getpwuid(uid);

return pn->pw_name;

}

相关文档