Linux ls命令实现

使用Linux c语言实现ls命令的实现

#include

#include
#include
#include
#include
#include
#include
#include
#include
#include
void do_ls(char[]);
void do_stat(char*);
void show_file_info(char*, struct stat*);
void mode_str(int, char[]);
char *uid_str(uid_t);
char *gid_str(gid_t);
int main(int argc,char *argv[])
{
   struct stat buf;
    DIR *dirp;
    struct dirent *dp;
    DIR* dir = opendir(".");
    struct dirent* ent=NULL;
    int i;
    while(1)
    {
    printf("1.实现ls -a命令\n");
    printf("2.实现ls -m命令\n");
    printf("3.实现ls -Q命令\n");
    printf("4.实现ls -i命令\n");
    printf("5.实现ls -l命令\n");
    printf("6.实现ls -s命令\n");
    scanf("%d",&i);
    switch(i)
    {
       case 1:
             dirp = opendir(".");
             while ((dp = readdir(dirp)) != NULL)
             {
              printf("%s\n", dp->d_name );
             }      
             (void) closedir(dirp);
             break;
       case 2:
             while((ent = readdir(dir)))
             {
         if((ent->d_type == 4||ent->d_type == 8)&&ent->d_name[0]!='.')
         printf("%s,",ent->d_name);
             }
             closedir(dir);
             puts("");
             break;
       case 3:
             while((ent = readdir(dir)))
             {
         if((ent->d_type == 4||ent->d_type == 8)&&ent->d_name[0]!='.')
         printf("\"%s\"",ent->d_name);
             }
             closedir(dir);
             puts("");
             break;
       case 4:
             while((ent = readdir(dir)))
             {
         if((ent->d_type == 4||ent->d_type == 8)&&ent->d_name[0]!='.')
         printf("%s  %ld\n",ent->d_name,ent->d_ino);
             }
             break;
       case 5:
             if(argc == 1)
                    do_ls(".");
             else
             {
                 while(--argc)
                 {
                            printf("%s:\n", *++argv);
                            do_ls(*argv);
                 }
             }
             break;
      case 6:
             while((ent = readdir(dir)))
             {
         if((ent->d_type == 4||ent->d_type == 8)&&ent->d_name[0]!='.')
          {
             if(stat(ent->d_name,&buf)>=0)
             {
                 printf("%s  %ld\n",ent->d_name,buf.st_size);
             
             }
          }
        }   
     }
   }        
}

void do_ls(char dirname[])
{
            DIR *dirp;           
            struct dirent *direntp;
            char full_path[256];
            if((dirp = opendir(dirname)) == NULL)
            {
                    fprintf(stderr, "ls2: cannot open %s\n", dirname);
            }
            else
            {
                    while((direntp = readdir(dirp)) != NULL)
                    {
                            strcpy(full_path, dirname);
                            int dir_len = strlen(dirname);
                            if(dirname[dir_len - 1] != '/')
                            {  
                                    full_path[dir_len] = '/';
                                    strcpy(full_path + dir_len + 1, direntp->d_name);
                            }
                            else
                                    strcpy(full_path + dir_len,
direntp->d_name);

                            do_stat(full_path);
                    }
                    closedir(dirp);
            }
    }
    void do_stat(char *filename)
    {
            struct stat info;
            if(stat(filename, &info) == -1)
            {
                    perror("stat");
                    printf("filename:%s\n", filename);
            }else
             {
                    char *pname = strrchr(filename, '/');
                    show_file_info(pname + 1, &info);
            }
    }

    void show_file_info(char *filename, struct stat *info_p)
    {
            char modestr[11];
            mode_str(info_p->st_mode, modestr);
            if(filename[0]!='.')
            {
            printf("%s", modestr);
            printf("%3d ", (int)info_p->st_nlink);
            printf("%-8s", uid_str(info_p->st_uid));
            printf("%-8s", gid_str(info_p->st_gid));
            printf("%4ld ", (long)info_p->st_size);
            printf("%.12s ", 4 + ctime(&info_p->st_mtime));
            printf("%s\n", filename);
            }
    }

    void mode_str(int mode, char str[])
    {
            strcpy(str, "----------");
            if(S_ISDIR(mode)) str[0] = 'd';
            if(S_ISCHR(mode)) str[0] = 'c';
            if(S_ISBLK(mode)) str[0] = 'b';
            if(S_ISLNK(mode)) str[0] = 'l';
            if(mode & S_IRUSR) str[1] = 'r';
            if(mode & S_IWUSR) str[2] = 'w';
            if(mode & S_IXUSR) str[3] = 'x';
            if(mode & S_IRGRP) str[4] = 'r';
            if(mode & S_IWGRP) str[5] = 'w';
            if(mode & S_IXGRP) str[6] = 'x';
            if(mode & S_IROTH) str[7] = 'r';
            if(mode & S_IWOTH) str[8] = 'w';
            if(mode & S_IXOTH) str[9] = 'x';
    }

    char *uid_str(uid_t uid)
    {
            static char numstr[10];
            struct passwd *pw_ptr;
            if((pw_ptr = getpwuid(uid)) == NULL)
            {
                    sprintf(numstr, "%d", uid);
                    return numstr;
            }
            else
                    return pw_ptr->pw_name;
    }

    char *gid_str(gid_t gid)
    {
            static char numstr[10];
            struct group *grp_ptr;
            if((grp_ptr = getgrgid(gid)) == NULL){
                    sprintf(numstr, "% d", gid);
                    return numstr;
            }
            else
                    return grp_ptr->gr_name;
    }

你可能感兴趣的:(Linux ls命令实现)