1 /* $Header: /src/pub/tcsh/sh.misc.c,v 3.23 1997/02/23 19:03:23 christos Exp $ */
3 * sh.misc.c: Miscelaneous functions
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 RCSID("$Id: sh.misc.c,v 3.23 1997/02/23 19:03:23 christos Exp $")
41 static int renum __P((int, int));
42 static Char **blkend __P((Char **));
43 static Char **blkcat __P((Char **, Char **));
55 return (0); /* Check for nil pointer */
75 register const char *s;
81 s = (const char *) "";
82 for (p = (char *) s; *p++ != '\0';)
84 n = p = (char *) xmalloc((size_t)
85 ((((const char *) p) - s) * sizeof(char)));
86 while ((*p++ = *s++) != '\0')
121 (void) Strcat(str, *av);
123 (void) Strcat(str, STRspace);
143 register Char **av = oav;
145 while ((*av++ = *bv++) != NULL)
155 (void) blkcpy(blkend(up), vp);
163 register Char **av = av0;
176 register Char **newv =
177 (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
181 *newv++ = Strsave(*v++);
185 #if !defined(SHORT_STRINGS) && !defined(POSIX)
188 register const char *s, *t;
191 register const char *ss = s;
192 register const char *tt = t;
197 while (*ss++ == *tt++);
198 } while (*s++ != '\0');
202 #endif /* !SHORT_STRINGS && !POSIX */
204 #ifndef SHORT_STRINGS
210 register char *p, *q;
216 for (p = cp; *p++ != '\0';)
218 for (q = dp; *q++ != '\0';)
220 ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
221 for (p = ep, q = cp; (*p++ = *q++) != '\0';)
223 for (p--, q = dp; (*p++ = *q++) != '\0';)
228 #endif /* !SHORT_STRINGS */
232 register Char **up, **vp;
235 (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
238 (void) blkcpy(wp, up);
239 return (blkcat(wp, vp));
257 * This routine is called after an error to close up
258 * any units which may have been left open accidentally.
266 /* suggested by Justin Bur; thanks to Karl Kleinpaste */
269 for (f = 0; f < NOFILE; f++)
270 if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
274 #endif /* MALLOC_TRACE */
280 (void) open(_PATH_DEVNULL, O_RDONLY);
285 #ifndef CLOSE_ON_EXEC
287 * Close files before executing a file.
288 * We could be MUCH more intelligent, since (on a version 7 system)
289 * we need only close files here during a source, the other
290 * shell fd's being in units 16-19 which are closed automatically!
304 isoutatty = isatty(SHOUT);
305 isdiagatty = isatty(SHDIAG);
306 for (f = 3; f < NOFILE; f++)
310 #endif /* CLOSE_ON_EXEC */
322 int fd = open(_PATH_DEVNULL, O_RDONLY);
334 * Move descriptor i to j.
335 * If j is -1 then we just want to get i to a safe place,
336 * i.e. to a unit > 2. This also happens in dcopy.
364 if (i == j || i < 0 || (j < 0 && i > 2))
374 return (renum(i, j));
381 register int k = dup(i);
385 if (j == -1 && k > 2)
396 * Left shift a command argument list, discarding
397 * the first c arguments. Used in "shift" commands
398 * as well as by commands like "repeat".
407 for (u = v; *u && --c >= 0; u++)
424 while (*cp && Isdigit(*cp))
434 (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
436 return (blkcpy(nv, v));
439 #ifndef SHORT_STRINGS
451 #endif /* SHORT_STRINGS */
457 register Char *dp = cp;
461 while ((*dp++ &= TRIM) != '\0')
470 register Char *dp = cp;
486 if (cmap(*s, _META | _DOL | _QF | _QB | _ESC | _GLOB))
499 setname(short2str(name));
500 stderror(ERR_NAME | ERR_UNDVAR);
505 register Char *sub, *str;
513 if ((*sub++ & TRIM) != (*str++ & TRIM))