Skip to content

Instantly share code, notes, and snippets.

@Max-E
Last active August 5, 2019 16:21
Show Gist options
  • Save Max-E/74c9dd4aec02cecccb1243feee298bbc to your computer and use it in GitHub Desktop.
Save Max-E/74c9dd4aec02cecccb1243feee298bbc to your computer and use it in GitHub Desktop.
// Build: gcc -std=c11 -Wall -Wextra slowcp.c -o slowcp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main (int argc, const char *argv[])
{
if (argc != 3) {
printf ("Usage: %s <src> <dst>:\n"
" Copy contents of <src> to <dst>, with a max speed of 2 MiB per second.\n",
argv[0]);
exit (0);
}
FILE *outfile = fopen (argv[2], "w");
FILE *infile = fopen (argv[1], "r");
printf ("Copying %s to %s\n", argv[1], argv[2]);
static char buf[1024*1024*2];
int nread;
do {
nread = fread (buf, 1, sizeof (buf), infile);
fwrite (buf, 1, nread, outfile);
sleep (1);
printf ("Copied %d bytes\n", nread);
} while (nread == sizeof (buf));
printf ("Copying done\n");
fclose (infile);
fclose (outfile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment