- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <stddef.h>
-
- int list (char const *directory);
- void fclean(char *s_buffer, FILE *stream);
-
- int main (void)
- {
- char directory[256] = "";
- do
- {
- printf ("Path or <q> for exit: ");
- fgets(directory, sizeof directory, stdin);
- fclean(directory, stdin);
-
- if (directory[0] != 'q')
- {
- switch (list (directory))
- {
- case 1:
- printf ("\n%s is unknown\n\n", directory);
- break;
- case 2: /*fallthrough */
- case 3:
- puts ("\nInternal error\n");
- break;
- case 4:
- printf ("\nError when reading %s\n\n", directory);
- break;
- }
- }
- }
- while (directory[0] != 'q');
- return 0;
- }
-
- int list (char const *directory)
- {
- DIR *dir = NULL;
- int err = 0;
-
- if ((dir = opendir (directory)) == NULL)
- {
- err = 4;
- }
- else
- {
- char cwd[256] = "";
- printf ("FOLDER %s:\n", directory);
-
- /* -tc- we memorise the current working directory */
- if (getcwd (cwd, sizeof cwd) == NULL)
- {
- err = 3;
- }
- else if (chdir (directory) != 0)
- {
- err = 2;
- }
- else
- {
- struct dirent *file;
- while ((file = readdir (dir)) != NULL)
- {
- struct stat infos;
- err = stat (file->d_name, &infos);
- if ((strcmp (file->d_name, ".") != 0) && (strcmp (file->d_name, "..") != 0))
- {
- if (err == 0)
- {
- printf ("\n%-11s%s\n", S_ISDIR (infos.st_mode) ? "[Folder]" : "[File]", file->d_name);
- }
- else
- {
- break;
- }
- }
- }
- chdir (cwd);
- printf ("\nEND OF THE FOLDER: %s\n", directory);
- }
-
- closedir (dir);
- dir = NULL;
- }
- return err;
- }
-
- void fclean(char *s_buffer, FILE *stream)
- {
- if (s_buffer != NULL && stream != NULL)
- {
- char *pc = strchr(s_buffer, '\n');
-
- if (pc != NULL) /* La saisie n'a pas ete tronquee */
- {
- /* On remplace '\n' par le caractere nul '\0' */
- *pc = 0;
- }
- else
- {
- /* La saisie a e'te' tronquee, on purge le flux d'entree */
- int c;
- while ((c = fgetc(stream)) != '\n' && c != EOF)
- {
- //Rien
- }
- }
- }
- }
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stddef.h>
int list (char const *directory);
void fclean(char *s_buffer, FILE *stream);
int main (void)
{
char directory[256] = "";
do
{
printf ("Path or <q> for exit: ");
fgets(directory, sizeof directory, stdin);
fclean(directory, stdin);
if (directory[0] != 'q')
{
switch (list (directory))
{
case 1:
printf ("\n%s is unknown\n\n", directory);
break;
case 2: /*fallthrough */
case 3:
puts ("\nInternal error\n");
break;
case 4:
printf ("\nError when reading %s\n\n", directory);
break;
}
}
}
while (directory[0] != 'q');
return 0;
}
int list (char const *directory)
{
DIR *dir = NULL;
int err = 0;
if ((dir = opendir (directory)) == NULL)
{
err = 4;
}
else
{
char cwd[256] = "";
printf ("FOLDER %s:\n", directory);
/* -tc- we memorise the current working directory */
if (getcwd (cwd, sizeof cwd) == NULL)
{
err = 3;
}
else if (chdir (directory) != 0)
{
err = 2;
}
else
{
struct dirent *file;
while ((file = readdir (dir)) != NULL)
{
struct stat infos;
err = stat (file->d_name, &infos);
if ((strcmp (file->d_name, ".") != 0) && (strcmp (file->d_name, "..") != 0))
{
if (err == 0)
{
printf ("\n%-11s%s\n", S_ISDIR (infos.st_mode) ? "[Folder]" : "[File]", file->d_name);
}
else
{
break;
}
}
}
chdir (cwd);
printf ("\nEND OF THE FOLDER: %s\n", directory);
}
closedir (dir);
dir = NULL;
}
return err;
}
void fclean(char *s_buffer, FILE *stream)
{
if (s_buffer != NULL && stream != NULL)
{
char *pc = strchr(s_buffer, '\n');
if (pc != NULL) /* La saisie n'a pas ete tronquee */
{
/* On remplace '\n' par le caractere nul '\0' */
*pc = 0;
}
else
{
/* La saisie a e'te' tronquee, on purge le flux d'entree */
int c;
while ((c = fgetc(stream)) != '\n' && c != EOF)
{
//Rien
}
}
}
}