-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
865 lines (738 loc) · 22.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//
// main.c
// sshpassword
//
// Created by Lance Haynie on 9/6/17.
// Copyright © 2017 Haynie Research & Development. All rights reserved.
//
/* vi:set ts=8 sw=4 sta et: */
#ifndef __APPLE__
#define _XOPEN_SOURCE 600 /* for posix_openpt() */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <regex.h>
#include <time.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#define BUFFSIZE (8 * 1024)
#define DEFAULT_COUNT 1
#define DEFAULT_TIMEOUT 0
#define DEFAULT_PASSWD "password"
#define DEFAULT_PROMPT "[Pp]assword: \\{0,1\\}$"
#define DEFAULT_YESNO "(yes/no)? \\{0,1\\}$"
#define ERROR_GENERAL (200 + 1)
#define ERROR_USAGE (200 + 2)
#define ERROR_TIMEOUT (200 + 3)
#define ERROR_SYS (200 + 4)
#define ERROR_MAX_TRIES (200 + 5)
static struct {
char *progname;
bool reset_on_exit;
struct termios save_termios;
bool SIGCHLDed;
bool received_winch;
bool stdin_is_tty;
bool now_interactive;
int fd_ptym;
struct {
bool ignore_case;
bool nohup_child;
bool fatal_no_prompt;
bool auto_yesno;
char *password;
char *passwd_prompt;
char *yesno_prompt;
regex_t re_prompt;
regex_t re_yesno;
int timeout;
int tries;
bool fatal_more_tries;
char **command;
char *log_to_pty;
char *log_from_pty;
} opt;
} g;
void
usage(int exitcode)
{
printf("Usage: %s <option> <command>\n"
"\n"
" -c <N> Send at most <N> passwords (0 means infinite. Default: %d)\n"
" -p <password> The password to be sent\n"
" -p env:<var> Read password from environment variable\n"
" -p file:<file> Read password from file\n"
"\n"
"", g.progname, DEFAULT_COUNT);
exit(exitcode);
}
void
fatal(int rcode, const char *fmt, ...)
{
va_list ap;
char buf[1024];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
/* in case stdout and stderr are the same */
fflush(stdout);
fprintf(stderr, "!! %s\r\n", buf);
/* flush all open files */
fflush(NULL);
exit(rcode);
}
void
fatal_sys(const char *fmt, ...)
{
va_list ap;
char buf[1024];
int error = errno;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
fatal(ERROR_SYS, "%s: %s (%d)", buf, strerror(error), error);
}
void
startup()
{
printf("\n"
"SSHPassword provided by Lance Haynie <[email protected]>\n"
"This service is provided as a courtesy and comes with no guarantee.\n"
"You're welcome!\n\n");
g.opt.passwd_prompt = DEFAULT_PROMPT;
g.opt.yesno_prompt = DEFAULT_YESNO;
g.opt.password = DEFAULT_PASSWD;
g.opt.tries = DEFAULT_COUNT;
g.opt.timeout = DEFAULT_TIMEOUT;
g.opt.fatal_more_tries = true;
}
char *
arg2pass(char *optarg)
{
char *pass = NULL;
if (strncmp(optarg, "file:", 5) == 0) {
FILE *fp = fopen(optarg + 5, "r");
char buf[64] = "";
fgets(buf, sizeof(buf), fp);
fclose(fp);
pass = strtok(buf, " \r\n");
if (pass) {
pass = strdup(pass);
} else {
pass = strdup("");
}
} else if (strncmp(optarg, "env:", 4) == 0) {
pass = getenv(optarg + 4);
if (pass) {
pass = strdup(pass);
}
} else {
pass = strdup(optarg);
}
return pass;
}
void
getargs(int argc, char **argv)
{
int ch, i, r, reflag;
if ((g.progname = strrchr(argv[0], '/')) != NULL) {
++g.progname;
} else {
g.progname = argv[0];
}
if (argc == 1 || (argc == 2 && strcmp("--help", argv[1]) == 0) ) {
usage(0);
}
/*
* If the first character of optstring is '+' or the environment variable
* POSIXLY_CORRECT is set, then option processing stops as soon as a
* nonoption argument is encountered.
*/
while ((ch = getopt(argc, argv, "+:c:Chil:L:np:P:t:Ty")) != -1) {
switch (ch) {
case 'c':
g.opt.tries = atoi(optarg);
break;
case 'C':
g.opt.fatal_more_tries = true;
break;
case 'p':
g.opt.password = arg2pass(optarg);
for (i = 0; i < strlen(optarg); ++i) {
optarg[i] = '*';
}
if (g.opt.password == NULL) {
fatal(ERROR_USAGE, "Error: failed to get password");
}
break;
case 't':
g.opt.timeout = atoi(optarg);
break;
case 'T':
g.opt.fatal_no_prompt = true;
break;
case 'y':
g.opt.auto_yesno = true;
break;
#if 0
case 'Y':
g.opt.yesno_prompt = optarg;
break;
#endif
case '?':
default:
fatal(ERROR_USAGE, "Error: unknown option '-%c'", optopt);
}
}
argc -= optind;
argv += optind;
if (0 == argc) {
fatal(ERROR_USAGE, "Error: no command specified");
}
g.opt.command = argv;
if (0 == strlen(g.opt.passwd_prompt) ) {
fatal(ERROR_USAGE, "Error: empty prompt");
}
/* Password: */
reflag = 0;
r = regcomp(&g.opt.re_prompt, g.opt.passwd_prompt, reflag);
/* (yes/no)? */
r = regcomp(&g.opt.re_yesno, g.opt.yesno_prompt, reflag);
}
int
ptym_open(char *pts_name, int pts_namesz)
{
char *ptr;
int fdm;
snprintf(pts_name, pts_namesz, "/dev/ptmx");
fdm = posix_openpt(O_RDWR);
if (fdm < 0)
return (-1);
if (grantpt(fdm) < 0) {
close(fdm);
return (-2);
}
if (unlockpt(fdm) < 0) {
close(fdm);
return (-3);
}
if ((ptr = ptsname(fdm)) == NULL) {
close(fdm);
return (-4);
}
snprintf(pts_name, pts_namesz, "%s", ptr);
return (fdm);
}
int
ptys_open(char *pts_name)
{
int fds;
if ((fds = open(pts_name, O_RDWR)) < 0)
return (-5);
return (fds);
}
pid_t
pty_fork(int *ptrfdm, char *slave_name, int slave_namesz,
const struct termios *slave_termios,
const struct winsize *slave_winsize)
{
int fdm, fds;
pid_t pid;
char pts_name[32];
if ((fdm = ptym_open(pts_name, sizeof(pts_name))) < 0)
fatal_sys("can't open master pty: %s, error %d", pts_name, fdm);
if (slave_name != NULL) {
/*
* Return name of slave. Null terminate to handle case
* where strlen(pts_name) > slave_namesz.
*/
snprintf(slave_name, slave_namesz, "%s", pts_name);
}
if ((pid = fork()) < 0) {
return (-1);
} else if (pid == 0) {
/*
* child
*/
if (setsid() < 0)
fatal_sys("setsid error");
/*
* System V acquires controlling terminal on open().
*/
if ((fds = ptys_open(pts_name)) < 0)
fatal_sys("can't open slave pty");
/* all done with master in child */
close(fdm);
#if defined(TIOCSCTTY)
/*
* TIOCSCTTY is the BSD way to acquire a controlling terminal.
*
* Don't check the return code. It would fail in Cygwin.
*/
ioctl(fds, TIOCSCTTY, (char *)0);
#endif
/*
* Set slave's termios and window size.
*/
if (slave_termios != NULL) {
if (tcsetattr(fds, TCSANOW, slave_termios) < 0)
fatal_sys("tcsetattr error on slave pty");
}
if (slave_winsize != NULL) {
if (ioctl(fds, TIOCSWINSZ, slave_winsize) < 0)
fatal_sys("TIOCSWINSZ error on slave pty");
}
/*
* Slave becomes stdin/stdout/stderr of child.
*/
if (dup2(fds, STDIN_FILENO) != STDIN_FILENO)
fatal_sys("dup2 error to stdin");
if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO)
fatal_sys("dup2 error to stdout");
if (dup2(fds, STDERR_FILENO) != STDERR_FILENO)
fatal_sys("dup2 error to stderr");
if (fds != STDIN_FILENO && fds != STDOUT_FILENO &&
fds != STDERR_FILENO) {
close(fds);
}
return (0);
} else {
/*
* parent
*/
*ptrfdm = fdm;
return (pid);
}
}
int
tty_raw(int fd, struct termios *save_termios)
{
int err;
struct termios buf;
if (tcgetattr(fd, &buf) < 0)
return (-1);
*save_termios = buf;
/*
* Echo off, canonical mode off, extended input
* processing off, signal chars off.
*/
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/*
* No SIGINT on BREAK, CR-to-NL off, input parity
* check off, don't strip 8th bit on input, output
* flow control off.
*/
buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*
* Clear size bits, parity checking off.
*/
buf.c_cflag &= ~(CSIZE | PARENB);
/*
* Set 8 bits/char.
*/
buf.c_cflag |= CS8;
/*
* Output processing off.
*/
buf.c_oflag &= ~(OPOST);
/*
* Case B: 1 byte at a time, no timer.
*/
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
return (-1);
/*
* Verify that the changes stuck. tcsetattr can return 0 on
* partial success.
*/
if (tcgetattr(fd, &buf) < 0) {
err = errno;
tcsetattr(fd, TCSAFLUSH, save_termios);
errno = err;
return (-1);
}
if ((buf.c_lflag & (ECHO | ICANON | IEXTEN | ISIG)) ||
(buf.c_iflag & (BRKINT | ICRNL | INPCK | ISTRIP | IXON)) ||
(buf.c_cflag & (CSIZE | PARENB | CS8)) != CS8 ||
(buf.c_oflag & OPOST) || buf.c_cc[VMIN] != 1 ||
buf.c_cc[VTIME] != 0) {
/*
* Only some of the changes were made. Restore the
* original settings.
*/
tcsetattr(fd, TCSAFLUSH, save_termios);
errno = EINVAL;
return (-1);
}
return (0);
}
int
tty_reset(int fd, struct termios *termio)
{
if (tcsetattr(fd, TCSAFLUSH, termio) < 0)
return (-1);
return (0);
}
void
tty_atexit(void)
{
if (g.reset_on_exit) {
tty_reset(STDIN_FILENO, &g.save_termios);
}
}
ssize_t
read_if_ready(int fd, char *buf, size_t n)
{
struct timeval timeout;
fd_set fds;
int nread;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (select(fd + 1, &fds, NULL, NULL, &timeout) < 0) {
return -1;
}
if (! FD_ISSET(fd, &fds) ) {
return 0;
}
if ((nread = read(fd, buf, n) ) < 0) {
return -1;
}
return nread;
}
ssize_t
writen(int fd, const void *ptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
nleft = n;
while (nleft > 0) {
if ((nwritten = write(fd, ptr, nleft)) < 0) {
if (nleft == n) {
return (-1);
} else {
/* error, return amount written so far */
break;
}
} else if (nwritten == 0) {
break;
}
nleft -= nwritten;
ptr += nwritten;
}
return (n - nleft);
}
void
sig_handle(int signo, void (*handler)(int) )
{
struct sigaction act;
memset(&act, 0, sizeof(act) );
act.sa_handler = handler;
sigaction(signo, &act, NULL);
}
void
sig_child(int signo)
{
g.SIGCHLDed = true;
}
void
sig_winch(int signum)
{
g.received_winch = true;
return;
}
#define write2(fd1, fd2, buf, len) \
do { \
int fds[2] = { fd1, fd2 }; \
int i; \
for (i = 0; i < 2; ++i) { \
if (fds[i] < 0) { \
continue; \
} \
if (writen(fds[i], buf, (len) ) != (len) ) { \
fatal_sys("write: fd %d", fds[i]); \
} \
} \
} while (0)
void
big_loop()
{
char buf1[BUFFSIZE]; /* for read() from stdin */
char buf2[2 * BUFFSIZE + 1]; /* for read() from ptym, `+1' for adding the '\000' */
char *cache = buf2;
int nread, ncache = 0;
struct timeval select_timeout;
fd_set readfds;
int i, r, status;
regmatch_t re_match[1];
time_t last_time = time(NULL);
bool given_up = false;
int passwords_seen = 0;
int fd_to_pty = -1, fd_from_pty = -1;
bool stdin_eof = false;
int exit_code = -1;
pid_t wait_return;
/*
* wait for the child to open the pty
*/
do {
/*
* On Mac, fcntl(O_NONBLOCK) may fail before the child opens the pty
* slave side. So wait a while for the child to open the pty slave.
*/
fd_set writefds;
select_timeout.tv_sec = 1;
select_timeout.tv_usec = 0;
FD_ZERO(&writefds);
FD_SET(g.fd_ptym, &writefds);
select(g.fd_ptym + 1, NULL, &writefds, NULL, &select_timeout);
if (! FD_ISSET(g.fd_ptym, &writefds) ) {
fatal(ERROR_GENERAL, "failed to wait for ptym to be writable");
}
} while (0);
while (true) {
L_chk_sigchld:
if (g.SIGCHLDed) {
wait_return = waitpid(-1, &status, WUNTRACED | WCONTINUED);
if (wait_return < 0) {
fatal_sys("received SIGCHLD but waitpid() failed");
}
g.SIGCHLDed = false;
if (WIFEXITED(status) ) {
exit_code = WEXITSTATUS(status);
goto L_done;
} else if (WIFSIGNALED(status) ) {
exit_code = status + 128;
goto L_done;
} else if (WIFSTOPPED(status) ) {
/* Do nothing. Just wait for the child to be continued and wait
* for the next SIGCHLD. */
} else if (WIFCONTINUED(status) ) {
/* */
} else {
/* This should not happen. */
goto L_done;
}
}
if (g.opt.timeout != 0 && g.opt.fatal_no_prompt && passwords_seen == 0
&& labs(time(NULL) - last_time) > g.opt.timeout) {
fatal(ERROR_TIMEOUT, "timeout waiting for password prompt");
}
if (g.received_winch && g.stdin_is_tty) {
struct winsize ttysize;
static int ourtty = -1;
g.received_winch = false;
if (ourtty < 0) {
#if 0
ourtty = open("/dev/tty", 0);
#else
ourtty = STDIN_FILENO;
#endif
}
if (ioctl(ourtty, TIOCGWINSZ, &ttysize) == 0) {
ioctl(g.fd_ptym, TIOCSWINSZ, &ttysize);
}
}
while (stdin_eof) {
struct termios term;
char eof_char;
static struct timeval last;
struct timeval now;
double diff;
if (last.tv_sec == 0) {
gettimeofday(&last, NULL);
break;
}
gettimeofday(&now, NULL);
diff = now.tv_sec + now.tv_usec / 1e6 - (last.tv_sec + last.tv_usec / 1e6);
if (diff > -0.05 && diff < 0.05) {
break;
}
last = now;
if (tcgetattr(g.fd_ptym, &term) < 0) {
goto L_done;
}
eof_char = term.c_cc[VEOF];
if (write(g.fd_ptym, &eof_char, 1) < 0) {
goto L_done;
}
write(fd_to_pty, &eof_char, 1);
break;
}
FD_ZERO(&readfds);
if (g.stdin_is_tty && !stdin_eof) {
FD_SET(STDIN_FILENO, &readfds);
}
FD_SET(g.fd_ptym, &readfds);
select_timeout.tv_sec = 1;
select_timeout.tv_usec = 100 * 1000;
r = select(g.fd_ptym + 1, &readfds, NULL, NULL, &select_timeout);
if (r == 0) {
/* timeout */
continue;
} else if (r < 0) {
if (errno == EINTR) {
continue;
} else {
fatal_sys("select error");
}
}
if (FD_ISSET(g.fd_ptym, &readfds) ) {
while (true) {
nread = read_if_ready(g.fd_ptym, cache + ncache,
2 * BUFFSIZE - (cache - buf2));
if (nread <= 0) {
/* child exited? */
goto L_chk_sigchld;
}
write2(STDOUT_FILENO, fd_from_pty, cache + ncache, nread);
if (! given_up && g.opt.timeout != 0
&& labs(time(NULL) - last_time) >= g.opt.timeout) {
given_up = true;
}
/* regexec() does not like NULLs */
if (! given_up) {
for (i = 0; i < nread; ++i) {
if (cache[ncache + i] == 0) {
cache[ncache + i] = 255;
}
}
}
ncache += nread;
/* make it NULL-terminated so regexec() would be happy */
cache[ncache] = 0;
/* match password prompt and send the password */
if (! g.now_interactive && ! given_up) {
if (g.opt.auto_yesno && passwords_seen == 0
&& regexec(&g.opt.re_yesno, cache, 1, re_match, 0) == 0)
{
/*
* (yes/no)?
*/
char *yes = "yes\r";
write2(g.fd_ptym, fd_to_pty, yes, strlen(yes) );
ncache -= re_match[0].rm_eo;
cache += re_match[0].rm_eo;
} else if (regexec(&g.opt.re_prompt, cache, 1, re_match, 0) == 0) {
/*
* Password:
*/
++passwords_seen;
last_time = time(NULL);
if (g.opt.fatal_more_tries) {
if (g.opt.tries != 0 && passwords_seen > g.opt.tries) {
fatal(ERROR_MAX_TRIES, "Max retries reached, exiting.", g.opt.tries);
}
} else if (g.opt.tries != 0 && passwords_seen >= g.opt.tries) {
given_up = true;
}
write(g.fd_ptym, g.opt.password, strlen(g.opt.password));
write(g.fd_ptym, "\r", 1);
write(fd_to_pty, "********\r", strlen("********\r") );
ncache -= re_match[0].rm_eo;
cache += re_match[0].rm_eo;
}
} else {
cache = buf2;
ncache = 0;
}
if (cache + ncache >= buf2 + 2 * BUFFSIZE) {
if (ncache > BUFFSIZE) {
cache += ncache - BUFFSIZE;
ncache = BUFFSIZE;
}
memmove(buf2, cache, ncache);
cache = buf2;
}
}
}
/*
* copy data from stdin to ptym
*/
if (!stdin_eof && FD_ISSET(STDIN_FILENO, &readfds) ) {
if ((nread = read(STDIN_FILENO, buf1, BUFFSIZE)) < 0)
fatal_sys("read error from stdin");
else if (nread == 0) {
/* EOF on stdin means we're done */
stdin_eof = true;
} else {
g.now_interactive = true;
write2(g.fd_ptym, fd_to_pty, buf1, nread);
}
}
}
L_done:
/* the child has exited but there may be still some data for us
* to read */
while ((nread = read_if_ready(g.fd_ptym, buf2, BUFFSIZE) ) > 0) {
write2(STDOUT_FILENO, fd_from_pty, buf2, nread);
}
if (fd_to_pty >= 0) {
close(fd_to_pty);
}
if (fd_from_pty >= 0) {
close(fd_from_pty);
}
if (exit_code < 0) {
exit(ERROR_GENERAL);
} else {
exit(exit_code);
}
}
int
main(int argc, char *argv[])
{
char slave_name[32];
pid_t pid;
struct termios orig_termios;
struct winsize size;
startup();
getargs(argc, argv);
g.stdin_is_tty = isatty(STDIN_FILENO);
sig_handle(SIGCHLD, sig_child);
if (g.stdin_is_tty) {
if (tcgetattr(STDIN_FILENO, &orig_termios) < 0)
fatal_sys("tcgetattr error on stdin");
if (ioctl(STDIN_FILENO, TIOCGWINSZ, (char *) &size) < 0)
fatal_sys("TIOCGWINSZ error");
pid = pty_fork(&g.fd_ptym, slave_name, sizeof(slave_name),
&orig_termios, &size);
} else {
pid = pty_fork(&g.fd_ptym, slave_name, sizeof(slave_name),
NULL, NULL);
}
if (pid < 0) {
fatal_sys("fork error");
} else if (pid == 0) {
/*
* child
*/
if (execvp(g.opt.command[0], g.opt.command) < 0)
fatal_sys("can't execute: %s", argv[optind]);
}
if (g.stdin_is_tty && isatty(STDOUT_FILENO) ) {
/* user's tty to raw mode */
if (tty_raw(STDIN_FILENO, &g.save_termios) < 0)
fatal_sys("tty_raw error");
g.reset_on_exit = true;
if (atexit(tty_atexit) < 0)
fatal_sys("atexit error");
sig_handle(SIGWINCH, sig_winch);
}
big_loop();
return 0;
}