1 2 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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include "myHeader.h"
int lister(char *directory);
int main (void)
{
int end;
do
{
char directory[256];
printf ("nom ou <enter> pour quitter) : ");
fgets (directory, sizeof directory, stdin);
fclean (directory, stdin);
end = (directory[0] == '\0');
if (!end)
{
lister(directory);
}
}
while (!end);
return 0;
}
int lister(char *directory)
{
DIR *dir = NULL;
struct dirent *file = NULL;
struct stat infos;
stat (directory, &infos);
if((dir = opendir(directory)) == NULL)
{
return EXIT_FAILURE;
}
printf("Repertoire %s :\n", directory);
while((file = readdir(dir)) != NULL)
{
if(strcmp(file->d_name, ".") && strcmp(file->d_name, ".."))
{
if(S_ISDIR (infos.st_mode))
{
stat (file->d_name, &infos);
lister(strncat(directory, file->d_name, 256));
}
else
{
printf("\n\t%s\n", file->d_name);
}
}
}
printf("\nFin repertoire %s\n", directory);
closedir(dir);
return EXIT_SUCCESS;
} |