3 ms·
Why did 16-bit _lopen and _lcreat functions return -1 on failure instead of 0?
- NikkiA 3y agoThat, surprisingly for raymond, doesn't actually answer the question, since you cannot request opening stdin/stdout/stderr via this API, the values 0..2 are also invalid return values. (/dev/stdin, /dev/stdout, /dev/stderr exist today, but did not then, and even today I don't think you can open/create them since they are symlinks to /proc/self/fd/0..2 and thus open("/dev/stdin", ...) returns -1) AFAIK if you close(0) you lose your stdin and never see it again.
- phoyd 3y agoUnder UNIX, the lowest available file descriptor for the process is returned from open/create (also in Posix). So if you close(0) and then immediately open() something, you will (probably) get 0 as the new file descriptor. This is the way redirection was done in early UNIX versions, at least I think until System 7, where dup2() was introduced to address the race condition lurking here.