Hash :
081977ba
Author :
Thomas de Grivel
Date :
2022-03-20T10:31:50
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
/* size - truncate standard input by size */
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 8192
int usage(char *argv0)
{
fprintf(stderr, "Usage: %s SIZE COMMAND [ARGS ...]\n", argv0);
return 1;
}
int main (int argc, char **argv)
{
char *a;
char cmd[BUFSIZE];
char *c = cmd;
char buf[BUFSIZE];
int i = 0;
size_t pos = 0;
ssize_t r;
unsigned long size;
FILE *pipe;
size_t len;
if (argc < 3)
return usage(argv[0]);
size = strtoul(argv[1], NULL, 10);
for (i = 2; i < argc; i++) {
a = argv[i];
while ((*c++ = *a++))
;
c--;
*c++ = ' ';
}
*c = 0;
pipe = popen(cmd, "w");
len = pos + BUFSIZE < size ? BUFSIZE : size - pos;
while (pos < size && (r = fread(buf, 1, len, stdin)) > 0) {
if (fwrite(buf, r, 1, pipe) != 1)
err(1, "fwrite");
pos += r;
len = pos + BUFSIZE < size ? BUFSIZE : size - pos;
}
if (r < 0)
err(1, "fread");
pclose(pipe);
return 0;
}