linux编程:ls命令的简单实现

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

struct passwd* pw;
struct group* gr;
struct tm* tm;

void display(struct stat info,char* path){
       int i;        
      switch(info.st_mode&S_IFMT){            
          case S_IFREG: printf("-");  break;            
          case S_IFDIR: printf("d");  break;
          case S_IFLNK: printf("l");  break;            
          case S_IFBLK: printf("b");  break;
          case S_IFCHR: printf("c");  break;            
          case S_IFIFO: printf("p");  break;            
          case S_IFSOCK: printf("s");  break;
        }
        
     for(i=8;i>=0;i--){            
         if(info.st_mode&(1<<i)){                
           switch(i%3){                    
              case 2: printf("r");  break;                    
              case 1: printf("w");  break;                    
              case 0: printf("x");  break;                
           }           
        }           
        else                
          printf("-");        
     }
     pw = getpwuid(info.st_uid);
     gr = getgrgid(info.st_gid);    
     printf(" %d %s %s %d ",info.st_nlink,pw->pw_name,gr->gr_name,info.st_size); 
     tm = localtime(&info.st_ctime);    
     printf("%02d月 %02d %02d:%02d %s\n",tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,path);
}
int main(){
   
    struct stat info;    
    char* path = malloc(100);    
    printf("请输入要ls查看的内容:");    
    scanf("%s",path);    
    //printf("%s\n",path);    
    int i;   
    int res = stat(path,&info);    
    if(res == -1)        
       perror("stat"),exit(-1);    
    if(S_ISREG(info.st_mode))        
        display(info,path);    
    if(S_ISDIR(info.st_mode)){        
        DIR* dir = opendir(path);        
        if(dir==NULL)  return ;        
        chdir(path);
        struct dirent* ent = NULL;        
        while(ent = readdir(dir)){            
           if((strcmp(ent->d_name,".")==0)||(strcmp(ent->d_name,"..")==0))                
              continue;            
           else{
              stat(ent->d_name,&info);                
              //chdir(ent->d_name);                
              display(info,ent->d_name);             
           }
        }
        chdir("..");    
     }  
      return 0;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现ls -l命令的关键是要读取当前目录下的文件文件夹的信息,并将其格式化输出。下面是一个简单的C语言程序,实现了类似于ls -l的功能: ```c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> #include <pwd.h> #include <grp.h> #include <time.h> int main(int argc, char *argv[]) { DIR *dp; struct dirent *entry; struct stat statbuf; struct passwd *pwd; struct group *grp; char date[20]; if ((dp = opendir(".")) == NULL) { fprintf(stderr, "Cannot open directory.\n"); exit(1); } while ((entry = readdir(dp)) != NULL) { if (entry->d_name[0] == '.') continue; if (lstat(entry->d_name, &statbuf) < 0) continue; printf((S_ISDIR(statbuf.st_mode)) ? "d" : "-"); printf((statbuf.st_mode & S_IRUSR) ? "r" : "-"); printf((statbuf.st_mode & S_IWUSR) ? "w" : "-"); printf((statbuf.st_mode & S_IXUSR) ? "x" : "-"); printf((statbuf.st_mode & S_IRGRP) ? "r" : "-"); printf((statbuf.st_mode & S_IWGRP) ? "w" : "-"); printf((statbuf.st_mode & S_IXGRP) ? "x" : "-"); printf((statbuf.st_mode & S_IROTH) ? "r" : "-"); printf((statbuf.st_mode & S_IWOTH) ? "w" : "-"); printf((statbuf.st_mode & S_IXOTH) ? "x" : "-"); printf(" %2lu", statbuf.st_nlink); if ((pwd = getpwuid(statbuf.st_uid)) != NULL) { printf(" %s", pwd->pw_name); } else { printf(" %d", statbuf.st_uid); } if ((grp = getgrgid(statbuf.st_gid)) != NULL) { printf(" %s", grp->gr_name); } else { printf(" %d", statbuf.st_gid); } strftime(date, 20, "%b %d %H:%M", localtime(&statbuf.st_mtime)); printf(" %s", date); printf(" %s\n", entry->d_name); } closedir(dp); return 0; } ``` 这个程序使用了许多系统调用和库函数,比如opendir、readdir、lstat、getpwuid、getgrgid等等。它首先打开当前目录("."),然后循环读取目录下的每个文件文件夹的信息,使用lstat函数获取文件的详细信息,再利用各种库函数将这些信息格式化输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值