Skip to content

Instantly share code, notes, and snippets.

@davel
Created March 3, 2014 17:39
Show Gist options
  • Save davel/9330255 to your computer and use it in GitHub Desktop.
Save davel/9330255 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[]) {
int fd_orig;
int fd_dup;
char c;
fd_orig = open("/etc/debian_version", O_RDONLY);
if (fd_orig < 0) {
fprintf(stderr, "Open failed\n");
exit(1);
}
fd_dup = dup(fd_orig);
if (fd_dup < 0) {
fprintf(stderr, "Dup failed\n");
exit(1);
}
printf("Original\n");
while (read(fd_orig, &c, 1)) {
printf("%c", c);
}
printf("Dupped\n");
while (read(fd_dup, &c, 1)) {
printf("%c", c);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment