1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#define LENGTHLEN 8
#define DEBUG 1
#ifdef DEBUG
#define N_DEBUG "debug.child"
void do_debug(const char *fmt, ...);
#endif
int main(int argc, char *argv[]) {
char buf[4096];
int len, i, nron;
/* first have to get the overview.fmt */
i = read(0, buf, LENGTHLEN);
buf[LENGTHLEN] = '\0';
#ifdef DEBUG
do_debug("Got %d bytes '%s'\n", i, buf);
#endif
len = atoi(buf);
if(len>0) {
i = read(0, buf, len);
}
#ifdef DEBUG
do_debug("Got %d bytes '%s'\n", i, buf);
#endif
nron = 0;
do {
i = read(0, buf, LENGTHLEN);
buf[LENGTHLEN] = '\0';
#ifdef DEBUG
do_debug("Got %d bytes '%s'\n", i, buf);
#endif
len = atoi(buf);
if(len>0) {
nron++;
i = read(0, buf, len);
#ifdef DEBUG
do_debug("Got %d bytes\n", i);
#endif
if((nron % 2) == 0) {
i = write(1, "0\n", 2);
#ifdef DEBUG
do_debug("Wrote %d bytes\n", i);
#endif
}
else {
i = write(1, "1\n", 2);
#ifdef DEBUG
do_debug("Wrote %d bytes\n", i);
#endif
}
}
}
while (len > 0);
return 0;
}
#ifdef DEBUG
/*-------------------------------------------------------------*/
void do_debug(const char *fmt, ...) {
FILE *fptr = NULL;
va_list args;
if((fptr = fopen(N_DEBUG, "a")) == NULL) {
fptr = stderr;
}
va_start(args, fmt);
vfprintf(fptr, fmt, args);
va_end(args);
if(fptr != stderr) {
fclose(fptr);
}
}
#endif
|