uuprog

diff tee.c @ 0:4f628556fa3e

uuprog initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 25 Aug 2011 08:53:01 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tee.c	Thu Aug 25 08:53:01 2011 +0300
     1.3 @@ -0,0 +1,47 @@
     1.4 +/*! cc -o tee tee.c */
     1.5 +#include <stdio.h>
     1.6 +#include <stdlib.h>
     1.7 +#include <string.h>
     1.8 +#include <errno.h>
     1.9 +
    1.10 +void output(FILE **farr, int count, char *data, size_t bytes);
    1.11 +
    1.12 +int main(int argc, char **argv)
    1.13 +{
    1.14 +	char buf[512];
    1.15 +	size_t rdbytes;
    1.16 +	FILE **files;
    1.17 +	int i, fnum = 0;
    1.18 +
    1.19 +	if(!(files = malloc((argc - 1) * sizeof *files))) {
    1.20 +		perror("malloc failed");
    1.21 +		return EXIT_FAILURE;
    1.22 +	}
    1.23 +
    1.24 +	for(i=1; i<argc; i++) {
    1.25 +		if(!(files[fnum] = fopen(argv[i], "wb"))) {
    1.26 +			fprintf(stderr, "failed to write to %s: %s\n", argv[i], strerror(errno));
    1.27 +		} else {
    1.28 +			fnum++;
    1.29 +		}
    1.30 +	}
    1.31 +
    1.32 +	while((rdbytes = fread(buf, 1, sizeof buf, stdin))) {
    1.33 +		output(files, fnum, buf, rdbytes);
    1.34 +	}
    1.35 +
    1.36 +	for(i=0; i<fnum; i++) {
    1.37 +		fclose(files[i]);
    1.38 +	}
    1.39 +	return 0;
    1.40 +}
    1.41 +
    1.42 +void output(FILE **farr, int count, char *data, size_t bytes)
    1.43 +{
    1.44 +	int i;
    1.45 +
    1.46 +	fwrite(data, 1, bytes, stdout);
    1.47 +	for(i=0; i<count; i++) {
    1.48 +		fwrite(data, 1, bytes, farr[i]);
    1.49 +	}
    1.50 +}