init version.
[bush.git] / lib / sh / oslib.c
blob476718f3d8d7e446e34394ac8ca78e50f5af3cf5
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/>.
21 #include <config.h>
23 #include <bushtypes.h>
24 #if defined (HAVE_SYS_PARAM_H)
25 # include <sys/param.h>
26 #endif
28 #if defined (HAVE_UNISTD_H)
29 # include <unistd.h>
30 #endif
32 #if defined (HAVE_LIMITS_H)
33 # include <limits.h>
34 #endif
36 #include <posixstat.h>
37 #include <filecntl.h>
38 #include <bushansi.h>
40 #if !defined (HAVE_KILLPG)
41 # include <signal.h>
42 #endif
44 #include <stdio.h>
45 #include <errno.h>
46 #include <chartypes.h>
48 #include <shell.h>
50 #if !defined (errno)
51 extern int errno;
52 #endif /* !errno */
54 /* Make the functions strchr and strrchr if they do not exist. */
55 #if !defined (HAVE_STRCHR)
56 char *
57 strchr (string, c)
58 char *string;
59 int c;
61 register char *s;
63 for (s = string; s && *s; s++)
64 if (*s == c)
65 return (s);
67 return ((char *) NULL);
70 char *
71 strrchr (string, c)
72 char *string;
73 int c;
75 register char *s, *t;
77 for (s = string, t = (char *)NULL; s && *s; s++)
78 if (*s == c)
79 t = s;
80 return (t);
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. */
87 int
88 dup2 (fd1, fd2)
89 int fd1, fd2;
91 int saved_errno, r;
93 /* If FD1 is not a valid file descriptor, then return immediately with
94 an error. */
95 if (fcntl (fd1, F_GETFL, 0) == -1)
96 return (-1);
98 if (fd2 < 0 || fd2 >= getdtablesize ())
100 errno = EBADF;
101 return (-1);
104 if (fd1 == fd2)
105 return (0);
107 saved_errno = errno;
109 (void) close (fd2);
110 r = fcntl (fd1, F_DUPFD, fd2);
112 if (r >= 0)
113 errno = saved_errno;
114 else
115 if (errno == EINVAL)
116 errno = EBADF;
118 /* Force the new file descriptor to remain open across exec () calls. */
119 SET_OPEN_ON_EXEC (fd2);
120 return (r);
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:
136 * return (NOFILE);
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)
146 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 */
155 return (NOFILE);
156 # else /* !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)
165 # if defined (bcopy)
166 # undef bcopy
167 # endif
168 void
169 bcopy (s,d,n)
170 char *d, *s;
171 int n;
173 FASTCOPY (s, d, n);
175 #endif /* !HAVE_BCOPY */
177 #if !defined (HAVE_BZERO)
178 # if defined (bzero)
179 # undef bzero
180 # endif
181 void
182 bzero (s, n)
183 char *s;
184 int n;
186 register int i;
187 register char *r;
189 for (i = 0, r = s; i < n; i++)
190 *r++ = '\0';
192 #endif
194 #if !defined (HAVE_GETHOSTNAME)
195 # if defined (HAVE_UNAME)
196 # include <sys/utsname.h>
198 gethostname (name, namelen)
199 char *name;
200 int namelen;
202 int i;
203 struct utsname ut;
205 --namelen;
207 uname (&ut);
208 i = strlen (ut.nodename) + 1;
209 strncpy (name, ut.nodename, i < namelen ? i : namelen);
210 name[namelen] = '\0';
211 return (0);
213 # else /* !HAVE_UNAME */
215 gethostname (name, namelen)
216 char *name;
217 int namelen;
219 strncpy (name, "unknown", namelen);
220 name[namelen] = '\0';
221 return 0;
223 # endif /* !HAVE_UNAME */
224 #endif /* !HAVE_GETHOSTNAME */
226 #if !defined (HAVE_KILLPG)
228 killpg (pgrp, sig)
229 pid_t pgrp;
230 int sig;
232 return (kill (-pgrp, sig));
234 #endif /* !HAVE_KILLPG */
236 #if !defined (HAVE_MKFIFO) && defined (PROCESS_SUBSTITUTION)
238 mkfifo (path, mode)
239 char *path;
240 int mode;
242 #if defined (S_IFIFO)
243 return (mknod (path, (mode | S_IFIFO), 0));
244 #else /* !S_IFIFO */
245 return (-1);
246 #endif /* !S_IFIFO */
248 #endif /* !HAVE_MKFIFO && PROCESS_SUBSTITUTION */
250 #define DEFAULT_MAXGROUPS 64
253 getmaxgroups ()
255 static int maxgroups = -1;
257 if (maxgroups > 0)
258 return maxgroups;
260 #if defined (HAVE_SYSCONF) && defined (_SC_NGROUPS_MAX)
261 maxgroups = sysconf (_SC_NGROUPS_MAX);
262 #else
263 # if defined (NGROUPS_MAX)
264 maxgroups = NGROUPS_MAX;
265 # else /* !NGROUPS_MAX */
266 # if defined (NGROUPS)
267 maxgroups = NGROUPS;
268 # else /* !NGROUPS */
269 maxgroups = DEFAULT_MAXGROUPS;
270 # endif /* !NGROUPS */
271 # endif /* !NGROUPS_MAX */
272 #endif /* !HAVE_SYSCONF || !SC_NGROUPS_MAX */
274 if (maxgroups <= 0)
275 maxgroups = DEFAULT_MAXGROUPS;
277 return maxgroups;
280 long
281 getmaxchild ()
283 static long maxchild = -1L;
285 if (maxchild > 0)
286 return maxchild;
288 #if defined (HAVE_SYSCONF) && defined (_SC_CHILD_MAX)
289 maxchild = sysconf (_SC_CHILD_MAX);
290 #else
291 # if defined (CHILD_MAX)
292 maxchild = CHILD_MAX;
293 # else
294 # if defined (MAXUPRC)
295 maxchild = MAXUPRC;
296 # endif /* MAXUPRC */
297 # endif /* CHILD_MAX */
298 #endif /* !HAVE_SYSCONF || !_SC_CHILD_MAX */
300 return (maxchild);