📄 conn.c
字号:
#endif#ifndef SYSIII ioctl(tty, TIOCHPCL, STBNULL); ioctl(tty, TIOCEXCL, STBNULL); linebaudrate = spwant;#endif return;}/* Bill Shannon recommends MR 2000, but that takes too much space on PDPs *//* Actually, the 'expect' algorithm should be rewritten. */#define MR 1000/*** * expect(str, fn) look for expected string * char *str; * * return codes: * 0 - found * FAIL - lost line or too many characters read * some character - timed out */expect(str, fn)register char *str;int fn;{ static char rdvec[MR]; register char *rp = rdvec; int kr; char nextch; if (strcmp(str, "\"\"") == SAME) return(0); *rp = 0; if (setjmp(Sjbuf)) { return(FAIL); } signal(SIGALRM, alarmtr);/* change MAXCHARTIME to MAXMSGTIME, outside while loop -- brl-bmd!dpk */ alarm(MAXMSGTIME); while (notin(str, rdvec)) { kr = read(fn, &nextch, 1); if (kr <= 0) { alarm(0); DEBUG(4, "lost line kr - %d\n, ", kr); logent("LOGIN", "LOST LINE"); return(FAIL); } { int c; c = nextch & 0177; DEBUG(4, c >= 040 ? "%c" : "\\%03o", c); } if ((*rp = nextch & 0177) != '\0') rp++;/* Check rdvec before null termination -- cmcl2!salkind */ if (rp >= rdvec + MR) { alarm(0); logent("LOGIN", "INPUT BUFFER EXCEEDED"); return(FAIL); } *rp = '\0'; } alarm(0); return(0);}/* * Determine next file descriptor that would be allocated. * This permits later closing of a file whose open was interrupted. * It is a UNIX kernel problem, but it has to be handled. * unc!smb (Steve Bellovin) probably first discovered it. */getnextfd(){ close(next_fd = open("/", 0));}/*** * sendthem(str, fn) send line of login sequence * char *str; * * return codes: none */sendthem(str, fn)register char *str;int fn;{ register char *strptr; int i, n, cr = 1; static int p_init = 0; /* Note: debugging authorized only for privileged users */ DEBUG(5, "send %s\n", str); if (!p_init) { p_init++; bld_partab(P_EVEN); } if (prefix("BREAK", str)) { sscanf(&str[5], "%1d", &i); if (i <= 0 || i > 10) i = 3; /* send break */ genbrk(fn, i); return; } if (prefix("PAUSE", str)) { sscanf(&str[5], "%1d", &i); if (i <= 0 || i > 10) i = 3; /* pause for a while */ sleep((unsigned)i); return; } if (strcmp(str, "EOT") == SAME) { p_chwrite(fn, '\04'); return; } /* LF, CR, and "" courtesy unc!smb */ /* Send a '\n' */ if (strcmp(str, "LF") == SAME) str = "\\n\\c"; /* Send a '\r' */ if (strcmp(str, "CR") == SAME) str = "\\r\\c"; /* Set parity as needed */ if (strcmp(str, "P_ZERO") == SAME) { bld_partab(P_ZERO); return; } if (strcmp(str, "P_ONE") == SAME) { bld_partab(P_ONE); return; } if (strcmp(str, "P_EVEN") == SAME) { bld_partab(P_EVEN); return; } if (strcmp(str, "P_ODD") == SAME) { bld_partab(P_ODD); return; } /* If "", just send '\r' */ if (strcmp(str, "\"\"") != SAME) for (strptr = str; *strptr; strptr++) { if (*strptr == '\\') switch(*++strptr) { case 's': DEBUG(5, "BLANK\n", ""); *strptr = ' '; break; case 'd': DEBUG(5, "DELAY\n", ""); sleep(1); continue; case 'r': DEBUG(5, "RETURN\n", ""); *strptr = '\r'; break; case 'b': if (isdigit(*(strptr+1))) { i = (*++strptr - '0'); if (i <= 0 || i > 10) i = 3; } else i = 3; /* send break */ genbrk(fn, i); continue; case 'c': if (*(strptr+1) == '\0') { DEBUG(5, "NO CR\n", ""); cr = 0; continue; } DEBUG(5, "NO CR - MIDDLE IGNORED\n", ""); continue; default: if (isdigit(strptr[1])) { i = 0; n = 0; while (isdigit(strptr[1]) && ++n <= 3) i = i*8 + (*++strptr - '0'); p_chwrite(fn, i); continue; } DEBUG(5, "BACKSLASH\n", ""); strptr--; } p_chwrite(fn, *strptr); } /* '\n' changed to '\r'--a better default. rti!trt */ if (cr) p_chwrite(fn, '\r'); return;}p_chwrite(fd, c)int fd;int c;{ char t[2]; t[0] = par_tab[c&0177]; t[1] = '\0'; ASSERT2(write(fd, t, 1) == 1, "BAD WRITE", "", t[0]);}/* * generate parity table for use by p_chwrite. */bld_partab(type)int type;{ register int i, j, n; for (i = 0; i < sizeof(par_tab); i++) { n = 0; for (j = i&(sizeof(par_tab)-1); j; j = (j-1)&j) n++; par_tab[i] = i; if (type == P_ONE || (type == P_EVEN && (n&01) != 0) || (type == P_ODD && (n&01) == 0)) par_tab[i] |= sizeof(par_tab); }}#define BSPEED B150/*** * genbrk send a break * * return codes; none */genbrk(fn, bnulls)register int fn, bnulls;{ register int ret;#ifdef SYSIII ret = ioctl(fn, TCSBRK, STBNULL); DEBUG(5, "break ioctl ret %d\n", ret);#endif#ifndef SYSIII#ifdef TIOCSBRK ret = ioctl(fn, TIOCSBRK, STBNULL); DEBUG(5, "break ioctl ret %d\n", ret);#ifdef TIOCCBRK ret = write(fn, "\0\0\0\0\0\0\0\0\0\0\0\0", bnulls); ASSERT2(ret > 0, "BAD WRITE genbrk", "", ret); sleep(1); ret = ioctl(fn, TIOCCBRK, STBNULL); DEBUG(5, "break ioctl ret %d\n", ret);#endif DEBUG(4, "ioctl 1 second break\n", STBNULL);#else struct sgttyb ttbuf; register int sospeed; ret = ioctl(fn, TIOCGETP, &ttbuf); sospeed = ttbuf.sg_ospeed; ttbuf.sg_ospeed = BSPEED; ret = ioctl(fn, TIOCSETP, &ttbuf); ret = write(fn, "\0\0\0\0\0\0\0\0\0\0\0\0", bnulls); ASSERT2(ret > 0, "BAD WRITE genbrk", "", ret); ttbuf.sg_ospeed = sospeed; ret = ioctl(fn, TIOCSETP, &ttbuf); ret = write(fn, "@", 1); ASSERT2(ret > 0, "BAD WRITE genbrk", "", ret); DEBUG(4, "sent BREAK nulls - %d\n", bnulls);#endif#endif}/*** * notin(sh, lg) check for occurrence of substring "sh" * char *sh, *lg; * * return codes: * 0 - found the string * 1 - not in the string */notin(sh, lg)register char *sh, *lg;{ while (*lg != '\0') { /* Dave Martingale: permit wild cards in 'expect' */ if (wprefix(sh, lg)) return(0); else lg++; } return(1);}/******* * ifdate(s) * char *s; * * ittvax!swatt * Allow multiple date specifications separated by '|'. * Calls ifadate, formerly "ifdate". * * return codes: * see ifadate */ifdate(s)char *s;{ register char *p; register int ret; for (p = s; p && (*p == '|' ? *++p : *p); p = index(p, '|')) if (ret = ifadate(p)) return(ret); return(0);}/******* * ifadate(s) * char *s; * * ifadate - this routine will check a string (s) * like "MoTu0800-1730" to see if the present * time is within the given limits. * SIDE EFFECT - Retrytime is set * * String alternatives: * Wk - Mo thru Fr * zero or one time means all day * Any - any day * * return codes: * 0 - not within limits * 1 - within limits */ifadate(s)char *s;{ static char *days[]={ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", 0 }; time_t clock; int rtime; int i, tl, th, tn, flag, dayok=0; struct tm *localtime(); struct tm *tp; char *index(); char *p; /* pick up retry time for failures */ /* global variable Retrytime is set here */ if ((p = index(s, ',')) == NULL) { Retrytime = RETRYTIME; } else { i = sscanf(p+1, "%d", &rtime); if (i < 1 || rtime < 5) rtime = 5; Retrytime = rtime * 60; } time(&clock); tp = localtime(&clock); while (isalpha(*s)) { for (i = 0; days[i]; i++) { if (prefix(days[i], s)) if (tp->tm_wday == i) dayok = 1; } if (prefix("Wk", s)) if (tp->tm_wday >= 1 && tp->tm_wday <= 5) dayok = 1; if (prefix("Any", s)) dayok = 1; s++; } if (dayok == 0) return(0); i = sscanf(s, "%d-%d", &tl, &th); tn = tp->tm_hour * 100 + tp->tm_min; if (i < 2) return(1); if (th < tl) flag = 0; /* set up for crossover 2400 test */ else flag = 1; if ((tn >= tl && tn <= th) || (tn >= th && tn <= tl)) /* test for crossover 2400 */ return(flag); else return(!flag);}/*** * char * * lastc(s) return pointer to last character * char *s; * */char *lastc(s)register char *s;{ while (*s != '\0') s++; return(s);}/*** * char * * fdig(cp) find first digit in string * * return - pointer to first digit in string or end of string */char *fdig(cp)register char *cp;{ register char *c; for (c = cp; *c; c++) if (*c >= '0' && *c <= '9') break; return(c);}/* * Compare strings: s1>s2: >0 s1==s2: 0 s1<s2: <0 * Strings are compared as if they contain all capital letters. */snccmp(s1, s2)register char *s1, *s2;{ char c1, c2; if (islower(*s1)) c1 = toupper(*s1); else c1 = *s1; if (islower(*s2)) c2 = toupper(*s2); else c2 = *s2; while (c1 == c2) { if (*s1++=='\0') return(0); s2++; if (islower(*s1)) c1 = toupper(*s1); else c1 = *s1; if (islower(*s2)) c2 = toupper(*s2); else c2 = *s2; } return(c1 - c2);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -