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 it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA */
23 #include <bashtypes.h>
25 # include <sys/param.h>
28 #if defined (HAVE_UNISTD_H)
32 #include <posixstat.h>
38 #include <chartypes.h>
46 /* Make the functions strchr and strrchr if they do not exist. */
47 #if !defined (HAVE_STRCHR)
55 for (s = string; s && *s; s++)
59 return ((char *) NULL);
69 for (s = string, t = (char *)NULL; s && *s; s++)
74 #endif /* !HAVE_STRCHR */
76 #if !defined (HAVE_DUP2) || defined (DUP2_BROKEN)
77 /* Replacement for dup2 (), for those systems which either don't have it,
78 or supply one with broken behaviour. */
85 /* If FD1 is not a valid file descriptor, then return immediately with
87 if (fcntl (fd1, F_GETFL, 0) == -1)
90 if (fd2 < 0 || fd2 >= getdtablesize ())
102 r = fcntl (fd1, F_DUPFD, fd2);
110 /* Force the new file descriptor to remain open across exec () calls. */
111 SET_OPEN_ON_EXEC (fd2);
114 #endif /* !HAVE_DUP2 */
117 * Return the total number of available file descriptors.
119 * On some systems, like 4.2BSD and its descendents, there is a system call
120 * that returns the size of the descriptor table: getdtablesize(). There are
121 * lots of ways to emulate this on non-BSD systems.
123 * On System V.3, this can be obtained via a call to ulimit:
124 * return (ulimit(4, 0L));
126 * On other System V systems, NOFILE is defined in /usr/include/sys/param.h
127 * (this is what we assume below), so we can simply use it:
130 * On POSIX systems, there are specific functions for retrieving various
131 * configuration parameters:
132 * return (sysconf(_SC_OPEN_MAX));
136 #if !defined (HAVE_GETDTABLESIZE)
140 # if defined (_POSIX_VERSION) && defined (HAVE_SYSCONF) && defined (_SC_OPEN_MAX)
141 return (sysconf(_SC_OPEN_MAX)); /* Posix systems use sysconf */
142 # else /* ! (_POSIX_VERSION && HAVE_SYSCONF && _SC_OPEN_MAX) */
143 # if defined (ULIMIT_MAXFDS)
144 return (ulimit (4, 0L)); /* System V.3 systems use ulimit(4, 0L) */
145 # else /* !ULIMIT_MAXFDS */
146 # if defined (NOFILE) /* Other systems use NOFILE */
149 return (20); /* XXX - traditional value is 20 */
150 # endif /* !NOFILE */
151 # endif /* !ULIMIT_MAXFDS */
152 # endif /* ! (_POSIX_VERSION && _SC_OPEN_MAX) */
154 #endif /* !HAVE_GETDTABLESIZE */
156 #if !defined (HAVE_BCOPY)
167 #endif /* !HAVE_BCOPY */
169 #if !defined (HAVE_BZERO)
181 for (i = 0, r = s; i < n; i++)
186 #if !defined (HAVE_GETHOSTNAME)
187 # if defined (HAVE_UNAME)
188 # include <sys/utsname.h>
190 gethostname (name, namelen)
200 i = strlen (ut.nodename) + 1;
201 strncpy (name, ut.nodename, i < namelen ? i : namelen);
202 name[namelen] = '\0';
205 # else /* !HAVE_UNAME */
207 gethostname (name, namelen)
210 strncpy (name, "unknown", namelen);
211 name[namelen] = '\0';
214 # endif /* !HAVE_UNAME */
215 #endif /* !HAVE_GETHOSTNAME */
217 #if !defined (HAVE_KILLPG)
223 return (kill (-pgrp, sig));
225 #endif /* !HAVE_KILLPG */
227 #if !defined (HAVE_MKFIFO) && defined (PROCESS_SUBSTITUTION)
233 #if defined (S_IFIFO)
234 return (mknod (path, (mode | S_IFIFO), 0));
237 #endif /* !S_IFIFO */
239 #endif /* !HAVE_MKFIFO && PROCESS_SUBSTITUTION */