annotate lib/cloexec.c @ 5848:a48fb0e98c8c

*** empty log message ***
author Paul Eggert <eggert@cs.ucla.edu>
date Sat, 14 May 2005 06:03:57 +0000
parents 87f0b7b6d498
children ace6ea191424
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4979
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
1 /* closexec.c - set or clear the close-on-exec descriptor flag
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
2 Copyright (C) 1991, 2004 Free Software Foundation, Inc.
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
3
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
7 any later version.
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
8
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 GNU General Public License for more details.
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
15 along with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 4982
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
4979
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
17
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18 The code is taken from glibc/manual/llio.texi */
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
19
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
20 #if HAVE_CONFIG_H
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21 # include <config.h>
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
22 #endif
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
23
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
24 #include "cloexec.h"
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
25
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
26 #if HAVE_UNISTD_H
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
27 # include <unistd.h>
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
28 #endif
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
29
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
30 #if HAVE_FCNTL_H
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
31 # include <fcntl.h>
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
32 #endif
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
33
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
34 #ifndef FD_CLOEXEC
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
35 # define FD_CLOEXEC 1
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
36 #endif
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
37
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
38 /* Set the `FD_CLOEXEC' flag of DESC if VALUE is true,
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
39 or clear the flag if VALUE is false.
4982
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
40 Return 0 on success, or -1 on error with `errno' set. */
4979
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
41
4982
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
42 int
4979
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
43 set_cloexec_flag (int desc, bool value)
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
44 {
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
45 #if defined F_GETFD && defined F_SETFD
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
46
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
47 int flags = fcntl (desc, F_GETFD, 0);
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
48
4982
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
49 if (0 <= flags)
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
50 {
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
51 int newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
4979
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
52
4982
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
53 if (flags == newflags
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
54 || fcntl (desc, F_SETFD, newflags) != -1)
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
55 return 0;
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
56 }
4979
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
57
4982
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
58 return -1;
4979
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
59
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
60 #else
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
61
4982
87f0b7b6d498 cloexec returns int not bool, to be more consistent with Unix conventions.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4979
diff changeset
62 return 0;
4979
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
63
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
64 #endif
6b9af4133e6f Merge changes to getloadavg.c from coreutils and Emacs; this
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
65 }