xref: /csrg-svn/bin/csh/misc.c (revision 50033)
147823Sbostic /*-
247823Sbostic  * Copyright (c) 1980, 1991 The Regents of the University of California.
347823Sbostic  * All rights reserved.
447823Sbostic  *
547823Sbostic  * %sccs.include.redist.c%
621938Sdist  */
721938Sdist 
817511Sedward #ifndef lint
9*50033Schristos static char sccsid[] = "@(#)misc.c	5.12 (Berkeley) 06/08/91";
1047823Sbostic #endif /* not lint */
111300Sbill 
1250028Sbostic #include <sys/param.h>
1350028Sbostic #include <stdlib.h>
1450028Sbostic #include <unistd.h>
15*50033Schristos #if __STDC__
16*50033Schristos # include <stdarg.h>
17*50033Schristos #else
18*50033Schristos # include <varargs.h>
19*50033Schristos #endif
20*50033Schristos 
2150023Sbostic #include "csh.h"
2250023Sbostic #include "extern.h"
231300Sbill 
2450024Schristos static int	renum __P((int, int));
251300Sbill 
2649992Sbostic int
2749992Sbostic any(s, c)
2849992Sbostic     register char *s;
2949992Sbostic     register int c;
3017511Sedward {
3149992Sbostic     if (!s)
3249992Sbostic 	return (0);		/* Check for nil pointer */
3349992Sbostic     while (*s)
3449992Sbostic 	if (*s++ == c)
3549992Sbostic 	    return (1);
3649992Sbostic     return (0);
3717511Sedward }
3817511Sedward 
3949992Sbostic void
4049992Sbostic setzero(cp, i)
4149992Sbostic     char   *cp;
4249992Sbostic     int     i;
4317511Sedward {
4449992Sbostic     if (i != 0)
4549992Sbostic 	do
4649992Sbostic 	    *cp++ = 0;
4749992Sbostic 	while (--i);
4817511Sedward }
4917511Sedward 
5049992Sbostic char   *
5149992Sbostic strsave(s)
5249992Sbostic     register char *s;
5317511Sedward {
5449992Sbostic     char   *n;
5549992Sbostic     register char *p;
5617511Sedward 
5750023Sbostic     if (s == NULL)
5849992Sbostic 	s = "";
5949992Sbostic     for (p = s; *p++;);
6049992Sbostic     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
6149992Sbostic     while (*p++ = *s++);
6249992Sbostic     return (n);
6317511Sedward }
6417511Sedward 
6549992Sbostic Char  **
6649992Sbostic blkend(up)
6749992Sbostic     register Char **up;
681300Sbill {
691300Sbill 
7049992Sbostic     while (*up)
7149992Sbostic 	up++;
7249992Sbostic     return (up);
731300Sbill }
741300Sbill 
7517511Sedward 
7649992Sbostic void
771300Sbill blkpr(av)
7849992Sbostic     register Char **av;
791300Sbill {
801300Sbill 
8149992Sbostic     for (; *av; av++) {
8249992Sbostic 	xprintf("%s", short2str(*av));
8349992Sbostic 	if (av[1])
8449992Sbostic 	    xprintf(" ");
8549992Sbostic     }
861300Sbill }
871300Sbill 
8849992Sbostic int
891300Sbill blklen(av)
9049992Sbostic     register Char **av;
911300Sbill {
9249992Sbostic     register int i = 0;
931300Sbill 
9449992Sbostic     while (*av++)
9549992Sbostic 	i++;
9649992Sbostic     return (i);
971300Sbill }
981300Sbill 
9949992Sbostic Char  **
1001300Sbill blkcpy(oav, bv)
10149992Sbostic     Char  **oav;
10249992Sbostic     register Char **bv;
1031300Sbill {
10449992Sbostic     register Char **av = oav;
1051300Sbill 
10649992Sbostic     while (*av++ = *bv++)
10749992Sbostic 	continue;
10849992Sbostic     return (oav);
1091300Sbill }
1101300Sbill 
11149992Sbostic Char  **
1121300Sbill blkcat(up, vp)
11349992Sbostic     Char  **up, **vp;
1141300Sbill {
1151300Sbill 
11649992Sbostic     (void) blkcpy(blkend(up), vp);
11749992Sbostic     return (up);
1181300Sbill }
1191300Sbill 
12049992Sbostic void
1211300Sbill blkfree(av0)
12249992Sbostic     Char  **av0;
1231300Sbill {
12449992Sbostic     register Char **av = av0;
1251300Sbill 
12649992Sbostic     if (!av0)
12749992Sbostic 	return;
12849992Sbostic     for (; *av; av++)
12949992Sbostic 	xfree((ptr_t) * av);
13049992Sbostic     xfree((ptr_t) av0);
1311300Sbill }
1321300Sbill 
13349992Sbostic Char  **
1341300Sbill saveblk(v)
13549992Sbostic     register Char **v;
1361300Sbill {
13749992Sbostic     register Char **newv =
13849992Sbostic     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
13949992Sbostic     Char  **onewv = newv;
1401300Sbill 
14149992Sbostic     while (*v)
14249992Sbostic 	*newv++ = Strsave(*v++);
14349992Sbostic     return (onewv);
1441300Sbill }
1451300Sbill 
14650011Schristos #ifdef NOTUSED
14749992Sbostic char   *
14849992Sbostic strstr(s, t)
14949992Sbostic     register char *s, *t;
15049992Sbostic {
15149992Sbostic     do {
15249992Sbostic 	register char *ss = s;
15349992Sbostic 	register char *tt = t;
15449992Sbostic 
15549992Sbostic 	do
15649992Sbostic 	    if (*tt == '\0')
15749992Sbostic 		return (s);
15849992Sbostic 	while (*ss++ == *tt++);
15949992Sbostic     } while (*s++ != '\0');
16050024Schristos     return (NULL);
16149992Sbostic }
16249992Sbostic 
16350011Schristos #endif /* NOTUSED */
16449992Sbostic 
16549992Sbostic #ifndef SHORT_STRINGS
16649992Sbostic char   *
1671300Sbill strspl(cp, dp)
16849992Sbostic     char   *cp, *dp;
1691300Sbill {
17049992Sbostic     char   *ep;
17149992Sbostic     register char *p, *q;
1721300Sbill 
17349992Sbostic     if (!cp)
17449992Sbostic 	cp = "";
17549992Sbostic     if (!dp)
17649992Sbostic 	dp = "";
17749992Sbostic     for (p = cp; *p++;);
17849992Sbostic     for (q = dp; *q++;);
17949992Sbostic     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
18049992Sbostic     for (p = ep, q = cp; *p++ = *q++;);
18149992Sbostic     for (p--, q = dp; *p++ = *q++;);
18249992Sbostic     return (ep);
1831300Sbill }
1841300Sbill 
18549992Sbostic #endif
18649992Sbostic 
18749992Sbostic Char  **
1881300Sbill blkspl(up, vp)
18949992Sbostic     register Char **up, **vp;
1901300Sbill {
19149992Sbostic     register Char **wp =
19249992Sbostic     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
19349992Sbostic 		      sizeof(Char **));
1941300Sbill 
19549992Sbostic     (void) blkcpy(wp, up);
19649992Sbostic     return (blkcat(wp, vp));
1971300Sbill }
1981300Sbill 
19949992Sbostic Char
2001300Sbill lastchr(cp)
20149992Sbostic     register Char *cp;
2021300Sbill {
2031300Sbill 
20449992Sbostic     if (!cp)
20549992Sbostic 	return (0);
20649992Sbostic     if (!*cp)
20749992Sbostic 	return (0);
20849992Sbostic     while (cp[1])
20949992Sbostic 	cp++;
21049992Sbostic     return (*cp);
2111300Sbill }
2121300Sbill 
2131300Sbill /*
2141300Sbill  * This routine is called after an error to close up
2151300Sbill  * any units which may have been left open accidentally.
2161300Sbill  */
21749992Sbostic void
2181300Sbill closem()
2191300Sbill {
22049992Sbostic     register int f;
2211300Sbill 
22249992Sbostic     for (f = 0; f < NOFILE; f++)
22349992Sbostic 	if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
22449992Sbostic 	    f != FSHTTY)
22549992Sbostic 	    (void) close(f);
2261300Sbill }
2271300Sbill 
22849992Sbostic void
2291300Sbill donefds()
2301300Sbill {
2311300Sbill 
23249992Sbostic     (void) close(0);
23349992Sbostic     (void) close(1);
23449992Sbostic     (void) close(2);
23549992Sbostic     didfds = 0;
2361300Sbill }
2371300Sbill 
2381300Sbill /*
2391300Sbill  * Move descriptor i to j.
2401300Sbill  * If j is -1 then we just want to get i to a safe place,
2411300Sbill  * i.e. to a unit > 2.  This also happens in dcopy.
2421300Sbill  */
24349992Sbostic int
2441300Sbill dmove(i, j)
24549992Sbostic     register int i, j;
2461300Sbill {
2471300Sbill 
24849992Sbostic     if (i == j || i < 0)
24949992Sbostic 	return (i);
25049992Sbostic     if (j >= 0) {
25149992Sbostic 	(void) dup2(i, j);
2521300Sbill 	return (j);
25349992Sbostic     }
25449992Sbostic     j = dcopy(i, j);
25549992Sbostic     if (j != i)
25649992Sbostic 	(void) close(i);
25749992Sbostic     return (j);
2581300Sbill }
2591300Sbill 
26049992Sbostic int
2611300Sbill dcopy(i, j)
26249992Sbostic     register int i, j;
2631300Sbill {
2641300Sbill 
26549992Sbostic     if (i == j || i < 0 || j < 0 && i > 2)
26649992Sbostic 	return (i);
26749992Sbostic     if (j >= 0) {
26849992Sbostic 	(void) dup2(i, j);
26949992Sbostic 	return (j);
27049992Sbostic     }
27149992Sbostic     (void) close(j);
27249992Sbostic     return (renum(i, j));
2731300Sbill }
2741300Sbill 
27549992Sbostic static int
2761300Sbill renum(i, j)
27749992Sbostic     register int i, j;
2781300Sbill {
27949992Sbostic     register int k = dup(i);
2801300Sbill 
28149992Sbostic     if (k < 0)
28249992Sbostic 	return (-1);
28349992Sbostic     if (j == -1 && k > 2)
2841300Sbill 	return (k);
28549992Sbostic     if (k != j) {
28649992Sbostic 	j = renum(k, j);
28749992Sbostic 	(void) close(k);
28849992Sbostic 	return (j);
28949992Sbostic     }
29049992Sbostic     return (k);
2911300Sbill }
2921300Sbill 
2931300Sbill /*
2941300Sbill  * Left shift a command argument list, discarding
2951300Sbill  * the first c arguments.  Used in "shift" commands
2961300Sbill  * as well as by commands like "repeat".
2971300Sbill  */
29849992Sbostic void
2991300Sbill lshift(v, c)
30049992Sbostic     register Char **v;
30149992Sbostic     register int c;
3021300Sbill {
30349992Sbostic     register Char **u = v;
3041300Sbill 
30549992Sbostic     while (*u && --c >= 0)
30649992Sbostic 	xfree((ptr_t) * u++);
30749992Sbostic     (void) blkcpy(v, u);
3081300Sbill }
3091300Sbill 
31049992Sbostic int
3111300Sbill number(cp)
31249992Sbostic     Char   *cp;
3131300Sbill {
31449992Sbostic     if (!cp)
31549992Sbostic 	return(0);
31649992Sbostic     if (*cp == '-') {
31749992Sbostic 	cp++;
31849992Sbostic 	if (!Isdigit(*cp))
31949992Sbostic 	    return (0);
32049992Sbostic 	cp++;
32149992Sbostic     }
32249992Sbostic     while (*cp && Isdigit(*cp))
32349992Sbostic 	cp++;
32449992Sbostic     return (*cp == 0);
3251300Sbill }
3261300Sbill 
32749992Sbostic Char  **
3281300Sbill copyblk(v)
32949992Sbostic     register Char **v;
3301300Sbill {
33149992Sbostic     Char  **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
3321300Sbill 
33349992Sbostic     return (blkcpy(nv, v));
3341300Sbill }
3351300Sbill 
33649992Sbostic #ifndef SHORT_STRINGS
33749992Sbostic char   *
3381300Sbill strend(cp)
33949992Sbostic     register char *cp;
3401300Sbill {
34149992Sbostic     if (!cp)
3421300Sbill 	return (cp);
34349992Sbostic     while (*cp)
34449992Sbostic 	cp++;
34549992Sbostic     return (cp);
3461300Sbill }
3471300Sbill 
34849992Sbostic #endif				/* SHORT_STRINGS */
34949992Sbostic 
35049992Sbostic Char   *
3511300Sbill strip(cp)
35249992Sbostic     Char   *cp;
3531300Sbill {
35449992Sbostic     register Char *dp = cp;
3551300Sbill 
35649992Sbostic     if (!cp)
3571300Sbill 	return (cp);
35849992Sbostic     while (*dp++ &= TRIM)
35949992Sbostic 	continue;
36049992Sbostic     return (cp);
3611300Sbill }
3621300Sbill 
36349992Sbostic void
3641300Sbill udvar(name)
36549992Sbostic     Char   *name;
3661300Sbill {
3671300Sbill 
36849992Sbostic     setname(short2str(name));
36949992Sbostic     stderror(ERR_NAME | ERR_UNDVAR);
3701300Sbill }
3711300Sbill 
37249992Sbostic int
3731300Sbill prefix(sub, str)
37449992Sbostic     register Char *sub, *str;
3751300Sbill {
3761300Sbill 
37749992Sbostic     for (;;) {
37849992Sbostic 	if (*sub == 0)
37949992Sbostic 	    return (1);
38049992Sbostic 	if (*str == 0)
38149992Sbostic 	    return (0);
38249992Sbostic 	if (*sub++ != *str++)
38349992Sbostic 	    return (0);
38449992Sbostic     }
3851300Sbill }
386