Skip to content

Instantly share code, notes, and snippets.

@josephok
Created April 18, 2016 08:18
Show Gist options
  • Select an option

  • Save josephok/034e7e99cb8f47d6632921bc0207a941 to your computer and use it in GitHub Desktop.

Select an option

Save josephok/034e7e99cb8f47d6632921bc0207a941 to your computer and use it in GitHub Desktop.
copy a file
// Copy a file.
// Removed all error hanlders for simplicity.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#ifndef BUF_SIZE
#define BUF_SIZE 4096
#endif
// ssize_t read(int fd, void *buf, size_t count);
//
// int open(const char *pathname, int flags);
// int open(const char *pathname, int flags, mode_t mode);
// ssize_t write(int fd, const void *buf, size_t count);
int main(int argc, char const *argv[]) {
int fd = open(argv[1], O_RDONLY);
char buf[BUF_SIZE];
int outfd = open(argv[2], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
ssize_t nread;
while ((nread = read(fd, buf, BUF_SIZE)) > 0) {
write(outfd, buf, nread);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment