1 /* $Header: /src/pub/tcsh/tc.func.c,v 3.119 2005/03/06 03:57:10 christos Exp $ */ 2 /* 3 * tc.func.c: New tcsh builtins. 4 */ 5 /*- 6 * Copyright (c) 1980, 1991 The Regents of the University of California. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. 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 #include "sh.h" 34 35 RCSID("$Id: tc.func.c,v 3.119 2005/03/06 03:57:10 christos Exp $") 36 37 #include "ed.h" 38 #include "ed.defns.h" /* for the function names */ 39 #include "tw.h" 40 #include "tc.h" 41 #ifdef WINNT_NATIVE 42 #include "nt.const.h" 43 #endif /* WINNT_NATIVE */ 44 45 #ifdef AFS 46 #define PASSMAX 16 47 #include <afs/stds.h> 48 #include <afs/kautils.h> 49 long ka_UserAuthenticateGeneral(); 50 #else 51 #ifndef PASSMAX 52 #define PASSMAX 8 53 #endif 54 #endif /* AFS */ 55 56 #ifdef TESLA 57 extern int do_logout; 58 #endif /* TESLA */ 59 extern time_t t_period; 60 extern int just_signaled; 61 static int precmd_active = 0; 62 static int jobcmd_active = 0; /* GrP */ 63 static int postcmd_active = 0; 64 static int periodic_active = 0; 65 static int cwdcmd_active = 0; /* PWP: for cwd_cmd */ 66 static int beepcmd_active = 0; 67 static signalfun_t alm_fun = NULL; 68 69 static void auto_logout __P((int)); 70 static char *xgetpass __P((const char *)); 71 static void auto_lock __P((int)); 72 #ifdef BSDJOBS 73 static void insert __P((struct wordent *, int)); 74 static void insert_we __P((struct wordent *, struct wordent *)); 75 static int inlist __P((Char *, Char *)); 76 #endif /* BSDJOBS */ 77 struct tildecache; 78 static int tildecompare __P((struct tildecache *, struct tildecache *)); 79 static Char *gethomedir __P((Char *)); 80 #ifdef REMOTEHOST 81 static RETSIGTYPE palarm __P((int)); 82 static void getremotehost __P((void)); 83 #endif /* REMOTEHOST */ 84 85 /* 86 * Tops-C shell 87 */ 88 89 /* 90 * expand_lex: Take the given lex and put an expanded version of it in 91 * the string buf. First guy in lex list is ignored; last guy is ^J 92 * which we ignore. Only take lex'es from position 'from' to position 93 * 'to' inclusive 94 * 95 * Note: csh sometimes sets bit 8 in characters which causes all kinds 96 * of problems if we don't mask it here. Note: excl's in lexes have been 97 * un-back-slashed and must be re-back-slashed 98 * 99 * (PWP: NOTE: this returns a pointer to the END of the string expanded 100 * (in other words, where the NUL is).) 101 */ 102 /* PWP: this is a combination of the old sprlex() and the expand_lex from 103 the magic-space stuff */ 104 105 Char * 106 expand_lex(buf, bufsiz, sp0, from, to) 107 Char *buf; 108 size_t bufsiz; 109 struct wordent *sp0; 110 int from, to; 111 { 112 struct wordent *sp; 113 Char *s, *d, *e; 114 Char prev_c; 115 int i; 116 117 /* 118 * Make sure we have enough space to expand into. E.g. we may have 119 * "a|b" turn to "a | b" (from 3 to 5 characters) which is the worst 120 * case scenario (even "a>&! b" turns into "a > & ! b", i.e. 6 to 9 121 * characters -- am I missing any other cases?). 122 */ 123 bufsiz = bufsiz / 2; 124 125 buf[0] = '\0'; 126 prev_c = '\0'; 127 d = buf; 128 e = &buf[bufsiz]; /* for bounds checking */ 129 130 if (!sp0) 131 return (buf); /* null lex */ 132 if ((sp = sp0->next) == sp0) 133 return (buf); /* nada */ 134 if (sp == (sp0 = sp0->prev)) 135 return (buf); /* nada */ 136 137 for (i = 0; i < NCARGS; i++) { 138 if ((i >= from) && (i <= to)) { /* if in range */ 139 for (s = sp->word; *s && d < e; s++) { 140 141 if (s[1] & QUOTE) { 142 int l = NLSSize(s, -1); 143 if (l > 1) { 144 while (l-- > 0) { 145 if (d < e) 146 *d++ = (*s & TRIM); 147 prev_c = *s++; 148 } 149 s--; 150 continue; 151 } 152 } 153 /* 154 * bugfix by Michael Bloom: anything but the current history 155 * character {(PWP) and backslash} seem to be dealt with 156 * elsewhere. 157 */ 158 if ((*s & QUOTE) 159 && (((*s & TRIM) == HIST) || 160 (((*s & TRIM) == '\'') && (prev_c != '\\')) || 161 (((*s & TRIM) == '\"') && (prev_c != '\\')) || 162 (((*s & TRIM) == '\\') && (prev_c != '\\')))) { 163 *d++ = '\\'; 164 } 165 if (d < e) 166 *d++ = (*s & TRIM); 167 prev_c = *s; 168 } 169 if (d < e) 170 *d++ = ' '; 171 } 172 sp = sp->next; 173 if (sp == sp0) 174 break; 175 } 176 if (d > buf) 177 d--; /* get rid of trailing space */ 178 179 return (d); 180 } 181 182 Char * 183 sprlex(buf, bufsiz, sp0) 184 Char *buf; 185 size_t bufsiz; 186 struct wordent *sp0; 187 { 188 Char *cp; 189 190 cp = expand_lex(buf, bufsiz, sp0, 0, NCARGS); 191 *cp = '\0'; 192 return (buf); 193 } 194 195 196 Char * 197 Itoa(n, s, min_digits, attributes) 198 int n; 199 Char *s; 200 int min_digits, attributes; 201 { 202 /* 203 * The array size here is derived from 204 * log8(UINT_MAX) 205 * which is guaranteed to be enough for a decimal 206 * representation. We add 1 because integer divide 207 * rounds down. 208 */ 209 #ifndef CHAR_BIT 210 # define CHAR_BIT 8 211 #endif 212 Char buf[CHAR_BIT * sizeof(int) / 3 + 1]; 213 Char *p; 214 unsigned int un; /* handle most negative # too */ 215 int pad = (min_digits != 0); 216 217 if ((int)(sizeof(buf) - 1) < min_digits) 218 min_digits = sizeof(buf) - 1; 219 220 un = n; 221 if (n < 0) { 222 un = -n; 223 *s++ = '-'; 224 } 225 226 p = buf; 227 do { 228 *p++ = un % 10 + '0'; 229 un /= 10; 230 } while ((pad && --min_digits > 0) || un != 0); 231 232 while (p > buf) 233 *s++ = *--p | attributes; 234 235 *s = '\0'; 236 return s; 237 } 238 239 240 /*ARGSUSED*/ 241 void 242 dolist(v, c) 243 Char **v; 244 struct command *c; 245 { 246 int i, k; 247 struct stat st; 248 249 USE(c); 250 if (*++v == NULL) { 251 (void) t_search(STRNULL, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0); 252 return; 253 } 254 gflag = 0; 255 tglob(v); 256 if (gflag) { 257 v = globall(v); 258 if (v == 0) 259 stderror(ERR_NAME | ERR_NOMATCH); 260 } 261 else 262 v = gargv = saveblk(v); 263 trim(v); 264 for (k = 0; v[k] != NULL && v[k][0] != '-'; k++) 265 continue; 266 if (v[k]) { 267 /* 268 * We cannot process a flag therefore we let ls do it right. 269 */ 270 Char *lspath; 271 struct command *t; 272 struct wordent cmd, *nextword, *lastword; 273 Char *cp; 274 struct varent *vp; 275 276 #ifdef BSDSIGS 277 sigmask_t omask = 0; 278 279 if (setintr) 280 omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT); 281 #else /* !BSDSIGS */ 282 (void) sighold(SIGINT); 283 #endif /* BSDSIGS */ 284 if (seterr) { 285 xfree((ptr_t) seterr); 286 seterr = NULL; 287 } 288 289 lspath = STRls; 290 STRmCF[1] = 'C'; 291 STRmCF[3] = '\0'; 292 /* Look at listflags, to add -A to the flags, to get a path 293 of ls if necessary */ 294 if ((vp = adrof(STRlistflags)) != NULL && vp->vec != NULL && 295 vp->vec[0] != STRNULL) { 296 if (vp->vec[1] != NULL && vp->vec[1][0] != '\0') 297 lspath = vp->vec[1]; 298 for (cp = vp->vec[0]; *cp; cp++) 299 switch (*cp) { 300 case 'x': 301 STRmCF[1] = 'x'; 302 break; 303 case 'a': 304 STRmCF[3] = 'a'; 305 break; 306 case 'A': 307 STRmCF[3] = 'A'; 308 break; 309 default: 310 break; 311 } 312 } 313 314 cmd.word = STRNULL; 315 lastword = &cmd; 316 nextword = (struct wordent *) xcalloc(1, sizeof cmd); 317 nextword->word = Strsave(lspath); 318 lastword->next = nextword; 319 nextword->prev = lastword; 320 lastword = nextword; 321 nextword = (struct wordent *) xcalloc(1, sizeof cmd); 322 nextword->word = Strsave(STRmCF); 323 lastword->next = nextword; 324 nextword->prev = lastword; 325 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE) 326 if (dspmbyte_ls) { 327 lastword = nextword; 328 nextword = (struct wordent *) xcalloc(1, sizeof cmd); 329 nextword->word = Strsave(STRmmliteral); 330 lastword->next = nextword; 331 nextword->prev = lastword; 332 } 333 #endif 334 #ifdef COLOR_LS_F 335 if (color_context_ls) { 336 lastword = nextword; 337 nextword = (struct wordent *) xcalloc(1, sizeof cmd); 338 nextword->word = Strsave(STRmmcolormauto); 339 lastword->next = nextword; 340 nextword->prev = lastword; 341 } 342 #endif /* COLOR_LS_F */ 343 lastword = nextword; 344 for (cp = *v; cp; cp = *++v) { 345 nextword = (struct wordent *) xcalloc(1, sizeof cmd); 346 nextword->word = quote(Strsave(cp)); 347 lastword->next = nextword; 348 nextword->prev = lastword; 349 lastword = nextword; 350 } 351 lastword->next = &cmd; 352 cmd.prev = lastword; 353 354 /* build a syntax tree for the command. */ 355 t = syntax(cmd.next, &cmd, 0); 356 if (seterr) 357 stderror(ERR_OLD); 358 /* expand aliases like process() does */ 359 /* alias(&cmd); */ 360 /* execute the parse tree. */ 361 execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, FALSE); 362 /* done. free the lex list and parse tree. */ 363 freelex(&cmd), freesyn(t); 364 if (setintr) 365 #ifdef BSDSIGS 366 (void) sigsetmask(omask); 367 #else /* !BSDSIGS */ 368 (void) sigrelse(SIGINT); 369 #endif /* BSDSIGS */ 370 } 371 else { 372 Char *dp, *tmp, buf[MAXPATHLEN]; 373 374 for (k = 0, i = 0; v[k] != NULL; k++) { 375 tmp = dnormalize(v[k], symlinks == SYM_IGNORE); 376 dp = &tmp[Strlen(tmp) - 1]; 377 if (*dp == '/' && dp != tmp) 378 #ifdef apollo 379 if (dp != &tmp[1]) 380 #endif /* apollo */ 381 *dp = '\0'; 382 if (stat(short2str(tmp), &st) == -1) { 383 if (k != i) { 384 if (i != 0) 385 xputchar('\n'); 386 print_by_column(STRNULL, &v[i], k - i, FALSE); 387 } 388 xprintf("%S: %s.\n", tmp, strerror(errno)); 389 i = k + 1; 390 } 391 else if (S_ISDIR(st.st_mode)) { 392 Char *cp; 393 394 if (k != i) { 395 if (i != 0) 396 xputchar('\n'); 397 print_by_column(STRNULL, &v[i], k - i, FALSE); 398 } 399 if (k != 0 && v[1] != NULL) 400 xputchar('\n'); 401 xprintf("%S:\n", tmp); 402 for (cp = tmp, dp = buf; *cp; *dp++ = (*cp++ | QUOTE)) 403 continue; 404 if ( 405 #ifdef WINNT_NATIVE 406 (dp[-1] != (Char) (':' | QUOTE)) && 407 #endif /* WINNT_NATIVE */ 408 (dp[-1] != (Char) ('/' | QUOTE))) 409 *dp++ = '/'; 410 else 411 dp[-1] &= TRIM; 412 *dp = '\0'; 413 (void) t_search(buf, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0); 414 i = k + 1; 415 } 416 xfree((ptr_t) tmp); 417 } 418 if (k != i) { 419 if (i != 0) 420 xputchar('\n'); 421 print_by_column(STRNULL, &v[i], k - i, FALSE); 422 } 423 } 424 425 if (gargv) { 426 blkfree(gargv); 427 gargv = 0; 428 } 429 } 430 431 extern int GotTermCaps; 432 433 /*ARGSUSED*/ 434 void 435 dotelltc(v, c) 436 Char **v; 437 struct command *c; 438 { 439 USE(v); 440 USE(c); 441 if (!GotTermCaps) 442 GetTermCaps(); 443 TellTC(); 444 } 445 446 /*ARGSUSED*/ 447 void 448 doechotc(v, c) 449 Char **v; 450 struct command *c; 451 { 452 USE(c); 453 if (!GotTermCaps) 454 GetTermCaps(); 455 EchoTC(++v); 456 } 457 458 /*ARGSUSED*/ 459 void 460 dosettc(v, c) 461 Char **v; 462 struct command *c; 463 { 464 char tv[2][BUFSIZE]; 465 466 USE(c); 467 if (!GotTermCaps) 468 GetTermCaps(); 469 470 (void) strcpy(tv[0], short2str(v[1])); 471 (void) strcpy(tv[1], short2str(v[2])); 472 SetTC(tv[0], tv[1]); 473 } 474 475 /* The dowhich() is by: 476 * Andreas Luik <luik@isaak.isa.de> 477 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung 478 * Azenberstr. 35 479 * D-7000 Stuttgart 1 480 * West-Germany 481 * Thanks!! 482 */ 483 int 484 cmd_expand(cmd, str) 485 Char *cmd; 486 Char *str; 487 { 488 struct wordent lexp[3]; 489 struct varent *vp; 490 int rv = TRUE; 491 492 lexp[0].next = &lexp[1]; 493 lexp[1].next = &lexp[2]; 494 lexp[2].next = &lexp[0]; 495 496 lexp[0].prev = &lexp[2]; 497 lexp[1].prev = &lexp[0]; 498 lexp[2].prev = &lexp[1]; 499 500 lexp[0].word = STRNULL; 501 lexp[2].word = STRret; 502 503 if ((vp = adrof1(cmd, &aliases)) != NULL && vp->vec != NULL) { 504 if (str == NULL) { 505 xprintf(CGETS(22, 1, "%S: \t aliased to "), cmd); 506 blkpr(vp->vec); 507 xputchar('\n'); 508 } 509 else 510 blkexpand(vp->vec, str); 511 } 512 else { 513 lexp[1].word = cmd; 514 rv = tellmewhat(lexp, str); 515 } 516 return rv; 517 } 518 519 520 /*ARGSUSED*/ 521 void 522 dowhich(v, c) 523 Char **v; 524 struct command *c; 525 { 526 int rv = TRUE; 527 USE(c); 528 529 #ifdef notdef 530 /* 531 * We don't want to glob dowhich args because we lose quoteing 532 * E.g. which \ls if ls is aliased will not work correctly if 533 * we glob here. 534 */ 535 gflag = 0, tglob(v); 536 if (gflag) { 537 v = globall(v); 538 if (v == 0) 539 stderror(ERR_NAME | ERR_NOMATCH); 540 } 541 else { 542 v = gargv = saveblk(v); 543 trim(v); 544 } 545 #endif 546 547 while (*++v) 548 rv &= cmd_expand(*v, NULL); 549 550 if (!rv) 551 set(STRstatus, Strsave(STR1), VAR_READWRITE); 552 553 #ifdef notdef 554 /* Again look at the comment above; since we don't glob, we don't free */ 555 if (gargv) 556 blkfree(gargv), gargv = 0; 557 #endif 558 } 559 560 /* PWP: a hack to start up your stopped editor on a single keystroke */ 561 /* jbs - fixed hack so it worked :-) 3/28/89 */ 562 563 struct process * 564 find_stop_ed() 565 { 566 struct process *pp, *retp; 567 const char *ep, *vp; 568 char *cp, *p; 569 int epl, vpl, pstatus; 570 571 if ((ep = getenv("EDITOR")) != NULL) { /* if we have a value */ 572 if ((p = strrchr(ep, '/')) != NULL) /* if it has a path */ 573 ep = p + 1; /* then we want only the last part */ 574 } 575 else 576 ep = "ed"; 577 578 if ((vp = getenv("VISUAL")) != NULL) { /* if we have a value */ 579 if ((p = strrchr(vp, '/')) != NULL) /* and it has a path */ 580 vp = p + 1; /* then we want only the last part */ 581 } 582 else 583 vp = "vi"; 584 585 for (vpl = 0; vp[vpl] && !isspace((unsigned char)vp[vpl]); vpl++) 586 continue; 587 for (epl = 0; ep[epl] && !isspace((unsigned char)ep[epl]); epl++) 588 continue; 589 590 if (pcurrent == NULL) /* see if we have any jobs */ 591 return NULL; /* nope */ 592 593 retp = NULL; 594 for (pp = proclist.p_next; pp; pp = pp->p_next) 595 if (pp->p_procid == pp->p_jobid) { 596 597 /* 598 * Only foreground an edit session if it is suspended. Some GUI 599 * editors have may be happily running in a separate window, no 600 * point in foregrounding these if they're already running - webb 601 */ 602 pstatus = (int) (pp->p_flags & PALLSTATES); 603 if (pstatus != PINTERRUPTED && pstatus != PSTOPPED && 604 pstatus != PSIGNALED) 605 continue; 606 607 p = short2str(pp->p_command); 608 /* get the first word */ 609 for (cp = p; *cp && !isspace((unsigned char) *cp); cp++) 610 continue; 611 *cp = '\0'; 612 613 if ((cp = strrchr(p, '/')) != NULL) /* and it has a path */ 614 cp = cp + 1; /* then we want only the last part */ 615 else 616 cp = p; /* else we get all of it */ 617 618 /* if we find either in the current name, fg it */ 619 if (strncmp(ep, cp, (size_t) epl) == 0 || 620 strncmp(vp, cp, (size_t) vpl) == 0) { 621 622 /* 623 * If there is a choice, then choose the current process if 624 * available, or the previous process otherwise, or else 625 * anything will do - Robert Webb (robertw@mulga.cs.mu.oz.au). 626 */ 627 if (pp == pcurrent) 628 return pp; 629 else if (retp == NULL || pp == pprevious) 630 retp = pp; 631 } 632 } 633 634 return retp; /* Will be NULL if we didn't find a job */ 635 } 636 637 void 638 fg_proc_entry(pp) 639 struct process *pp; 640 { 641 #ifdef BSDSIGS 642 sigmask_t omask; 643 #endif 644 jmp_buf_t osetexit; 645 int ohaderr; 646 Char oGettingInput; 647 648 getexit(osetexit); 649 650 #ifdef BSDSIGS 651 omask = sigblock(sigmask(SIGINT)); 652 #else 653 (void) sighold(SIGINT); 654 #endif 655 oGettingInput = GettingInput; 656 GettingInput = 0; 657 658 ohaderr = haderr; /* we need to ignore setting of haderr due to 659 * process getting stopped by a signal */ 660 if (setexit() == 0) { /* come back here after pjwait */ 661 pendjob(); 662 (void) alarm(0); /* No autologout */ 663 if (!pstart(pp, 1)) { 664 pp->p_procid = 0; 665 stderror(ERR_BADJOB, pp->p_command, strerror(errno)); 666 } 667 pjwait(pp); 668 } 669 setalarm(1); /* Autologout back on */ 670 resexit(osetexit); 671 haderr = ohaderr; 672 GettingInput = oGettingInput; 673 674 #ifdef BSDSIGS 675 (void) sigsetmask(omask); 676 #else /* !BSDSIGS */ 677 (void) sigrelse(SIGINT); 678 #endif /* BSDSIGS */ 679 680 } 681 682 static char * 683 xgetpass(prm) 684 const char *prm; 685 { 686 static char pass[PASSMAX + 1]; 687 int fd, i; 688 signalfun_t sigint; 689 690 sigint = (signalfun_t) sigset(SIGINT, SIG_IGN); 691 (void) Rawmode(); /* Make sure, cause we want echo off */ 692 if ((fd = open("/dev/tty", O_RDWR|O_LARGEFILE)) == -1) 693 fd = SHIN; 694 695 xprintf("%s", prm); flush(); 696 for (i = 0;;) { 697 if (read(fd, &pass[i], 1) < 1 || pass[i] == '\n') 698 break; 699 if (i < PASSMAX) 700 i++; 701 } 702 703 pass[i] = '\0'; 704 705 if (fd != SHIN) 706 (void) close(fd); 707 (void) sigset(SIGINT, sigint); 708 709 return(pass); 710 } 711 712 #ifndef NO_CRYPT 713 #ifndef __STDC__ 714 extern char *crypt __P(()); 715 #endif 716 #ifdef __linux__ 717 #include <crypt.h> 718 #endif 719 #endif 720 721 /* 722 * Ask the user for his login password to continue working 723 * On systems that have a shadow password, this will only 724 * work for root, but what can we do? 725 * 726 * If we fail to get the password, then we log the user out 727 * immediately 728 */ 729 /*ARGSUSED*/ 730 static void 731 auto_lock(n) 732 int n; 733 { 734 #ifndef NO_CRYPT 735 736 int i; 737 char *srpp = NULL; 738 struct passwd *pw; 739 740 #undef XCRYPT 741 742 #if defined(HAVE_AUTH_H) 743 744 struct authorization *apw; 745 extern char *crypt16 __P((const char *, const char *)); 746 747 # define XCRYPT(a, b) crypt16(a, b) 748 749 if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */ 750 (apw = getauthuid(euid)) != NULL) /* enhanced ultrix passwd */ 751 srpp = apw->a_password; 752 753 #elif defined(HAVE_SHADOW_H) 754 755 struct spwd *spw; 756 757 # define XCRYPT(a, b) crypt(a, b) 758 759 if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */ 760 (spw = getspnam(pw->pw_name)) != NULL) /* shadowed passwd */ 761 srpp = spw->sp_pwdp; 762 763 #else 764 765 #define XCRYPT(a, b) crypt(a, b) 766 767 #if !defined(__MVS__) 768 if ((pw = getpwuid(euid)) != NULL) /* effective user passwd */ 769 srpp = pw->pw_passwd; 770 #endif /* !MVS */ 771 772 #endif 773 774 if (srpp == NULL) { 775 auto_logout(0); 776 /*NOTREACHED*/ 777 return; 778 } 779 780 setalarm(0); /* Not for locking any more */ 781 #ifdef BSDSIGS 782 (void) sigsetmask(sigblock(0) & ~(sigmask(SIGALRM))); 783 #else /* !BSDSIGS */ 784 (void) sigrelse(SIGALRM); 785 #endif /* BSDSIGS */ 786 xputchar('\n'); 787 for (i = 0; i < 5; i++) { 788 const char *crpp; 789 char *pp; 790 #ifdef AFS 791 char *afsname; 792 Char *safs; 793 794 if ((safs = varval(STRafsuser)) != STRNULL) 795 afsname = short2str(safs); 796 else 797 if ((afsname = getenv("AFSUSER")) == NULL) 798 afsname = pw->pw_name; 799 #endif 800 pp = xgetpass("Password:"); 801 802 crpp = XCRYPT(pp, srpp); 803 if ((strcmp(crpp, srpp) == 0) 804 #ifdef AFS 805 || (ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, 806 afsname, /* name */ 807 NULL, /* instance */ 808 NULL, /* realm */ 809 pp, /* password */ 810 0, /* lifetime */ 811 0, 0, /* spare */ 812 NULL) /* reason */ 813 == 0) 814 #endif /* AFS */ 815 ) { 816 (void) memset(pp, 0, PASSMAX); 817 if (GettingInput && !just_signaled) { 818 (void) Rawmode(); 819 ClearLines(); 820 ClearDisp(); 821 Refresh(); 822 } 823 just_signaled = 0; 824 return; 825 } 826 xprintf(CGETS(22, 2, "\nIncorrect passwd for %s\n"), pw->pw_name); 827 } 828 #endif /* NO_CRYPT */ 829 auto_logout(0); 830 USE(n); 831 } 832 833 834 static void 835 auto_logout(n) 836 int n; 837 { 838 USE(n); 839 xprintf("auto-logout\n"); 840 /* Don't leave the tty in raw mode */ 841 if (editing) 842 (void) Cookedmode(); 843 (void) close(SHIN); 844 set(STRlogout, Strsave(STRautomatic), VAR_READWRITE); 845 child = 1; 846 #ifdef TESLA 847 do_logout = 1; 848 #endif /* TESLA */ 849 GettingInput = FALSE; /* make flush() work to write hist files. Huber*/ 850 goodbye(NULL, NULL); 851 } 852 853 RETSIGTYPE 854 /*ARGSUSED*/ 855 alrmcatch(snum) 856 int snum; 857 { 858 USE(snum); 859 #ifdef UNRELSIGS 860 if (snum) 861 (void) sigset(SIGALRM, alrmcatch); 862 #endif /* UNRELSIGS */ 863 864 (*alm_fun)(0); 865 866 setalarm(1); 867 } 868 869 /* 870 * Karl Kleinpaste, 21oct1983. 871 * Added precmd(), which checks for the alias 872 * precmd in aliases. If it's there, the alias 873 * is executed as a command. This is done 874 * after mailchk() and just before print- 875 * ing the prompt. Useful for things like printing 876 * one's current directory just before each command. 877 */ 878 void 879 precmd() 880 { 881 #ifdef BSDSIGS 882 sigmask_t omask; 883 884 omask = sigblock(sigmask(SIGINT)); 885 #else /* !BSDSIGS */ 886 (void) sighold(SIGINT); 887 #endif /* BSDSIGS */ 888 if (precmd_active) { /* an error must have been caught */ 889 aliasrun(2, STRunalias, STRprecmd); 890 xprintf(CGETS(22, 3, "Faulty alias 'precmd' removed.\n")); 891 goto leave; 892 } 893 precmd_active = 1; 894 if (!whyles && adrof1(STRprecmd, &aliases)) 895 aliasrun(1, STRprecmd, NULL); 896 leave: 897 precmd_active = 0; 898 #ifdef BSDSIGS 899 (void) sigsetmask(omask); 900 #else /* !BSDSIGS */ 901 (void) sigrelse(SIGINT); 902 #endif /* BSDSIGS */ 903 } 904 905 void 906 postcmd() 907 { 908 #ifdef BSDSIGS 909 sigmask_t omask; 910 911 omask = sigblock(sigmask(SIGINT)); 912 #else /* !BSDSIGS */ 913 (void) sighold(SIGINT); 914 #endif /* BSDSIGS */ 915 if (postcmd_active) { /* an error must have been caught */ 916 aliasrun(2, STRunalias, STRpostcmd); 917 xprintf(CGETS(22, 3, "Faulty alias 'postcmd' removed.\n")); 918 goto leave; 919 } 920 postcmd_active = 1; 921 if (!whyles && adrof1(STRpostcmd, &aliases)) 922 aliasrun(1, STRpostcmd, NULL); 923 leave: 924 postcmd_active = 0; 925 #ifdef BSDSIGS 926 (void) sigsetmask(omask); 927 #else /* !BSDSIGS */ 928 (void) sigrelse(SIGINT); 929 #endif /* BSDSIGS */ 930 } 931 932 /* 933 * Paul Placeway 11/24/87 Added cwd_cmd by hacking precmd() into 934 * submission... Run every time $cwd is set (after it is set). Useful 935 * for putting your machine and cwd (or anything else) in an xterm title 936 * space. 937 */ 938 void 939 cwd_cmd() 940 { 941 #ifdef BSDSIGS 942 sigmask_t omask; 943 944 omask = sigblock(sigmask(SIGINT)); 945 #else /* !BSDSIGS */ 946 (void) sighold(SIGINT); 947 #endif /* BSDSIGS */ 948 if (cwdcmd_active) { /* an error must have been caught */ 949 aliasrun(2, STRunalias, STRcwdcmd); 950 xprintf(CGETS(22, 4, "Faulty alias 'cwdcmd' removed.\n")); 951 goto leave; 952 } 953 cwdcmd_active = 1; 954 if (!whyles && adrof1(STRcwdcmd, &aliases)) 955 aliasrun(1, STRcwdcmd, NULL); 956 leave: 957 cwdcmd_active = 0; 958 #ifdef BSDSIGS 959 (void) sigsetmask(omask); 960 #else /* !BSDSIGS */ 961 (void) sigrelse(SIGINT); 962 #endif /* BSDSIGS */ 963 } 964 965 /* 966 * Joachim Hoenig 07/16/91 Added beep_cmd, run every time tcsh wishes 967 * to beep the terminal bell. Useful for playing nice sounds instead. 968 */ 969 void 970 beep_cmd() 971 { 972 #ifdef BSDSIGS 973 sigmask_t omask; 974 975 omask = sigblock(sigmask(SIGINT)); 976 #else /* !BSDSIGS */ 977 (void) sighold(SIGINT); 978 #endif /* BSDSIGS */ 979 if (beepcmd_active) { /* an error must have been caught */ 980 aliasrun(2, STRunalias, STRbeepcmd); 981 xprintf(CGETS(22, 5, "Faulty alias 'beepcmd' removed.\n")); 982 } 983 else { 984 beepcmd_active = 1; 985 if (!whyles && adrof1(STRbeepcmd, &aliases)) 986 aliasrun(1, STRbeepcmd, NULL); 987 } 988 beepcmd_active = 0; 989 #ifdef BSDSIGS 990 (void) sigsetmask(omask); 991 #else /* !BSDSIGS */ 992 (void) sigrelse(SIGINT); 993 #endif /* BSDSIGS */ 994 } 995 996 997 /* 998 * Karl Kleinpaste, 18 Jan 1984. 999 * Added period_cmd(), which executes the alias "periodic" every 1000 * $tperiod minutes. Useful for occasional checking of msgs and such. 1001 */ 1002 void 1003 period_cmd() 1004 { 1005 Char *vp; 1006 time_t t, interval; 1007 #ifdef BSDSIGS 1008 sigmask_t omask; 1009 1010 omask = sigblock(sigmask(SIGINT)); 1011 #else /* !BSDSIGS */ 1012 (void) sighold(SIGINT); 1013 #endif /* BSDSIGS */ 1014 if (periodic_active) { /* an error must have been caught */ 1015 aliasrun(2, STRunalias, STRperiodic); 1016 xprintf(CGETS(22, 6, "Faulty alias 'periodic' removed.\n")); 1017 goto leave; 1018 } 1019 periodic_active = 1; 1020 if (!whyles && adrof1(STRperiodic, &aliases)) { 1021 vp = varval(STRtperiod); 1022 if (vp == STRNULL) { 1023 aliasrun(1, STRperiodic, NULL); 1024 goto leave; 1025 } 1026 interval = getn(vp); 1027 (void) time(&t); 1028 if (t - t_period >= interval * 60) { 1029 t_period = t; 1030 aliasrun(1, STRperiodic, NULL); 1031 } 1032 } 1033 leave: 1034 periodic_active = 0; 1035 #ifdef BSDSIGS 1036 (void) sigsetmask(omask); 1037 #else /* !BSDSIGS */ 1038 (void) sigrelse(SIGINT); 1039 #endif /* BSDSIGS */ 1040 } 1041 1042 1043 /* 1044 * GrP Greg Parker May 2001 1045 * Added job_cmd(), which is run every time a job is started or 1046 * foregrounded. The command is passed a single argument, the string 1047 * used to start the job originally. With precmd, useful for setting 1048 * xterm titles. 1049 * Cloned from cwd_cmd(). 1050 */ 1051 void 1052 job_cmd(args) 1053 Char *args; 1054 { 1055 #ifdef BSDSIGS 1056 sigmask_t omask; 1057 1058 omask = sigblock(sigmask(SIGINT)); 1059 #else /* !BSDSIGS */ 1060 (void) sighold(SIGINT); 1061 #endif /* BSDSIGS */ 1062 if (jobcmd_active) { /* an error must have been caught */ 1063 aliasrun(2, STRunalias, STRjobcmd); 1064 xprintf(CGETS(22, 14, "Faulty alias 'jobcmd' removed.\n")); 1065 goto leave; 1066 } 1067 jobcmd_active = 1; 1068 if (!whyles && adrof1(STRjobcmd, &aliases)) { 1069 struct process *pp = pcurrjob; /* put things back after the hook */ 1070 aliasrun(2, STRjobcmd, args); 1071 pcurrjob = pp; 1072 } 1073 leave: 1074 jobcmd_active = 0; 1075 #ifdef BSDSIGS 1076 (void) sigsetmask(omask); 1077 #else /* !BSDSIGS */ 1078 (void) sigrelse(SIGINT); 1079 #endif /* BSDSIGS */ 1080 } 1081 1082 1083 /* 1084 * Karl Kleinpaste, 21oct1983. 1085 * Set up a one-word alias command, for use for special things. 1086 * This code is based on the mainline of process(). 1087 */ 1088 void 1089 aliasrun(cnt, s1, s2) 1090 int cnt; 1091 Char *s1, *s2; 1092 { 1093 struct wordent w, *new1, *new2; /* for holding alias name */ 1094 struct command *t = NULL; 1095 jmp_buf_t osetexit; 1096 int status; 1097 1098 getexit(osetexit); 1099 if (seterr) { 1100 xfree((ptr_t) seterr); 1101 seterr = NULL; /* don't repeatedly print err msg. */ 1102 } 1103 w.word = STRNULL; 1104 new1 = (struct wordent *) xcalloc(1, sizeof w); 1105 new1->word = Strsave(s1); 1106 if (cnt == 1) { 1107 /* build a lex list with one word. */ 1108 w.next = w.prev = new1; 1109 new1->next = new1->prev = &w; 1110 } 1111 else { 1112 /* build a lex list with two words. */ 1113 new2 = (struct wordent *) xcalloc(1, sizeof w); 1114 new2->word = Strsave(s2); 1115 w.next = new2->prev = new1; 1116 new1->next = w.prev = new2; 1117 new1->prev = new2->next = &w; 1118 } 1119 1120 /* Save the old status */ 1121 status = getn(varval(STRstatus)); 1122 1123 /* expand aliases like process() does. */ 1124 alias(&w); 1125 /* build a syntax tree for the command. */ 1126 t = syntax(w.next, &w, 0); 1127 if (seterr) 1128 stderror(ERR_OLD); 1129 1130 psavejob(); 1131 1132 1133 /* catch any errors here */ 1134 if (setexit() == 0) 1135 /* execute the parse tree. */ 1136 /* 1137 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de> 1138 * was execute(t, tpgrp); 1139 */ 1140 execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, TRUE); 1141 /* done. free the lex list and parse tree. */ 1142 freelex(&w), freesyn(t); 1143 if (haderr) { 1144 haderr = 0; 1145 /* 1146 * Either precmd, or cwdcmd, or periodic had an error. Call it again so 1147 * that it is removed 1148 */ 1149 if (precmd_active) 1150 precmd(); 1151 if (postcmd_active) 1152 postcmd(); 1153 #ifdef notdef 1154 /* 1155 * XXX: On the other hand, just interrupting them causes an error too. 1156 * So if we hit ^C in the middle of cwdcmd or periodic the alias gets 1157 * removed. We don't want that. Note that we want to remove precmd 1158 * though, cause that could lead into an infinite loop. This should be 1159 * fixed correctly, but then haderr should give us the whole exit 1160 * status not just true or false. 1161 */ 1162 else if (cwdcmd_active) 1163 cwd_cmd(); 1164 else if (beepcmd_active) 1165 beep_cmd(); 1166 else if (periodic_active) 1167 period_cmd(); 1168 #endif /* notdef */ 1169 } 1170 /* reset the error catcher to the old place */ 1171 resexit(osetexit); 1172 prestjob(); 1173 pendjob(); 1174 /* Restore status */ 1175 set(STRstatus, putn(status), VAR_READWRITE); 1176 } 1177 1178 void 1179 setalarm(lck) 1180 int lck; 1181 { 1182 struct varent *vp; 1183 Char *cp; 1184 unsigned alrm_time = 0, logout_time, lock_time; 1185 time_t cl, nl, sched_dif; 1186 1187 if ((vp = adrof(STRautologout)) != NULL && vp->vec != NULL) { 1188 if ((cp = vp->vec[0]) != 0) { 1189 if ((logout_time = (unsigned) atoi(short2str(cp)) * 60) > 0) { 1190 #ifdef SOLARIS2 1191 /* 1192 * Solaris alarm(2) uses a timer based in clock ticks 1193 * internally so it multiplies our value with CLK_TCK... 1194 * Of course that can overflow leading to unexpected 1195 * results, so we clip it here. Grr. Where is that 1196 * documented folks? 1197 */ 1198 if (logout_time >= 0x7fffffff / CLK_TCK) 1199 logout_time = 0x7fffffff / CLK_TCK; 1200 #endif /* SOLARIS2 */ 1201 alrm_time = logout_time; 1202 alm_fun = auto_logout; 1203 } 1204 } 1205 if ((cp = vp->vec[1]) != 0) { 1206 if ((lock_time = (unsigned) atoi(short2str(cp)) * 60) > 0) { 1207 if (lck) { 1208 if (alrm_time == 0 || lock_time < alrm_time) { 1209 alrm_time = lock_time; 1210 alm_fun = auto_lock; 1211 } 1212 } 1213 else /* lock_time always < alrm_time */ 1214 if (alrm_time) 1215 alrm_time -= lock_time; 1216 } 1217 } 1218 } 1219 if ((nl = sched_next()) != -1) { 1220 (void) time(&cl); 1221 sched_dif = nl > cl ? nl - cl : 0; 1222 if ((alrm_time == 0) || ((unsigned) sched_dif < alrm_time)) { 1223 alrm_time = ((unsigned) sched_dif) + 1; 1224 alm_fun = sched_run; 1225 } 1226 } 1227 (void) alarm(alrm_time); /* Autologout ON */ 1228 } 1229 1230 #undef RMDEBUG /* For now... */ 1231 1232 void 1233 rmstar(cp) 1234 struct wordent *cp; 1235 { 1236 struct wordent *we, *args; 1237 struct wordent *tmp, *del; 1238 1239 #ifdef RMDEBUG 1240 static Char STRrmdebug[] = {'r', 'm', 'd', 'e', 'b', 'u', 'g', '\0'}; 1241 Char *tag; 1242 #endif /* RMDEBUG */ 1243 Char *charac; 1244 char c; 1245 int ask, doit, star = 0, silent = 0; 1246 1247 if (!adrof(STRrmstar)) 1248 return; 1249 #ifdef RMDEBUG 1250 tag = varval(STRrmdebug); 1251 #endif /* RMDEBUG */ 1252 we = cp->next; 1253 while (*we->word == ';' && we != cp) 1254 we = we->next; 1255 while (we != cp) { 1256 #ifdef RMDEBUG 1257 if (*tag) 1258 xprintf(CGETS(22, 7, "parsing command line\n")); 1259 #endif /* RMDEBUG */ 1260 if (!Strcmp(we->word, STRrm)) { 1261 args = we->next; 1262 ask = (*args->word != '-'); 1263 while (*args->word == '-' && !silent) { /* check options */ 1264 for (charac = (args->word + 1); *charac && !silent; charac++) 1265 silent = (*charac == 'i' || *charac == 'f'); 1266 args = args->next; 1267 } 1268 ask = (ask || (!ask && !silent)); 1269 if (ask) { 1270 for (; !star && *args->word != ';' 1271 && args != cp; args = args->next) 1272 if (!Strcmp(args->word, STRstar)) 1273 star = 1; 1274 if (ask && star) { 1275 xprintf(CGETS(22, 8, 1276 "Do you really want to delete all files? [n/y] ")); 1277 flush(); 1278 (void) force_read(SHIN, &c, 1); 1279 /* 1280 * Perhaps we should use the yesexpr from the 1281 * actual locale 1282 */ 1283 doit = (strchr(CGETS(22, 14, "Yy"), c) != NULL); 1284 while (c != '\n' && force_read(SHIN, &c, 1) == 1) 1285 continue; 1286 if (!doit) { 1287 /* remove the command instead */ 1288 #ifdef RMDEBUG 1289 if (*tag) 1290 xprintf(CGETS(22, 9, 1291 "skipping deletion of files!\n")); 1292 #endif /* RMDEBUG */ 1293 for (tmp = we; 1294 *tmp->word != '\n' && 1295 *tmp->word != ';' && tmp != cp;) { 1296 tmp->prev->next = tmp->next; 1297 tmp->next->prev = tmp->prev; 1298 xfree((ptr_t) tmp->word); 1299 del = tmp; 1300 tmp = tmp->next; 1301 xfree((ptr_t) del); 1302 } 1303 if (*tmp->word == ';') { 1304 tmp->prev->next = tmp->next; 1305 tmp->next->prev = tmp->prev; 1306 xfree((ptr_t) tmp->word); 1307 del = tmp; 1308 tmp = tmp->next; 1309 xfree((ptr_t) del); 1310 } 1311 we = tmp; 1312 continue; 1313 } 1314 } 1315 } 1316 } 1317 for (we = we->next; 1318 *we->word != ';' && we != cp; 1319 we = we->next) 1320 continue; 1321 if (*we->word == ';') 1322 we = we->next; 1323 } 1324 #ifdef RMDEBUG 1325 if (*tag) { 1326 xprintf(CGETS(22, 10, "command line now is:\n")); 1327 for (we = cp->next; we != cp; we = we->next) 1328 xprintf("%S ", we->word); 1329 } 1330 #endif /* RMDEBUG */ 1331 return; 1332 } 1333 1334 #ifdef BSDJOBS 1335 /* Check if command is in continue list 1336 and do a "aliasing" if it exists as a job in background */ 1337 1338 #undef CNDEBUG /* For now */ 1339 void 1340 continue_jobs(cp) 1341 struct wordent *cp; 1342 { 1343 struct wordent *we; 1344 struct process *pp, *np; 1345 Char *cmd, *continue_list, *continue_args_list; 1346 1347 #ifdef CNDEBUG 1348 Char *tag; 1349 static Char STRcndebug[] = 1350 {'c', 'n', 'd', 'e', 'b', 'u', 'g', '\0'}; 1351 #endif /* CNDEBUG */ 1352 int in_cont_list, in_cont_arg_list; 1353 1354 1355 #ifdef CNDEBUG 1356 tag = varval(STRcndebug); 1357 #endif /* CNDEBUG */ 1358 continue_list = varval(STRcontinue); 1359 continue_args_list = varval(STRcontinue_args); 1360 if (*continue_list == '\0' && *continue_args_list == '\0') 1361 return; 1362 1363 we = cp->next; 1364 while (*we->word == ';' && we != cp) 1365 we = we->next; 1366 while (we != cp) { 1367 #ifdef CNDEBUG 1368 if (*tag) 1369 xprintf(CGETS(22, 11, "parsing command line\n")); 1370 #endif /* CNDEBUG */ 1371 cmd = we->word; 1372 in_cont_list = inlist(continue_list, cmd); 1373 in_cont_arg_list = inlist(continue_args_list, cmd); 1374 if (in_cont_list || in_cont_arg_list) { 1375 #ifdef CNDEBUG 1376 if (*tag) 1377 xprintf(CGETS(22, 12, "in one of the lists\n")); 1378 #endif /* CNDEBUG */ 1379 np = NULL; 1380 for (pp = proclist.p_next; pp; pp = pp->p_next) { 1381 if (prefix(cmd, pp->p_command)) { 1382 if (pp->p_index) { 1383 np = pp; 1384 break; 1385 } 1386 } 1387 } 1388 if (np) { 1389 insert(we, in_cont_arg_list); 1390 } 1391 } 1392 for (we = we->next; 1393 *we->word != ';' && we != cp; 1394 we = we->next) 1395 continue; 1396 if (*we->word == ';') 1397 we = we->next; 1398 } 1399 #ifdef CNDEBUG 1400 if (*tag) { 1401 xprintf(CGETS(22, 13, "command line now is:\n")); 1402 for (we = cp->next; we != cp; we = we->next) 1403 xprintf("%S ", we->word); 1404 } 1405 #endif /* CNDEBUG */ 1406 return; 1407 } 1408 1409 /* The actual "aliasing" of for backgrounds() is done here 1410 with the aid of insert_we(). */ 1411 static void 1412 insert(pl, file_args) 1413 struct wordent *pl; 1414 int file_args; 1415 { 1416 struct wordent *now, *last; 1417 Char *cmd, *bcmd, *cp1, *cp2; 1418 int cmd_len; 1419 Char *upause = STRunderpause; 1420 int p_len = (int) Strlen(upause); 1421 1422 cmd_len = (int) Strlen(pl->word); 1423 cmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 1) * sizeof(Char))); 1424 (void) Strcpy(cmd, pl->word); 1425 /* Do insertions at beginning, first replace command word */ 1426 1427 if (file_args) { 1428 now = pl; 1429 xfree((ptr_t) now->word); 1430 now->word = (Char *) xcalloc(1, (size_t) (5 * sizeof(Char))); 1431 (void) Strcpy(now->word, STRecho); 1432 1433 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent)); 1434 now->word = (Char *) xcalloc(1, (size_t) (6 * sizeof(Char))); 1435 (void) Strcpy(now->word, STRbackqpwd); 1436 insert_we(now, pl); 1437 1438 for (last = now; *last->word != '\n' && *last->word != ';'; 1439 last = last->next) 1440 continue; 1441 1442 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent)); 1443 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char))); 1444 (void) Strcpy(now->word, STRgt); 1445 insert_we(now, last->prev); 1446 1447 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent)); 1448 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char))); 1449 (void) Strcpy(now->word, STRbang); 1450 insert_we(now, last->prev); 1451 1452 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent)); 1453 now->word = (Char *) xcalloc(1, (size_t) cmd_len + p_len + 4); 1454 cp1 = now->word; 1455 cp2 = cmd; 1456 *cp1++ = '~'; 1457 *cp1++ = '/'; 1458 *cp1++ = '.'; 1459 while ((*cp1++ = *cp2++) != '\0') 1460 continue; 1461 cp1--; 1462 cp2 = upause; 1463 while ((*cp1++ = *cp2++) != '\0') 1464 continue; 1465 insert_we(now, last->prev); 1466 1467 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent)); 1468 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char))); 1469 (void) Strcpy(now->word, STRsemi); 1470 insert_we(now, last->prev); 1471 bcmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 2) * sizeof(Char))); 1472 cp1 = bcmd; 1473 cp2 = cmd; 1474 *cp1++ = '%'; 1475 while ((*cp1++ = *cp2++) != '\0') 1476 continue; 1477 now = (struct wordent *) xcalloc(1, (size_t) (sizeof(struct wordent))); 1478 now->word = bcmd; 1479 insert_we(now, last->prev); 1480 } 1481 else { 1482 struct wordent *del; 1483 1484 now = pl; 1485 xfree((ptr_t) now->word); 1486 now->word = (Char *) xcalloc(1, 1487 (size_t) ((cmd_len + 2) * sizeof(Char))); 1488 cp1 = now->word; 1489 cp2 = cmd; 1490 *cp1++ = '%'; 1491 while ((*cp1++ = *cp2++) != '\0') 1492 continue; 1493 for (now = now->next; 1494 *now->word != '\n' && *now->word != ';' && now != pl;) { 1495 now->prev->next = now->next; 1496 now->next->prev = now->prev; 1497 xfree((ptr_t) now->word); 1498 del = now; 1499 now = now->next; 1500 xfree((ptr_t) del); 1501 } 1502 } 1503 } 1504 1505 static void 1506 insert_we(new, where) 1507 struct wordent *new, *where; 1508 { 1509 1510 new->prev = where; 1511 new->next = where->next; 1512 where->next = new; 1513 new->next->prev = new; 1514 } 1515 1516 static int 1517 inlist(list, name) 1518 Char *list, *name; 1519 { 1520 Char *l, *n; 1521 1522 l = list; 1523 n = name; 1524 1525 while (*l && *n) { 1526 if (*l == *n) { 1527 l++; 1528 n++; 1529 if (*n == '\0' && (*l == ' ' || *l == '\0')) 1530 return (1); 1531 else 1532 continue; 1533 } 1534 else { 1535 while (*l && *l != ' ') 1536 l++; /* skip to blank */ 1537 while (*l && *l == ' ') 1538 l++; /* and find first nonblank character */ 1539 n = name; 1540 } 1541 } 1542 return (0); 1543 } 1544 1545 #endif /* BSDJOBS */ 1546 1547 1548 /* 1549 * Implement a small cache for tilde names. This is used primarily 1550 * to expand tilde names to directories, but also 1551 * we can find users from their home directories for the tilde 1552 * prompt, on machines where yp lookup is slow this can be a big win... 1553 * As with any cache this can run out of sync, rehash can sync it again. 1554 */ 1555 static struct tildecache { 1556 Char *user; 1557 Char *home; 1558 int hlen; 1559 } *tcache = NULL; 1560 1561 #define TILINCR 10 1562 int tlength = 0; 1563 static int tsize = TILINCR; 1564 1565 static int 1566 tildecompare(p1, p2) 1567 struct tildecache *p1, *p2; 1568 { 1569 return Strcmp(p1->user, p2->user); 1570 } 1571 1572 static Char * 1573 gethomedir(us) 1574 Char *us; 1575 { 1576 struct passwd *pp; 1577 #ifdef HESIOD 1578 char **res, **res1, *cp; 1579 Char *rp; 1580 #endif /* HESIOD */ 1581 1582 pp = getpwnam(short2str(us)); 1583 #ifdef YPBUGS 1584 fix_yp_bugs(); 1585 #endif /* YPBUGS */ 1586 if (pp != NULL) { 1587 #if 0 1588 /* Don't return if root */ 1589 if (pp->pw_dir[0] == '/' && pp->pw_dir[1] == '\0') 1590 return NULL; 1591 else 1592 #endif 1593 return Strsave(str2short(pp->pw_dir)); 1594 } 1595 #ifdef HESIOD 1596 res = hes_resolve(short2str(us), "filsys"); 1597 rp = NULL; 1598 if (res != NULL) { 1599 if ((*res) != NULL) { 1600 /* 1601 * Look at the first token to determine how to interpret 1602 * the rest of it. 1603 * Yes, strtok is evil (it's not thread-safe), but it's also 1604 * easy to use. 1605 */ 1606 cp = strtok(*res, " "); 1607 if (strcmp(cp, "AFS") == 0) { 1608 /* next token is AFS pathname.. */ 1609 cp = strtok(NULL, " "); 1610 if (cp != NULL) 1611 rp = Strsave(str2short(cp)); 1612 } else if (strcmp(cp, "NFS") == 0) { 1613 cp = NULL; 1614 if ((strtok(NULL, " ")) && /* skip remote pathname */ 1615 (strtok(NULL, " ")) && /* skip host */ 1616 (strtok(NULL, " ")) && /* skip mode */ 1617 (cp = strtok(NULL, " "))) { 1618 rp = Strsave(str2short(cp)); 1619 } 1620 } 1621 } 1622 for (res1 = res; *res1; res1++) 1623 free(*res1); 1624 #if 0 1625 /* Don't return if root */ 1626 if (rp != NULL && rp[0] == '/' && rp[1] == '\0') { 1627 xfree((ptr_t)rp); 1628 rp = NULL; 1629 } 1630 #endif 1631 return rp; 1632 } 1633 #endif /* HESIOD */ 1634 return NULL; 1635 } 1636 1637 Char * 1638 gettilde(us) 1639 Char *us; 1640 { 1641 struct tildecache *bp1, *bp2, *bp; 1642 Char *hd; 1643 1644 /* Ignore NIS special names */ 1645 if (*us == '+' || *us == '-') 1646 return NULL; 1647 1648 if (tcache == NULL) 1649 tcache = (struct tildecache *) xmalloc((size_t) (TILINCR * 1650 sizeof(struct tildecache))); 1651 /* 1652 * Binary search 1653 */ 1654 for (bp1 = tcache, bp2 = tcache + tlength; bp1 < bp2;) { 1655 int i; 1656 1657 bp = bp1 + ((bp2 - bp1) >> 1); 1658 if ((i = *us - *bp->user) == 0 && (i = Strcmp(us, bp->user)) == 0) 1659 return (bp->home); 1660 if (i < 0) 1661 bp2 = bp; 1662 else 1663 bp1 = bp + 1; 1664 } 1665 /* 1666 * Not in the cache, try to get it from the passwd file 1667 */ 1668 hd = gethomedir(us); 1669 if (hd == NULL) 1670 return NULL; 1671 1672 /* 1673 * Update the cache 1674 */ 1675 tcache[tlength].user = Strsave(us); 1676 tcache[tlength].home = hd; 1677 tcache[tlength++].hlen = (int) Strlen(hd); 1678 1679 qsort((ptr_t) tcache, (size_t) tlength, sizeof(struct tildecache), 1680 (int (*) __P((const void *, const void *))) tildecompare); 1681 1682 if (tlength == tsize) { 1683 tsize += TILINCR; 1684 tcache = (struct tildecache *) xrealloc((ptr_t) tcache, 1685 (size_t) (tsize * 1686 sizeof(struct tildecache))); 1687 } 1688 return (hd); 1689 } 1690 1691 /* 1692 * Return the username if the directory path passed contains a 1693 * user's home directory in the tilde cache, otherwise return NULL 1694 * hm points to the place where the path became different. 1695 * Special case: Our own home directory. 1696 * If we are passed a null pointer, then we flush the cache. 1697 */ 1698 Char * 1699 getusername(hm) 1700 Char **hm; 1701 { 1702 Char *h, *p; 1703 int i, j; 1704 1705 if (hm == NULL) { 1706 for (i = 0; i < tlength; i++) { 1707 xfree((ptr_t) tcache[i].home); 1708 xfree((ptr_t) tcache[i].user); 1709 } 1710 xfree((ptr_t) tcache); 1711 tlength = 0; 1712 tsize = TILINCR; 1713 tcache = NULL; 1714 return NULL; 1715 } 1716 if (((h = varval(STRhome)) != STRNULL) && 1717 (Strncmp(p = *hm, h, (size_t) (j = (int) Strlen(h))) == 0) && 1718 (p[j] == '/' || p[j] == '\0')) { 1719 *hm = &p[j]; 1720 return STRNULL; 1721 } 1722 for (i = 0; i < tlength; i++) 1723 if ((Strncmp(p = *hm, tcache[i].home, (size_t) 1724 (j = tcache[i].hlen)) == 0) && (p[j] == '/' || p[j] == '\0')) { 1725 *hm = &p[j]; 1726 return tcache[i].user; 1727 } 1728 return NULL; 1729 } 1730 1731 #ifdef OBSOLETE 1732 /* 1733 * PWP: read a bunch of aliases out of a file QUICKLY. The format 1734 * is almost the same as the result of saying "alias > FILE", except 1735 * that saying "aliases > FILE" does not expand non-letters to printable 1736 * sequences. 1737 */ 1738 /*ARGSUSED*/ 1739 void 1740 doaliases(v, c) 1741 Char **v; 1742 struct command *c; 1743 { 1744 jmp_buf_t oldexit; 1745 Char **vec, *lp; 1746 int fd; 1747 Char buf[BUFSIZE], line[BUFSIZE]; 1748 char tbuf[BUFSIZE + 1], *tmp; 1749 extern int output_raw; /* PWP: in sh.print.c */ 1750 1751 USE(c); 1752 v++; 1753 if (*v == 0) { 1754 output_raw = 1; 1755 plist(&aliases, VAR_ALL); 1756 output_raw = 0; 1757 return; 1758 } 1759 1760 gflag = 0, tglob(v); 1761 if (gflag) { 1762 v = globall(v); 1763 if (v == 0) 1764 stderror(ERR_NAME | ERR_NOMATCH); 1765 } 1766 else { 1767 v = gargv = saveblk(v); 1768 trim(v); 1769 } 1770 1771 if ((fd = open(tmp = short2str(*v), O_RDONLY|O_LARGEFILE)) < 0) 1772 stderror(ERR_NAME | ERR_SYSTEM, tmp, strerror(errno)); 1773 1774 getexit(oldexit); 1775 if (setexit() == 0) { 1776 for (;;) { 1777 Char *p = NULL; 1778 int n = 0; 1779 lp = line; 1780 for (;;) { 1781 if (n <= 0) { 1782 int i; 1783 1784 if ((n = read(fd, tbuf, BUFSIZE)) <= 0) { 1785 #ifdef convex 1786 stderror(ERR_SYSTEM, progname, strerror(errno)); 1787 #endif /* convex */ 1788 goto eof; 1789 } 1790 for (i = 0; i < n; i++) 1791 buf[i] = (Char) tbuf[i]; 1792 p = buf; 1793 } 1794 n--; 1795 if ((*lp++ = *p++) == '\n') { 1796 lp[-1] = '\0'; 1797 break; 1798 } 1799 } 1800 for (lp = line; *lp; lp++) { 1801 if (isspc(*lp)) { 1802 *lp++ = '\0'; 1803 while (isspc(*lp)) 1804 lp++; 1805 vec = (Char **) xmalloc((size_t) 1806 (2 * sizeof(Char **))); 1807 vec[0] = Strsave(lp); 1808 vec[1] = NULL; 1809 setq(strip(line), vec, &aliases, VAR_READWRITE); 1810 break; 1811 } 1812 } 1813 } 1814 } 1815 1816 eof: 1817 (void) close(fd); 1818 tw_cmd_free(); 1819 if (gargv) 1820 blkfree(gargv), gargv = 0; 1821 resexit(oldexit); 1822 } 1823 #endif /* OBSOLETE */ 1824 1825 1826 /* 1827 * set the shell-level var to 1 or apply change to it. 1828 */ 1829 void 1830 shlvl(val) 1831 int val; 1832 { 1833 char *cp; 1834 1835 if ((cp = getenv("SHLVL")) != NULL) { 1836 1837 if (loginsh) 1838 val = 1; 1839 else 1840 val += atoi(cp); 1841 1842 if (val <= 0) { 1843 if (adrof(STRshlvl) != NULL) 1844 unsetv(STRshlvl); 1845 Unsetenv(STRKSHLVL); 1846 } 1847 else { 1848 Char buff[BUFSIZE]; 1849 1850 (void) Itoa(val, buff, 0, 0); 1851 set(STRshlvl, Strsave(buff), VAR_READWRITE); 1852 tsetenv(STRKSHLVL, buff); 1853 } 1854 } 1855 else { 1856 set(STRshlvl, SAVE("1"), VAR_READWRITE); 1857 tsetenv(STRKSHLVL, str2short("1")); 1858 } 1859 } 1860 1861 1862 /* fixio(): 1863 * Try to recover from a read error 1864 */ 1865 int 1866 fixio(fd, e) 1867 int fd, e; 1868 { 1869 switch (e) { 1870 case -1: /* Make sure that the code is reachable */ 1871 1872 #ifdef EWOULDBLOCK 1873 case EWOULDBLOCK: 1874 # define FDRETRY 1875 #endif /* EWOULDBLOCK */ 1876 1877 #if defined(POSIX) && defined(EAGAIN) 1878 # if !defined(EWOULDBLOCK) || EWOULDBLOCK != EAGAIN 1879 case EAGAIN: 1880 # define FDRETRY 1881 # endif /* !EWOULDBLOCK || EWOULDBLOCK != EAGAIN */ 1882 #endif /* POSIX && EAGAIN */ 1883 1884 e = 0; 1885 #ifdef FDRETRY 1886 # ifdef F_SETFL 1887 /* 1888 * Great! we have on suns 3 flavors and 5 names... 1889 * I hope that will cover everything. 1890 * I added some more defines... many systems have different defines. 1891 * Rather than dealing with getting the right includes, we'll just 1892 * cover all the known possibilities here. -- sterling@netcom.com 1893 */ 1894 # ifndef O_NONBLOCK 1895 # define O_NONBLOCK 0 1896 # endif /* O_NONBLOCK */ 1897 # ifndef O_NDELAY 1898 # define O_NDELAY 0 1899 # endif /* O_NDELAY */ 1900 # ifndef FNBIO 1901 # define FNBIO 0 1902 # endif /* FNBIO */ 1903 # ifndef _FNBIO 1904 # define _FNBIO 0 1905 # endif /* _FNBIO */ 1906 # ifndef FNONBIO 1907 # define FNONBIO 0 1908 # endif /* FNONBIO */ 1909 # ifndef FNONBLOCK 1910 # define FNONBLOCK 0 1911 # endif /* FNONBLOCK */ 1912 # ifndef _FNONBLOCK 1913 # define _FNONBLOCK 0 1914 # endif /* _FNONBLOCK */ 1915 # ifndef FNDELAY 1916 # define FNDELAY 0 1917 # endif /* FNDELAY */ 1918 # ifndef _FNDELAY 1919 # define _FNDELAY 0 1920 # endif /* _FNDELAY */ 1921 # ifndef FNDLEAY /* Some linux versions have this typo */ 1922 # define FNDLEAY 0 1923 # endif /* FNDLEAY */ 1924 if ((e = fcntl(fd, F_GETFL, 0)) == -1) 1925 return -1; 1926 1927 e &= ~(O_NDELAY|O_NONBLOCK|FNBIO|_FNBIO|FNONBIO|FNONBLOCK|_FNONBLOCK| 1928 FNDELAY|_FNDELAY|FNDLEAY); /* whew! */ 1929 if (fcntl(fd, F_SETFL, e) == -1) 1930 return -1; 1931 else 1932 e = 1; 1933 # endif /* F_SETFL */ 1934 1935 # ifdef FIONBIO 1936 e = 0; 1937 if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1) 1938 return -1; 1939 else 1940 e = 1; 1941 # endif /* FIONBIO */ 1942 1943 #endif /* FDRETRY */ 1944 return e ? 0 : -1; 1945 1946 case EINTR: 1947 return 0; 1948 1949 default: 1950 return -1; 1951 } 1952 } 1953 1954 /* collate(): 1955 * String collation 1956 */ 1957 int 1958 collate(a, b) 1959 const Char *a; 1960 const Char *b; 1961 { 1962 int rv; 1963 #ifdef SHORT_STRINGS 1964 /* This strips the quote bit as a side effect */ 1965 char *sa = strsave(short2str(a)); 1966 char *sb = strsave(short2str(b)); 1967 #else 1968 char *sa = strip(strsave(a)); 1969 char *sb = strip(strsave(b)); 1970 #endif /* SHORT_STRINGS */ 1971 1972 #if defined(NLS) && !defined(NOSTRCOLL) 1973 errno = 0; /* strcoll sets errno, another brain-damage */ 1974 1975 rv = strcoll(sa, sb); 1976 1977 /* 1978 * We should be checking for errno != 0, but some systems 1979 * forget to reset errno to 0. So we only check for the 1980 * only documented valid errno value for strcoll [EINVAL] 1981 */ 1982 if (errno == EINVAL) { 1983 xfree((ptr_t) sa); 1984 xfree((ptr_t) sb); 1985 stderror(ERR_SYSTEM, "strcoll", strerror(errno)); 1986 } 1987 #else 1988 rv = strcmp(sa, sb); 1989 #endif /* NLS && !NOSTRCOLL */ 1990 1991 xfree((ptr_t) sa); 1992 xfree((ptr_t) sb); 1993 1994 return rv; 1995 } 1996 1997 #ifdef HASHBANG 1998 /* 1999 * From: peter@zeus.dialix.oz.au (Peter Wemm) 2000 * If exec() fails look first for a #! [word] [word] .... 2001 * If it is, splice the header into the argument list and retry. 2002 */ 2003 #define HACKBUFSZ 1024 /* Max chars in #! vector */ 2004 #define HACKVECSZ 128 /* Max words in #! vector */ 2005 int 2006 hashbang(fd, vp) 2007 int fd; 2008 Char ***vp; 2009 { 2010 unsigned char lbuf[HACKBUFSZ]; 2011 char *sargv[HACKVECSZ]; 2012 unsigned char *p, *ws; 2013 int sargc = 0; 2014 #ifdef WINNT_NATIVE 2015 int fw = 0; /* found at least one word */ 2016 int first_word = 0; 2017 #endif /* WINNT_NATIVE */ 2018 2019 if (read(fd, (char *) lbuf, HACKBUFSZ) <= 0) 2020 return -1; 2021 2022 ws = 0; /* word started = 0 */ 2023 2024 for (p = lbuf; p < &lbuf[HACKBUFSZ]; ) 2025 switch (*p) { 2026 case ' ': 2027 case '\t': 2028 #ifdef WINNT_NATIVE 2029 case '\r': 2030 #endif /* WINNT_NATIVE */ 2031 if (ws) { /* a blank after a word.. save it */ 2032 *p = '\0'; 2033 #ifndef WINNT_NATIVE 2034 if (sargc < HACKVECSZ - 1) 2035 sargv[sargc++] = ws; 2036 ws = NULL; 2037 #else /* WINNT_NATIVE */ 2038 if (sargc < HACKVECSZ - 1) { 2039 sargv[sargc] = first_word ? NULL: hb_subst(ws); 2040 if (sargv[sargc] == NULL) 2041 sargv[sargc] = ws; 2042 sargc++; 2043 } 2044 ws = NULL; 2045 fw = 1; 2046 first_word = 1; 2047 #endif /* WINNT_NATIVE */ 2048 } 2049 p++; 2050 continue; 2051 2052 case '\0': /* Whoa!! what the hell happened */ 2053 return -1; 2054 2055 case '\n': /* The end of the line. */ 2056 if ( 2057 #ifdef WINNT_NATIVE 2058 fw || 2059 #endif /* WINNT_NATIVE */ 2060 ws) { /* terminate the last word */ 2061 *p = '\0'; 2062 #ifndef WINNT_NATIVE 2063 if (sargc < HACKVECSZ - 1) 2064 sargv[sargc++] = ws; 2065 #else /* WINNT_NATIVE */ 2066 if (sargc < HACKVECSZ - 1) { /* deal with the 1-word case */ 2067 sargv[sargc] = first_word? NULL : hb_subst(ws); 2068 if (sargv[sargc] == NULL) 2069 sargv[sargc] = ws; 2070 sargc++; 2071 } 2072 #endif /* !WINNT_NATIVE */ 2073 } 2074 sargv[sargc] = NULL; 2075 ws = NULL; 2076 if (sargc > 0) { 2077 *vp = blk2short(sargv); 2078 return 0; 2079 } 2080 else 2081 return -1; 2082 2083 default: 2084 if (!ws) /* Start a new word? */ 2085 ws = p; 2086 p++; 2087 break; 2088 } 2089 return -1; 2090 } 2091 #endif /* HASHBANG */ 2092 2093 #ifdef REMOTEHOST 2094 2095 static RETSIGTYPE 2096 palarm(snum) 2097 int snum; 2098 { 2099 USE(snum); 2100 #ifdef UNRELSIGS 2101 if (snum) 2102 (void) sigset(snum, SIG_IGN); 2103 #endif /* UNRELSIGS */ 2104 (void) alarm(0); 2105 reset(); 2106 } 2107 2108 2109 static void 2110 getremotehost() 2111 { 2112 const char *host = NULL; 2113 #ifdef INET6 2114 struct sockaddr_storage saddr; 2115 socklen_t len = sizeof(struct sockaddr_storage); 2116 static char hbuf[NI_MAXHOST]; 2117 #else 2118 struct hostent* hp; 2119 struct sockaddr_in saddr; 2120 socklen_t len = sizeof(struct sockaddr_in); 2121 #endif 2122 #ifdef HAVE_STRUCT_UTMP_UT_HOST 2123 char *sptr = NULL; 2124 #endif 2125 2126 #ifdef INET6 2127 if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 && 2128 (saddr.ss_family == AF_INET6 || saddr.ss_family == AF_INET)) { 2129 int flag = NI_NUMERICHOST; 2130 2131 #ifdef NI_WITHSCOPEID 2132 flag |= NI_WITHSCOPEID; 2133 #endif 2134 getnameinfo((struct sockaddr *)&saddr, len, hbuf, sizeof(hbuf), 2135 NULL, 0, flag); 2136 host = hbuf; 2137 #else 2138 if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 && 2139 saddr.sin_family == AF_INET) { 2140 #if 0 2141 if ((hp = gethostbyaddr((char *)&saddr.sin_addr, sizeof(struct in_addr), 2142 AF_INET)) != NULL) 2143 host = hp->h_name; 2144 else 2145 #endif 2146 host = inet_ntoa(saddr.sin_addr); 2147 #endif 2148 } 2149 #ifdef HAVE_STRUCT_UTMP_UT_HOST 2150 else { 2151 char *ptr; 2152 char *name = utmphost(); 2153 /* Avoid empty names and local X displays */ 2154 if (name != NULL && *name != '\0' && *name != ':') { 2155 struct in_addr addr; 2156 2157 /* Look for host:display.screen */ 2158 /* 2159 * There is conflict with IPv6 address and X DISPLAY. So, 2160 * we assume there is no IPv6 address in utmp and don't 2161 * touch here. 2162 */ 2163 if ((sptr = strchr(name, ':')) != NULL) 2164 *sptr = '\0'; 2165 /* Leave IPv4 address as is */ 2166 /* 2167 * we use inet_addr here, not inet_aton because many systems 2168 * have not caught up yet. 2169 */ 2170 addr.s_addr = inet_addr(name); 2171 if (addr.s_addr != (unsigned int)~0) 2172 host = name; 2173 else { 2174 if (sptr != name) { 2175 #ifdef INET6 2176 char *s, *domain; 2177 char dbuf[MAXHOSTNAMELEN], cbuf[MAXHOSTNAMELEN]; 2178 struct addrinfo hints, *res = NULL; 2179 2180 memset(&hints, 0, sizeof(hints)); 2181 hints.ai_family = PF_UNSPEC; 2182 hints.ai_socktype = SOCK_STREAM; 2183 hints.ai_flags = AI_PASSIVE | AI_CANONNAME; 2184 if (strlen(name) < utmphostsize()) 2185 { 2186 if (getaddrinfo(name, NULL, &hints, &res) != 0) 2187 res = NULL; 2188 } else if (gethostname(dbuf, sizeof(dbuf) - 1) == 0 && 2189 (domain = strchr(dbuf, '.')) != NULL) { 2190 for (s = strchr(name, '.'); 2191 s != NULL; s = strchr(s + 1, '.')) { 2192 if (*(s + 1) != '\0' && 2193 (ptr = strstr(domain, s)) != NULL) { 2194 len = s - name; 2195 if (len + strlen(ptr) >= sizeof(cbuf)) 2196 break; 2197 strncpy(cbuf, name, len); 2198 strcpy(cbuf + len, ptr); 2199 if (getaddrinfo(cbuf, NULL, &hints, &res) != 0) 2200 res = NULL; 2201 break; 2202 } 2203 } 2204 } 2205 if (res != NULL) { 2206 if (res->ai_canonname != NULL) { 2207 strncpy(hbuf, res->ai_canonname, sizeof(hbuf)); 2208 host = hbuf; 2209 } 2210 freeaddrinfo(res); 2211 } 2212 #else 2213 if ((hp = gethostbyname(name)) == NULL) { 2214 /* Try again eliminating the trailing domain */ 2215 if ((ptr = strchr(name, '.')) != NULL) { 2216 *ptr = '\0'; 2217 if ((hp = gethostbyname(name)) != NULL) 2218 host = hp->h_name; 2219 *ptr = '.'; 2220 } 2221 } 2222 else 2223 host = hp->h_name; 2224 #endif 2225 } 2226 } 2227 } 2228 } 2229 #endif 2230 2231 if (host) 2232 tsetenv(STRREMOTEHOST, str2short(host)); 2233 2234 #ifdef HAVE_STRUCT_UTMP_UT_HOST 2235 if (sptr) 2236 *sptr = ':'; 2237 #endif 2238 } 2239 2240 2241 /* 2242 * From: <lesv@ppvku.ericsson.se> (Lennart Svensson) 2243 */ 2244 void 2245 remotehost() 2246 { 2247 /* Don't get stuck if the resolver does not work! */ 2248 signalfun_t osig = sigset(SIGALRM, palarm); 2249 2250 jmp_buf_t osetexit; 2251 getexit(osetexit); 2252 2253 (void) alarm(2); 2254 2255 if (setexit() == 0) 2256 getremotehost(); 2257 2258 resexit(osetexit); 2259 2260 (void) alarm(0); 2261 (void) sigset(SIGALRM, osig); 2262 2263 #ifdef YPBUGS 2264 /* From: casper@fwi.uva.nl (Casper H.S. Dik), for Solaris 2.3 */ 2265 fix_yp_bugs(); 2266 #endif /* YPBUGS */ 2267 2268 } 2269 #endif /* REMOTEHOST */ 2270 2271 #ifndef WINNT_NATIVE 2272 /* 2273 * indicate if a terminal type is defined in terminfo/termcap 2274 * (by default the current term type). This allows ppl to look 2275 * for a working term type automatically in their login scripts 2276 * when using a terminal known as different things on different 2277 * platforms 2278 */ 2279 void 2280 dotermname(v, c) 2281 Char **v; 2282 struct command *c; 2283 { 2284 char *termtype; 2285 /* 2286 * Maximum size of a termcap record. We make it twice as large. 2287 */ 2288 char termcap_buffer[2048]; 2289 2290 USE(c); 2291 /* try to find which entry we should be looking for */ 2292 termtype = (v[1] == NULL ? getenv("TERM") : short2str(v[1])); 2293 if (termtype == NULL) { 2294 /* no luck - the user didn't provide one and none is 2295 * specified in the environment 2296 */ 2297 set(STRstatus, Strsave(STR1), VAR_READWRITE); 2298 return; 2299 } 2300 2301 /* 2302 * we use the termcap function - if we are using terminfo we 2303 * will end up with it's compatibility function 2304 * terminfo/termcap will be initialized with the new 2305 * type but we don't care because tcsh has cached all the things 2306 * it needs. 2307 */ 2308 if (tgetent(termcap_buffer, termtype) == 1) { 2309 xprintf("%s\n", termtype); 2310 set(STRstatus, Strsave(STR0), VAR_READWRITE); 2311 } else { 2312 set(STRstatus, Strsave(STR1), VAR_READWRITE); 2313 } 2314 } 2315 #endif /* WINNT_NATIVE */ 2316