codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#define UBUFFER_INT8_SIZE 65536
#define ELEMENT_SIZE 1 // element size in bytes.
#define ELEMENT_COUNT 4096 // number of elements asked to fread per cycle (min 1 max UBUFFER_INT8_SIZE).
#define DELAY 0 //delay in milliseconds added to every pump cicle.
#define LOGFILE "F:/cPump.log"
#if defined _MSC_VER || defined __MINGW32__
#include <sys/utime.h> /* for utime() */
#include <io.h> /* for chmod(), _setmode(), unlink() */
#include <fcntl.h> /* for _O_BINARY */
#else
#include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
#include <utime.h> /* for utime() */
#endif
#if defined __CYGWIN__ || defined __EMX__
#include <io.h> /* for setmode(), O_BINARY */
#include <fcntl.h> /* for _O_BINARY */
#endif
#include <sys/stat.h> /* for stat(), maybe chmod() */
#if defined _WIN32 && !defined __CYGWIN__
#else
#include <unistd.h> /* for unlink() */
#endif
#if defined _WIN32 && !defined __CYGWIN__
// for GetFileInformationByHandle() etc
#include <windows.h>
#include <winbase.h>
#endif
#if defined(_MSC_VER) && _MSC_VER < 1600
/* Microsoft Visual Studio earlier than the 2010 version did not provide
* the 1999 ISO C Standard header file <stdint.h>.
*/
typedef __int8 x__int8;
typedef unsigned __int8 x__uint8;
typedef __int16 x__int16;
typedef __int32 x__int32;
typedef __int64 x__int64;
typedef unsigned __int16 x__uint16;
typedef unsigned __int32 x__uint32;
typedef unsigned __int64 x__uint64;
#else
/* For MSVC 2010 and everything else which provides <stdint.h>. */
#include <stdint.h>
typedef int8_t x__int8;
typedef uint8_t x__uint8;
typedef int16_t x__int16;
typedef int32_t x__int32;
typedef int64_t x__int64;
typedef uint16_t x__uint16;
typedef uint32_t x__uint32;
typedef uint64_t x__uint64;
#endif
typedef int x__bool;
static union {
x__int8 s8[UBUFFER_INT8_SIZE];
x__uint8 u8[UBUFFER_INT8_SIZE];
x__int16 s16[UBUFFER_INT8_SIZE/2];
x__uint16 u16[UBUFFER_INT8_SIZE/2];
} ubuffer;
void delay(int milliseconds)
{
long pause;
clock_t now,then;
pause = milliseconds*(CLOCKS_PER_SEC/1000);
now = then = clock();
while( (now-then) < pause )
now = clock();
}
FILE *get_binary_stdin(void)
{
/* if something breaks here it is probably due to the presence or
* absence of an underscore before the identifiers 'setmode',
* 'fileno', and/or 'O_BINARY'; check your system header files.
*/
#if defined _MSC_VER || defined __MINGW32__
_setmode(_fileno(stdin), _O_BINARY);
//ioctlsocket(_fileno(stdin), FIONBIO,1);
#elif defined __CYGWIN__
/* almost certainly not needed for any modern Cygwin, but let's be safe... */
setmode(fileno(stdin), O_BINARY);
#elif defined __EMX__
setmode(fileno(stdin), O_BINARY);
#endif
return stdin;
}
FILE *get_binary_stdout(void)
{
/* if something breaks here it is probably due to the presence or
* absence of an underscore before the identifiers 'setmode',
* 'fileno', and/or 'O_BINARY'; check your system header files.
*/
#if defined _MSC_VER || defined __MINGW32__
_setmode(_fileno(stdout), _O_BINARY);
#elif defined __CYGWIN__
/* almost certainly not needed for any modern Cygwin, but let's be safe... */
setmode(fileno(stdout), _O_BINARY);
#elif defined __EMX__
setmode(fileno(stdout), O_BINARY);
#endif
return stdout;
}
int pump (FILE *infile, FILE *outfile, size_t elementSize, size_t elementCount, int millisecond, FILE *logfile){
size_t element_read;
size_t element_write;
size_t total_read;
size_t total_write;
x__bool keepOn = 1;
total_write=0;
total_read=0;
fprintf(logfile, "element size is : %d, element cound is: %d\n",elementSize, elementCount);
while(keepOn) {
element_read = fread(ubuffer.u8, elementSize, elementCount, infile);
if (!element_read){
fprintf(logfile, "element read is undef\n");
fflush (logfile);
element_read=0;
}
total_read = total_read + (element_read*elementSize);
if (ferror(infile)){
fprintf(logfile, "ERROR during read STDIN %d\n",ferror(infile));
fflush (logfile);
return 0;
} else {
element_write = fwrite(ubuffer.u8, elementSize, element_read, outfile);
total_write = total_write + (element_write*elementSize);
fflush(outfile);
if(element_write != element_read) {
if(errno == EPIPE){
fprintf(logfile, "ERROR %d during write to STDOUT 1\n", errno);
fflush (logfile);
return 0;
} else {
fprintf(logfile, "ERROR %d during write to STDOUT 2\n", errno);
fflush (logfile);
}
}
else if(element_write == 0) {
if(ferror(outfile)) {
fprintf(logfile, "ERROR during write fo STDOUT 3\n");
fflush (logfile);
return 0;
}
}
}
if (feof(infile)) {
keepOn=0;
fprintf(logfile, "detected EOF\n");
fflush (logfile);
} else if (millisecond){
delay (millisecond);
}
}
return total_write;
}
int main(int argc, char** argv) {
FILE *logfile = fopen(LOGFILE,"w");
FILE *infile = get_binary_stdin();
FILE *outfile = get_binary_stdout();
size_t elementSize=ELEMENT_SIZE;
size_t elementCount = ELEMENT_COUNT;
int delay = DELAY;
size_t total=0;
fprintf(logfile, "element size is : %d, element cound is: %d, delay is: %d\n",elementSize, elementCount, delay);
fflush (logfile);
total = pump(infile, outfile, elementSize, elementCount, delay, logfile);
fprintf(logfile, "copied %d bytes\n", total );
fflush (outfile);
fclose (outfile);
fprintf(logfile, "outfile closed");
fflush (logfile);
fclose (logfile);
return 0;
}
}
Le righe da modificare sono: