/* overwrite.c -- program to exhaust stdin, then write it to a file */ #include #include #include #include #include "support.h" int main(int argc, char **argv) { int buflen, strlen; char *buf; strlen = 0; buflen = 0; /* arbitrary */ buflen = 4096; if(argc == 1) { throw(NEEDARG_FAIL, mastrcat(argv[0], " files")); } if((buf = malloc(buflen))) { int c; c = 0; FILE *fp; for(;((c = getchar()) != EOF);) { if(strlen == buflen) { buf = realloc(buf, (buflen*=2)); } buf[strlen++] = c; } if(!(fp = fopen(argv[1], "a"))) { throw(FOPEN_FAIL, argv[1]); } for(c = 0; c <= strlen; c++) { if(fputc(buf[c], fp) == EOF) { throw(WRITE_FAIL, argv[1]); } } /* close the file and exit successfully */ fclose(fp); } else { throw(MALLOC_FAIL, NULL); } return 0; }