1 /* $Header: /src/pub/tcsh/sh.misc.c,v 3.34 2005/01/18 20:24:50 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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$Id: sh.misc.c,v 3.34 2005/01/18 20:24:50 christos Exp $")
37 static int renum __P((int, int));
38 static Char **blkend __P((Char **));
39 static Char **blkcat __P((Char **, Char **));
51 return (0); /* Check for nil pointer */
78 for (p = s; *p++ != '\0';)
80 r = n = (char *) xmalloc((size_t)((((const char *) p) - s) * sizeof(char)));
81 while ((*n++ = *s++) != '\0')
116 (void) Strcat(str, *av);
118 (void) Strcat(str, STRspace);
140 while ((*av++ = *bv++) != NULL)
150 (void) blkcpy(blkend(up), vp);
172 (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
176 *newv++ = Strsave(*v++);
192 while (*ss++ == *tt++);
193 } while (*s++ != '\0');
196 #endif /* !HAVE_STRSTR */
198 #ifndef SHORT_STRINGS
212 ep = (char *) xmalloc((cl + dl + 1) * sizeof(char));
214 memcpy(ep + cl, dp, dl + 1);
218 #endif /* !SHORT_STRINGS */
225 (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
228 (void) blkcpy(wp, up);
229 return (blkcat(wp, vp));
247 * This routine is called after an error to close up
248 * any units which may have been left open accidentally.
258 #endif /* NLS_CATALOGS */
259 #endif /* NLS_BUGS */
261 /* suggested by Justin Bur; thanks to Karl Kleinpaste */
264 for (f = 0; f < NOFILE; f++)
265 if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
269 #endif /* MALLOC_TRACE */
275 (void) open(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
281 #endif /* NLS_CATALOGS */
282 #endif /* NLS_BUGS */
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|O_LARGEFILE);
331 * Move descriptor i to j.
332 * If j is -1 then we just want to get i to a safe place,
333 * i.e. to a unit > FSAFE. This also happens in dcopy.
361 if (i == j || i < 0 || (j < 0 && i > FSAFE))
371 return (renum(i, j));
382 if (j == -1 && k > FSAFE)
393 * Left shift a command argument list, discarding
394 * the first c arguments. Used in "shift" commands
395 * as well as by commands like "repeat".
404 for (u = v; *u && --c >= 0; u++)
421 while (*cp && Isdigit(*cp))
431 (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
433 return (blkcpy(nv, v));
436 #ifndef SHORT_STRINGS
448 #endif /* SHORT_STRINGS */
458 while ((*dp++ &= TRIM) != '\0')
483 if (cmap(*s, _META | _DOL | _QF | _QB | _ESC | _GLOB))
496 setname(short2str(name));
497 stderror(ERR_NAME | ERR_UNDVAR);
502 const Char *sub, *str;
510 if ((*sub++ & TRIM) != (*str++ & TRIM))