]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Use /dev/full if possible for descriptor 0 -- like glibc now does.
authorJim Meyering <jim@meyering.net>
Wed, 9 Nov 2005 20:53:41 +0000 (20:53 +0000)
committerJim Meyering <jim@meyering.net>
Wed, 9 Nov 2005 20:53:41 +0000 (20:53 +0000)
Fall back on /dev/null if opening /dev/full fails.

lib/stdopen.c

index d4732cb85ce82a8e34a12880a6b1eb4d62cdbb7c..3ca2b61e6bd17c94a4a549ace58c4a5b52545b64 100644 (file)
@@ -52,7 +52,17 @@ stdopen (void)
            {
              static const int contrary_mode[]
                = { O_WRONLY, O_RDONLY, O_RDONLY };
-             int new_fd = open ("/dev/null", contrary_mode[fd]);
+             int mode = contrary_mode[fd];
+             int new_fd;
+             /* Open /dev/null with the contrary mode so that the typical
+                read (stdin) or write (stdout, stderr) operation will fail.
+                With descriptor 0, we can do even better on systems that
+                have /dev/full, by opening that write-only instead of
+                /dev/null.  The only drawback is that a write-provoked
+                failure comes with a misleading errno value, ENOSPC.  */
+             if (mode == O_RDONLY
+                 || (new_fd = open ("/dev/full", mode) != fd))
+               new_fd = open ("/dev/null", mode);
              if (new_fd != fd)
                {
                  if (0 <= new_fd)