DUP3(3)
π
DUP3(3) β the dup3 glow-up, bestie
okay so real talk, this is the dup3 page from the Library Functions Manual, and honestly? she's iconic. she's dup2's cooler little sister and she will let you pick the descriptor number yourself. no cap. let's get into it π
β¨ LIBRARY β¨
Standard C Library (libc, -lc). she lives in libc, obviously π
πΈ SYNOPSIS πΈ
like, here's the vibe, literally copy-paste:
#include <fcntl.h>
#include <unistd.h>
int
dup3(int oldd, int newd, int flags);
three args, no drama. well, mostly no drama, wait for the ERRORS section π
π¦ DESCRIPTION π¦
okay so dup3() duplicates an existing object descriptor, but the whole main character move is that you get to specify the value of the new descriptor. newd. your choice. your moment. periodt β¨
the close-on-exec flag on the new file descriptor? that's determined by the O_CLOEXEC bit in flags. yup, she's listening π
the close-on-fork flag on the new file descriptor? determined by the O_CLOFORK bit in flags. very demure, very mindful π
the resolve-beneath flag on the new file descriptor is preserved. we don't touch it, we respect it π«Ά
if oldd β newd and flags == 0, the behavior is identical to dup2(oldd, newd). same energy, same result, just a different function name fr fr.
β οΈ but ifoldd == newd, thendup3()fails β unlikedup2(2). yeah. that's the plot twist. that's the whole thing.dup2would've been chill about it,dup3is not. π
π₯ RETURN VALUES π₯
the value -1 is returned if an error occurs. and the external variable errno indicates the cause of the error. so if you get -1, check errno, she's telling you what went wrong π
π ERRORS π
dup3() fails if:
[EBADF]β theolddargument is not a valid active descriptor, or thenewdargument is negative or exceeds the maximum allowable descriptor number. like, either she doesn't exist or she's out of range, and neither is cute π[EINVAL]β theolddargument is equal to thenewdargument. yup, the self-dupe situation. we said no π[EINVAL]β theflagsargument has bits set other thanO_CLOEXECorO_CLOFORK. only those two bits are invited to the party, bestie, everyone else is getting turned away at the door π
π SEE ALSO π
the whole friend group, go say hi:
accept(2), close(2), dup2(2), fcntl(2), getdtablesize(2), open(2), pipe(2), socket(2), socketpair(2)
β STANDARDS β
the dup3() function does not conform to any standard. she's ungovernable. she's free. she answers to no one π¦ (ngl this is lowkey important info if you care about portability, so like, know that about her)
π HISTORY π
the dup3() function appeared in FreeBSD 10.0. and the O_CLOFORK flag appeared in FreeBSD 15.0. glow-ups take time, okay? π
okay that's the whole page, you ate that, go duplicate some descriptors responsibly π
sign-off szn: this little masterpiece is from Debian, dated May 17, 2025, and it's the DUP3(3) page. mwah π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2013 Jilles Tjoelker
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms