You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
928 B
C
62 lines
928 B
C
/* overwrite.c -- program to exhaust stdin, then write it to a file */
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#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;
|
|
}
|