Linux下ls命令的简略实现

Linux下ls命令的简单实现


  #include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <stdlib.h>

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;
}