| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 
 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
 
char path[512];
void listDirectory(DIR * directory ,  struct dirent *dir , char  chemin[512]);
void savePath(char chemin[512]);
void restaurPath(char chemin[512]);
 
int main(int argc, char *argv[])
{
    struct dirent *dir;
    DIR * directory;
    int i;
    char chemin[512];
    bzero(chemin,512);
    strcpy(chemin,"./");
    if(argc == 1)
    {
        printf("Listage du repertoire courant :\n");
        listDirectory(directory ,  dir , chemin);
    }
    else
    {
        for(i = 1;i < argc;i++)
        {
            if(!strcmp(argv[i],"-h"))
            {
                printf("\n-h\t\tAfiche cette aide\n");
                printf("%s rep\t\tListe les repertoire et les sous repertoire de rep\n",argv[0]);
            }
            else
            {
                if (opendir(argv[i]))
                {
                    strcpy(chemin , argv[i]);
                    listDirectory(directory ,  dir , chemin);
                }
                else
                {
                    printf("L'argument est inconu ou le repertoire donner n'existe pas\n");
                }
            }
        }
    }
    return 0;
}
 
 
 
void listDirectory(DIR * directory ,  struct dirent *dir , char chemin[512])
{
    directory = opendir(chemin);
    while((dir = readdir(directory)))
    {
        bzero(chemin , strlen(chemin));
        if(!(!(strcmp(dir->d_name,".")) || !(strcmp(dir->d_name,".."))))
        {
            restaurPath(chemin);
 
            if(opendir(dir->d_name) != NULL)
            {
                if(strcmp(chemin,dir->d_name) != 0)
                    strncat(chemin, dir->d_name, 512);
                printf("(D)\t%s\n" , dir->d_name);
                savePath(chemin);
                listDirectory(directory, dir, chemin);
            }
            else
            {
                printf("(F)\t%s/%s\n",chemin,dir->d_name);
            }
        }
    }
    closedir(directory);
}
 
void savePath(char chemin[512])
{
    strcpy(path , chemin);
}
 
void restaurPath(char chemin[512])
{
            strcpy(chemin , path);
} | 
Partager