/* mycp.c */ /* copies file named by argv[1] to file named by argv[2] */ #include #include #include #define BUF_SZB 512 void an_error(char *s) { printf("%s\n", s); exit(1); } void main(int argc, char *argv[]) { int from_fd, to_fd; char buf[BUF_SZB]; int bytes_read; if (argc != 3) { an_error("usage: mycp from-file to-file"); } if ((from_fd = open(argv[1], O_RDONLY)) == -1) { an_error("couldn't open from-file for reading"); } if ((to_fd = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) { an_error("couldn't create to-file for writing"); } while ((bytes_read = read(from_fd, buf, BUF_SZB)) != 0) { if (write(to_fd, buf, bytes_read) == -1) { an_error("error writing to to-file"); } } if (close(from_fd) == -1) { an_error("couldn't close from-file"); } if (close(to_fd) == -1) { an_error("couldn't close to-file"); } exit(0); } /* end of mycp.c */