codice:
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/file.h>
#include<sys/wait.h>
#include<dirent.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
struct stat s,s1;
struct dirent *dir;
DIR *d;
char *nome,c,car;
int i,n,fd;
char *buf;
int esiste=0;
int fd1;
d=opendir(argv[1]);
while((dir=readdir(d))!=NULL)
{
stat(dir->d_name,&s);
nome=dir->d_name;
if(S_ISREG(s.st_mode))
{
if(strstr(dir->d_name,".txt"))
{
printf("i file txt esistenti sono: %s\n",nome);
fd=open(nome,O_RDWR,0666);
while((read(fd,&c,1))>0)
{
for(i=0;i<6;i++)
{
if(c%2!=0)
{
fd1=open("byteletti.txt", O_RDWR | O_CREAT | O_TRUNC , 0666);
buf=malloc(s.st_size);
write(fd1,buf,s.st_size);
esiste=1;
}
}
}
}
}
}
if(esiste==1)
{
int fd2=open("temp", O_RDWR | O_CREAT | O_TRUNC , 0666);
fd1=open("byteletti.txt", O_RDWR );
int lunghezza=lseek(fd1,0,SEEK_END);
int mezza=lunghezza/2;
n=lseek(fd1,0,SEEK_SET);
while(n<=mezza)
{
read(fd1,buf,1);
write(fd2,buf,1);
lseek(fd1,1,SEEK_CUR);
}
n=lseek(fd1,-1,SEEK_END);
while(n>=mezza)
{
read(fd1,buf,1);
write(fd2,buf,1);
lseek(fd1,-2,SEEK_CUR);
}
}
}
grazie dovrò leggermi anche attentamente il man