#include #include #include using namespace std; int main(int argc, char * argv[]) { if (argc != 3) { cerr << "Usage " << argv[0] << " source dest" << endl; return 1; } string sourceFileName{argv[1]}; string destFileName{argv[2]}; cout << "Copying from " << sourceFileName << " to " << destFileName << endl; int infd{open(sourceFileName.c_str(), O_RDONLY)} ; if (infd == -1) { string msg = "\tError open " + sourceFileName + " :"; perror(msg.c_str()); return -1; } int flags{O_WRONLY | O_CREAT | O_EXCL}; int perms{S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH}; int outfd{open(destFileName.c_str(), flags, perms)}; if (outfd == -1) { string msg = "\tError open " + destFileName + " :"; perror(msg.c_str()); return -1; close(infd); } const size_t BLOCK_SIZE{1000}; size_t actualRead{0}; char buffer[BLOCK_SIZE]; while ((actualRead = read(infd, buffer, BLOCK_SIZE)) > 0) { if(write(outfd, buffer, actualRead) != actualRead) { perror("\tError write: "); } } if (actualRead < 0) { perror("\tError read: "); } close(infd); close(outfd); return 0; }