Skip to content

Instantly share code, notes, and snippets.

@hamadu
Last active July 20, 2017 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamadu/c45d9f06202953deff05bb84f8000d61 to your computer and use it in GitHub Desktop.
Save hamadu/c45d9f06202953deff05bb84f8000d61 to your computer and use it in GitHub Desktop.
copy.c
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void error_and_exit() {
printf("error(%d): %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
int main(int argc, char* argv[]) {
int srcFd = open("./src.txt", O_RDONLY);
if (srcFd == -1) {
error_and_exit();
}
int dstFd = open("./dst.txt", O_WRONLY | O_CREAT);
if (dstFd == -1) {
error_and_exit();
}
char buffer[16];
while (1) {
int num = read(srcFd, buffer, 16);
if (num == 0) {
break;
}
write(dstFd, buffer, num);
}
close(srcFd);
close(dstFd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment