Skip to content

Instantly share code, notes, and snippets.

@ph1ee
Last active October 9, 2015 06:03
Show Gist options
  • Save ph1ee/9fda8d520184dd1eb0b3 to your computer and use it in GitHub Desktop.
Save ph1ee/9fda8d520184dd1eb0b3 to your computer and use it in GitHub Desktop.
Progress Pipe: Read a block of data from stdin, write it to stdout. Activity is indicated by a '.' to stderr.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define PIPE_PROGRESS_SIZE 4096
/* Read a block of data from stdin, write it to stdout.
* Activity is indicated by a '.' to stderr
*/
int main(int argc, char **argv)
{
char buf[PIPE_PROGRESS_SIZE];
time_t t = time(NULL);
size_t len;
while ((len = fread(buf, 1, PIPE_PROGRESS_SIZE, stdin)) > 0) {
time_t new_time = time(NULL);
if (new_time != t) {
t = new_time;
fputc('.', stderr);
}
fwrite(buf, 1, len, stdout);
}
fputc('\n', stderr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment