xref: /csrg-svn/bin/csh/misc.c (revision 50028)
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*50028Sbostic static char sccsid[] = "@(#)misc.c	5.11 (Berkeley) 06/08/91";
1047823Sbostic #endif /* not lint */
111300Sbill 
12*50028Sbostic #include <sys/param.h>
13*50028Sbostic #include <stdlib.h>
14*50028Sbostic #include <unistd.h>
1550023Sbostic #include "csh.h"
1650023Sbostic #include "extern.h"
171300Sbill 
1850024Schristos static int	renum __P((int, int));
191300Sbill 
2049992Sbostic int
2149992Sbostic any(s, c)
2249992Sbostic     register char *s;
2349992Sbostic     register int c;
2417511Sedward {
2549992Sbostic     if (!s)
2649992Sbostic 	return (0);		/* Check for nil pointer */
2749992Sbostic     while (*s)
2849992Sbostic 	if (*s++ == c)
2949992Sbostic 	    return (1);
3049992Sbostic     return (0);
3117511Sedward }
3217511Sedward 
3349992Sbostic void
3449992Sbostic setzero(cp, i)
3549992Sbostic     char   *cp;
3649992Sbostic     int     i;
3717511Sedward {
3849992Sbostic     if (i != 0)
3949992Sbostic 	do
4049992Sbostic 	    *cp++ = 0;
4149992Sbostic 	while (--i);
4217511Sedward }
4317511Sedward 
4449992Sbostic char   *
4549992Sbostic strsave(s)
4649992Sbostic     register char *s;
4717511Sedward {
4849992Sbostic     char   *n;
4949992Sbostic     register char *p;
5017511Sedward 
5150023Sbostic     if (s == NULL)
5249992Sbostic 	s = "";
5349992Sbostic     for (p = s; *p++;);
5449992Sbostic     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
5549992Sbostic     while (*p++ = *s++);
5649992Sbostic     return (n);
5717511Sedward }
5817511Sedward 
5949992Sbostic Char  **
6049992Sbostic blkend(up)
6149992Sbostic     register Char **up;
621300Sbill {
631300Sbill 
6449992Sbostic     while (*up)
6549992Sbostic 	up++;
6649992Sbostic     return (up);
671300Sbill }
681300Sbill 
6917511Sedward 
7049992Sbostic void
711300Sbill blkpr(av)
7249992Sbostic     register Char **av;
731300Sbill {
741300Sbill 
7549992Sbostic     for (; *av; av++) {
7649992Sbostic 	xprintf("%s", short2str(*av));
7749992Sbostic 	if (av[1])
7849992Sbostic 	    xprintf(" ");
7949992Sbostic     }
801300Sbill }
811300Sbill 
8249992Sbostic int
831300Sbill blklen(av)
8449992Sbostic     register Char **av;
851300Sbill {
8649992Sbostic     register int i = 0;
871300Sbill 
8849992Sbostic     while (*av++)
8949992Sbostic 	i++;
9049992Sbostic     return (i);
911300Sbill }
921300Sbill 
9349992Sbostic Char  **
941300Sbill blkcpy(oav, bv)
9549992Sbostic     Char  **oav;
9649992Sbostic     register Char **bv;
971300Sbill {
9849992Sbostic     register Char **av = oav;
991300Sbill 
10049992Sbostic     while (*av++ = *bv++)
10149992Sbostic 	continue;
10249992Sbostic     return (oav);
1031300Sbill }
1041300Sbill 
10549992Sbostic Char  **
1061300Sbill blkcat(up, vp)
10749992Sbostic     Char  **up, **vp;
1081300Sbill {
1091300Sbill 
11049992Sbostic     (void) blkcpy(blkend(up), vp);
11149992Sbostic     return (up);
1121300Sbill }
1131300Sbill 
11449992Sbostic void
1151300Sbill blkfree(av0)
11649992Sbostic     Char  **av0;
1171300Sbill {
11849992Sbostic     register Char **av = av0;
1191300Sbill 
12049992Sbostic     if (!av0)
12149992Sbostic 	return;
12249992Sbostic     for (; *av; av++)
12349992Sbostic 	xfree((ptr_t) * av);
12449992Sbostic     xfree((ptr_t) av0);
1251300Sbill }
1261300Sbill 
12749992Sbostic Char  **
1281300Sbill saveblk(v)
12949992Sbostic     register Char **v;
1301300Sbill {
13149992Sbostic     register Char **newv =
13249992Sbostic     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
13349992Sbostic     Char  **onewv = newv;
1341300Sbill 
13549992Sbostic     while (*v)
13649992Sbostic 	*newv++ = Strsave(*v++);
13749992Sbostic     return (onewv);
1381300Sbill }
1391300Sbill 
14050011Schristos #ifdef NOTUSED
14149992Sbostic char   *
14249992Sbostic strstr(s, t)
14349992Sbostic     register char *s, *t;
14449992Sbostic {
14549992Sbostic     do {
14649992Sbostic 	register char *ss = s;
14749992Sbostic 	register char *tt = t;
14849992Sbostic 
14949992Sbostic 	do
15049992Sbostic 	    if (*tt == '\0')
15149992Sbostic 		return (s);
15249992Sbostic 	while (*ss++ == *tt++);
15349992Sbostic     } while (*s++ != '\0');
15450024Schristos     return (NULL);
15549992Sbostic }
15649992Sbostic 
15750011Schristos #endif /* NOTUSED */
15849992Sbostic 
15949992Sbostic #ifndef SHORT_STRINGS
16049992Sbostic char   *
1611300Sbill strspl(cp, dp)
16249992Sbostic     char   *cp, *dp;
1631300Sbill {
16449992Sbostic     char   *ep;
16549992Sbostic     register char *p, *q;
1661300Sbill 
16749992Sbostic     if (!cp)
16849992Sbostic 	cp = "";
16949992Sbostic     if (!dp)
17049992Sbostic 	dp = "";
17149992Sbostic     for (p = cp; *p++;);
17249992Sbostic     for (q = dp; *q++;);
17349992Sbostic     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
17449992Sbostic     for (p = ep, q = cp; *p++ = *q++;);
17549992Sbostic     for (p--, q = dp; *p++ = *q++;);
17649992Sbostic     return (ep);
1771300Sbill }
1781300Sbill 
17949992Sbostic #endif
18049992Sbostic 
18149992Sbostic Char  **
1821300Sbill blkspl(up, vp)
18349992Sbostic     register Char **up, **vp;
1841300Sbill {
18549992Sbostic     register Char **wp =
18649992Sbostic     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
18749992Sbostic 		      sizeof(Char **));
1881300Sbill 
18949992Sbostic     (void) blkcpy(wp, up);
19049992Sbostic     return (blkcat(wp, vp));
1911300Sbill }
1921300Sbill 
19349992Sbostic Char
1941300Sbill lastchr(cp)
19549992Sbostic     register Char *cp;
1961300Sbill {
1971300Sbill 
19849992Sbostic     if (!cp)
19949992Sbostic 	return (0);
20049992Sbostic     if (!*cp)
20149992Sbostic 	return (0);
20249992Sbostic     while (cp[1])
20349992Sbostic 	cp++;
20449992Sbostic     return (*cp);
2051300Sbill }
2061300Sbill 
2071300Sbill /*
2081300Sbill  * This routine is called after an error to close up
2091300Sbill  * any units which may have been left open accidentally.
2101300Sbill  */
21149992Sbostic void
2121300Sbill closem()
2131300Sbill {
21449992Sbostic     register int f;
2151300Sbill 
21649992Sbostic     for (f = 0; f < NOFILE; f++)
21749992Sbostic 	if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
21849992Sbostic 	    f != FSHTTY)
21949992Sbostic 	    (void) close(f);
2201300Sbill }
2211300Sbill 
22249992Sbostic void
2231300Sbill donefds()
2241300Sbill {
2251300Sbill 
22649992Sbostic     (void) close(0);
22749992Sbostic     (void) close(1);
22849992Sbostic     (void) close(2);
22949992Sbostic     didfds = 0;
2301300Sbill }
2311300Sbill 
2321300Sbill /*
2331300Sbill  * Move descriptor i to j.
2341300Sbill  * If j is -1 then we just want to get i to a safe place,
2351300Sbill  * i.e. to a unit > 2.  This also happens in dcopy.
2361300Sbill  */
23749992Sbostic int
2381300Sbill dmove(i, j)
23949992Sbostic     register int i, j;
2401300Sbill {
2411300Sbill 
24249992Sbostic     if (i == j || i < 0)
24349992Sbostic 	return (i);
24449992Sbostic     if (j >= 0) {
24549992Sbostic 	(void) dup2(i, j);
2461300Sbill 	return (j);
24749992Sbostic     }
24849992Sbostic     j = dcopy(i, j);
24949992Sbostic     if (j != i)
25049992Sbostic 	(void) close(i);
25149992Sbostic     return (j);
2521300Sbill }
2531300Sbill 
25449992Sbostic int
2551300Sbill dcopy(i, j)
25649992Sbostic     register int i, j;
2571300Sbill {
2581300Sbill 
25949992Sbostic     if (i == j || i < 0 || j < 0 && i > 2)
26049992Sbostic 	return (i);
26149992Sbostic     if (j >= 0) {
26249992Sbostic 	(void) dup2(i, j);
26349992Sbostic 	return (j);
26449992Sbostic     }
26549992Sbostic     (void) close(j);
26649992Sbostic     return (renum(i, j));
2671300Sbill }
2681300Sbill 
26949992Sbostic static int
2701300Sbill renum(i, j)
27149992Sbostic     register int i, j;
2721300Sbill {
27349992Sbostic     register int k = dup(i);
2741300Sbill 
27549992Sbostic     if (k < 0)
27649992Sbostic 	return (-1);
27749992Sbostic     if (j == -1 && k > 2)
2781300Sbill 	return (k);
27949992Sbostic     if (k != j) {
28049992Sbostic 	j = renum(k, j);
28149992Sbostic 	(void) close(k);
28249992Sbostic 	return (j);
28349992Sbostic     }
28449992Sbostic     return (k);
2851300Sbill }
2861300Sbill 
2871300Sbill /*
2881300Sbill  * Left shift a command argument list, discarding
2891300Sbill  * the first c arguments.  Used in "shift" commands
2901300Sbill  * as well as by commands like "repeat".
2911300Sbill  */
29249992Sbostic void
2931300Sbill lshift(v, c)
29449992Sbostic     register Char **v;
29549992Sbostic     register int c;
2961300Sbill {
29749992Sbostic     register Char **u = v;
2981300Sbill 
29949992Sbostic     while (*u && --c >= 0)
30049992Sbostic 	xfree((ptr_t) * u++);
30149992Sbostic     (void) blkcpy(v, u);
3021300Sbill }
3031300Sbill 
30449992Sbostic int
3051300Sbill number(cp)
30649992Sbostic     Char   *cp;
3071300Sbill {
30849992Sbostic     if (!cp)
30949992Sbostic 	return(0);
31049992Sbostic     if (*cp == '-') {
31149992Sbostic 	cp++;
31249992Sbostic 	if (!Isdigit(*cp))
31349992Sbostic 	    return (0);
31449992Sbostic 	cp++;
31549992Sbostic     }
31649992Sbostic     while (*cp && Isdigit(*cp))
31749992Sbostic 	cp++;
31849992Sbostic     return (*cp == 0);
3191300Sbill }
3201300Sbill 
32149992Sbostic Char  **
3221300Sbill copyblk(v)
32349992Sbostic     register Char **v;
3241300Sbill {
32549992Sbostic     Char  **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
3261300Sbill 
32749992Sbostic     return (blkcpy(nv, v));
3281300Sbill }
3291300Sbill 
33049992Sbostic #ifndef SHORT_STRINGS
33149992Sbostic char   *
3321300Sbill strend(cp)
33349992Sbostic     register char *cp;
3341300Sbill {
33549992Sbostic     if (!cp)
3361300Sbill 	return (cp);
33749992Sbostic     while (*cp)
33849992Sbostic 	cp++;
33949992Sbostic     return (cp);
3401300Sbill }
3411300Sbill 
34249992Sbostic #endif				/* SHORT_STRINGS */
34349992Sbostic 
34449992Sbostic Char   *
3451300Sbill strip(cp)
34649992Sbostic     Char   *cp;
3471300Sbill {
34849992Sbostic     register Char *dp = cp;
3491300Sbill 
35049992Sbostic     if (!cp)
3511300Sbill 	return (cp);
35249992Sbostic     while (*dp++ &= TRIM)
35349992Sbostic 	continue;
35449992Sbostic     return (cp);
3551300Sbill }
3561300Sbill 
35749992Sbostic void
3581300Sbill udvar(name)
35949992Sbostic     Char   *name;
3601300Sbill {
3611300Sbill 
36249992Sbostic     setname(short2str(name));
36349992Sbostic     stderror(ERR_NAME | ERR_UNDVAR);
3641300Sbill }
3651300Sbill 
36649992Sbostic int
3671300Sbill prefix(sub, str)
36849992Sbostic     register Char *sub, *str;
3691300Sbill {
3701300Sbill 
37149992Sbostic     for (;;) {
37249992Sbostic 	if (*sub == 0)
37349992Sbostic 	    return (1);
37449992Sbostic 	if (*str == 0)
37549992Sbostic 	    return (0);
37649992Sbostic 	if (*sub++ != *str++)
37749992Sbostic 	    return (0);
37849992Sbostic     }
3791300Sbill }
380