#include #include #include #include #include #include main(int argc, char *args[]) { int fd, n; char buffer[1024]; struct stat st; if(argc > 3) { fprintf(stderr, "Usage: %s [inputfile [outputfile]]", args[0]); exit(1); } if(argc >= 2 && strcmp(args[1], "-") ) { if( ((fd = open(args[1], O_RDONLY)) == -1) || (dup2(fd, STDIN_FILENO) == -1) ) { fprintf(stderr, "%s: Unable to open %s for input.\n", args[0], args[2]); exit(1); } close(fd); fstat(STDIN_FILENO, &st); } else st.st_mode = S_IRUSR | S_IWUSR; if(argc == 3 && strcmp(args[2], "-") ) { if( ((fd = open(args[2], O_WRONLY | O_CREAT | O_TRUNC, st.st_mode)) == -1) || (dup2(fd, STDOUT_FILENO) == -1)) { fprintf(stderr, "%s: Unable to open %s for output.\n", args[0], args[2]); exit(1); } close(fd); } while( (n = read(STDIN_FILENO, buffer, 1024)) > 0) { if(write(STDOUT_FILENO, buffer, n) != n) { fprintf(stderr, "%s: Error writing.\n", args[0]); exit(1); } } }