diff options
author | Jim Meyering <jim@meyering.net> | 2005-06-30 20:10:39 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2005-06-30 20:10:39 +0000 |
commit | 58000d181ca1462799f43da1fb86957b357f319c (patch) | |
tree | ba73b7e2f76ca8c0fd3ed66a8a81a6d865486c2c /lib | |
parent | 0f4b63f1acbabd8554d9d1dfa0c7f82bec6b630d (diff) | |
download | coreutils-58000d181ca1462799f43da1fb86957b357f319c.tar.xz |
New file. As yet unused.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/stdopen.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/stdopen.c b/lib/stdopen.c new file mode 100644 index 000000000..6cf521896 --- /dev/null +++ b/lib/stdopen.c @@ -0,0 +1,31 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +/* Try to ensure that each of the standard file numbers (0, 1, 2) + is in use. Without this, each application would have to guard + every call to open, dup, and fopen with tests to ensure they don't + use one of the special file numbers when opening a user's file. */ +void +stdopen (void) +{ + int fd; + for (fd = 0; fd <= 2; fd++) + { + struct stat sb; + if (fstat (fd, &sb) < 0) + { + static const int flag[] = { O_WRONLY, O_RDONLY, O_RDONLY }; + /* FIXME: worry about errno != EBADF? */ + char const *file = "/dev/null"; + int fd2 = open (file, flag[fd]); + if (fd2 < 0) + break; + if (fd2 != fd) + { + close (fd2); + break; + } + } + } +} |