Home | History | Annotate | Line # | Download | only in csh
misc.c revision 1.1.1.2
      1 /*-
      2  * Copyright (c) 1980, 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. 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.
     20  *
     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
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 5/31/93";
     36 #endif /* not lint */
     37 
     38 #include <sys/param.h>
     39 #include <stdlib.h>
     40 #include <unistd.h>
     41 #if __STDC__
     42 # include <stdarg.h>
     43 #else
     44 # include <varargs.h>
     45 #endif
     46 
     47 #include "csh.h"
     48 #include "extern.h"
     49 
     50 static int	renum __P((int, int));
     51 
     52 int
     53 any(s, c)
     54     register char *s;
     55     register int c;
     56 {
     57     if (!s)
     58 	return (0);		/* Check for nil pointer */
     59     while (*s)
     60 	if (*s++ == c)
     61 	    return (1);
     62     return (0);
     63 }
     64 
     65 void
     66 setzero(cp, i)
     67     char   *cp;
     68     int     i;
     69 {
     70     if (i != 0)
     71 	do
     72 	    *cp++ = 0;
     73 	while (--i);
     74 }
     75 
     76 char   *
     77 strsave(s)
     78     register char *s;
     79 {
     80     char   *n;
     81     register char *p;
     82 
     83     if (s == NULL)
     84 	s = "";
     85     for (p = s; *p++;)
     86 	continue;
     87     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
     88     while ((*p++ = *s++) != '\0')
     89 	continue;
     90     return (n);
     91 }
     92 
     93 Char  **
     94 blkend(up)
     95     register Char **up;
     96 {
     97 
     98     while (*up)
     99 	up++;
    100     return (up);
    101 }
    102 
    103 
    104 void
    105 blkpr(fp, av)
    106     FILE *fp;
    107     register Char **av;
    108 {
    109 
    110     for (; *av; av++) {
    111 	(void) fprintf(fp, "%s", vis_str(*av));
    112 	if (av[1])
    113 	    (void) fprintf(fp, " ");
    114     }
    115 }
    116 
    117 int
    118 blklen(av)
    119     register Char **av;
    120 {
    121     register int i = 0;
    122 
    123     while (*av++)
    124 	i++;
    125     return (i);
    126 }
    127 
    128 Char  **
    129 blkcpy(oav, bv)
    130     Char  **oav;
    131     register Char **bv;
    132 {
    133     register Char **av = oav;
    134 
    135     while ((*av++ = *bv++) != NULL)
    136 	continue;
    137     return (oav);
    138 }
    139 
    140 Char  **
    141 blkcat(up, vp)
    142     Char  **up, **vp;
    143 {
    144 
    145     (void) blkcpy(blkend(up), vp);
    146     return (up);
    147 }
    148 
    149 void
    150 blkfree(av0)
    151     Char  **av0;
    152 {
    153     register Char **av = av0;
    154 
    155     if (!av0)
    156 	return;
    157     for (; *av; av++)
    158 	xfree((ptr_t) * av);
    159     xfree((ptr_t) av0);
    160 }
    161 
    162 Char  **
    163 saveblk(v)
    164     register Char **v;
    165 {
    166     register Char **newv =
    167     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
    168     Char  **onewv = newv;
    169 
    170     while (*v)
    171 	*newv++ = Strsave(*v++);
    172     return (onewv);
    173 }
    174 
    175 #ifdef NOTUSED
    176 char   *
    177 strstr(s, t)
    178     register char *s, *t;
    179 {
    180     do {
    181 	register char *ss = s;
    182 	register char *tt = t;
    183 
    184 	do
    185 	    if (*tt == '\0')
    186 		return (s);
    187 	while (*ss++ == *tt++);
    188     } while (*s++ != '\0');
    189     return (NULL);
    190 }
    191 
    192 #endif /* NOTUSED */
    193 
    194 #ifndef SHORT_STRINGS
    195 char   *
    196 strspl(cp, dp)
    197     char   *cp, *dp;
    198 {
    199     char   *ep;
    200     register char *p, *q;
    201 
    202     if (!cp)
    203 	cp = "";
    204     if (!dp)
    205 	dp = "";
    206     for (p = cp; *p++;)
    207 	continue;
    208     for (q = dp; *q++;)
    209 	continue;
    210     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
    211     for (p = ep, q = cp; *p++ = *q++;)
    212 	continue;
    213     for (p--, q = dp; *p++ = *q++;)
    214 	continue;
    215     return (ep);
    216 }
    217 
    218 #endif
    219 
    220 Char  **
    221 blkspl(up, vp)
    222     register Char **up, **vp;
    223 {
    224     register Char **wp =
    225     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
    226 		      sizeof(Char **));
    227 
    228     (void) blkcpy(wp, up);
    229     return (blkcat(wp, vp));
    230 }
    231 
    232 Char
    233 lastchr(cp)
    234     register Char *cp;
    235 {
    236 
    237     if (!cp)
    238 	return (0);
    239     if (!*cp)
    240 	return (0);
    241     while (cp[1])
    242 	cp++;
    243     return (*cp);
    244 }
    245 
    246 /*
    247  * This routine is called after an error to close up
    248  * any units which may have been left open accidentally.
    249  */
    250 void
    251 closem()
    252 {
    253     register int f;
    254 
    255     for (f = 0; f < NOFILE; f++)
    256 	if (f != SHIN && f != SHOUT && f != SHERR && f != OLDSTD &&
    257 	    f != FSHTTY)
    258 	    (void) close(f);
    259 }
    260 
    261 void
    262 donefds()
    263 {
    264     (void) close(0);
    265     (void) close(1);
    266     (void) close(2);
    267 
    268     didfds = 0;
    269 }
    270 
    271 /*
    272  * Move descriptor i to j.
    273  * If j is -1 then we just want to get i to a safe place,
    274  * i.e. to a unit > 2.  This also happens in dcopy.
    275  */
    276 int
    277 dmove(i, j)
    278     register int i, j;
    279 {
    280 
    281     if (i == j || i < 0)
    282 	return (i);
    283     if (j >= 0) {
    284 	(void) dup2(i, j);
    285 	if (j != i)
    286 	    (void) close(i);
    287 	return (j);
    288     }
    289     j = dcopy(i, j);
    290     if (j != i)
    291 	(void) close(i);
    292     return (j);
    293 }
    294 
    295 int
    296 dcopy(i, j)
    297     register int i, j;
    298 {
    299 
    300     if (i == j || i < 0 || (j < 0 && i > 2))
    301 	return (i);
    302     if (j >= 0) {
    303 	(void) dup2(i, j);
    304 	return (j);
    305     }
    306     (void) close(j);
    307     return (renum(i, j));
    308 }
    309 
    310 static int
    311 renum(i, j)
    312     register int i, j;
    313 {
    314     register int k = dup(i);
    315 
    316     if (k < 0)
    317 	return (-1);
    318     if (j == -1 && k > 2)
    319 	return (k);
    320     if (k != j) {
    321 	j = renum(k, j);
    322 	(void) close(k);
    323 	return (j);
    324     }
    325     return (k);
    326 }
    327 
    328 /*
    329  * Left shift a command argument list, discarding
    330  * the first c arguments.  Used in "shift" commands
    331  * as well as by commands like "repeat".
    332  */
    333 void
    334 lshift(v, c)
    335     register Char **v;
    336     register int c;
    337 {
    338     register Char **u;
    339 
    340     for (u = v; *u && --c >= 0; u++)
    341 	xfree((ptr_t) *u);
    342     (void) blkcpy(v, u);
    343 }
    344 
    345 int
    346 number(cp)
    347     Char   *cp;
    348 {
    349     if (!cp)
    350 	return(0);
    351     if (*cp == '-') {
    352 	cp++;
    353 	if (!Isdigit(*cp))
    354 	    return (0);
    355 	cp++;
    356     }
    357     while (*cp && Isdigit(*cp))
    358 	cp++;
    359     return (*cp == 0);
    360 }
    361 
    362 Char  **
    363 copyblk(v)
    364     register Char **v;
    365 {
    366     Char  **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
    367 
    368     return (blkcpy(nv, v));
    369 }
    370 
    371 #ifndef SHORT_STRINGS
    372 char   *
    373 strend(cp)
    374     register char *cp;
    375 {
    376     if (!cp)
    377 	return (cp);
    378     while (*cp)
    379 	cp++;
    380     return (cp);
    381 }
    382 
    383 #endif /* SHORT_STRINGS */
    384 
    385 Char   *
    386 strip(cp)
    387     Char   *cp;
    388 {
    389     register Char *dp = cp;
    390 
    391     if (!cp)
    392 	return (cp);
    393     while ((*dp++ &= TRIM) != '\0')
    394 	continue;
    395     return (cp);
    396 }
    397 
    398 void
    399 udvar(name)
    400     Char   *name;
    401 {
    402 
    403     setname(vis_str(name));
    404     stderror(ERR_NAME | ERR_UNDVAR);
    405 }
    406 
    407 int
    408 prefix(sub, str)
    409     register Char *sub, *str;
    410 {
    411 
    412     for (;;) {
    413 	if (*sub == 0)
    414 	    return (1);
    415 	if (*str == 0)
    416 	    return (0);
    417 	if (*sub++ != *str++)
    418 	    return (0);
    419     }
    420 }
    421