1 1.19 mrg /* $NetBSD: expand.c,v 1.19 2023/08/03 08:03:19 mrg Exp $ */ 2 1.7 thorpej 3 1.1 cgd /* 4 1.5 cgd * Copyright (c) 1983, 1993 5 1.5 cgd * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.16 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 cgd * may be used to endorse or promote products derived from this software 17 1.1 cgd * without specific prior written permission. 18 1.1 cgd * 19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 cgd * SUCH DAMAGE. 30 1.1 cgd */ 31 1.1 cgd 32 1.10 lukem #include <sys/cdefs.h> 33 1.1 cgd #ifndef lint 34 1.7 thorpej #if 0 35 1.7 thorpej static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93"; 36 1.7 thorpej #else 37 1.19 mrg __RCSID("$NetBSD: expand.c,v 1.19 2023/08/03 08:03:19 mrg Exp $"); 38 1.7 thorpej #endif 39 1.1 cgd #endif /* not lint */ 40 1.9 mrg 41 1.9 mrg #include <sys/types.h> 42 1.9 mrg 43 1.9 mrg #include <errno.h> 44 1.9 mrg #include <pwd.h> 45 1.1 cgd 46 1.1 cgd #include "defs.h" 47 1.1 cgd 48 1.1 cgd #define GAVSIZ NCARGS / 6 49 1.1 cgd #define LC '{' 50 1.1 cgd #define RC '}' 51 1.1 cgd 52 1.1 cgd static char shchars[] = "${[*?"; 53 1.1 cgd 54 1.1 cgd int which; /* bit mask of types to expand */ 55 1.1 cgd int eargc; /* expanded arg count */ 56 1.1 cgd char **eargv; /* expanded arg vectors */ 57 1.1 cgd char *path; 58 1.1 cgd char *pathp; 59 1.1 cgd char *lastpathp; 60 1.17 lukem const char *tilde; /* "~user" if not expanding tilde, else "" */ 61 1.1 cgd char *tpathp; 62 1.1 cgd int nleft; 63 1.1 cgd 64 1.1 cgd int expany; /* any expansions done? */ 65 1.1 cgd char *entp; 66 1.1 cgd char **sortbase; 67 1.1 cgd 68 1.1 cgd #define sort() qsort((char *)sortbase, &eargv[eargc] - sortbase, \ 69 1.1 cgd sizeof(*sortbase), argcmp), sortbase = &eargv[eargc] 70 1.1 cgd 71 1.17 lukem static void Cat(const char *, const char *); 72 1.14 wiz static void addpath(int); 73 1.14 wiz static int amatch(char *, char *); 74 1.14 wiz static int argcmp(const void *, const void *); 75 1.14 wiz static int execbrc(char *, char *); 76 1.14 wiz static void expsh(char *); 77 1.14 wiz static void expstr(char *); 78 1.14 wiz static int match(char *, char *); 79 1.14 wiz static void matchdir(char *); 80 1.5 cgd 81 1.1 cgd /* 82 1.1 cgd * Take a list of names and expand any macros, etc. 83 1.1 cgd * wh = E_VARS if expanding variables. 84 1.1 cgd * wh = E_SHELL if expanding shell characters. 85 1.1 cgd * wh = E_TILDE if expanding `~'. 86 1.1 cgd * or any of these or'ed together. 87 1.1 cgd * 88 1.1 cgd * Major portions of this were snarfed from csh/sh.glob.c. 89 1.1 cgd */ 90 1.1 cgd struct namelist * 91 1.14 wiz expand(struct namelist *list, int wh) 92 1.1 cgd { 93 1.10 lukem struct namelist *nl, *prev; 94 1.10 lukem int n; 95 1.1 cgd char pathbuf[BUFSIZ]; 96 1.1 cgd char *argvbuf[GAVSIZ]; 97 1.1 cgd 98 1.1 cgd if (debug) { 99 1.10 lukem printf("expand(%lx, %d)\nlist = ", (long)list, wh); 100 1.1 cgd prnames(list); 101 1.1 cgd } 102 1.1 cgd 103 1.1 cgd if (wh == 0) { 104 1.10 lukem char *cp; 105 1.1 cgd 106 1.1 cgd for (nl = list; nl != NULL; nl = nl->n_next) 107 1.1 cgd for (cp = nl->n_name; *cp; cp++) 108 1.1 cgd *cp = *cp & TRIM; 109 1.1 cgd return(list); 110 1.1 cgd } 111 1.1 cgd 112 1.1 cgd which = wh; 113 1.1 cgd path = tpathp = pathp = pathbuf; 114 1.1 cgd *pathp = '\0'; 115 1.1 cgd lastpathp = &path[sizeof pathbuf - 2]; 116 1.1 cgd tilde = ""; 117 1.1 cgd eargc = 0; 118 1.1 cgd eargv = sortbase = argvbuf; 119 1.1 cgd *eargv = 0; 120 1.1 cgd nleft = NCARGS - 4; 121 1.1 cgd /* 122 1.1 cgd * Walk the name list and expand names into eargv[]; 123 1.1 cgd */ 124 1.1 cgd for (nl = list; nl != NULL; nl = nl->n_next) 125 1.1 cgd expstr(nl->n_name); 126 1.1 cgd /* 127 1.1 cgd * Take expanded list of names from eargv[] and build a new list. 128 1.1 cgd */ 129 1.1 cgd list = prev = NULL; 130 1.1 cgd for (n = 0; n < eargc; n++) { 131 1.1 cgd nl = makenl(NULL); 132 1.1 cgd nl->n_name = eargv[n]; 133 1.1 cgd if (prev == NULL) 134 1.1 cgd list = prev = nl; 135 1.1 cgd else { 136 1.1 cgd prev->n_next = nl; 137 1.1 cgd prev = nl; 138 1.1 cgd } 139 1.1 cgd } 140 1.1 cgd if (debug) { 141 1.1 cgd printf("expanded list = "); 142 1.1 cgd prnames(list); 143 1.1 cgd } 144 1.19 mrg sortbase = NULL; 145 1.19 mrg eargv = NULL; 146 1.19 mrg path = tpathp = pathp = NULL; 147 1.19 mrg lastpathp = NULL; 148 1.1 cgd return(list); 149 1.1 cgd } 150 1.1 cgd 151 1.5 cgd static void 152 1.14 wiz expstr(char *s) 153 1.1 cgd { 154 1.10 lukem char *cp, *cp1; 155 1.10 lukem struct namelist *tp; 156 1.1 cgd char *tail; 157 1.17 lukem char expbuf[BUFSIZ]; 158 1.1 cgd int savec, oeargc; 159 1.1 cgd extern char homedir[]; 160 1.1 cgd 161 1.1 cgd if (s == NULL || *s == '\0') 162 1.1 cgd return; 163 1.1 cgd 164 1.10 lukem if ((which & E_VARS) && (cp = strchr(s, '$')) != NULL) { 165 1.1 cgd *cp++ = '\0'; 166 1.1 cgd if (*cp == '\0') { 167 1.1 cgd yyerror("no variable name after '$'"); 168 1.1 cgd return; 169 1.1 cgd } 170 1.1 cgd if (*cp == LC) { 171 1.1 cgd cp++; 172 1.10 lukem if ((tail = strchr(cp, RC)) == NULL) { 173 1.1 cgd yyerror("unmatched '{'"); 174 1.1 cgd return; 175 1.1 cgd } 176 1.1 cgd *tail++ = savec = '\0'; 177 1.1 cgd if (*cp == '\0') { 178 1.1 cgd yyerror("no variable name after '$'"); 179 1.1 cgd return; 180 1.1 cgd } 181 1.1 cgd } else { 182 1.1 cgd tail = cp + 1; 183 1.1 cgd savec = *tail; 184 1.1 cgd *tail = '\0'; 185 1.1 cgd } 186 1.8 pk tp = lookup(cp, 0, 0); 187 1.1 cgd if (savec != '\0') 188 1.1 cgd *tail = savec; 189 1.1 cgd if (tp != NULL) { 190 1.1 cgd for (; tp != NULL; tp = tp->n_next) { 191 1.17 lukem snprintf(expbuf, sizeof(expbuf), "%s%s%s", s, 192 1.6 thorpej tp->n_name, tail); 193 1.17 lukem expstr(expbuf); 194 1.1 cgd } 195 1.1 cgd return; 196 1.1 cgd } 197 1.17 lukem snprintf(expbuf, sizeof(expbuf), "%s%s", s, tail); 198 1.17 lukem expstr(expbuf); 199 1.1 cgd return; 200 1.1 cgd } 201 1.1 cgd if ((which & ~E_VARS) == 0 || !strcmp(s, "{") || !strcmp(s, "{}")) { 202 1.1 cgd Cat(s, ""); 203 1.1 cgd sort(); 204 1.1 cgd return; 205 1.1 cgd } 206 1.1 cgd if (*s == '~') { 207 1.1 cgd cp = ++s; 208 1.1 cgd if (*cp == '\0' || *cp == '/') { 209 1.1 cgd tilde = "~"; 210 1.1 cgd cp1 = homedir; 211 1.1 cgd } else { 212 1.17 lukem tilde = cp1 = expbuf; 213 1.1 cgd *cp1++ = '~'; 214 1.1 cgd do 215 1.1 cgd *cp1++ = *cp++; 216 1.1 cgd while (*cp && *cp != '/'); 217 1.1 cgd *cp1 = '\0'; 218 1.17 lukem if (pw == NULL || strcmp(pw->pw_name, expbuf+1) != 0) { 219 1.17 lukem if ((pw = getpwnam(expbuf+1)) == NULL) { 220 1.17 lukem strlcat(expbuf, ": unknown user name", 221 1.17 lukem sizeof(expbuf)); 222 1.17 lukem yyerror(expbuf+1); 223 1.1 cgd return; 224 1.1 cgd } 225 1.1 cgd } 226 1.1 cgd cp1 = pw->pw_dir; 227 1.1 cgd s = cp; 228 1.1 cgd } 229 1.10 lukem for (cp = path; (*cp++ = *cp1++) != 0; ) 230 1.1 cgd ; 231 1.1 cgd tpathp = pathp = cp - 1; 232 1.1 cgd } else { 233 1.1 cgd tpathp = pathp = path; 234 1.1 cgd tilde = ""; 235 1.1 cgd } 236 1.1 cgd *pathp = '\0'; 237 1.1 cgd if (!(which & E_SHELL)) { 238 1.1 cgd if (which & E_TILDE) 239 1.1 cgd Cat(path, s); 240 1.1 cgd else 241 1.1 cgd Cat(tilde, s); 242 1.1 cgd sort(); 243 1.1 cgd return; 244 1.1 cgd } 245 1.1 cgd oeargc = eargc; 246 1.1 cgd expany = 0; 247 1.1 cgd expsh(s); 248 1.1 cgd if (eargc == oeargc) 249 1.1 cgd Cat(s, ""); /* "nonomatch" is set */ 250 1.1 cgd sort(); 251 1.1 cgd } 252 1.1 cgd 253 1.5 cgd static int 254 1.14 wiz argcmp(const void *a1, const void *a2) 255 1.1 cgd { 256 1.1 cgd 257 1.17 lukem return (strcmp(*(const char * const *)a1, *(const char * const *)a2)); 258 1.1 cgd } 259 1.1 cgd 260 1.1 cgd /* 261 1.1 cgd * If there are any Shell meta characters in the name, 262 1.1 cgd * expand into a list, after searching directory 263 1.1 cgd */ 264 1.5 cgd static void 265 1.14 wiz expsh(char *s) 266 1.1 cgd { 267 1.10 lukem char *cp; 268 1.10 lukem char *spathp, *oldcp; 269 1.1 cgd struct stat stb; 270 1.1 cgd 271 1.1 cgd spathp = pathp; 272 1.1 cgd cp = s; 273 1.1 cgd while (!any(*cp, shchars)) { 274 1.1 cgd if (*cp == '\0') { 275 1.1 cgd if (!expany || stat(path, &stb) >= 0) { 276 1.1 cgd if (which & E_TILDE) 277 1.1 cgd Cat(path, ""); 278 1.1 cgd else 279 1.1 cgd Cat(tilde, tpathp); 280 1.1 cgd } 281 1.1 cgd goto endit; 282 1.1 cgd } 283 1.1 cgd addpath(*cp++); 284 1.1 cgd } 285 1.1 cgd oldcp = cp; 286 1.1 cgd while (cp > s && *cp != '/') 287 1.1 cgd cp--, pathp--; 288 1.1 cgd if (*cp == '/') 289 1.1 cgd cp++, pathp++; 290 1.1 cgd *pathp = '\0'; 291 1.1 cgd if (*oldcp == '{') { 292 1.1 cgd execbrc(cp, NULL); 293 1.1 cgd return; 294 1.1 cgd } 295 1.1 cgd matchdir(cp); 296 1.1 cgd endit: 297 1.1 cgd pathp = spathp; 298 1.1 cgd *pathp = '\0'; 299 1.1 cgd } 300 1.1 cgd 301 1.5 cgd static void 302 1.14 wiz matchdir(char *pattern) 303 1.1 cgd { 304 1.1 cgd struct stat stb; 305 1.13 christos struct dirent *dp; 306 1.1 cgd DIR *dirp; 307 1.1 cgd 308 1.1 cgd dirp = opendir(path); 309 1.1 cgd if (dirp == NULL) { 310 1.1 cgd if (expany) 311 1.1 cgd return; 312 1.1 cgd goto patherr2; 313 1.1 cgd } 314 1.1 cgd if (fstat(dirp->dd_fd, &stb) < 0) 315 1.1 cgd goto patherr1; 316 1.11 mycroft if (!S_ISDIR(stb.st_mode)) { 317 1.1 cgd errno = ENOTDIR; 318 1.1 cgd goto patherr1; 319 1.1 cgd } 320 1.1 cgd while ((dp = readdir(dirp)) != NULL) 321 1.1 cgd if (match(dp->d_name, pattern)) { 322 1.1 cgd if (which & E_TILDE) 323 1.1 cgd Cat(path, dp->d_name); 324 1.1 cgd else { 325 1.1 cgd strcpy(pathp, dp->d_name); 326 1.1 cgd Cat(tilde, tpathp); 327 1.1 cgd *pathp = '\0'; 328 1.1 cgd } 329 1.1 cgd } 330 1.1 cgd closedir(dirp); 331 1.1 cgd return; 332 1.1 cgd 333 1.1 cgd patherr1: 334 1.1 cgd closedir(dirp); 335 1.1 cgd patherr2: 336 1.1 cgd strcat(path, ": "); 337 1.1 cgd strcat(path, strerror(errno)); 338 1.1 cgd yyerror(path); 339 1.1 cgd } 340 1.1 cgd 341 1.5 cgd static int 342 1.14 wiz execbrc(char *p, char *s) 343 1.1 cgd { 344 1.1 cgd char restbuf[BUFSIZ + 2]; 345 1.10 lukem char *pe, *pm, *pl; 346 1.1 cgd int brclev = 0; 347 1.1 cgd char *lm, savec, *spathp; 348 1.1 cgd 349 1.1 cgd for (lm = restbuf; *p != '{'; *lm++ = *p++) 350 1.1 cgd continue; 351 1.1 cgd for (pe = ++p; *pe; pe++) 352 1.1 cgd switch (*pe) { 353 1.1 cgd 354 1.1 cgd case '{': 355 1.1 cgd brclev++; 356 1.1 cgd continue; 357 1.1 cgd 358 1.1 cgd case '}': 359 1.1 cgd if (brclev == 0) 360 1.1 cgd goto pend; 361 1.1 cgd brclev--; 362 1.1 cgd continue; 363 1.1 cgd 364 1.1 cgd case '[': 365 1.1 cgd for (pe++; *pe && *pe != ']'; pe++) 366 1.1 cgd continue; 367 1.1 cgd if (!*pe) 368 1.1 cgd yyerror("Missing ']'"); 369 1.1 cgd continue; 370 1.1 cgd } 371 1.1 cgd pend: 372 1.1 cgd if (brclev || !*pe) { 373 1.1 cgd yyerror("Missing '}'"); 374 1.1 cgd return (0); 375 1.1 cgd } 376 1.1 cgd for (pl = pm = p; pm <= pe; pm++) 377 1.1 cgd switch (*pm & (QUOTE|TRIM)) { 378 1.1 cgd 379 1.1 cgd case '{': 380 1.1 cgd brclev++; 381 1.1 cgd continue; 382 1.1 cgd 383 1.1 cgd case '}': 384 1.1 cgd if (brclev) { 385 1.1 cgd brclev--; 386 1.1 cgd continue; 387 1.1 cgd } 388 1.1 cgd goto doit; 389 1.1 cgd 390 1.1 cgd case ',': 391 1.1 cgd if (brclev) 392 1.1 cgd continue; 393 1.1 cgd doit: 394 1.1 cgd savec = *pm; 395 1.1 cgd *pm = 0; 396 1.15 itojun strlcpy(lm, pl, sizeof(restbuf) - (lm - restbuf)); 397 1.15 itojun strlcat(restbuf, pe + 1, sizeof(restbuf)); 398 1.1 cgd *pm = savec; 399 1.1 cgd if (s == 0) { 400 1.1 cgd spathp = pathp; 401 1.1 cgd expsh(restbuf); 402 1.1 cgd pathp = spathp; 403 1.1 cgd *pathp = 0; 404 1.1 cgd } else if (amatch(s, restbuf)) 405 1.1 cgd return (1); 406 1.1 cgd sort(); 407 1.1 cgd pl = pm + 1; 408 1.1 cgd continue; 409 1.1 cgd 410 1.1 cgd case '[': 411 1.1 cgd for (pm++; *pm && *pm != ']'; pm++) 412 1.1 cgd continue; 413 1.1 cgd if (!*pm) 414 1.1 cgd yyerror("Missing ']'"); 415 1.1 cgd continue; 416 1.1 cgd } 417 1.1 cgd return (0); 418 1.1 cgd } 419 1.1 cgd 420 1.5 cgd static int 421 1.14 wiz match(char *s, char *p) 422 1.1 cgd { 423 1.10 lukem int c; 424 1.10 lukem char *sentp; 425 1.1 cgd char sexpany = expany; 426 1.1 cgd 427 1.1 cgd if (*s == '.' && *p != '.') 428 1.1 cgd return (0); 429 1.1 cgd sentp = entp; 430 1.1 cgd entp = s; 431 1.1 cgd c = amatch(s, p); 432 1.1 cgd entp = sentp; 433 1.1 cgd expany = sexpany; 434 1.1 cgd return (c); 435 1.1 cgd } 436 1.1 cgd 437 1.5 cgd static int 438 1.14 wiz amatch(char *s, char *p) 439 1.1 cgd { 440 1.10 lukem int scc; 441 1.1 cgd int ok, lc; 442 1.1 cgd char *spathp; 443 1.1 cgd struct stat stb; 444 1.1 cgd int c, cc; 445 1.1 cgd 446 1.1 cgd expany = 1; 447 1.1 cgd for (;;) { 448 1.1 cgd scc = *s++ & TRIM; 449 1.1 cgd switch (c = *p++) { 450 1.1 cgd 451 1.1 cgd case '{': 452 1.1 cgd return (execbrc(p - 1, s - 1)); 453 1.1 cgd 454 1.1 cgd case '[': 455 1.1 cgd ok = 0; 456 1.1 cgd lc = 077777; 457 1.10 lukem while ((cc = *p++) != 0) { 458 1.1 cgd if (cc == ']') { 459 1.1 cgd if (ok) 460 1.1 cgd break; 461 1.1 cgd return (0); 462 1.1 cgd } 463 1.1 cgd if (cc == '-') { 464 1.1 cgd if (lc <= scc && scc <= *p++) 465 1.1 cgd ok++; 466 1.1 cgd } else 467 1.1 cgd if (scc == (lc = cc)) 468 1.1 cgd ok++; 469 1.1 cgd } 470 1.1 cgd if (cc == 0) { 471 1.1 cgd yyerror("Missing ']'"); 472 1.1 cgd return (0); 473 1.1 cgd } 474 1.1 cgd continue; 475 1.1 cgd 476 1.1 cgd case '*': 477 1.1 cgd if (!*p) 478 1.1 cgd return (1); 479 1.1 cgd if (*p == '/') { 480 1.1 cgd p++; 481 1.1 cgd goto slash; 482 1.1 cgd } 483 1.1 cgd for (s--; *s; s++) 484 1.1 cgd if (amatch(s, p)) 485 1.1 cgd return (1); 486 1.1 cgd return (0); 487 1.1 cgd 488 1.1 cgd case '\0': 489 1.1 cgd return (scc == '\0'); 490 1.1 cgd 491 1.1 cgd default: 492 1.1 cgd if ((c & TRIM) != scc) 493 1.1 cgd return (0); 494 1.1 cgd continue; 495 1.1 cgd 496 1.1 cgd case '?': 497 1.1 cgd if (scc == '\0') 498 1.1 cgd return (0); 499 1.1 cgd continue; 500 1.1 cgd 501 1.1 cgd case '/': 502 1.1 cgd if (scc) 503 1.1 cgd return (0); 504 1.1 cgd slash: 505 1.1 cgd s = entp; 506 1.1 cgd spathp = pathp; 507 1.1 cgd while (*s) 508 1.1 cgd addpath(*s++); 509 1.1 cgd addpath('/'); 510 1.12 ross if (stat(path, &stb) == 0 && S_ISDIR(stb.st_mode)) { 511 1.1 cgd if (*p == '\0') { 512 1.1 cgd if (which & E_TILDE) 513 1.1 cgd Cat(path, ""); 514 1.1 cgd else 515 1.1 cgd Cat(tilde, tpathp); 516 1.1 cgd } else 517 1.1 cgd expsh(p); 518 1.12 ross } 519 1.1 cgd pathp = spathp; 520 1.1 cgd *pathp = '\0'; 521 1.1 cgd return (0); 522 1.1 cgd } 523 1.1 cgd } 524 1.1 cgd } 525 1.1 cgd 526 1.5 cgd static void 527 1.17 lukem Cat(const char *s1, const char *s2) 528 1.1 cgd { 529 1.1 cgd int len = strlen(s1) + strlen(s2) + 1; 530 1.10 lukem char *s; 531 1.1 cgd 532 1.1 cgd nleft -= len; 533 1.1 cgd if (nleft <= 0 || ++eargc >= GAVSIZ) 534 1.1 cgd yyerror("Arguments too long"); 535 1.1 cgd eargv[eargc] = 0; 536 1.1 cgd eargv[eargc - 1] = s = malloc(len); 537 1.1 cgd if (s == NULL) 538 1.1 cgd fatal("ran out of memory\n"); 539 1.10 lukem while ((*s++ = *s1++ & TRIM) != 0) 540 1.1 cgd ; 541 1.1 cgd s--; 542 1.10 lukem while ((*s++ = *s2++ & TRIM) != 0) 543 1.1 cgd ; 544 1.1 cgd } 545 1.1 cgd 546 1.5 cgd static void 547 1.14 wiz addpath(int c) 548 1.1 cgd { 549 1.1 cgd 550 1.1 cgd if (pathp >= lastpathp) 551 1.1 cgd yyerror("Pathname too long"); 552 1.1 cgd else { 553 1.1 cgd *pathp++ = c & TRIM; 554 1.1 cgd *pathp = '\0'; 555 1.1 cgd } 556 1.1 cgd } 557 1.1 cgd 558 1.1 cgd /* 559 1.1 cgd * Expand file names beginning with `~' into the 560 1.1 cgd * user's home directory path name. Return a pointer in buf to the 561 1.1 cgd * part corresponding to `file'. 562 1.1 cgd */ 563 1.1 cgd char * 564 1.17 lukem exptilde(char *expbuf, char *file) 565 1.1 cgd { 566 1.10 lukem char *s1, *s2, *s3; 567 1.1 cgd extern char homedir[]; 568 1.1 cgd 569 1.1 cgd if (*file != '~') { 570 1.17 lukem strcpy(expbuf, file); 571 1.17 lukem return(expbuf); 572 1.1 cgd } 573 1.1 cgd if (*++file == '\0') { 574 1.1 cgd s2 = homedir; 575 1.1 cgd s3 = NULL; 576 1.1 cgd } else if (*file == '/') { 577 1.1 cgd s2 = homedir; 578 1.1 cgd s3 = file; 579 1.1 cgd } else { 580 1.1 cgd s3 = file; 581 1.1 cgd while (*s3 && *s3 != '/') 582 1.1 cgd s3++; 583 1.1 cgd if (*s3 == '/') 584 1.1 cgd *s3 = '\0'; 585 1.1 cgd else 586 1.1 cgd s3 = NULL; 587 1.1 cgd if (pw == NULL || strcmp(pw->pw_name, file) != 0) { 588 1.1 cgd if ((pw = getpwnam(file)) == NULL) { 589 1.1 cgd error("%s: unknown user name\n", file); 590 1.1 cgd if (s3 != NULL) 591 1.1 cgd *s3 = '/'; 592 1.1 cgd return(NULL); 593 1.1 cgd } 594 1.1 cgd } 595 1.1 cgd if (s3 != NULL) 596 1.1 cgd *s3 = '/'; 597 1.1 cgd s2 = pw->pw_dir; 598 1.1 cgd } 599 1.17 lukem for (s1 = expbuf; (*s1++ = *s2++) != 0; ) 600 1.1 cgd ; 601 1.1 cgd s2 = --s1; 602 1.1 cgd if (s3 != NULL) { 603 1.1 cgd s2++; 604 1.10 lukem while ((*s1++ = *s3++) != 0) 605 1.1 cgd ; 606 1.1 cgd } 607 1.1 cgd return(s2); 608 1.1 cgd } 609