1 /* oslib.c - functions present only in some unix versions. */
3 /* Copyright (C) 1995 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash 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 Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include <bashtypes.h>
25 # include <sys/param.h>
28 #if defined (HAVE_UNISTD_H)
32 #if defined (HAVE_LIMITS_H)
36 #include <posixstat.h>
42 #include <chartypes.h>
50 /* Make the functions strchr and strrchr if they do not exist. */
51 #if !defined (HAVE_STRCHR)
59 for (s = string; s && *s; s++)
63 return ((char *) NULL);
73 for (s = string, t = (char *)NULL; s && *s; s++)
78 #endif /* !HAVE_STRCHR */
80 #if !defined (HAVE_DUP2) || defined (DUP2_BROKEN)
81 /* Replacement for dup2 (), for those systems which either don't have it,
82 or supply one with broken behaviour. */
89 /* If FD1 is not a valid file descriptor, then return immediately with
91 if (fcntl (fd1, F_GETFL, 0) == -1)
94 if (fd2 < 0 || fd2 >= getdtablesize ())
106 r = fcntl (fd1, F_DUPFD, fd2);
114 /* Force the new file descriptor to remain open across exec () calls. */
115 SET_OPEN_ON_EXEC (fd2);
118 #endif /* !HAVE_DUP2 */
121 * Return the total number of available file descriptors.
123 * On some systems, like 4.2BSD and its descendents, there is a system call
124 * that returns the size of the descriptor table: getdtablesize(). There are
125 * lots of ways to emulate this on non-BSD systems.
127 * On System V.3, this can be obtained via a call to ulimit:
128 * return (ulimit(4, 0L));
130 * On other System V systems, NOFILE is defined in /usr/include/sys/param.h
131 * (this is what we assume below), so we can simply use it:
134 * On POSIX systems, there are specific functions for retrieving various
135 * configuration parameters:
136 * return (sysconf(_SC_OPEN_MAX));
140 #if !defined (HAVE_GETDTABLESIZE)
144 # if defined (_POSIX_VERSION) && defined (HAVE_SYSCONF) && defined (_SC_OPEN_MAX)
145 return (sysconf(_SC_OPEN_MAX)); /* Posix systems use sysconf */
146 # else /* ! (_POSIX_VERSION && HAVE_SYSCONF && _SC_OPEN_MAX) */
147 # if defined (ULIMIT_MAXFDS)
148 return (ulimit (4, 0L)); /* System V.3 systems use ulimit(4, 0L) */
149 # else /* !ULIMIT_MAXFDS */
150 # if defined (NOFILE) /* Other systems use NOFILE */
153 return (20); /* XXX - traditional value is 20 */
154 # endif /* !NOFILE */
155 # endif /* !ULIMIT_MAXFDS */
156 # endif /* ! (_POSIX_VERSION && _SC_OPEN_MAX) */
158 #endif /* !HAVE_GETDTABLESIZE */
160 #if !defined (HAVE_BCOPY)
171 #endif /* !HAVE_BCOPY */
173 #if !defined (HAVE_BZERO)
185 for (i = 0, r = s; i < n; i++)
190 #if !defined (HAVE_GETHOSTNAME)
191 # if defined (HAVE_UNAME)
192 # include <sys/utsname.h>
194 gethostname (name, namelen)
204 i = strlen (ut.nodename) + 1;
205 strncpy (name, ut.nodename, i < namelen ? i : namelen);
206 name[namelen] = '\0';
209 # else /* !HAVE_UNAME */
211 gethostname (name, namelen)
214 strncpy (name, "unknown", namelen);
215 name[namelen] = '\0';
218 # endif /* !HAVE_UNAME */
219 #endif /* !HAVE_GETHOSTNAME */
221 #if !defined (HAVE_KILLPG)
227 return (kill (-pgrp, sig));
229 #endif /* !HAVE_KILLPG */
231 #if !defined (HAVE_MKFIFO) && defined (PROCESS_SUBSTITUTION)
237 #if defined (S_IFIFO)
238 return (mknod (path, (mode | S_IFIFO), 0));
241 #endif /* !S_IFIFO */
243 #endif /* !HAVE_MKFIFO && PROCESS_SUBSTITUTION */
245 #define DEFAULT_MAXGROUPS 64
250 static int maxgroups = -1;
255 #if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
256 maxgroups = sysconf (_SC_NGROUPS_MAX);
258 # if defined (NGROUPS_MAX)
259 maxgroups = NGROUPS_MAX;
260 # else /* !NGROUPS_MAX */
261 # if defined (NGROUPS)
263 # else /* !NGROUPS */
264 maxgroups = DEFAULT_MAXGROUPS;
265 # endif /* !NGROUPS */
266 # endif /* !NGROUPS_MAX */
267 #endif /* !HAVE_SYSCONF || !SC_NGROUPS_MAX */
270 maxgroups = DEFAULT_MAXGROUPS;
278 static long maxchild = -1L;
283 #if defined (HAVE_SYSCONF) && defined (_SC_CHILD_MAX)
284 maxchild = sysconf (_SC_CHILD_MAX);
286 # if defined (CHILD_MAX)
287 maxchild = CHILD_MAX;
289 # if defined (MAXUPRC)
291 # endif /* MAXUPRC */
292 # endif /* CHILD_MAX */
293 #endif /* !HAVE_SYSCONF || !_SC_CHILD_MAX */