r/C_Programming • • Sep 25 '26

POSIX exec

hey guys i am trying to write POSIX compatible program and i need to close the fds before i exec, i cant guarante they all fd's are O_CLOEXEC becouse i use libraries

0 Upvotes

28 comments sorted by

View all comments

Show parent comment

6

u/EpochVanquisher Sep 25 '26
#include <unistd.h>
for (int fd = 3; fd < 1024; fd++) {
  close(fd);
}

It doesn’t work if you have >1024 files open, but it is POSIX.