linux 下实现ls命令,Linux下ls命令的简单实现



#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

void rm(char *pathname) {

struct stat buf;

if (stat(pathname, &buf) == -1) {

printf("path not exist\n");

return;

}

if (S_ISREG(buf.st_mode)) {

unlink(pathname);

printf("remove file:%s\n", pathname);

} else if (S_ISDIR(buf.st_mode)) {

DIR *pdir = opendir(pathname);

struct dirent *pdirent = NULL;

while ((pdirent = readdir(pdir)) != NULL) {

if (strcmp(pdirent->d_name, ".") == 0 || strcmp(pdirent->d_name,

"..") == 0)

continue;

char temp[128] = { '\0' };

//strcat(temp,pathname);

//strcat(temp,"/");

//strcat(temp,pdirent->d_name);

sprintf(temp, "%s/%s", pathname, pdirent->d_name);

rm(temp);

}

rmdir(pathname);

printf("remove directory:%s\n", pathname);

closedir(pdir);

}

}

void ls_file(char *dirname, char *filename) {

char temp[128] = { '\0' };

sprintf(temp, "%s/%s", dirname, filename);

char perms[11] = "----------";

struct stat buf;

if (stat(temp, &buf) == -1) {

printf("path not exist\n");

return;

}

switch (buf.st_mode & S_IFMT) {

case S_IFREG:

perms[0] = '-';

break;

case S_IFDIR:

perms[0] = 'd';

break;

case S_IFLNK:

perms[0] = 'l';

break;

case S_IFBLK:

perms[0] = 'b';

break;

case S_IFCHR:

perms[0] = 'c';

break;

case S_IFSOCK:

perms[0] = 's';

break;

default:

break;

}

if (buf.st_mode & S_IRUSR)

perms[1] = 'r';

if (buf.st_mode & S_IWUSR)

perms[2] = 'w';

if (buf.st_mode & S_IXUSR)

perms[3] = 'x';

if (buf.st_mode & S_IRGRP)

perms[4] = 'r';

if (buf.st_mode & S_IWGRP)

perms[5] = 'w';

if (buf.st_mode & S_IXGRP)

perms[6] = 'x';

if (buf.st_mode & S_IROTH)

perms[7] = 'r';

if (buf.st_mode & S_IWOTH)

perms[8] = 'w';

if (buf.st_mode & S_IXOTH)

perms[9] = 'x';

struct tm *tm = localtime(&buf.st_mtime);

char file[30] = { '\0' };

strftime(file, sizeof(file), "%m-%d %H:%M", tm);

printf("%s %2d %s %s %10ld %s %s\n", perms, buf.st_nlink, getpwuid(

buf.st_uid)->pw_name, getgrgid(buf.st_gid)->gr_name, buf.st_size,

file, filename);

}

char* split(char *src,char *right)

{

char *temp = malloc(32);

strcpy(temp,src);

char *p = strrchr(temp,'/');

*p = '\0';

p++;

strcpy(right,p);

return temp;

}

void ls(char *pathname) {

struct stat buf;

if (stat(pathname, &buf) == -1) {

printf("path not exist\n");

return;

}

if (S_ISREG(buf.st_mode)) {

char name[20]={'\0'};

char *temp = split(pathname,name);

ls_file(temp, name);

free(temp);

} else if (S_ISDIR(buf.st_mode)) {

DIR *pdir = opendir(pathname);

struct dirent *pdirent = NULL;

while ((pdirent = readdir(pdir)) != NULL) {

ls_file(pathname, pdirent->d_name);

}

closedir(pdir);

}

}

int main(int argc, char *argv[]) {  char a[30]="/wepull/a.txt";  char b[30]={'\0'};  char *c = split(a,b);  printf("content:%s,%s\n",c,b);  ls("/bak/a.txt");//你的目录名  ls("/bak");  return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值