Opening the two ends of a copy
Opening the two ends of a copy
Answer
int src = open(argv[1], O_RDONLY); int dst = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666); if (src == -1 || dst == -1) { perror("open"); return 1; }
The destination needs O_CREAT (so the third argument is mandatory) and O_TRUNC (so a shorter copy does not leave the old tail behind). The stored mode is 0666 & ~umask.
S&K 3e ch18 §18.8; signatures and return values per POSIX.1-2024 (open, read, write, close, lseek)