PHP Code:
/*
*
*
* upper to lower ( and viceversa) v2
*
*
*/
#include <unistd.h>
#include <st***.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
int main ( int nentry, char ** names ){
int fd1,fd2,r_bytes,w_bytes,i;
void * buffer;
buffer = malloc(100);
if (buffer == NULL){
//impossibile allocare
exit(-2);
}
fd1 = open (*(names +1 ),O_RDONLY);
if (fd1 < 0){
fprintf(stderr,"Sorgente non presente\n");
exit (-3);
}
fd2 = open (*(names +2 ),O_WRONLY | O_TRUNC);
if ( fd2<0){
fprintf(stdout,"Creazione file destinazione in corso ..\n");
fd2 = open (*(names +2 ),O_CREAT | O_WRONLY,00700);
if (fd2<0){
fprintf(stderr,"Impossibile creare file di destinazione\n");
exit(-4);
}
}
for (r_bytes= read( fd1, buffer, 100); r_bytes>0; r_bytes= read( fd1, buffer, 100)){
for ( i=0;i<r_bytes;i++) {
if ( islower(*((char *) buffer + i))){
*((char *) buffer + i) = toupper(*((char *) buffer + i));
}else{
*((char *) buffer + i) = tolower(*((char *) buffer + i));
}
}
w_bytes = write (fd2,buffer,r_bytes);
if ( w_bytes != r_bytes){
fprintf(stderr,"Errore in scrittura sul file destinazione\n");
exit(-5);
}
}
if ( r_bytes < 0){
fprintf(stderr, "Errore durante la lettura del file sorgente\n");
exit(-6);
}
close(fd1);
close(fd2);
fprintf(stdout,"Operazione completata senza errori\n");
return 0;
}