各位程序员在自己的虚拟机里一定没少执行过“ls”这个功能吧,这个代码就是实现了ls和ls-l功能,话不多说,上代码。

实现代码

int process_ls(char * path)
{
    DIR * dirp;
    struct dirent *direntp ;
    dirp = opendir(path);
    if(dirp == NULL)
    {
        perror("opendir"); 
        exit(-1);
    }
    while(  (direntp=readdir(dirp)) != NULL)
    {
        if(strncmp(direntp->d_name,".",1) == 0) continue;
        printf("%s  ",direntp->d_name);
    }
    printf("\n");
    closedir(dirp);

    return 0 ;
}

这部分代码就已经实现了ls的功能。

int process_ls_l(char *path)
{
    DIR * dirp;
    struct dirent *direntp ;
    char filename[100] ={0};
    struct stat st;
    int ret; 
    char buf[100] ={0};
    char tmp[100] ={0};
    dirp = opendir(path);
    if(dirp == NULL)
    {
        perror("opendir"); 
        exit(-1);
    }
    while(  (direntp=readdir(dirp)) != NULL)
    {
        if(strncmp(direntp->d_name,".",1) == 0) continue;
        strcpy(filename,path);
        if(filename[strlen(filename) -1] !='/')
        {
            strcat(filename,"/");
        }
        strcat(filename,direntp->d_name);
       // printf("filename:%s\n",filename);
        ret = stat(filename,&st);
        if(ret < 0)
        {
            perror("stat"); 
            exit(-1);
        }
        memset(buf,'-',10);
        if(S_ISREG(st.st_mode))
        {
            buf[0] = '-';
        }
        else if(S_ISDIR(st.st_mode))
        {
            buf[0] = 'd';
        }
        else if(S_ISCHR(st.st_mode))
        {
            buf[0] = 'c';
        }
        else if(S_ISBLK(st.st_mode))
        {
            buf[0] = 'b';
        }
        else if(S_ISFIFO(st.st_mode))
        {
            buf[0] = 'p';
        }
        else if(S_ISLNK(st.st_mode))
        {
            buf[0] = 'l';
        }
        else if(S_ISSOCK(st.st_mode))
        {
            buf[0] = 's';
        }
        else 
        {
            printf("error\n");
            exit(-1);
        }
        if(st.st_mode&S_IRUSR)
        {
            buf[1] = 'r';
        }
        if(st.st_mode&S_IWUSR)
        {
            buf[2] = 'w';
        }
        if(st.st_mode&S_IXUSR)
        {
            buf[3] = 'x';
        }
/************************************************/
        if(st.st_mode&S_IRGRP)
        {
            buf[4] = 'r';
        }
        if(st.st_mode&S_IWGRP)
        {
            buf[5] = 'w';
        }
        if(st.st_mode&S_IXGRP)
        {
            buf[6] = 'x';
        }
/***********************************************/
        if(st.st_mode&S_IROTH)
        {
            buf[7] = 'r';
        }
        if(st.st_mode&S_IWOTH)
        {
            buf[8] = 'w';
        }
        if(st.st_mode&S_IXOTH)
        {
            buf[9] = 'x';
        }
        buf[10] = ' ';
        sprintf(&buf[11],"%ld",st.st_nlink);
        strcat(buf," ");
        struct passwd *pw;
        pw = getpwuid(st.st_uid);
        sprintf(tmp,"%s",pw->pw_name);
        strcat(buf,tmp);
        strcat(buf," ");
        struct group *gr;
        gr = getgrgid(st.st_gid);
        sprintf(tmp,"%s",gr->gr_name);
        strcat(buf,tmp);
        strcat(buf," ");
        sprintf(tmp,"%5ld",st.st_size);
        strcat(buf,tmp);
        strcat(buf," ");
        sprintf(tmp,"%s",ctime(&st.st_mtime));
        tmp[strlen(tmp) -1 ] = '\0' ;
        strcat(buf,tmp);
        strcat(buf," ");
        strcat(buf,direntp->d_name);
        printf("%s\n",buf);
    }
    closedir(dirp);
    return 0 ;
}

这段代码就是实现了ls-l的功能了。

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

   if(argc == 2 )
   {
       process_ls(argv[1]); // myls path
   }
   else if(argc == 3) // ./myls -l path
   {
       process_ls_l(argv[2]);
   }
   else 
   {
       printf("error\n");
       exit(-1);
   }
   return 0;
}

再加上主函数的判断,在执行文件后加上想要ls的那个文件路径,就可以想实现哪个就实现哪个了。

效果预览

[领卓教育]用C语言实现 ls 以及 ls-l 功能
[领卓教育]用C语言实现 ls 以及 ls-l 功能

到这里就结束了,祝大家天天开心,心想事成(不脱发)!嘿嘿!

相关文章: