Τρέχοντας το λοιπόν το πρόγραμμα ενώ δουλεύει για μικρές εισόδους και/ή για μικρούς buffer(15 char max) όταν προσπαθώ να βάλω ως είσοδο ένα μεγαλύτερο αρχείο και να χρησιμοποιήσω μεγαλύτερο buffer τότε δεν περνάει ολόκληρο το string από τη συνάρτηση my_read στην συνάρτηση my_write.
Έχει κανείς καμία ιδέα τι μπορεί να φταίει?
Code: Select all
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int my_read(int fd, char **output){
char buff[10000];
ssize_t rcnt;
rcnt = read(fd, buff, sizeof(buff)-1);
if(rcnt == 0)
return rcnt;
if (rcnt == -1){
perror("read :");
return rcnt;
};
buff[rcnt] = '\0';
*output = buff;
return rcnt;
}
int my_write(char *buffer, int fd) {
size_t len, idx;
ssize_t wcnt;
idx = 0;
len = strlen(buffer);
// printf("string= %s--->", buffer);
printf("len = %zu \n", len);
do {
wcnt = write(fd, buffer + idx, len - idx);
if (wcnt == -1) {
perror("write :");
return 1;
}
idx += wcnt;
}while (idx < len);
return 0;
}
void copyfile (int fd1, int fd2){
char *buff = NULL;
for(;my_read(fd1, &buff)>0;){
printf("len = %zu \n", strlen(buff));
my_write( buff, fd2);
}
}
int main(int argc, char **argv)
{
int fd1, fd2, fd3, oflags1, oflags2, mode;
oflags1 = O_RDONLY;
oflags2 = O_CREAT | O_WRONLY | O_TRUNC;
mode = S_IRUSR | S_IWUSR;
fd1 = open(argv[1], oflags1);
if (fd1 == -1) {
perror("open1 :");
exit(1);
}
fd2 = open(argv[2], oflags1);
if (fd2 == -1) {
perror("open2 :");
exit(1);
}
fd3 = open(argv[3], oflags2, mode);
if (fd3 == -1){
perror("open3 :");
exit(1);
}
copyfile(fd1, fd3);
copyfile(fd2, fd3);
return 0;
}