xref: /csrg-svn/bin/csh/misc.c (revision 50024)
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*50024Schristos static char sccsid[] = "@(#)misc.c	5.10 (Berkeley) 06/07/91";
1047823Sbostic #endif /* not lint */
111300Sbill 
1250023Sbostic #include "csh.h"
1350023Sbostic #include "extern.h"
141300Sbill 
15*50024Schristos static int	renum __P((int, int));
161300Sbill 
1749992Sbostic int
1849992Sbostic any(s, c)
1949992Sbostic     register char *s;
2049992Sbostic     register int c;
2117511Sedward {
2249992Sbostic     if (!s)
2349992Sbostic 	return (0);		/* Check for nil pointer */
2449992Sbostic     while (*s)
2549992Sbostic 	if (*s++ == c)
2649992Sbostic 	    return (1);
2749992Sbostic     return (0);
2817511Sedward }
2917511Sedward 
3049992Sbostic void
3149992Sbostic setzero(cp, i)
3249992Sbostic     char   *cp;
3349992Sbostic     int     i;
3417511Sedward {
3549992Sbostic     if (i != 0)
3649992Sbostic 	do
3749992Sbostic 	    *cp++ = 0;
3849992Sbostic 	while (--i);
3917511Sedward }
4017511Sedward 
4149992Sbostic char   *
4249992Sbostic strsave(s)
4349992Sbostic     register char *s;
4417511Sedward {
4549992Sbostic     char   *n;
4649992Sbostic     register char *p;
4717511Sedward 
4850023Sbostic     if (s == NULL)
4949992Sbostic 	s = "";
5049992Sbostic     for (p = s; *p++;);
5149992Sbostic     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
5249992Sbostic     while (*p++ = *s++);
5349992Sbostic     return (n);
5417511Sedward }
5517511Sedward 
5649992Sbostic Char  **
5749992Sbostic blkend(up)
5849992Sbostic     register Char **up;
591300Sbill {
601300Sbill 
6149992Sbostic     while (*up)
6249992Sbostic 	up++;
6349992Sbostic     return (up);
641300Sbill }
651300Sbill 
6617511Sedward 
6749992Sbostic void
681300Sbill blkpr(av)
6949992Sbostic     register Char **av;
701300Sbill {
711300Sbill 
7249992Sbostic     for (; *av; av++) {
7349992Sbostic 	xprintf("%s", short2str(*av));
7449992Sbostic 	if (av[1])
7549992Sbostic 	    xprintf(" ");
7649992Sbostic     }
771300Sbill }
781300Sbill 
7949992Sbostic int
801300Sbill blklen(av)
8149992Sbostic     register Char **av;
821300Sbill {
8349992Sbostic     register int i = 0;
841300Sbill 
8549992Sbostic     while (*av++)
8649992Sbostic 	i++;
8749992Sbostic     return (i);
881300Sbill }
891300Sbill 
9049992Sbostic Char  **
911300Sbill blkcpy(oav, bv)
9249992Sbostic     Char  **oav;
9349992Sbostic     register Char **bv;
941300Sbill {
9549992Sbostic     register Char **av = oav;
961300Sbill 
9749992Sbostic     while (*av++ = *bv++)
9849992Sbostic 	continue;
9949992Sbostic     return (oav);
1001300Sbill }
1011300Sbill 
10249992Sbostic Char  **
1031300Sbill blkcat(up, vp)
10449992Sbostic     Char  **up, **vp;
1051300Sbill {
1061300Sbill 
10749992Sbostic     (void) blkcpy(blkend(up), vp);
10849992Sbostic     return (up);
1091300Sbill }
1101300Sbill 
11149992Sbostic void
1121300Sbill blkfree(av0)
11349992Sbostic     Char  **av0;
1141300Sbill {
11549992Sbostic     register Char **av = av0;
1161300Sbill 
11749992Sbostic     if (!av0)
11849992Sbostic 	return;
11949992Sbostic     for (; *av; av++)
12049992Sbostic 	xfree((ptr_t) * av);
12149992Sbostic     xfree((ptr_t) av0);
1221300Sbill }
1231300Sbill 
12449992Sbostic Char  **
1251300Sbill saveblk(v)
12649992Sbostic     register Char **v;
1271300Sbill {
12849992Sbostic     register Char **newv =
12949992Sbostic     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
13049992Sbostic     Char  **onewv = newv;
1311300Sbill 
13249992Sbostic     while (*v)
13349992Sbostic 	*newv++ = Strsave(*v++);
13449992Sbostic     return (onewv);
1351300Sbill }
1361300Sbill 
13750011Schristos #ifdef NOTUSED
13849992Sbostic char   *
13949992Sbostic strstr(s, t)
14049992Sbostic     register char *s, *t;
14149992Sbostic {
14249992Sbostic     do {
14349992Sbostic 	register char *ss = s;
14449992Sbostic 	register char *tt = t;
14549992Sbostic 
14649992Sbostic 	do
14749992Sbostic 	    if (*tt == '\0')
14849992Sbostic 		return (s);
14949992Sbostic 	while (*ss++ == *tt++);
15049992Sbostic     } while (*s++ != '\0');
151*50024Schristos     return (NULL);
15249992Sbostic }
15349992Sbostic 
15450011Schristos #endif /* NOTUSED */
15549992Sbostic 
15649992Sbostic #ifndef SHORT_STRINGS
15749992Sbostic char   *
1581300Sbill strspl(cp, dp)
15949992Sbostic     char   *cp, *dp;
1601300Sbill {
16149992Sbostic     char   *ep;
16249992Sbostic     register char *p, *q;
1631300Sbill 
16449992Sbostic     if (!cp)
16549992Sbostic 	cp = "";
16649992Sbostic     if (!dp)
16749992Sbostic 	dp = "";
16849992Sbostic     for (p = cp; *p++;);
16949992Sbostic     for (q = dp; *q++;);
17049992Sbostic     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
17149992Sbostic     for (p = ep, q = cp; *p++ = *q++;);
17249992Sbostic     for (p--, q = dp; *p++ = *q++;);
17349992Sbostic     return (ep);
1741300Sbill }
1751300Sbill 
17649992Sbostic #endif
17749992Sbostic 
17849992Sbostic Char  **
1791300Sbill blkspl(up, vp)
18049992Sbostic     register Char **up, **vp;
1811300Sbill {
18249992Sbostic     register Char **wp =
18349992Sbostic     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
18449992Sbostic 		      sizeof(Char **));
1851300Sbill 
18649992Sbostic     (void) blkcpy(wp, up);
18749992Sbostic     return (blkcat(wp, vp));
1881300Sbill }
1891300Sbill 
19049992Sbostic Char
1911300Sbill lastchr(cp)
19249992Sbostic     register Char *cp;
1931300Sbill {
1941300Sbill 
19549992Sbostic     if (!cp)
19649992Sbostic 	return (0);
19749992Sbostic     if (!*cp)
19849992Sbostic 	return (0);
19949992Sbostic     while (cp[1])
20049992Sbostic 	cp++;
20149992Sbostic     return (*cp);
2021300Sbill }
2031300Sbill 
2041300Sbill /*
2051300Sbill  * This routine is called after an error to close up
2061300Sbill  * any units which may have been left open accidentally.
2071300Sbill  */
20849992Sbostic void
2091300Sbill closem()
2101300Sbill {
21149992Sbostic     register int f;
2121300Sbill 
21349992Sbostic     for (f = 0; f < NOFILE; f++)
21449992Sbostic 	if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
21549992Sbostic 	    f != FSHTTY)
21649992Sbostic 	    (void) close(f);
2171300Sbill }
2181300Sbill 
21949992Sbostic void
2201300Sbill donefds()
2211300Sbill {
2221300Sbill 
22349992Sbostic     (void) close(0);
22449992Sbostic     (void) close(1);
22549992Sbostic     (void) close(2);
22649992Sbostic     didfds = 0;
2271300Sbill }
2281300Sbill 
2291300Sbill /*
2301300Sbill  * Move descriptor i to j.
2311300Sbill  * If j is -1 then we just want to get i to a safe place,
2321300Sbill  * i.e. to a unit > 2.  This also happens in dcopy.
2331300Sbill  */
23449992Sbostic int
2351300Sbill dmove(i, j)
23649992Sbostic     register int i, j;
2371300Sbill {
2381300Sbill 
23949992Sbostic     if (i == j || i < 0)
24049992Sbostic 	return (i);
24149992Sbostic     if (j >= 0) {
24249992Sbostic 	(void) dup2(i, j);
2431300Sbill 	return (j);
24449992Sbostic     }
24549992Sbostic     j = dcopy(i, j);
24649992Sbostic     if (j != i)
24749992Sbostic 	(void) close(i);
24849992Sbostic     return (j);
2491300Sbill }
2501300Sbill 
25149992Sbostic int
2521300Sbill dcopy(i, j)
25349992Sbostic     register int i, j;
2541300Sbill {
2551300Sbill 
25649992Sbostic     if (i == j || i < 0 || j < 0 && i > 2)
25749992Sbostic 	return (i);
25849992Sbostic     if (j >= 0) {
25949992Sbostic 	(void) dup2(i, j);
26049992Sbostic 	return (j);
26149992Sbostic     }
26249992Sbostic     (void) close(j);
26349992Sbostic     return (renum(i, j));
2641300Sbill }
2651300Sbill 
26649992Sbostic static int
2671300Sbill renum(i, j)
26849992Sbostic     register int i, j;
2691300Sbill {
27049992Sbostic     register int k = dup(i);
2711300Sbill 
27249992Sbostic     if (k < 0)
27349992Sbostic 	return (-1);
27449992Sbostic     if (j == -1 && k > 2)
2751300Sbill 	return (k);
27649992Sbostic     if (k != j) {
27749992Sbostic 	j = renum(k, j);
27849992Sbostic 	(void) close(k);
27949992Sbostic 	return (j);
28049992Sbostic     }
28149992Sbostic     return (k);
2821300Sbill }
2831300Sbill 
2841300Sbill /*
2851300Sbill  * Left shift a command argument list, discarding
2861300Sbill  * the first c arguments.  Used in "shift" commands
2871300Sbill  * as well as by commands like "repeat".
2881300Sbill  */
28949992Sbostic void
2901300Sbill lshift(v, c)
29149992Sbostic     register Char **v;
29249992Sbostic     register int c;
2931300Sbill {
29449992Sbostic     register Char **u = v;
2951300Sbill 
29649992Sbostic     while (*u && --c >= 0)
29749992Sbostic 	xfree((ptr_t) * u++);
29849992Sbostic     (void) blkcpy(v, u);
2991300Sbill }
3001300Sbill 
30149992Sbostic int
3021300Sbill number(cp)
30349992Sbostic     Char   *cp;
3041300Sbill {
30549992Sbostic     if (!cp)
30649992Sbostic 	return(0);
30749992Sbostic     if (*cp == '-') {
30849992Sbostic 	cp++;
30949992Sbostic 	if (!Isdigit(*cp))
31049992Sbostic 	    return (0);
31149992Sbostic 	cp++;
31249992Sbostic     }
31349992Sbostic     while (*cp && Isdigit(*cp))
31449992Sbostic 	cp++;
31549992Sbostic     return (*cp == 0);
3161300Sbill }
3171300Sbill 
31849992Sbostic Char  **
3191300Sbill copyblk(v)
32049992Sbostic     register Char **v;
3211300Sbill {
32249992Sbostic     Char  **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
3231300Sbill 
32449992Sbostic     return (blkcpy(nv, v));
3251300Sbill }
3261300Sbill 
32749992Sbostic #ifndef SHORT_STRINGS
32849992Sbostic char   *
3291300Sbill strend(cp)
33049992Sbostic     register char *cp;
3311300Sbill {
33249992Sbostic     if (!cp)
3331300Sbill 	return (cp);
33449992Sbostic     while (*cp)
33549992Sbostic 	cp++;
33649992Sbostic     return (cp);
3371300Sbill }
3381300Sbill 
33949992Sbostic #endif				/* SHORT_STRINGS */
34049992Sbostic 
34149992Sbostic Char   *
3421300Sbill strip(cp)
34349992Sbostic     Char   *cp;
3441300Sbill {
34549992Sbostic     register Char *dp = cp;
3461300Sbill 
34749992Sbostic     if (!cp)
3481300Sbill 	return (cp);
34949992Sbostic     while (*dp++ &= TRIM)
35049992Sbostic 	continue;
35149992Sbostic     return (cp);
3521300Sbill }
3531300Sbill 
35449992Sbostic void
3551300Sbill udvar(name)
35649992Sbostic     Char   *name;
3571300Sbill {
3581300Sbill 
35949992Sbostic     setname(short2str(name));
36049992Sbostic     stderror(ERR_NAME | ERR_UNDVAR);
3611300Sbill }
3621300Sbill 
36349992Sbostic int
3641300Sbill prefix(sub, str)
36549992Sbostic     register Char *sub, *str;
3661300Sbill {
3671300Sbill 
36849992Sbostic     for (;;) {
36949992Sbostic 	if (*sub == 0)
37049992Sbostic 	    return (1);
37149992Sbostic 	if (*str == 0)
37249992Sbostic 	    return (0);
37349992Sbostic 	if (*sub++ != *str++)
37449992Sbostic 	    return (0);
37549992Sbostic     }
3761300Sbill }
377