Mercurial > gnulib
annotate lib/stdopen.c @ 40186:8964917f9574
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 18 Feb 2019 08:02:49 -0800 |
parents | 30186c67361d |
children |
rev | line source |
---|---|
40085
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
1 /* stdopen.c - ensure that the three standard file descriptors are in use |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
2 |
40086
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
3 Copyright (C) 2005-2006, 2019 Free Software Foundation, Inc. |
40085
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
4 |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
6 it under the terms of the GNU General Public License as published by |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
7 the Free Software Foundation, either version 3 of the License, or |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
8 (at your option) any later version. |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
9 |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
13 GNU General Public License for more details. |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
14 |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
17 |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
18 /* Written by Paul Eggert and Jim Meyering. */ |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
19 |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
20 #include <config.h> |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
21 |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
22 #include "stdopen.h" |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
23 |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
24 #include <sys/stat.h> |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
25 #include <fcntl.h> |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
26 #include <unistd.h> |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
27 #include <errno.h> |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
28 |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
29 /* Try to ensure that all of the standard file numbers (0, 1, 2) |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
30 are in use. Without this, each application would have to guard |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
31 every call to open, dup, fopen, etc. with tests to ensure they |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
32 don't use one of the special file numbers when opening a file. |
40086
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
33 Return zero if successful, an errno value if at least one of |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
34 the file descriptors is initially closed and could not be opened. */ |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
35 |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
36 int |
40085
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
37 stdopen (void) |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
38 { |
40089
30186c67361d
stdopen: Fix compilation error with IRIX cc.
Bruno Haible <bruno@clisp.org>
parents:
40086
diff
changeset
|
39 int fd; |
30186c67361d
stdopen: Fix compilation error with IRIX cc.
Bruno Haible <bruno@clisp.org>
parents:
40086
diff
changeset
|
40 for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) |
40085
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
41 { |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
42 if (fcntl (fd, F_GETFD) < 0) |
40086
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
43 { |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
44 /* Open /dev/null with the contrary mode so that the typical |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
45 read (stdin) or write (stdout, stderr) operation will fail. |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
46 With descriptor 0, we can do even better on systems that |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
47 have /dev/full, by opening that write-only instead of |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
48 /dev/null. The only drawback is that a write-provoked |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
49 failure comes with a misleading errno value, ENOSPC. */ |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
50 int mode = fd == STDIN_FILENO ? O_WRONLY : O_RDONLY; |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
51 int full_fd = fd == STDIN_FILENO ? open ("/dev/full", mode) : -1; |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
52 int new_fd = full_fd < 0 ? open ("/dev/null", mode) : full_fd; |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
53 if (new_fd < 0) |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
54 return errno; |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
55 if (STDERR_FILENO < new_fd) |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
56 { |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
57 /* 0, 1, and 2 are already open somehow. |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
58 Our is not to reason why. */ |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
59 close (new_fd); |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
60 return 0; |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
61 } |
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
62 } |
40085
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
63 } |
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
64 |
40086
d6de7c427079
stdopen: modernize and simplify
Paul Eggert <eggert@cs.ucla.edu>
parents:
40085
diff
changeset
|
65 return 0; |
40085
a238d0c7991f
stdopen: copy from last use in coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
66 } |