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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <sys/stat.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX 1024
void copieArbre (char *ancien, char *nouveau);
void copieFile (char *ancien, char *nouveau);
void copieDir (char *ancien, char *nouveau);
// Fonction pour copier une "arboréscence"
void copieArbre (char *ancien, char *nouveau)
{
struct stat st;
stat (ancien, &st);
switch (st.st_mode & S_IFMT)
{
// Repertoire
case S_IFDIR : copieDir (ancien, nouveau);
break;
// Fichier
case S_IFREG : copieFile (ancien, nouveau);
break;
default : printf ("Ce n'est ni un fichier, ni un répertoire\n");
}
return;
}
// Fonction pour copier un fichier
void copieFile (char *ancien, char *nouveau)
{
int goku, luffy, m;
char buf[MAX];
goku = open (ancien, O_RDONLY);
if (goku == -1) {
perror ("Ouverture du fichier à copier impossible.\n");
exit(1);
}
luffy = open (nouveau, O_RDONLY | O_CREAT | O_TRUNC, 0666);
if (luffy == -1) {
perror ("Création du fichier ou copié impossible.\n");
exit (1);
}
while ( (m = read(goku, buf, MAX)) > 0)
write (luffy, buf, m);
if (m == -1) {
perror ("Erreur de lecture.\n");
exit (1);
}
close (goku);
close (luffy);
return;
}
// Fonction pour copier un répertoire
void copieDir (char *ancien, char *nouveau)
{
DIR *dir;
struct dirent *dp;
char ancienBis[MAX], nouveauBis[MAX];
dir = opendir (ancien);
mkdir (nouveau, 0777);
chdir (ancien);
dp = readdir (dir);
while ( dp != NULL) {
printf ("%s/", dp -> d_name);
dp = readdir (dir);
}
printf ("\n");
while ( dir != NULL) {
if ( (strcmp (dp -> d_name, ".") != 0) && (strcmp (dp -> d_name, "..") != 0)) {
strcpy (ancienBis, ancien);
strcpy (nouveauBis, dp ->d_name);
strcpy (nouveauBis, ancien);
strcpy (nouveauBis, dp-> d_name);
copieArbre (ancienBis, nouveauBis);
}
dp = readdir (dir);
}
closedir (ancien);
return;
}
main(int argc, char *argv[])
{
if (argc != 3) {
printf ("Erreur: veuillez entrez 2 fichiers (ancien et nouveau)\n");
exit (1);
}
copieArbre (argv[1], argv[2]);
return 0;
} |
Partager