13151Seric # include <stdio.h> 2298Seric # include <sysexits.h> 3298Seric # include "useful.h" 42900Seric # include <ctype.h> 5298Seric 6*4086Seric static char SccsId[] = "@(#)util.c 3.6 08/09/81"; 7409Seric 8298Seric /* 9298Seric ** STRIPQUOTES -- Strip quotes & quote bits from a string. 10298Seric ** 11298Seric ** Runs through a string and strips off unquoted quote 12298Seric ** characters and quote bits. This is done in place. 13298Seric ** 14298Seric ** Parameters: 15298Seric ** s -- the string to strip. 16298Seric ** 17298Seric ** Returns: 18298Seric ** none. 19298Seric ** 20298Seric ** Side Effects: 21298Seric ** none. 22298Seric ** 23298Seric ** Called By: 24298Seric ** deliver 25298Seric */ 26298Seric 27298Seric stripquotes(s) 28298Seric char *s; 29298Seric { 30298Seric register char *p; 31298Seric register char *q; 32298Seric register char c; 33298Seric 34298Seric for (p = q = s; (c = *p++) != '\0'; ) 35298Seric { 36298Seric if (c != '"') 37298Seric *q++ = c & 0177; 38298Seric } 39298Seric *q = '\0'; 40298Seric } 41298Seric /* 422900Seric ** CAPITALIZE -- return a copy of a string, properly capitalized. 432900Seric ** 442900Seric ** Parameters: 452900Seric ** s -- the string to capitalize. 462900Seric ** 472900Seric ** Returns: 482900Seric ** a pointer to a properly capitalized string. 492900Seric ** 502900Seric ** Side Effects: 512900Seric ** none. 522900Seric */ 532900Seric 542900Seric char * 552900Seric capitalize(s) 562900Seric register char *s; 572900Seric { 582900Seric static char buf[50]; 592900Seric register char *p; 602900Seric 612900Seric p = buf; 622900Seric 632900Seric for (;;) 642900Seric { 652900Seric while (!isalpha(*s) && *s != '\0') 662900Seric *p++ = *s++; 672900Seric if (*s == '\0') 682900Seric break; 692900Seric *p++ = toupper(*s++); 702900Seric while (isalpha(*s)) 712900Seric *p++ = *s++; 722900Seric } 732900Seric 742900Seric *p = '\0'; 752900Seric return (buf); 762900Seric } 772900Seric /* 78298Seric ** XALLOC -- Allocate memory and bitch wildly on failure. 79298Seric ** 80298Seric ** THIS IS A CLUDGE. This should be made to give a proper 81298Seric ** error -- but after all, what can we do? 82298Seric ** 83298Seric ** Parameters: 84298Seric ** sz -- size of area to allocate. 85298Seric ** 86298Seric ** Returns: 87298Seric ** pointer to data region. 88298Seric ** 89298Seric ** Side Effects: 90298Seric ** Memory is allocated. 91298Seric */ 92298Seric 93298Seric char * 94298Seric xalloc(sz) 95298Seric register unsigned int sz; 96298Seric { 97298Seric register char *p; 98298Seric 99298Seric p = malloc(sz); 100298Seric if (p == NULL) 101298Seric { 102298Seric syserr("Out of memory!!"); 1031598Seric exit(EX_UNAVAILABLE); 104298Seric } 105298Seric return (p); 106298Seric } 107298Seric /* 1082900Seric ** NEWSTR -- make copy of string. 1092900Seric ** 1102900Seric ** Space is allocated for it using xalloc. 1112900Seric ** 1122900Seric ** Parameters: 1132900Seric ** string to copy. 1142900Seric ** 1152900Seric ** Returns: 1162900Seric ** pointer to new string. 1172900Seric ** 1182900Seric ** Side Effects: 1192900Seric ** none. 1202900Seric */ 1212900Seric 1222900Seric char * 1232900Seric newstr(s) 1242900Seric register char *s; 1252900Seric { 1262900Seric register char *p; 1272900Seric 1282992Seric p = xalloc((unsigned) (strlen(s) + 1)); 1292900Seric strcpy(p, s); 1302900Seric return (p); 1312900Seric } 1323151Seric /* 1333151Seric ** COPYPLIST -- copy list of pointers. 1343151Seric ** 1353151Seric ** This routine is the equivalent of newstr for lists of 1363151Seric ** pointers. 1373151Seric ** 1383151Seric ** Parameters: 1393151Seric ** list -- list of pointers to copy. 1403151Seric ** Must be NULL terminated. 1413151Seric ** copycont -- if TRUE, copy the contents of the vector 1423151Seric ** (which must be a string) also. 1433151Seric ** 1443151Seric ** Returns: 1453151Seric ** a copy of 'list'. 1463151Seric ** 1473151Seric ** Side Effects: 1483151Seric ** none. 1493151Seric */ 1503151Seric 1513151Seric char ** 1523151Seric copyplist(list, copycont) 1533151Seric char **list; 1543151Seric bool copycont; 1553151Seric { 1563151Seric register char **vp; 1573151Seric register char **newvp; 1583151Seric 1593151Seric for (vp = list; *vp != NULL; vp++) 1603151Seric continue; 1613151Seric 1623151Seric vp++; 1633151Seric 164*4086Seric newvp = (char **) xalloc((unsigned) (vp - list) * sizeof *vp); 165*4086Seric bmove((char *) list, (char *) newvp, (vp - list) * sizeof *vp); 1663151Seric 1673151Seric if (copycont) 1683151Seric { 1693151Seric for (vp = newvp; *vp != NULL; vp++) 1703151Seric *vp = newstr(*vp); 1713151Seric } 1723151Seric 1733151Seric return (newvp); 1743151Seric } 1753151Seric /* 1763151Seric ** PRINTAV -- print argument vector. 1773151Seric ** 1783151Seric ** Parameters: 1793151Seric ** av -- argument vector. 1803151Seric ** 1813151Seric ** Returns: 1823151Seric ** none. 1833151Seric ** 1843151Seric ** Side Effects: 1853151Seric ** prints av. 1863151Seric */ 1873151Seric 1883151Seric # ifdef DEBUG 1893151Seric printav(av) 1903151Seric register char **av; 1913151Seric { 1923151Seric while (*av != NULL) 1933151Seric { 1943151Seric printf("\t%08x=", *av); 1953151Seric xputs(*av++); 1963151Seric putchar('\n'); 1973151Seric } 1983151Seric } 1993151Seric # endif DEBUG 2003151Seric /* 2013151Seric ** LOWER -- turn letter into lower case. 2023151Seric ** 2033151Seric ** Parameters: 2043151Seric ** c -- character to turn into lower case. 2053151Seric ** 2063151Seric ** Returns: 2073151Seric ** c, in lower case. 2083151Seric ** 2093151Seric ** Side Effects: 2103151Seric ** none. 2113151Seric */ 2123151Seric 2133151Seric char 2143151Seric lower(c) 2153151Seric register char c; 2163151Seric { 2173151Seric if (isascii(c) && isupper(c)) 2183151Seric c = c - 'A' + 'a'; 2193151Seric return (c); 2203151Seric } 2213151Seric /* 2223151Seric ** XPUTS -- put string doing control escapes. 2233151Seric ** 2243151Seric ** Parameters: 2253151Seric ** s -- string to put. 2263151Seric ** 2273151Seric ** Returns: 2283151Seric ** none. 2293151Seric ** 2303151Seric ** Side Effects: 2313151Seric ** output to stdout 2323151Seric */ 2333151Seric 2343151Seric # ifdef DEBUG 2353151Seric xputs(s) 2363151Seric register char *s; 2373151Seric { 2383151Seric register char c; 2393151Seric 2403151Seric while ((c = *s++) != '\0') 2413151Seric { 2423151Seric if (!isascii(c)) 2433151Seric { 2443151Seric putchar('\\'); 2453151Seric c &= 0177; 2463151Seric } 2473151Seric if (iscntrl(c)) 2483151Seric { 2493151Seric putchar('^'); 2503151Seric c |= 0100; 2513151Seric } 2523151Seric putchar(c); 2533151Seric } 254*4086Seric (void) fflush(stdout); 2553151Seric } 2563151Seric # endif DEBUG 2573151Seric /* 2583151Seric ** MAKELOWER -- Translate a line into lower case 2593151Seric ** 2603151Seric ** Parameters: 2613151Seric ** p -- the string to translate. If NULL, return is 2623151Seric ** immediate. 2633151Seric ** 2643151Seric ** Returns: 2653151Seric ** none. 2663151Seric ** 2673151Seric ** Side Effects: 2683151Seric ** String pointed to by p is translated to lower case. 2693151Seric ** 2703151Seric ** Called By: 2713151Seric ** parse 2723151Seric */ 2733151Seric 2743151Seric makelower(p) 2753151Seric register char *p; 2763151Seric { 2773151Seric register char c; 2783151Seric 2793151Seric if (p == NULL) 2803151Seric return; 2813151Seric for (; (c = *p) != '\0'; p++) 2823151Seric if (isascii(c) && isupper(c)) 2833151Seric *p = c - 'A' + 'a'; 2843151Seric } 2854059Seric /* 2864059Seric ** SAMEWORD -- return TRUE if the words are the same 2874059Seric ** 2884059Seric ** Ignores case. 2894059Seric ** 2904059Seric ** Parameters: 2914059Seric ** a, b -- the words to compare. 2924059Seric ** 2934059Seric ** Returns: 2944059Seric ** TRUE if a & b match exactly (modulo case) 2954059Seric ** FALSE otherwise. 2964059Seric ** 2974059Seric ** Side Effects: 2984059Seric ** none. 2994059Seric */ 3004059Seric 3014059Seric bool 3024059Seric sameword(a, b) 3034059Seric register char *a, *b; 3044059Seric { 3054059Seric while (lower(*a) == lower(*b)) 3064059Seric { 3074059Seric if (*a == '\0') 3084059Seric return (TRUE); 3094059Seric a++; 3104059Seric b++; 3114059Seric } 3124059Seric return (FALSE); 3134059Seric } 314*4086Seric /* 315*4086Seric ** SYSLOG -- fake entry to fool lint 316*4086Seric */ 317*4086Seric 318*4086Seric # ifdef LOG 319*4086Seric # ifdef lint 320*4086Seric 321*4086Seric /*VARARGS2*/ 322*4086Seric syslog(pri, fmt, args) 323*4086Seric int pri; 324*4086Seric char *fmt; 325*4086Seric { 326*4086Seric pri = *fmt; 327*4086Seric args = pri; 328*4086Seric pri = args; 329*4086Seric } 330*4086Seric 331*4086Seric # endif lint 332*4086Seric # endif LOG 333