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*50011Schristos static char sccsid[] = "@(#)misc.c 5.8 (Berkeley) 06/05/91"; 1047823Sbostic #endif /* not lint */ 111300Sbill 121300Sbill #include "sh.h" 131300Sbill 1449992Sbostic static int renum(); 151300Sbill 1649992Sbostic int 1749992Sbostic any(s, c) 1849992Sbostic register char *s; 1949992Sbostic register int c; 2017511Sedward { 2149992Sbostic if (!s) 2249992Sbostic return (0); /* Check for nil pointer */ 2349992Sbostic while (*s) 2449992Sbostic if (*s++ == c) 2549992Sbostic return (1); 2649992Sbostic return (0); 2717511Sedward } 2817511Sedward 2949992Sbostic void 3049992Sbostic setzero(cp, i) 3149992Sbostic char *cp; 3249992Sbostic int i; 3317511Sedward { 3449992Sbostic if (i != 0) 3549992Sbostic do 3649992Sbostic *cp++ = 0; 3749992Sbostic while (--i); 3817511Sedward } 3917511Sedward 4049992Sbostic char * 4149992Sbostic strsave(s) 4249992Sbostic register char *s; 4317511Sedward { 4449992Sbostic char *n; 4549992Sbostic register char *p; 4617511Sedward 4749992Sbostic if (s == 0) 4849992Sbostic s = ""; 4949992Sbostic for (p = s; *p++;); 5049992Sbostic n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char))); 5149992Sbostic while (*p++ = *s++); 5249992Sbostic return (n); 5317511Sedward } 5417511Sedward 5549992Sbostic Char ** 5649992Sbostic blkend(up) 5749992Sbostic register Char **up; 581300Sbill { 591300Sbill 6049992Sbostic while (*up) 6149992Sbostic up++; 6249992Sbostic return (up); 631300Sbill } 641300Sbill 6517511Sedward 6649992Sbostic void 671300Sbill blkpr(av) 6849992Sbostic register Char **av; 691300Sbill { 701300Sbill 7149992Sbostic for (; *av; av++) { 7249992Sbostic xprintf("%s", short2str(*av)); 7349992Sbostic if (av[1]) 7449992Sbostic xprintf(" "); 7549992Sbostic } 761300Sbill } 771300Sbill 7849992Sbostic int 791300Sbill blklen(av) 8049992Sbostic register Char **av; 811300Sbill { 8249992Sbostic register int i = 0; 831300Sbill 8449992Sbostic while (*av++) 8549992Sbostic i++; 8649992Sbostic return (i); 871300Sbill } 881300Sbill 8949992Sbostic Char ** 901300Sbill blkcpy(oav, bv) 9149992Sbostic Char **oav; 9249992Sbostic register Char **bv; 931300Sbill { 9449992Sbostic register Char **av = oav; 951300Sbill 9649992Sbostic while (*av++ = *bv++) 9749992Sbostic continue; 9849992Sbostic return (oav); 991300Sbill } 1001300Sbill 10149992Sbostic Char ** 1021300Sbill blkcat(up, vp) 10349992Sbostic Char **up, **vp; 1041300Sbill { 1051300Sbill 10649992Sbostic (void) blkcpy(blkend(up), vp); 10749992Sbostic return (up); 1081300Sbill } 1091300Sbill 11049992Sbostic void 1111300Sbill blkfree(av0) 11249992Sbostic Char **av0; 1131300Sbill { 11449992Sbostic register Char **av = av0; 1151300Sbill 11649992Sbostic if (!av0) 11749992Sbostic return; 11849992Sbostic for (; *av; av++) 11949992Sbostic xfree((ptr_t) * av); 12049992Sbostic xfree((ptr_t) av0); 1211300Sbill } 1221300Sbill 12349992Sbostic Char ** 1241300Sbill saveblk(v) 12549992Sbostic register Char **v; 1261300Sbill { 12749992Sbostic register Char **newv = 12849992Sbostic (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **)); 12949992Sbostic Char **onewv = newv; 1301300Sbill 13149992Sbostic while (*v) 13249992Sbostic *newv++ = Strsave(*v++); 13349992Sbostic return (onewv); 1341300Sbill } 1351300Sbill 136*50011Schristos #ifdef NOTUSED 13749992Sbostic char * 13849992Sbostic strstr(s, t) 13949992Sbostic register char *s, *t; 14049992Sbostic { 14149992Sbostic do { 14249992Sbostic register char *ss = s; 14349992Sbostic register char *tt = t; 14449992Sbostic 14549992Sbostic do 14649992Sbostic if (*tt == '\0') 14749992Sbostic return (s); 14849992Sbostic while (*ss++ == *tt++); 14949992Sbostic } while (*s++ != '\0'); 15049992Sbostic return ((char *) 0); 15149992Sbostic } 15249992Sbostic 153*50011Schristos #endif /* NOTUSED */ 15449992Sbostic 15549992Sbostic #ifndef SHORT_STRINGS 15649992Sbostic char * 1571300Sbill strspl(cp, dp) 15849992Sbostic char *cp, *dp; 1591300Sbill { 16049992Sbostic char *ep; 16149992Sbostic register char *p, *q; 1621300Sbill 16349992Sbostic if (!cp) 16449992Sbostic cp = ""; 16549992Sbostic if (!dp) 16649992Sbostic dp = ""; 16749992Sbostic for (p = cp; *p++;); 16849992Sbostic for (q = dp; *q++;); 16949992Sbostic ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char))); 17049992Sbostic for (p = ep, q = cp; *p++ = *q++;); 17149992Sbostic for (p--, q = dp; *p++ = *q++;); 17249992Sbostic return (ep); 1731300Sbill } 1741300Sbill 17549992Sbostic #endif 17649992Sbostic 17749992Sbostic Char ** 1781300Sbill blkspl(up, vp) 17949992Sbostic register Char **up, **vp; 1801300Sbill { 18149992Sbostic register Char **wp = 18249992Sbostic (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1), 18349992Sbostic sizeof(Char **)); 1841300Sbill 18549992Sbostic (void) blkcpy(wp, up); 18649992Sbostic return (blkcat(wp, vp)); 1871300Sbill } 1881300Sbill 18949992Sbostic Char 1901300Sbill lastchr(cp) 19149992Sbostic register Char *cp; 1921300Sbill { 1931300Sbill 19449992Sbostic if (!cp) 19549992Sbostic return (0); 19649992Sbostic if (!*cp) 19749992Sbostic return (0); 19849992Sbostic while (cp[1]) 19949992Sbostic cp++; 20049992Sbostic return (*cp); 2011300Sbill } 2021300Sbill 2031300Sbill /* 2041300Sbill * This routine is called after an error to close up 2051300Sbill * any units which may have been left open accidentally. 2061300Sbill */ 20749992Sbostic void 2081300Sbill closem() 2091300Sbill { 21049992Sbostic register int f; 2111300Sbill 21249992Sbostic for (f = 0; f < NOFILE; f++) 21349992Sbostic if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD && 21449992Sbostic f != FSHTTY) 21549992Sbostic (void) close(f); 2161300Sbill } 2171300Sbill 21849992Sbostic void 2191300Sbill donefds() 2201300Sbill { 2211300Sbill 22249992Sbostic (void) close(0); 22349992Sbostic (void) close(1); 22449992Sbostic (void) close(2); 22549992Sbostic didfds = 0; 2261300Sbill } 2271300Sbill 2281300Sbill /* 2291300Sbill * Move descriptor i to j. 2301300Sbill * If j is -1 then we just want to get i to a safe place, 2311300Sbill * i.e. to a unit > 2. This also happens in dcopy. 2321300Sbill */ 23349992Sbostic int 2341300Sbill dmove(i, j) 23549992Sbostic register int i, j; 2361300Sbill { 2371300Sbill 23849992Sbostic if (i == j || i < 0) 23949992Sbostic return (i); 24049992Sbostic if (j >= 0) { 24149992Sbostic (void) dup2(i, j); 2421300Sbill return (j); 24349992Sbostic } 24449992Sbostic j = dcopy(i, j); 24549992Sbostic if (j != i) 24649992Sbostic (void) close(i); 24749992Sbostic return (j); 2481300Sbill } 2491300Sbill 25049992Sbostic int 2511300Sbill dcopy(i, j) 25249992Sbostic register int i, j; 2531300Sbill { 2541300Sbill 25549992Sbostic if (i == j || i < 0 || j < 0 && i > 2) 25649992Sbostic return (i); 25749992Sbostic if (j >= 0) { 25849992Sbostic (void) dup2(i, j); 25949992Sbostic return (j); 26049992Sbostic } 26149992Sbostic (void) close(j); 26249992Sbostic return (renum(i, j)); 2631300Sbill } 2641300Sbill 26549992Sbostic static int 2661300Sbill renum(i, j) 26749992Sbostic register int i, j; 2681300Sbill { 26949992Sbostic register int k = dup(i); 2701300Sbill 27149992Sbostic if (k < 0) 27249992Sbostic return (-1); 27349992Sbostic if (j == -1 && k > 2) 2741300Sbill return (k); 27549992Sbostic if (k != j) { 27649992Sbostic j = renum(k, j); 27749992Sbostic (void) close(k); 27849992Sbostic return (j); 27949992Sbostic } 28049992Sbostic return (k); 2811300Sbill } 2821300Sbill 2831300Sbill /* 2841300Sbill * Left shift a command argument list, discarding 2851300Sbill * the first c arguments. Used in "shift" commands 2861300Sbill * as well as by commands like "repeat". 2871300Sbill */ 28849992Sbostic void 2891300Sbill lshift(v, c) 29049992Sbostic register Char **v; 29149992Sbostic register int c; 2921300Sbill { 29349992Sbostic register Char **u = v; 2941300Sbill 29549992Sbostic while (*u && --c >= 0) 29649992Sbostic xfree((ptr_t) * u++); 29749992Sbostic (void) blkcpy(v, u); 2981300Sbill } 2991300Sbill 30049992Sbostic int 3011300Sbill number(cp) 30249992Sbostic Char *cp; 3031300Sbill { 30449992Sbostic if (!cp) 30549992Sbostic return(0); 30649992Sbostic if (*cp == '-') { 30749992Sbostic cp++; 30849992Sbostic if (!Isdigit(*cp)) 30949992Sbostic return (0); 31049992Sbostic cp++; 31149992Sbostic } 31249992Sbostic while (*cp && Isdigit(*cp)) 31349992Sbostic cp++; 31449992Sbostic return (*cp == 0); 3151300Sbill } 3161300Sbill 31749992Sbostic Char ** 3181300Sbill copyblk(v) 31949992Sbostic register Char **v; 3201300Sbill { 32149992Sbostic Char **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **)); 3221300Sbill 32349992Sbostic return (blkcpy(nv, v)); 3241300Sbill } 3251300Sbill 32649992Sbostic #ifndef SHORT_STRINGS 32749992Sbostic char * 3281300Sbill strend(cp) 32949992Sbostic register char *cp; 3301300Sbill { 33149992Sbostic if (!cp) 3321300Sbill return (cp); 33349992Sbostic while (*cp) 33449992Sbostic cp++; 33549992Sbostic return (cp); 3361300Sbill } 3371300Sbill 33849992Sbostic #endif /* SHORT_STRINGS */ 33949992Sbostic 34049992Sbostic Char * 3411300Sbill strip(cp) 34249992Sbostic Char *cp; 3431300Sbill { 34449992Sbostic register Char *dp = cp; 3451300Sbill 34649992Sbostic if (!cp) 3471300Sbill return (cp); 34849992Sbostic while (*dp++ &= TRIM) 34949992Sbostic continue; 35049992Sbostic return (cp); 3511300Sbill } 3521300Sbill 35349992Sbostic void 3541300Sbill udvar(name) 35549992Sbostic Char *name; 3561300Sbill { 3571300Sbill 35849992Sbostic setname(short2str(name)); 35949992Sbostic stderror(ERR_NAME | ERR_UNDVAR); 3601300Sbill } 3611300Sbill 36249992Sbostic int 3631300Sbill prefix(sub, str) 36449992Sbostic register Char *sub, *str; 3651300Sbill { 3661300Sbill 36749992Sbostic for (;;) { 36849992Sbostic if (*sub == 0) 36949992Sbostic return (1); 37049992Sbostic if (*str == 0) 37149992Sbostic return (0); 37249992Sbostic if (*sub++ != *str++) 37349992Sbostic return (0); 37449992Sbostic } 3751300Sbill } 376