ls的簡單實現

main.c

#include "ls.h"
int main(int argc,char* argv[])
{
    DIR *fp_dir=opendir(argv[1]);
    struct dirent* pent;
    if(NULL==fp_dir)
    {
        perror("open error");
        exit(-1);
    }
    printf("OK!\n");
    chdir(argv[1]);
    while((pent=readdir(fp_dir))!=NULL)
    {
        show_ls(pent->d_name);
    }
    closedir(fp_dir);
    return 0;
}

ls.h

#ifndef __SL_H__
#define __SL_H__
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<time.h>
#include<pwd.h>
#include<string.h>
#include<dirent.h>
#include<grp.h>
#endif

show_ls.c

#include "ls.h"
static char* time_handle(char* pt);
static void mode_to_str(mode_t md,char *buf);
void show_ls(char* path)
{
    char buf[16] = {"---------------"};
    struct stat sb;
    char path2[1024];
    sprintf(path2, "%s/%s", get_current_dir_name(), path);
    if(-1==stat(path2,&sb))
    {
        perror("fstat failed");
        exit(EXIT_FAILURE);
    }
    mode_to_str(sb.st_mode,buf);
    printf("%s. %u %s %s %u %s %s",buf,sb.st_nlink,
            getpwuid(sb.st_uid)->pw_name,getgrgid(sb.st_gid)->gr_name,\
            sb.st_size,time_handle(ctime(&sb.st_mtime)),path);
    putchar(10);
}
static void mode_to_str(mode_t md,char* buf)
{
    switch(S_IFMT&md)
        {
            case S_IFREG:buf[0]='-'; break;
            case S_IFDIR:buf[0]='d'; break;
            case S_IFCHR:buf[0]='c'; break;
            case S_IFIFO:buf[0]='p'; break;
            case S_IFBLK:buf[0]='b'; break;
            case S_IFLNK:buf[0]='l'; break;
            case S_IFSOCK:buf[0]='s'; break;
            default:;
        }
    if(S_IRUSR&md) buf[1]='r';
    if(S_IWUSR&md) buf[2]='w';
    if(S_IXUSR&md) buf[3]='x';

    if(S_IRGRP&md) buf[4]='r';
    if(S_IWGRP&md) buf[5]='w';
    if(S_IXGRP&md) buf[6]='x';

    if(S_IROTH&md) buf[7]='r';
    if(S_IWOTH&md) buf[8]='w';
    if(S_IXOTH&md) buf[9]='x';
    buf[10]=0;
}
static char* time_handle(char* pt)
{
    pt[strlen(pt)-1]=0;
    return (pt+4);
}

編譯

#gcc main.c show_ls.c -o main
運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章