关闭

C语言实现tree命令

478人阅读 评论(0) 收藏 举报
分类:

原文

#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
 
#define MAXNAME 200
void s_printf(char *filename,int depth);
void s_dirwalk(char *dirname,int depth,void (*fcn)(char *,int));
void listdirtree(char *dirname,int depth);
 
int main(int argc,char **argv)
{
    if(argc == 1)
        listdirtree(".",0);
    else
    {
        printf("%s\n",argv[1]);
        listdirtree(*++argv,0);
    }
    return 0;
}
 
void listdirtree(char *dirname,int depth)
{
    struct stat stbuf;
    if((stat(dirname,&stbuf)) == -1)
    {
        fprintf(stderr,"listdirtree:can't read file %s information!\n",dirname);
        return;
    }
    if((stbuf.st_mode & S_IFMT) == S_IFDIR)
        s_dirwalk(dirname,depth,listdirtree);
}
 
void s_dirwalk(char *dirname,int depth,void (*fcn)(char *,int))
{
    char name[MAXNAME];
    struct dirent *fip;
    DIR *dp;
    if((dp = opendir(dirname)) == NULL)
    {
        fprintf(stderr,"s_dirwalk:can't open %s\n",dirname);
        return;
    }
    while((fip = readdir(dp)) != NULL)
    {
        if(strcmp(fip->d_name,".") == 0 || strcmp(fip->d_name,"..") == 0)/*skip directory '.' and '..'*/
            continue;
        if(strlen(dirname) + strlen(fip->d_name) + 2 > sizeof(name))
        {
            fprintf(stderr,"s_dirwalk:%s/%s is too long!\n",dirname,fip->d_name);
            return;
        }
        else
        {
            s_printf(fip->d_name,depth);
            sprintf(name,"%s/%s",dirname,fip->d_name);
            (*fcn)(name,depth + 1);
        }
    }
    closedir(dp);
}
 
void s_printf(char *filename,int depth)
{
    while(depth-- > 0)
        printf("|  ");
    printf("|--");
    printf("%s\n",filename);
}


第一部


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44943次
    • 积分:1107
    • 等级:
    • 排名:千里之外
    • 原创:70篇
    • 转载:10篇
    • 译文:0篇
    • 评论:5条
    最新评论