Skip to content

Instantly share code, notes, and snippets.

@isRuslan
Created August 17, 2016 19:45
Show Gist options
  • Save isRuslan/29f0cc3d019340447bca2192bf123685 to your computer and use it in GitHub Desktop.
Save isRuslan/29f0cc3d019340447bca2192bf123685 to your computer and use it in GitHub Desktop.
cp(1) copy file1 to file2
#include <stdio.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/syscall.h>
#define PERMS 0666
#define BUFSIZE 10000
void error(char *fmt, ...){
va_list args;
fprintf(stderr, fmt, args);
}
int main(int argc, char *argv[])
{
int f1, f2, n;
char buf[BUFSIZE];
if(argc != 3){
error("Usage: cp from to");
}
f1 = open(argv[1], PERMS);
if(f1 == -1){
error("cannot open %s", argv[1]);
}
f2 = open(argv[2], PERMS);
if(f1 == -1){
error("cannot open %s", argv[2]);
}
while((n = read(f1, buf, BUFSIZE)) > 0){
if(write(f2, buf, n) != n){
error("write error %s", argv[2]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment