git - c - standard file descriptor - open if missing -



git - c - standard file descriptor - open if missing -

i saw next function in setup.c of git source code.

code:

/* if standard file descriptor missing open /dev/null */ void sanitize_stdfds(void) { int fd = open("/dev/null", o_rdwr, 0); while (fd != -1 && fd < 2) fd = dup(fd); if (fd == -1) die_errno("open /dev/null or dup failed"); if (fd > 2) close(fd); }

it seek open stdio file descriptors (0/1/2) /dev/null, if missing.

my question is:

in while (fd != -1 && fd < 2) , why utilize 2, not 3.

because if fd == 2 have opened file descriptors 0,1,2 , there nil more do. needed open 3 descriptors stdin, stdout , stderr.

if set there 3, loop open file descriptors 0,1,2,3. line if (fd > 2) close(fd); close descriptor 3. work in both cases, original solution better.

c git stdio

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -