c 实现linux命令wc,C写的实现wc和ls函数

#include //用lstat函数获取文件的类型

int get_mode(char *file)

{

struct stat buf;

char *ptr;

if(lstat(file, &buf) < 0)

{

printf("error\n!");

}

switch(S_IFMT&buf.st_mode)

{

case S_IFREG:ptr = "-";break;

case S_IFDIR:ptr = "d";break;

case S_IFCHR:ptr = "c";break;

case S_IFBLK:ptr = "b";break;

case S_IFIFO:ptr = "f";break;

case S_IFLNK:ptr = "l";break;

case S_IFSOCK:ptr = "s";break;

default:ptr = "UNKNOW";

}

printf("%s", ptr);

return 0;

}

//用stat获取文件拥有者,用户组和其他组的权限

int get_rwx(char *file)

{

struct stat buf;

if(stat(file, &buf) == -1)

{

perror("stat \n");

return 0;

}

//文件拥有者的权限获取

if(S_IRUSR&buf.st_mode) {

printf("r");

} else {

printf("-");

}

if(S_IWUSR&buf.st_mode) {

printf("w");

} else {

printf("-");

}

if(S_IXUSR&buf.st_mode) {

printf("x");

} else {

printf("-");

}

//文件用户组的权限获取

if(S_IRGRP&buf.st_mode) {

printf("r");

} else {

printf("-");

}

if(S_IWGRP&buf.st_mode) {

printf("w");

} else {

printf("-");

}

if(S_IXGRP&buf.st_mode) {

printf("x");

} else {

printf("-");

}

//文件其他组的权限获取

if(S_IROTH&buf.st_mode) {

printf("r");

} else {

printf("-");

}

if(S_IWOTH&buf.st_mode) {

printf("w");

} else {

printf("-");

}

if(S_IXOTH&buf.st_mode) {

printf("x");

} else {

printf("-");

}

return 0;

}

//获取文件的大小

long get_size(char *file)

{

struct stat buf;

long n;

stat(file, &buf);

n = buf.st_size;

return n;

}

//获取文件的修改时间

int get_time(char *file, char *ch)

{

struct stat buf;

stat(file, &buf);

strcpy(ch, ctime(&buf.st_mtime));

ch[strlen(ch) - 1] = '\0';

return 0;

}

//文件的硬链接数目,并且打印出来

int get_nlink(char *file)

{

struct stat buf;

stat(file, &buf);

printf(" %d", buf.st_nlink);

return 0;

}

//获取文件拥有者和用户组的name

int get_name(char *file)

{

struct stat buf;

char name1[20], name2[20];

stat(file, &buf);

strcpy(name1, getpwuid(buf.st_uid)->pw_name);

strcpy(name2, getpwuid(buf.st_gid)->pw_name);

printf(" %s %s", name1, name2);

return 0;

}

//浏览文件夹的内容

struct dirent **scan_dir(int *n)

{

struct dirent **namelist;

(*n) = scandir(".", &namelist, 0, alphasort);

if ((*n) < 0)

perror("scandir");

return namelist;

}

//实现ls不带参数的命令

int no_ls()

{

struct dirent **namelist;

int i, n;

long all_size = 0, size;

namelist = scan_dir(&n);

for(i = 0; i < n; i++) {

if(namelist[i]->d_name[0] != '.') {

printf("%-s  ", namelist[i]->d_name);

}

free(namelist[i]);

}

printf("\n");

free(namelist);

return 0;

}

//实现ls -l的命令

int l_ls()

{

struct dirent **namelist;

int n, i;

long all_size = 0, size;

char ch[40] = {0};

namelist = scan_dir(&n);

//得到文件总的大小

for(i = 0; i < n; i++) {

if(namelist[i]->d_name[0] != '.') {

size = get_size(namelist[i]->d_name);

all_size += size;

}

}

//打印信息

printf("总用量:%ld\n", all_size);

for(i = 0; i < n; i++) {

if(namelist[i]->d_name[0] != '.') {

get_mode(namelist[i]->d_name);

get_rwx(namelist[i]->d_name);

get_nlink(namelist[i]->d_name);

get_name(namelist[i]->d_name);

printf(" %5ld", get_size(namelist[i]->d_name));

get_time(namelist[i]->d_name, ch);

printf(" %s", ch);

printf(" %s", namelist[i]->d_name);

printf("\n");

}

free(namelist[i]);

}

free(namelist);

return 0;

}

//实现ls -al的命令

int al_ls()

{

struct dirent **namelist;

int n, i;

long all_size = 0, size;

char ch[40] = {0};

namelist = scan_dir(&n);

//得到文件总的大小

for(i = 0; i < n; i++) {

size = get_size(namelist[i]->d_name);

all_size += size;

}

printf("总用量:%ld\n", all_size);

for(i = 0; i < n; i++) {

get_mode(namelist[i]->d_name);

get_rwx(namelist[i]->d_name);

get_nlink(namelist[i]->d_name);

get_name(namelist[i]->d_name);

all_size += size;

printf(" %5ld", get_size(namelist[i]->d_name));

get_time(namelist[i]->d_name, ch);

printf(" %s", ch);

printf(" %s", namelist[i]->d_name);

printf("\n");

free(namelist[i]);

}

free(namelist);

return 0;

}

//实现ls -a的命令

int a_ls()

{

struct dirent **namelist;

int i, n;

namelist = scan_dir(&n);

for(i = 0; i < n; i++) {

printf("%-s  ", namelist[i]->d_name);

free(namelist[i]);

}

printf("\n");

free(namelist);

return 0;

}

//实现ls -A的命令

int A_ls()

{

struct dirent **namelist;

int i, n, test1, test2;

namelist = scan_dir(&n);

for(i = 0; i < n; i++) {

test1 = strcmp(".", namelist[i]->d_name) == 0;

test2 = strcmp("..", namelist[i]->d_name) == 0;

if(test1 || test2){

free(namelist[i]);

continue;

}

printf("%-s  ", namelist[i]->d_name);

free(namelist[i]);

}

printf("\n");

free(namelist);

return 0;

}

//浏览指定目录下的文件

int dir_ls(char *name)

{

struct dirent **namelist;

char file[20] = {0};

int i, n;

strcpy(file, "./");

strcat(file, name);

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

n = scandir(file, &namelist, 0, alphasort);

for(i = 0; i < n; i++) {

if(namelist[i]->d_name[0] == '.') {

free(namelist[i]);

continue;

}

printf("%-s  ", namelist[i]->d_name);

free(namelist[i]);

}

printf("\n");

free(namelist);

return 0;

}

//实现多个输入文件时的输出name_ls

int name_ls(int argc, char **argv)

{

int i;

struct stat buf;

for(i = 1; i < argc; i++) {

if(lstat(argv[i], &buf) < 0) {

perror("lstat:\n");

}

switch(S_IFMT&buf.st_mode) {

case S_IFDIR:dir_ls(argv[i]);break;

default:file_ls(argv[i]);

}

}

return 0;

}

//实现file_ls

int file_ls(char *file)

{

int fd;

struct stat buf;

if(lstat(file, &buf) < 0) {

perror("lstat:\n");

return 0;

}

switch(S_IFMT&buf.st_mode) {

case S_IFDIR:dir_ls(file);return 0;

}

if((fd = open(file, O_RDONLY)) == -1) {

printf("没有找到%s文件!\n", file);

} else {

close(fd);

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

}

return 0;

}

//实现mylist

int mylist(char *option)

{

if(strcmp(option, "-l") == 0) {

l_ls();

} else if(strcmp(option, "-al") == 0 || strcmp(option, "-la") == 0) {

al_ls();

} else if(strcmp(option, "-a") == 0) {

a_ls();

} else if(strcmp(option, "-A") == 0) {

A_ls();

} else {

file_ls(option);

}

return 0;

}

//主函数

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

{

if(argc == 1) {

no_ls();

} else if(argc == 2) {

mylist(argv[1]);

} else if(argc > 2) {

name_ls(argc, argv);

} else {

perror("Input error!\n");

}

return 0;

}

你可能感兴趣的:(c,实现linux命令wc)