1 # include <stdio.h> 2 # include <sysexits.h> 3 # include "useful.h" 4 # include <ctype.h> 5 6 static char SccsId[] = "@(#)util.c 3.6 08/09/81"; 7 8 /* 9 ** STRIPQUOTES -- Strip quotes & quote bits from a string. 10 ** 11 ** Runs through a string and strips off unquoted quote 12 ** characters and quote bits. This is done in place. 13 ** 14 ** Parameters: 15 ** s -- the string to strip. 16 ** 17 ** Returns: 18 ** none. 19 ** 20 ** Side Effects: 21 ** none. 22 ** 23 ** Called By: 24 ** deliver 25 */ 26 27 stripquotes(s) 28 char *s; 29 { 30 register char *p; 31 register char *q; 32 register char c; 33 34 for (p = q = s; (c = *p++) != '\0'; ) 35 { 36 if (c != '"') 37 *q++ = c & 0177; 38 } 39 *q = '\0'; 40 } 41 /* 42 ** CAPITALIZE -- return a copy of a string, properly capitalized. 43 ** 44 ** Parameters: 45 ** s -- the string to capitalize. 46 ** 47 ** Returns: 48 ** a pointer to a properly capitalized string. 49 ** 50 ** Side Effects: 51 ** none. 52 */ 53 54 char * 55 capitalize(s) 56 register char *s; 57 { 58 static char buf[50]; 59 register char *p; 60 61 p = buf; 62 63 for (;;) 64 { 65 while (!isalpha(*s) && *s != '\0') 66 *p++ = *s++; 67 if (*s == '\0') 68 break; 69 *p++ = toupper(*s++); 70 while (isalpha(*s)) 71 *p++ = *s++; 72 } 73 74 *p = '\0'; 75 return (buf); 76 } 77 /* 78 ** XALLOC -- Allocate memory and bitch wildly on failure. 79 ** 80 ** THIS IS A CLUDGE. This should be made to give a proper 81 ** error -- but after all, what can we do? 82 ** 83 ** Parameters: 84 ** sz -- size of area to allocate. 85 ** 86 ** Returns: 87 ** pointer to data region. 88 ** 89 ** Side Effects: 90 ** Memory is allocated. 91 */ 92 93 char * 94 xalloc(sz) 95 register unsigned int sz; 96 { 97 register char *p; 98 99 p = malloc(sz); 100 if (p == NULL) 101 { 102 syserr("Out of memory!!"); 103 exit(EX_UNAVAILABLE); 104 } 105 return (p); 106 } 107 /* 108 ** NEWSTR -- make copy of string. 109 ** 110 ** Space is allocated for it using xalloc. 111 ** 112 ** Parameters: 113 ** string to copy. 114 ** 115 ** Returns: 116 ** pointer to new string. 117 ** 118 ** Side Effects: 119 ** none. 120 */ 121 122 char * 123 newstr(s) 124 register char *s; 125 { 126 register char *p; 127 128 p = xalloc((unsigned) (strlen(s) + 1)); 129 strcpy(p, s); 130 return (p); 131 } 132 /* 133 ** COPYPLIST -- copy list of pointers. 134 ** 135 ** This routine is the equivalent of newstr for lists of 136 ** pointers. 137 ** 138 ** Parameters: 139 ** list -- list of pointers to copy. 140 ** Must be NULL terminated. 141 ** copycont -- if TRUE, copy the contents of the vector 142 ** (which must be a string) also. 143 ** 144 ** Returns: 145 ** a copy of 'list'. 146 ** 147 ** Side Effects: 148 ** none. 149 */ 150 151 char ** 152 copyplist(list, copycont) 153 char **list; 154 bool copycont; 155 { 156 register char **vp; 157 register char **newvp; 158 159 for (vp = list; *vp != NULL; vp++) 160 continue; 161 162 vp++; 163 164 newvp = (char **) xalloc((unsigned) (vp - list) * sizeof *vp); 165 bmove((char *) list, (char *) newvp, (vp - list) * sizeof *vp); 166 167 if (copycont) 168 { 169 for (vp = newvp; *vp != NULL; vp++) 170 *vp = newstr(*vp); 171 } 172 173 return (newvp); 174 } 175 /* 176 ** PRINTAV -- print argument vector. 177 ** 178 ** Parameters: 179 ** av -- argument vector. 180 ** 181 ** Returns: 182 ** none. 183 ** 184 ** Side Effects: 185 ** prints av. 186 */ 187 188 # ifdef DEBUG 189 printav(av) 190 register char **av; 191 { 192 while (*av != NULL) 193 { 194 printf("\t%08x=", *av); 195 xputs(*av++); 196 putchar('\n'); 197 } 198 } 199 # endif DEBUG 200 /* 201 ** LOWER -- turn letter into lower case. 202 ** 203 ** Parameters: 204 ** c -- character to turn into lower case. 205 ** 206 ** Returns: 207 ** c, in lower case. 208 ** 209 ** Side Effects: 210 ** none. 211 */ 212 213 char 214 lower(c) 215 register char c; 216 { 217 if (isascii(c) && isupper(c)) 218 c = c - 'A' + 'a'; 219 return (c); 220 } 221 /* 222 ** XPUTS -- put string doing control escapes. 223 ** 224 ** Parameters: 225 ** s -- string to put. 226 ** 227 ** Returns: 228 ** none. 229 ** 230 ** Side Effects: 231 ** output to stdout 232 */ 233 234 # ifdef DEBUG 235 xputs(s) 236 register char *s; 237 { 238 register char c; 239 240 while ((c = *s++) != '\0') 241 { 242 if (!isascii(c)) 243 { 244 putchar('\\'); 245 c &= 0177; 246 } 247 if (iscntrl(c)) 248 { 249 putchar('^'); 250 c |= 0100; 251 } 252 putchar(c); 253 } 254 (void) fflush(stdout); 255 } 256 # endif DEBUG 257 /* 258 ** MAKELOWER -- Translate a line into lower case 259 ** 260 ** Parameters: 261 ** p -- the string to translate. If NULL, return is 262 ** immediate. 263 ** 264 ** Returns: 265 ** none. 266 ** 267 ** Side Effects: 268 ** String pointed to by p is translated to lower case. 269 ** 270 ** Called By: 271 ** parse 272 */ 273 274 makelower(p) 275 register char *p; 276 { 277 register char c; 278 279 if (p == NULL) 280 return; 281 for (; (c = *p) != '\0'; p++) 282 if (isascii(c) && isupper(c)) 283 *p = c - 'A' + 'a'; 284 } 285 /* 286 ** SAMEWORD -- return TRUE if the words are the same 287 ** 288 ** Ignores case. 289 ** 290 ** Parameters: 291 ** a, b -- the words to compare. 292 ** 293 ** Returns: 294 ** TRUE if a & b match exactly (modulo case) 295 ** FALSE otherwise. 296 ** 297 ** Side Effects: 298 ** none. 299 */ 300 301 bool 302 sameword(a, b) 303 register char *a, *b; 304 { 305 while (lower(*a) == lower(*b)) 306 { 307 if (*a == '\0') 308 return (TRUE); 309 a++; 310 b++; 311 } 312 return (FALSE); 313 } 314 /* 315 ** SYSLOG -- fake entry to fool lint 316 */ 317 318 # ifdef LOG 319 # ifdef lint 320 321 /*VARARGS2*/ 322 syslog(pri, fmt, args) 323 int pri; 324 char *fmt; 325 { 326 pri = *fmt; 327 args = pri; 328 pri = args; 329 } 330 331 # endif lint 332 # endif LOG 333