1 /* oslib.c - functions present only in some unix versions. */
3 /* Copyright (C) 1995,2010 Free Software Foundation, Inc.
5 This file is part of GNU Bush, the Bourne Again SHell.
7 Bush is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bush is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bush. If not, see <http://www.gnu.org/licenses/>.
23 #include <bushtypes.h>
24 #if defined (HAVE_SYS_PARAM_H)
25 # include <sys/param.h>
28 #if defined (HAVE_UNISTD_H)
32 #if defined (HAVE_LIMITS_H)
36 #include <posixstat.h>
40 #if !defined (HAVE_KILLPG)
46 #include <chartypes.h>
54 /* Make the functions strchr and strrchr if they do not exist. */
55 #if !defined (HAVE_STRCHR)
63 for (s
= string
; s
&& *s
; s
++)
67 return ((char *) NULL
);
77 for (s
= string
, t
= (char *)NULL
; s
&& *s
; s
++)
82 #endif /* !HAVE_STRCHR */
84 #if !defined (HAVE_DUP2) || defined (DUP2_BROKEN)
85 /* Replacement for dup2 (), for those systems which either don't have it,
86 or supply one with broken behaviour. */
93 /* If FD1 is not a valid file descriptor, then return immediately with
95 if (fcntl (fd1
, F_GETFL
, 0) == -1)
98 if (fd2
< 0 || fd2
>= getdtablesize ())
110 r
= fcntl (fd1
, F_DUPFD
, fd2
);
118 /* Force the new file descriptor to remain open across exec () calls. */
119 SET_OPEN_ON_EXEC (fd2
);
122 #endif /* !HAVE_DUP2 */
125 * Return the total number of available file descriptors.
127 * On some systems, like 4.2BSD and its descendants, there is a system call
128 * that returns the size of the descriptor table: getdtablesize(). There are
129 * lots of ways to emulate this on non-BSD systems.
131 * On System V.3, this can be obtained via a call to ulimit:
132 * return (ulimit(4, 0L));
134 * On other System V systems, NOFILE is defined in /usr/include/sys/param.h
135 * (this is what we assume below), so we can simply use it:
138 * On POSIX systems, there are specific functions for retrieving various
139 * configuration parameters:
140 * return (sysconf(_SC_OPEN_MAX));
144 #if !defined (HAVE_GETDTABLESIZE)
148 # if defined (_POSIX_VERSION) && defined (HAVE_SYSCONF) && defined (_SC_OPEN_MAX)
149 return (sysconf(_SC_OPEN_MAX
)); /* Posix systems use sysconf */
150 # else /* ! (_POSIX_VERSION && HAVE_SYSCONF && _SC_OPEN_MAX) */
151 # if defined (ULIMIT_MAXFDS)
152 return (ulimit (4, 0L)); /* System V.3 systems use ulimit(4, 0L) */
153 # else /* !ULIMIT_MAXFDS */
154 # if defined (NOFILE) /* Other systems use NOFILE */
157 return (20); /* XXX - traditional value is 20 */
158 # endif /* !NOFILE */
159 # endif /* !ULIMIT_MAXFDS */
160 # endif /* ! (_POSIX_VERSION && _SC_OPEN_MAX) */
162 #endif /* !HAVE_GETDTABLESIZE */
164 #if !defined (HAVE_BCOPY)
175 #endif /* !HAVE_BCOPY */
177 #if !defined (HAVE_BZERO)
189 for (i
= 0, r
= s
; i
< n
; i
++)
194 #if !defined (HAVE_GETHOSTNAME)
195 # if defined (HAVE_UNAME)
196 # include <sys/utsname.h>
198 gethostname (name
, namelen
)
208 i
= strlen (ut
.nodename
) + 1;
209 strncpy (name
, ut
.nodename
, i
< namelen
? i
: namelen
);
210 name
[namelen
] = '\0';
213 # else /* !HAVE_UNAME */
215 gethostname (name
, namelen
)
219 strncpy (name
, "unknown", namelen
);
220 name
[namelen
] = '\0';
223 # endif /* !HAVE_UNAME */
224 #endif /* !HAVE_GETHOSTNAME */
226 #if !defined (HAVE_KILLPG)
232 return (kill (-pgrp
, sig
));
234 #endif /* !HAVE_KILLPG */
236 #if !defined (HAVE_MKFIFO) && defined (PROCESS_SUBSTITUTION)
242 #if defined (S_IFIFO)
243 return (mknod (path
, (mode
| S_IFIFO
), 0));
246 #endif /* !S_IFIFO */
248 #endif /* !HAVE_MKFIFO && PROCESS_SUBSTITUTION */
250 #define DEFAULT_MAXGROUPS 64
255 static int maxgroups
= -1;
260 #if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
261 maxgroups
= sysconf (_SC_NGROUPS_MAX
);
263 # if defined (NGROUPS_MAX)
264 maxgroups
= NGROUPS_MAX
;
265 # else /* !NGROUPS_MAX */
266 # if defined (NGROUPS)
268 # else /* !NGROUPS */
269 maxgroups
= DEFAULT_MAXGROUPS
;
270 # endif /* !NGROUPS */
271 # endif /* !NGROUPS_MAX */
272 #endif /* !HAVE_SYSCONF || !SC_NGROUPS_MAX */
275 maxgroups
= DEFAULT_MAXGROUPS
;
283 static long maxchild
= -1L;
288 #if defined (HAVE_SYSCONF) && defined (_SC_CHILD_MAX)
289 maxchild
= sysconf (_SC_CHILD_MAX
);
291 # if defined (CHILD_MAX)
292 maxchild
= CHILD_MAX
;
294 # if defined (MAXUPRC)
296 # endif /* MAXUPRC */
297 # endif /* CHILD_MAX */
298 #endif /* !HAVE_SYSCONF || !_SC_CHILD_MAX */