#include #include #include #include #include #define PERM 0644 /* in UNIX */ int fd; /* per file/dispositivo /dev/tty */ int copyfile (char *f1, char * f2) { int infile, outfile, nread; char s[100] = "Valore di infile "; char s1[5]; char s2[] = " e di outfile "; char s3[] = "\n"; char buffer [BUFSIZ]; /* usato per i caratteri */ fd = open("/dev/tty", O_WRONLY); close(0); if (( infile = open (f1, O_RDONLY) ) < 0) return (2); /* ERRORE se non si riesce ad aprire in LETTURA il primo file */ close(1); if (( outfile = creat (f2, PERM) ) <0 ) /* ERRORE se non si riesce a creare il secondo file */ {close (infile); return (3); } sprintf(s1, "%d", infile); strcat(s, s1); strcat(s, s2); sprintf(s1, "%d", outfile); strcat(s, s1); strcat(s, s3); write(fd, s, strlen(s)); while (( nread = read (infile, buffer, BUFSIZ) ) > 0 ) { if ( write (outfile , buffer, nread) < nread ) /* ERRORE se non si riesce a SCRIVERE */ { close (infile); close (outfile); return (4); } } close (infile); close (outfile); return (0); /* se arriviamo qui, vuol dire che tutto e' andato bene */ } int main (int argc, char** argv) { int status; if (argc != 3) /* controllo sul numero di argomenti */ { printf("Errore: numero di argomenti sbagliato\n"); printf("Ci vogliono 2 argomenti: nome-file-sorgente e nome-file-destinazione\n"); exit (1); } status = copyfile (argv[1], argv[2]); if (status != 0) write(fd, "Ci sono stati degli errori durante la copia\n", 44); return status; }