rlogin.c revision 1.47 1 /* $NetBSD: rlogin.c,v 1.47 2020/05/03 16:32:16 christos Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)rlogin.c 8.4 (Berkeley) 4/29/95";
41 #else
42 __RCSID("$NetBSD: rlogin.c,v 1.47 2020/05/03 16:32:16 christos Exp $");
43 #endif
44 #endif /* not lint */
45
46 /*
47 * rlogin - remote login
48 */
49 #include <sys/param.h>
50 #include <sys/ioctl.h>
51 #include <sys/socket.h>
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 #include <sys/wait.h>
55
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/tcp.h>
60
61 #include <err.h>
62 #include <errno.h>
63 #include <fcntl.h>
64 #include <netdb.h>
65 #include <pwd.h>
66 #include <setjmp.h>
67 #include <signal.h>
68 #include <stdarg.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <termios.h>
73 #include <unistd.h>
74
75 #include "getport.h"
76
77 #ifndef TIOCPKT_WINDOW
78 #define TIOCPKT_WINDOW 0x80
79 #endif
80
81 /* concession to Sun */
82 #ifndef SIGUSR1
83 #define SIGUSR1 30
84 #endif
85
86 #ifndef CCEQ
87 #define CCEQ(val, c) (c == val ? val != _POSIX_VDISABLE : 0)
88 #endif
89
90 static int eight, rem;
91 static struct termios deftty;
92
93 static int noescape;
94 static u_char escapechar = '~';
95
96 #ifdef OLDSUN
97 struct winsize {
98 unsigned short ws_row, ws_col;
99 unsigned short ws_xpixel, ws_ypixel;
100 };
101 #else
102 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
103 #endif
104 static struct winsize winsize;
105
106 static void catch_child(int);
107 static void copytochild(int);
108 __dead static void doit(sigset_t *);
109 __dead static void done(int);
110 static void echo(int);
111 static u_int getescape(char *);
112 __dead static void lostpeer(int);
113 static void mode(int);
114 static void msg(const char *);
115 static void oob(int);
116 static int reader(sigset_t *);
117 static void sendwindow(void);
118 static void setsignal(int);
119 static void sigwinch(int);
120 static void stop(int);
121 __dead static void usage(void);
122 static void writer(void);
123 static void writeroob(int);
124
125 #ifdef OLDSUN
126 static int get_window_size(int, struct winsize *);
127 #endif
128
129 int
130 main(int argc, char *argv[])
131 {
132 struct passwd *pw;
133 struct servent *sp;
134 struct termios tty;
135 sigset_t imask, omask;
136 uid_t uid;
137 int argoff, ch, dflag, nflag, one;
138 int i, len, len2;
139 int family = AF_UNSPEC;
140 char *host, *p, *user, *name, term[1024] = "network";
141 speed_t ospeed;
142 struct sigaction sa;
143 char *service = NULL;
144 struct rlimit rlim;
145
146 argoff = dflag = nflag = 0;
147 one = 1;
148 host = user = NULL;
149 sp = NULL;
150
151 if (strcmp(getprogname(), "rlogin") != 0) {
152 host = strdup(getprogname());
153 if (host == NULL)
154 err(1, NULL);
155 }
156
157 /* handle "rlogin host flags" */
158 if (!host && argc > 2 && argv[1][0] != '-') {
159 host = argv[1];
160 argoff = 1;
161 }
162
163 #define OPTIONS "468dEe:l:np:"
164 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
165 switch(ch) {
166 case '4':
167 family = AF_INET;
168 break;
169 case '6':
170 family = AF_INET6;
171 break;
172 case '8':
173 eight = 1;
174 break;
175 case 'd':
176 dflag = 1;
177 break;
178 case 'E':
179 noescape = 1;
180 break;
181 case 'e':
182 noescape = 0;
183 escapechar = getescape(optarg);
184 break;
185 case 'l':
186 user = optarg;
187 break;
188 case 'n':
189 nflag = 1;
190 break;
191 case 'p':
192 sp = getport(service = optarg, "tcp");
193 break;
194 case '?':
195 default:
196 usage();
197 }
198 optind += argoff;
199 argc -= optind;
200 argv += optind;
201
202 /* if haven't gotten a host yet, do so */
203 if (!host && !(host = *argv++))
204 usage();
205
206 if (*argv)
207 usage();
208
209 if (!(pw = getpwuid(uid = getuid())))
210 errx(1, "unknown user id.");
211 /* Accept user1@host format, though "-l user2" overrides user1 */
212 p = strchr(host, '@');
213 if (p) {
214 *p = '\0';
215 if (!user && p > host)
216 user = host;
217 host = p + 1;
218 if (*host == '\0')
219 usage();
220 }
221 if ((name = strdup(pw->pw_name)) == NULL)
222 err(1, "malloc");
223 if (!user)
224 user = name;
225
226 if (sp == NULL)
227 sp = getservbyname("login", "tcp");
228 if (sp == NULL)
229 errx(1, "login/tcp: unknown service.");
230
231 if ((p = getenv("TERM")) != NULL)
232 (void)strlcpy(term, p, sizeof(term));
233 len = strlen(term);
234 if (len < (int)(sizeof(term) - 1) && tcgetattr(0, &tty) == 0) {
235 /* start at 2 to include the / */
236 for (ospeed = i = cfgetospeed(&tty), len2 = 2; i > 9; len2++)
237 i /= 10;
238
239 if (len + len2 < (int)sizeof(term))
240 (void)snprintf(term + len, len2 + 1, "/%d", ospeed);
241 }
242
243 (void)get_window_size(0, &winsize);
244
245 sigemptyset(&sa.sa_mask);
246 sa.sa_flags = SA_RESTART;
247 sa.sa_handler = lostpeer;
248 (void)sigaction(SIGPIPE, &sa, NULL);
249 /* will use SIGUSR1 for window size hack, so hold it off */
250 sigemptyset(&imask);
251 sigaddset(&imask, SIGURG);
252 sigaddset(&imask, SIGUSR1);
253 (void)sigprocmask(SIG_SETMASK, &imask, &omask);
254 /*
255 * We set SIGURG and SIGUSR1 below so that an
256 * incoming signal will be held pending rather than being
257 * discarded. Note that these routines will be ready to get
258 * a signal by the time that they are unblocked below.
259 */
260 sa.sa_handler = copytochild;
261 (void)sigaction(SIGURG, &sa, NULL);
262 sa.sa_handler = writeroob;
263 (void)sigaction(SIGUSR1, &sa, NULL);
264
265 /* don't dump core */
266 rlim.rlim_cur = rlim.rlim_max = 0;
267 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
268 warn("setrlimit");
269
270 rem = rcmd_af(&host, sp->s_port, name, user, term, 0, family);
271 if (rem < 0)
272 exit(1);
273
274 if (dflag &&
275 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
276 warn("setsockopt DEBUG (ignored)");
277 if (nflag &&
278 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
279 warn("setsockopt NODELAY (ignored)");
280
281 {
282 struct sockaddr_storage ss;
283 socklen_t sslen = sizeof(ss);
284 if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0
285 && ((struct sockaddr *)&ss)->sa_family == AF_INET) {
286 one = IPTOS_LOWDELAY;
287 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
288 sizeof(int)) < 0)
289 warn("setsockopt TOS (ignored)");
290 }
291 }
292
293 (void)setuid(uid);
294 doit(&omask);
295 /*NOTREACHED*/
296 return (0);
297 }
298
299 static pid_t child;
300
301 static void
302 doit(sigset_t *smask)
303 {
304 struct sigaction sa;
305
306 sigemptyset(&sa.sa_mask);
307 sa.sa_flags = SA_RESTART;
308 sa.sa_handler = SIG_IGN;
309 (void)sigaction(SIGINT, &sa, NULL);
310 setsignal(SIGHUP);
311 setsignal(SIGQUIT);
312 mode(1);
313 child = fork();
314 if (child == -1) {
315 warn("fork");
316 done(1);
317 }
318 if (child == 0) {
319 mode(1);
320 if (reader(smask) == 0) {
321 msg("connection closed.");
322 exit(0);
323 }
324 sleep(1);
325 msg("\aconnection closed.");
326 exit(1);
327 }
328
329 /*
330 * We may still own the socket, and may have a pending SIGURG (or might
331 * receive one soon) that we really want to send to the reader. When
332 * one of these comes in, the trap copytochild simply copies such
333 * signals to the child. We can now unblock SIGURG and SIGUSR1
334 * that were set above.
335 */
336 (void)sigprocmask(SIG_SETMASK, smask, NULL);
337 sa.sa_handler = catch_child;
338 (void)sigaction(SIGCHLD, &sa, NULL);
339 writer();
340 msg("closed connection.");
341 done(0);
342 }
343
344 /* trap a signal, unless it is being ignored. */
345 static void
346 setsignal(int sig)
347 {
348 struct sigaction isa, osa;
349 sigset_t isigs, osigs;
350
351 sigemptyset(&isigs);
352 sigaddset(&isigs, sig);
353 sigprocmask(SIG_BLOCK, &isigs, &osigs);
354
355 sigemptyset(&isa.sa_mask);
356 isa.sa_handler = exit;
357 isa.sa_flags = SA_RESTART;
358 (void)sigaction(sig, &isa, &osa);
359 if (osa.sa_handler == SIG_IGN)
360 (void)sigaction(sig, &osa, NULL);
361
362 (void)sigprocmask(SIG_SETMASK, &osigs, NULL);
363 }
364
365 static void
366 done(int status)
367 {
368 pid_t w;
369 int wstatus;
370 struct sigaction sa;
371
372 mode(0);
373 if (child > 0) {
374 /* make sure catch_child does not snap it up */
375 sigemptyset(&sa.sa_mask);
376 sa.sa_handler = SIG_DFL;
377 sa.sa_flags = 0;
378 (void)sigaction(SIGCHLD, &sa, NULL);
379 if (kill(child, SIGKILL) >= 0)
380 while ((w = wait(&wstatus)) > 0 && w != child)
381 continue;
382 }
383 exit(status);
384 }
385
386 static int dosigwinch;
387
388 /*
389 * This is called when the reader process gets the out-of-band (urgent)
390 * request to turn on the window-changing protocol.
391 */
392 static void
393 writeroob(int signo)
394 {
395 struct sigaction sa;
396
397 if (dosigwinch == 0) {
398 sendwindow();
399 sigemptyset(&sa.sa_mask);
400 sa.sa_handler = sigwinch;
401 sa.sa_flags = SA_RESTART;
402 (void)sigaction(SIGWINCH, &sa, NULL);
403 }
404 dosigwinch = 1;
405 }
406
407 static void
408 catch_child(int signo)
409 {
410 int status;
411 pid_t pid;
412
413 for (;;) {
414 pid = waitpid(-1, &status, WNOHANG|WUNTRACED);
415 if (pid == 0)
416 return;
417 /* if the child (reader) dies, just quit */
418 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
419 done(WEXITSTATUS(status) | WTERMSIG(status));
420 }
421 /* NOTREACHED */
422 }
423
424 /*
425 * writer: write to remote: 0 -> line.
426 * ~. terminate
427 * ~^Z suspend rlogin process.
428 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
429 */
430 static void
431 writer(void)
432 {
433 int bol, local;
434 ssize_t n;
435 char c;
436
437 bol = 1; /* beginning of line */
438 local = 0;
439 for (;;) {
440 n = read(STDIN_FILENO, &c, 1);
441 if (n <= 0) {
442 if (n < 0 && errno == EINTR)
443 continue;
444 break;
445 }
446 /*
447 * If we're at the beginning of the line and recognize a
448 * command character, then we echo locally. Otherwise,
449 * characters are echo'd remotely. If the command character
450 * is doubled, this acts as a force and local echo is
451 * suppressed.
452 */
453 if (bol) {
454 bol = 0;
455 if (!noescape && c == escapechar) {
456 local = 1;
457 continue;
458 }
459 } else if (local) {
460 local = 0;
461 if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
462 echo((int)c);
463 break;
464 }
465 if (CCEQ(deftty.c_cc[VSUSP], c)) {
466 bol = 1;
467 echo((int)c);
468 stop(1);
469 continue;
470 }
471 if (CCEQ(deftty.c_cc[VDSUSP], c)) {
472 bol = 1;
473 echo((int)c);
474 stop(0);
475 continue;
476 }
477 if (c != escapechar) {
478 (void)write(rem, &escapechar, 1);
479 }
480 }
481
482 if (write(rem, &c, 1) == 0) {
483 msg("line gone");
484 break;
485 }
486
487 bol = CCEQ(deftty.c_cc[VKILL], c) ||
488 CCEQ(deftty.c_cc[VEOF], c) ||
489 CCEQ(deftty.c_cc[VINTR], c) ||
490 CCEQ(deftty.c_cc[VSUSP], c) ||
491 c == '\r' || c == '\n';
492 }
493 }
494
495 static void
496 echo(int i)
497 {
498 char c = (char)i;
499 char *p;
500 char buf[8];
501
502 p = buf;
503 c &= 0177;
504 *p++ = escapechar;
505 if (c < ' ') {
506 *p++ = '^';
507 *p++ = c + '@';
508 } else if (c == 0177) {
509 *p++ = '^';
510 *p++ = '?';
511 } else
512 *p++ = c;
513 *p++ = '\r';
514 *p++ = '\n';
515 (void)write(STDOUT_FILENO, buf, p - buf);
516 }
517
518 static void
519 stop(int all)
520 {
521 struct sigaction sa;
522
523 mode(0);
524 sigemptyset(&sa.sa_mask);
525 sa.sa_handler = SIG_IGN;
526 sa.sa_flags = SA_RESTART;
527 (void)sigaction(SIGCHLD, &sa, NULL);
528 (void)kill(all ? 0 : getpid(), SIGTSTP);
529 sa.sa_handler = catch_child;
530 (void)sigaction(SIGCHLD, &sa, NULL);
531 mode(1);
532 sigwinch(0); /* check for size changes */
533 }
534
535 static void
536 sigwinch(int signo)
537 {
538 struct winsize ws;
539
540 if (dosigwinch && get_window_size(0, &ws) == 0 &&
541 memcmp(&ws, &winsize, sizeof(ws))) {
542 winsize = ws;
543 sendwindow();
544 }
545 }
546
547 /*
548 * Send the window size to the server via the magic escape
549 */
550 static void
551 sendwindow(void)
552 {
553 struct winsize *wp;
554 char obuf[4 + sizeof (struct winsize)];
555
556 wp = (struct winsize *)(obuf+4);
557 obuf[0] = 0377;
558 obuf[1] = 0377;
559 obuf[2] = 's';
560 obuf[3] = 's';
561 wp->ws_row = htons(winsize.ws_row);
562 wp->ws_col = htons(winsize.ws_col);
563 wp->ws_xpixel = htons(winsize.ws_xpixel);
564 wp->ws_ypixel = htons(winsize.ws_ypixel);
565
566 (void)write(rem, obuf, sizeof(obuf));
567 }
568
569 /*
570 * reader: read from remote: line -> 1
571 */
572 #define READING 1
573 #define WRITING 2
574
575 static jmp_buf rcvtop;
576 static pid_t ppid;
577 static ssize_t rcvcnt, rcvstate;
578 static char rcvbuf[8 * 1024];
579
580 static int
581 recvx(int fd, void *buf, size_t len, int flags, int *msgflags)
582 {
583 struct msghdr msg;
584 struct iovec iov;
585 int error;
586
587 memset(&msg, 0, sizeof(msg));
588 msg.msg_iov = &iov;
589 iov.iov_base = buf;
590 iov.iov_len = len;
591 error = recvmsg(fd, &msg, flags);
592 if (error)
593 return error;
594 *msgflags = msg.msg_flags;
595 return 0;
596 }
597
598 static void
599 oob(int signo)
600 {
601 struct termios tty;
602 int atmark = 0;
603 ssize_t n, rcvd;
604 char waste[BUFSIZ], mark;
605
606 rcvd = 0;
607 while (recvx(rem, &mark, 1, MSG_OOB, &atmark) == -1) {
608 switch (errno) {
609 case EWOULDBLOCK:
610 /*
611 * Urgent data not here yet. It may not be possible
612 * to send it yet if we are blocked for output and
613 * our input buffer is full.
614 */
615 if (rcvcnt < (ssize_t)sizeof(rcvbuf)) {
616 n = read(rem, rcvbuf + rcvcnt,
617 sizeof(rcvbuf) - rcvcnt);
618 if (n <= 0)
619 return;
620 rcvd += n;
621 } else {
622 n = read(rem, waste, sizeof(waste));
623 if (n <= 0)
624 return;
625 }
626 continue;
627 default:
628 return;
629 }
630 }
631 atmark &= MSG_OOB;
632 if (mark & TIOCPKT_WINDOW) {
633 /* Let server know about window size changes */
634 (void)kill(ppid, SIGUSR1);
635 }
636 if (!eight && (mark & TIOCPKT_NOSTOP)) {
637 (void)tcgetattr(0, &tty);
638 tty.c_iflag &= ~IXON;
639 (void)tcsetattr(0, TCSANOW, &tty);
640 }
641 if (!eight && (mark & TIOCPKT_DOSTOP)) {
642 (void)tcgetattr(0, &tty);
643 tty.c_iflag |= (deftty.c_iflag & IXON);
644 (void)tcsetattr(0, TCSANOW, &tty);
645 }
646 if (mark & TIOCPKT_FLUSHWRITE) {
647 (void)tcflush(1, TCIOFLUSH);
648 if (!atmark)
649 n = read(rem, waste, sizeof (waste));
650 /*
651 * Don't want any pending data to be output, so clear the recv
652 * buffer. If we were hanging on a write when interrupted,
653 * don't want it to restart. If we were reading, restart
654 * anyway.
655 */
656 rcvcnt = 0;
657 longjmp(rcvtop, 1);
658 }
659
660 /* oob does not do FLUSHREAD (alas!) */
661
662 /*
663 * If we filled the receive buffer while a read was pending, longjmp
664 * to the top to restart appropriately. Don't abort a pending write,
665 * however, or we won't know how much was written.
666 */
667 if (rcvd && rcvstate == READING)
668 longjmp(rcvtop, 1);
669 }
670
671 /* reader: read from remote: line -> 1 */
672 static int
673 reader(sigset_t *smask)
674 {
675 pid_t pid;
676 ssize_t n, remaining;
677 char *bufp;
678 struct sigaction sa;
679
680 pid = getpid(); /* modern systems use positives for pid */
681 sigemptyset(&sa.sa_mask);
682 sa.sa_flags = SA_RESTART;
683 sa.sa_handler = SIG_IGN;
684 (void)sigaction(SIGTTOU, &sa, NULL);
685 sa.sa_handler = oob;
686 (void)sigaction(SIGURG, &sa, NULL);
687 ppid = getppid();
688 (void)fcntl(rem, F_SETOWN, pid);
689 (void)setjmp(rcvtop);
690 (void)sigprocmask(SIG_SETMASK, smask, NULL);
691 bufp = rcvbuf;
692 for (;;) {
693 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
694 rcvstate = WRITING;
695 n = write(STDOUT_FILENO, bufp, remaining);
696 if (n < 0) {
697 if (errno != EINTR)
698 return (-1);
699 continue;
700 }
701 bufp += n;
702 }
703 bufp = rcvbuf;
704 rcvcnt = 0;
705 rcvstate = READING;
706
707 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
708 if (rcvcnt == 0)
709 return (0);
710 if (rcvcnt < 0) {
711 if (errno == EINTR)
712 continue;
713 warn("read");
714 return (-1);
715 }
716 }
717 }
718
719 static void
720 mode(int f)
721 {
722 struct termios tty;
723
724 switch (f) {
725 case 0:
726 (void)tcsetattr(0, TCSANOW, &deftty);
727 break;
728 case 1:
729 (void)tcgetattr(0, &deftty);
730 tty = deftty;
731 /* This is loosely derived from sys/compat/tty_compat.c. */
732 tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
733 tty.c_iflag &= ~ICRNL;
734 tty.c_oflag &= ~OPOST;
735 tty.c_cc[VMIN] = 1;
736 tty.c_cc[VTIME] = 0;
737 if (eight) {
738 tty.c_iflag &= IXOFF;
739 tty.c_cflag &= ~(CSIZE|PARENB);
740 tty.c_cflag |= CS8;
741 }
742 (void)tcsetattr(0, TCSANOW, &tty);
743 break;
744
745 default:
746 return;
747 }
748 }
749
750 static void
751 lostpeer(int signo)
752 {
753 struct sigaction sa;
754 sa.sa_flags = SA_RESTART;
755 sa.sa_handler = SIG_IGN;
756 sigemptyset(&sa.sa_mask);
757 (void)sigaction(SIGPIPE, &sa, NULL);
758 msg("\aconnection closed.");
759 done(1);
760 }
761
762 /* copy SIGURGs to the child process. */
763 static void
764 copytochild(int signo)
765 {
766
767 (void)kill(child, SIGURG);
768 }
769
770 static void
771 msg(const char *str)
772 {
773
774 (void)fprintf(stderr, "rlogin: %s\r\n", str);
775 }
776
777 static void
778 usage(void)
779 {
780 (void)fprintf(stderr,
781 "Usage: %s [-468dEn] [-e char] [-l username] [-p port] "
782 "[username@]host\n", getprogname());
783 exit(1);
784 }
785
786 /*
787 * The following routine provides compatibility (such as it is) between older
788 * Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
789 */
790 #ifdef OLDSUN
791 static int
792 get_window_size(int fd, struct winsize *wp)
793 {
794 struct ttysize ts;
795 int error;
796
797 if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
798 return (error);
799 wp->ws_row = ts.ts_lines;
800 wp->ws_col = ts.ts_cols;
801 wp->ws_xpixel = 0;
802 wp->ws_ypixel = 0;
803 return (0);
804 }
805 #endif
806
807 static u_int
808 getescape(char *p)
809 {
810 long val;
811 size_t len;
812
813 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
814 return ((u_int)*p);
815 /* otherwise, \nnn */
816 if (*p == '\\' && len >= 2 && len <= 4) {
817 val = strtol(++p, NULL, 8);
818 for (;;) {
819 if (!*++p)
820 return ((u_int)val);
821 if (*p < '0' || *p > '8')
822 break;
823 }
824 }
825 msg("illegal option value -- e");
826 usage();
827 /* NOTREACHED */
828 return (0);
829 }
830