Tak vyzkoušeno, až na ten dup2 to funguje podle očekávání. dup2 taky jen nevrací newfd, ale nulu...
int main(int argc, char *argv[])
{
int fd, fd2, fd3;
write(1, "stdout\r\n", 8);
write(2, "stderr\r\n", 8);
fd = open("xxx", O_RDWR|O_CREAT, 0600);
if (-1 == (fd2 = dup(1))) {
perror("dup: %m");
return 1;
}
write(fd2, "dup\r\n", 5);
if (-1 == (fd3 = dup2(1, fd))) {
perror("dup2: %m");
return 1;
}
write(fd3, "dup2\r\n", 6);
write(fd, "dup2_2\r\n", 8);
fprintf(stderr, "fd=%i fd2=%i fd3=%i\n", fd, fd2, fd3);
close(fd);
close(fd2);
Sleep(25000);
return 0;
}
stdout
stderr
dup
dup2_2
fd=3 fd2=4 fd3=0
Na běžné utility je fakt rozdíl jen v #ifdef __NT__ #include "windows.h" #endif