Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <limits.h>
- #include <dirent.h>
- #include <stdlib.h>
- #include <stdio.h>
- void writer(int level){
- const char characters[] = "| ";
- unsigned int length=strlen(characters)*(level+1);
- char postfix[length];
- memset( postfix, '\0', sizeof(char)*length );
- for(level;level>0;level--)
- strcat(postfix, characters);
- printf("%s", postfix);
- }
- void tree(char *path, int level)
- {
- struct dirent *direntry;
- DIR *directory;
- chdir(path); //in neues Verzeichnis wechseln
- directory=opendir(".");
- if(directory)
- {
- while((direntry=readdir(directory)) != 0)
- {
- if(strcmp(direntry->d_name, ".") != 0 && strcmp(direntry->d_name, "..") != 0)
- {
- if(strlen(direntry->d_name)>PATH_MAX)
- printf("path name is too long\n");
- if(direntry->d_type==DT_DIR)
- {
- writer(level+1);
- printf("|---[%s]\n",direntry->d_name );
- tree(direntry->d_name,level+1);
- }
- else
- writer(level);
- printf("|---[%s]\n", direntry->d_name );
- }
- }
- closedir(directory);
- }
- }
- int main(int argc, char const *argv[])
- {
- DIR *dir;
- char *path;
- path=argv[1];
- if(path==0) //wenn nichts angegben wurde dann
- path="."; //wähle aktuelles Verzeichnis
- dir=opendir(path);
- if(dir!=0)
- {
- tree(path,0);
- }
- else
- {
- printf("%s is a wrong directory name\n", argv[1]);
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment