17 #include <asm/termbits.h>
18 #include <asm/ioctls.h>
19 #else /* needed for termios2, which is itself needed for non-standard bauds */
30 #define IXONXOFF (IXON | IXOFF)
33 #define TERMIOS_STRUCT termios2
35 #define TERMIOS_STRUCT termios
38 #if defined(CCDTR_IFLOW) && defined(CDSR_OFLOW)
39 #define CDTRDSR (CDTR_IFLOW | CDSR_OFLOW)
45 int offset
; /* can only be a 1 or a -1 */
62 /* including here to access CRLFOpt */
65 static int gettermattr(int dv
, struct TERMIOS_STRUCT
*optst
);
66 static int settermattr(int dv
, const struct TERMIOS_STRUCT
*optst
);
67 static void settermspd(unsigned int lispeed
, unsigned int lospeed
, struct TERMIOS_STRUCT
*optst
);
68 static int openport();
69 static unsigned int strtoui(const char *str
, const char *msg
, unsigned int min
);
70 static int troptions(CRLFOpt
*options
, const char *str
);
71 static int setroptions();
72 static inline unsigned int lsbmask(unsigned int n
);
73 static inline int termchck(const void *term
);
74 static void interchck();
76 static void *writeport(void *unused
);
77 static void *readport(void *unused
);
78 static void getcmd(int escape
);
79 static inline void replacechar(char *buff
, ssize_t size
, const Args
*args
);
80 static inline ssize_t
addchar(char *buff
, ssize_t size
, int *scratch
, const Sizes
*msizes
, const Args
*args
);
81 static inline ssize_t
rmchar(char *buff
, ssize_t sread
, int *scratch
, const Args
*args
);
82 static void sighandl(int signo
);
83 static void die(int code
, const char *msg
, ...);
85 static struct TERMIOS_STRUCT cntrl
, origterm
= { 0 }, newterm
= { 0 };
87 static Args bsargs
= {DEL
, DEL
, 0};
88 static const Args nocrargs
= {CR
, 0, 0},
89 nolfargs
= {LF
, '\0', 0}, /* null-terminator is for use inside `getcmd()` */
90 lfincrargs
= {CR
, LF
, 1},
91 crinlfargs
= {LF
, CR
, -1};
92 static struct timespec wts
;
93 static const unsigned int uintmax
= ~(unsigned int)0; /* unsigned -1 to return on error */
94 static int interactive
= 0;
95 static char *writebuff
= NULL
;
96 static char *readbuff
= NULL
;
97 static int *scratchr
= NULL
;
98 static int *scratchw
= NULL
;
102 gettermattr(int dv
, struct TERMIOS_STRUCT
*optst
)
105 return ioctl(dv
, TCGETS2
, optst
);
107 return tcgetattr(dv
, optst
);
112 settermattr(int dv
, const struct TERMIOS_STRUCT
*optst
)
115 return ioctl(dv
, TCSETS2
, optst
);
117 return tcsetattr(dv
, TCSANOW
, optst
);
122 settermspd(unsigned int lispeed
, unsigned int lospeed
, struct TERMIOS_STRUCT
*optst
)
125 optst
->c_cflag
&= ~CBAUD
;
126 optst
->c_cflag
|= BOTHER
;
127 optst
->c_ispeed
= ispeed
;
128 optst
->c_ospeed
= ospeed
;
130 cfsetispeed(optst
, ispeed
);
131 cfsetospeed(optst
, ospeed
);
139 struct flock fl
= { 0 };
141 if (verbose
) fprintf(stderr
, "opening \"%s\"\n", line
);
143 fd
= open(line
, (O_RDWR
| O_NOCTTY
| O_NDELAY
));
145 perror("error opening device");
149 if (verbose
) fprintf(stderr
, "checking if \"%s\" is a TTY\n", line
);
152 die(2, "device \"%s\" is not a TTY\n", line
);
154 flags
= fcntl(fd
, F_GETFL
, 0);
155 /* opened to check with non-blocking mode, now set to blocking */
156 flags
&= ~(O_NONBLOCK
| O_NDELAY
);
158 if (fcntl(fd
, F_SETFL
, flags
) == -1) {
160 die(1, "exiting now\n");
163 if (gettermattr(fd
, &cntrl
) == -1)
164 die(1, "failed to get device attributes\n");
167 fl
.l_whence
= SEEK_SET
;
172 if (fcntl(fd
, F_SETLK
, &fl
) == -1) {
174 die(1, "failed to lock the device, exiting now\n");
180 if (verbose
) fprintf(stderr
, "setting baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
182 settermspd(ispeed
, ospeed
, &cntrl
);
183 if (settermattr(fd
, &cntrl
) == -1)
184 die(2,"failed to set baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
190 cntrl
.c_iflag
&= ~(ISTRIP
| BRKINT
);
191 cntrl
.c_cflag
&= ~(PARENB
| PARODD
);
193 if (verbose
) fprintf(stderr
, "setting parity [even: %d, odd: %d]\n", parity
& 1, (parity
& 2) >> 1);
196 cntrl
.c_cflag
|= PARENB
;
198 cntrl
.c_cflag
|= PARENB
| PARODD
;
200 if (settermattr(fd
, &cntrl
) == -1)
201 die(2, "failed to set parity [even: %d, odd: %d]\n", parity
& 1, (parity
& 2) >> 1);
208 /* it's so ugly and beautiful at the same time */
209 int t
= (((hard
& 1) << 1) & (hard
& 2)) >> 1;
210 fprintf(stderr
, "setting flow control [XON/XOFF: %d, RTS/CTS: %d, DTR/DSR: %d, DCD: %d]\n",\
211 soft
, t
^ ((hard
& 1) << 1) >> 1, t
^ (hard
& 2) >> 1, t
);
214 cntrl
.c_cflag
|= CLOCAL
;
215 cntrl
.c_iflag
&= ~IXONXOFF
;
218 cntrl
.c_iflag
|= IXONXOFF
;
221 cntrl
.c_cflag
|= CRTSCTS
;
222 } else if (hard
== 2) {
224 fprintf(stderr
, "DTR/DSR flow control is not supported on this platform\n"
225 "enabling this option does nothing!\n");
227 cntrl
.c_lflag
|= CDTRDSR
;
229 } else if (hard
== 3) {
230 cntrl
.c_cflag
&= ~CLOCAL
;
232 cntrl
.c_lflag
|= CCAR_OFLOW
;
236 if (settermattr(fd
, &cntrl
) == -1)
237 die(2, "failed to set flow control\n");
242 cntrl
.c_iflag
&= ~(ICRNL
| INLCR
);
243 cntrl
.c_oflag
&= ~(OCRNL
| ONLRET
| ONLCR
);
246 die(2, "failed to set cr-lf translation options\n");
251 if (verbose
) fprintf(stderr
, "setting data bits [%c]\n", datab
);
264 cntrl
.c_cflag
&= ~CSIZE
;
267 if (settermattr(fd
, &cntrl
) == -1)
268 die(2, "failed to set data bits [%c]\n", datab
);
273 if (verbose
) fprintf(stderr
, "setting stop bits [%d]\n", stopb
+1);
276 cntrl
.c_cflag
|= CSTOPB
;
278 cntrl
.c_cflag
&= ~CSTOPB
;
280 if (settermattr(fd
, &cntrl
) == -1)
281 die(1, "failed to set stop bits [%d]\n", stopb
+1);
283 cntrl
.c_cc
[VMIN
] = 1;
284 cntrl
.c_cc
[VTIME
] = 0;
286 /* flush both hardware buffers */
288 ioctl(fd
, TCFLSH
, TCIOFLUSH
);
290 tcflush(fd
, TCIOFLUSH
);
296 strtoui(const char *str
, const char *msg
, unsigned int min
)
301 t
= strtol(str
, &endptr
, 10);
302 if (errno
!= 0 || *endptr
!= '\0' || t
< min
|| t
> uintmax
) {
303 fprintf(stderr
, msg
, str
);
307 * conversion like this results in 'undefined behavior' according to C spec,
308 * but almost all compilers will just truncate the value so it's OK
310 return (unsigned int)t
;
314 troptions(CRLFOpt
*options
, const char *str
)
318 if (!strcmp(str
, "lf-in-cr")) {
319 options
->lfincr
^= 1; return 0;
320 } else if (!strcmp(str
, "lf-to-cr")) {
321 options
->lftocr
^= 1; return 0;
327 if (!strcmp(str
, "cr-in-lf")) {
328 options
->crinlf
^= 1; return 0;
329 } else if (!strcmp(str
, "cr-to-lf")) {
330 options
->crtolf
^= 1; return 0;
336 if (!strcmp(str
, "no-lf")) {
337 options
->nolf
^= 1; return 0;
338 } else if (!strcmp(str
, "no-cr")) {
339 options
->nocr
^= 1; return 0;
346 fprintf(stderr
, "invalid translation option: %s\n", str
);
354 cntrl
.c_iflag
^= (ICRNL
& lsbmask(itropts
.crtolf
));
355 cntrl
.c_iflag
^= (INLCR
& lsbmask(itropts
.lftocr
));
356 cntrl
.c_oflag
^= (OCRNL
& lsbmask(tropts
.crtolf
));
357 cntrl
.c_oflag
^= (ONLRET
& lsbmask(tropts
.lftocr
));
358 cntrl
.c_oflag
^= (ONLCR
& lsbmask(tropts
.crinlf
));
360 return settermattr(fd
, &cntrl
);
364 lsbmask(unsigned int n
)
366 /* if n == 1, n fills with all 1s, 0 stays the same */
367 for (int i
= (sizeof(int) * CHAR_BIT
); i
> 0; i
--)
373 termchck(const void *term
)
375 for (size_t i
= 0; i
< sizeof(struct TERMIOS_STRUCT
); i
++)
376 if (((char*)term
)[i
] != 0)
383 writeport(void *unused
)
386 struct timespec timeout
, ts
;
388 timeout
.tv_sec
= stimeout
;
389 timeout
.tv_nsec
= nstimeout
;
390 ts
.tv_sec
= swritedelay
;
391 ts
.tv_nsec
= nswritedelay
;
393 wts
.tv_nsec
= chardelay
;
394 msizes
.sizesm
= scratchwsz
;
395 msizes
.sizebm
= wbuffsz
;
397 writebuff
= malloc(wbuffsz
* sizeof(char));
398 scratchw
= malloc(scratchwsz
* sizeof(int));
400 if (writebuff
== NULL
)
401 die(1, "buffer allocation failed\n");
402 if (scratchw
== NULL
)
403 die(1, "scratch buffer allocation failed\n");
408 ssize_t inln
= read(STDIN_FILENO
, writebuff
, wbuffsz
- 1);
414 if (writebuff
[0] == escapechar
) {
421 if (bsargs
.find
!= bsargs
.input
)
422 replacechar(writebuff
, inln
, &bsargs
);
424 inln
= rmchar(writebuff
, inln
, scratchw
, &nocrargs
);
426 inln
= rmchar(writebuff
, inln
, scratchw
, &nolfargs
);
428 inln
= addchar(writebuff
, inln
, scratchw
, &msizes
, &lfincrargs
);
431 for (int i
= 0; i
<= inln
; i
++) {
432 write(fd
, &writebuff
[i
], 1);
433 nanosleep(&wts
, NULL
);
436 write(fd
, writebuff
, 1);
438 } else if (inln
== 0 && !interactive
) {
441 nanosleep(&ts
, NULL
);
443 /* ust kills itself upon receiving a signal so no fancy `nanosleep()` features needed */
444 nanosleep(&timeout
, NULL
);
450 readport(void *unused
)
454 ts
.tv_sec
= sreaddelay
;
455 ts
.tv_nsec
= nsreaddelay
;
456 msizes
.sizesm
= scratchrsz
;
457 msizes
.sizebm
= rbuffsz
;
459 readbuff
= malloc(rbuffsz
* sizeof(char));
460 scratchr
= malloc(scratchrsz
* sizeof(int));
462 if (readbuff
== NULL
)
463 die(1, "buffer allocation failed\n");
464 if (scratchr
== NULL
)
465 die(1, "scratch buffer allocation failed\n");
469 /* disable stdout buffering */
470 setvbuf(stdout
, NULL
, _IONBF
, 0);
473 ssize_t outln
= read(fd
, readbuff
, rbuffsz
- 1);
476 outln
= rmchar(readbuff
, outln
, scratchr
, &nocrargs
);
478 outln
= rmchar(readbuff
, outln
, scratchr
, &nolfargs
);
480 outln
= addchar(readbuff
, outln
, scratchr
, &msizes
, &crinlfargs
);
482 outln
= addchar(readbuff
, outln
, scratchr
, &msizes
, &lfincrargs
);
484 write(STDOUT_FILENO
, readbuff
, outln
);
486 nanosleep(&ts
, NULL
);
488 if (isatty(STDIN_FILENO
))
489 settermattr(STDIN_FILENO
, &origterm
);
493 inline void __attribute__((hot
))
494 replacechar(char *buff
, ssize_t size
, const Args
*args
)
496 for (int i
= 0; i
< size
; i
++)
497 if (buff
[i
] == args
->find
)
498 buff
[i
] = args
->input
;
501 inline ssize_t
__attribute__((hot
))
502 addchar(char *buff
, ssize_t size
, int *scratch
, const Sizes
*sizes
, const Args
*args
)
506 /* turns negative numbers into 1, positive into 0 */
507 to
= ((args
->offset
>> ((sizeof(int) * CHAR_BIT
) - 1)) & 1);
509 for (int i
= 0; i
< size
; i
++) {
510 if (buff
[i
] == args
->find
) {
514 if (c
>= sizes
->sizesm
)
519 if (size
== 1 && c
== 1 && scratch
[0] == 0) {
520 buff
[to
^ 1] = args
->input
;
521 buff
[to
] = args
->find
;
524 if ((size
+ c
) > sizes
->sizebm
)
525 c
= sizes
->sizebm
- size
;
527 for (int i
= c
- 1; i
>= 0; i
--) {
529 memmove(&buff
[t
] + 1, &buff
[t
], (size
+ c
) - t
);
530 buff
[t
+ (to
^ 1)] = args
->input
;
532 printf("%s\n", buff
);
536 inline ssize_t
__attribute__((hot
))
537 rmchar(char *buff
, ssize_t size
, int *scratch
, const Args
*args
)
540 for (int i
= 0; i
< size
; i
++) {
541 if (buff
[i
] == args
->find
) {
548 if (size
== 1 && c
== 1 && scratch
[0] == 0)
551 for (int i
= c
; i
>= 0; i
--) {
553 memmove(&buff
[t
], &buff
[t
] + 1, (size
+ c
) - t
);
562 if (gettermattr(STDIN_FILENO
, &origterm
) < 0 )
563 die(1, "failed to get terminal attributes\n");
568 newterm
.c_lflag
&= ~ECHO
;
570 newterm
.c_lflag
&= ~ICANON
;
573 newterm
.c_iflag
|= IXONXOFF
;
575 newterm
.c_iflag
|= INLCR
;
576 newterm
.c_cc
[VMIN
] = minchars
;
577 newterm
.c_cc
[VTIME
] = 0;
578 newterm
.c_cc
[VINTR
] = newterm
.c_cc
[VSUSP
]\
579 = newterm
.c_cc
[VQUIT
] = newterm
.c_cc
[VLNEXT
]\
580 = newterm
.c_cc
[VDISCARD
] = _POSIX_VDISABLE
;
583 newterm
.c_cc
[VDSUSP
] = _POSIX_VDISABLE
;
591 bsargs
.find
= origterm
.c_cc
[VERASE
];
593 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
594 die(1, "failed to set terminal attributes\n");
602 if ((!half
|| !canonical
) && interactive
) {
603 newterm
.c_lflag
|= ECHO
;
604 newterm
.c_lflag
|= ICANON
;
605 if (settermattr(STDIN_FILENO
, &newterm
) < 0)
606 fprintf(stderr
, "failed to enable echo and/or canonical mode\n");
619 msizes
.sizesm
= scratchwsz
;
620 msizes
.sizebm
= wbuffsz
;
622 if (isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
))
628 cmdchar
= writebuff
[0];
630 cmdchar
= writebuff
[1];
633 case EOT
: /* FALLTHROUGH */
645 newterm
.c_lflag
&= ~ECHO
;
647 newterm
.c_lflag
|= ECHO
;
649 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
650 die(1, "failed to set terminal attributes\n");
654 newterm
.c_lflag
&= ~ICANON
;
656 newterm
.c_lflag
|= ICANON
;
658 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
659 die(1, "failed to set terminal attributes\n");
663 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
664 replacechar(ttr
, 63, &nolfargs
);
666 int wfd
= open(ttr
, O_RDONLY
);
668 perror("error opening file");
671 while ((frln
= read(wfd
, writebuff
, wbuffsz
- 1)) > 0) {
672 for (int i
= 0; i
<= frln
; i
++) {
674 frln
= rmchar(writebuff
, frln
, scratchw
, &nocrargs
);
676 frln
= rmchar(writebuff
, frln
, scratchw
, &nolfargs
);
678 frln
= addchar(writebuff
, frln
, scratchw
, &msizes
, &lfincrargs
);
680 write(fd
, &writebuff
[i
], 1);
681 nanosleep(&wts
, NULL
);
688 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
689 replacechar(ttr
, 63, &nolfargs
);
690 tspd
= strtoui(ttr
, "invalid speed\n", 0);
691 if (tspd
!= uintmax
) {
692 ospeed
= ispeed
= tspd
;
693 settermspd(ispeed
, ospeed
, &cntrl
);
694 if (settermattr(fd
, &cntrl
) != 0)
695 fprintf(stderr
, "failed to set baudrate "
696 "[RX:%u | TX:%u]\n", ispeed
, ospeed
);
702 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
703 replacechar(ttr
, 63, &nolfargs
);
704 chardelay
= strtoui(ttr
, "invalid delay\n", 0);
705 if (chardelay
!= uintmax
) {
707 wts
.tv_nsec
= chardelay
;
713 fprintf(stderr
, "additional output translation option: ");
714 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
715 replacechar(ttr
, 63, &nolfargs
);
716 if(troptions(&tropts
, ttr
))
719 fprintf(stderr
, "could not set new options\n");
724 printf("additional input translation option: ");
725 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
726 replacechar(ttr
, 63, &nolfargs
);
727 if (troptions(&itropts
, ttr
))
730 fprintf(stderr
, "could not set new options\n");
733 if (!half
&& interactive
)
734 newterm
.c_lflag
&= ~ECHO
;
735 if (!canonical
&& interactive
)
736 newterm
.c_lflag
&= ~ICANON
;
737 if (settermattr(STDIN_FILENO
, &newterm
) == -1)
738 fprintf(stderr
, "failed to restore echo and/or canonical mode\n");
743 ts
.tv_sec
= spulsedelay
;
744 ts
.tv_nsec
= nspulsedelay
;
746 if (ioctl(fd
, TIOCMGET
, &st
) != 0) {
747 fprintf(stderr
, "failed to get port status\n");
751 if (ioctl(fd
, TIOCMSET
, &st
) != 0) {
752 fprintf(stderr
, "failed to set DTR [assertion]\n");
755 nanosleep(&ts
, NULL
);
758 if (ioctl(fd
, TIOCMSET
, &st
) != 0)
759 fprintf(stderr
, "failed to set DTR [negation]\n");
761 case BS
: /* FALLTHROUGH */
766 fprintf(stderr
, "not a valid command [%c]\n", cmdchar
);
775 die(128 + signo
, "\nrecieved signal [%d], exiting\n", signo
);
779 die(int code
, const char *msg
, ...)
794 if (termchck(&newterm
))
795 settermattr(STDIN_FILENO
, &origterm
);
798 vfprintf(stderr
, msg
, fpa
);
805 main(int argc
, char **argv
)
808 /* glibc's `asprintf()` won't set the string to NULL on failure automatically */
809 for (int i
= 1; i
< argc
; i
++ ) {
810 if (argv
[i
][0] == '-' && argv
[i
][1] >= '0' && argv
[i
][1] <= '9') {
811 asprintf(&t
, "-s%s", argv
[i
] + 1);
813 fprintf(stderr
, "%s: cannot convert -# to -s#\n", argv
[0]);
820 static struct option longopt
[] = {
821 {"line", required_argument
, NULL
, 'l'},
822 {"speed", required_argument
, NULL
, 's'},
823 {"rx-speed", required_argument
, NULL
, 'i'},
824 {"echo", no_argument
, NULL
, 'h'},
825 {"canonical", no_argument
, NULL
, 'c'},
826 {"odd", no_argument
, NULL
, 'o'},
827 {"even", no_argument
, NULL
, 'e'},
828 {"hardware-rts-cts", no_argument
, NULL
, 'R'},
829 {"hardware-dtr-dsr", no_argument
, NULL
, 'r'},
830 {"software", no_argument
, NULL
, 'X'},
831 {"data", required_argument
, NULL
, 'D'},
832 {"delay", required_argument
, NULL
, 'd'},
833 {"min-chars", required_argument
, NULL
, 'm'},
834 {"timeout-ns", required_argument
, NULL
, 'f'},
835 {"timeout-s", required_argument
, NULL
, 'F'},
836 {"stop-bits", no_argument
, NULL
, 'S'},
837 {"backspace", no_argument
, NULL
, 'b'},
838 {"translation", required_argument
, NULL
, 't'},
839 {"input-translation", required_argument
, NULL
, 'T'},
840 {"verbose", no_argument
, NULL
, 'v'},
845 int oind
, rxspdset
, devset
, c
;
846 oind
= rxspdset
= devset
= 0;
847 while ((c
= getopt_long(argc
, argv
, "D:F:T:d:f:i:l:m:s:t:RXbcehorv", longopt
, &oind
)) != -1) {
852 backspace
^= 1; break;
854 canonical
^= 1; break;
861 case 'd': /* FALLTHROUGH */
866 t
= strtol(optarg
, &endptr
, 10);
867 if (errno
!= 0 || *endptr
!= '\0' || t
< 0) {
868 fprintf(stderr
, "%s: invalid character delay: %s\n", argv
[0], optarg
);
870 } else if (c
== 'd') {
872 } else if (c
== 'f'){
879 if (strlen(optarg
) != 1 || !(optarg
[0] >= '5' && optarg
[0] <= '8')) {
880 fprintf(stderr
, "%s: invalid number of data bits: %s\n", argv
[0], optarg
);
893 tui
= strtoui(optarg
, "invalid rx speed: %s\n", 1);
900 tui
= strtoui(optarg
, "invalid speed: %s\n", 1);
909 fprintf(stderr
, "%s: cannot specify multiple devices\n", argv
[0]);
912 if (strlen(optarg
) > 10) {
913 fprintf(stderr
, "%s: device name too long\n", argv
[0]);
916 if (strchr(optarg
, '/')) {
917 strcpy(line
, optarg
);
920 sprintf(line
, "/dev/%s", optarg
);
925 tui
= strtoui(optarg
, "invalid number of characters: %s\n", 1);
931 if (troptions(&tropts
, optarg
))
935 if (troptions(&itropts
, optarg
))
942 die(2, "usage: %s [--line|-l line] [--speed|-s #|-#] [--rx-speed|-i #]\n"
943 " [--data-bits|-D #] [--stop-bits|-S]"
944 " [--even|-e] [--odd|-o]\n"
945 " [--hardware-rts-cts|-R]"
946 " [--hardware-dtr-dsr|-r] [--software|-X]\n"
947 " [--delay|-d #] [--min-chars|-m #]"
948 " [--canonical|-c] [--echo|-h]\n"
949 " [--translation|-t option]"
950 " [--input-translation|-T option]\n"
951 " [--verbose|-v] [--backspace|-b]"
952 " [--timeout-s|-F #] [--timeout-ns|-f #]",
958 * not the best practice, but in order for this `free()`
959 * to not get executed ust needs to be killed,
960 * which means the OS *should* free the memory
967 if (isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
))
970 signal(SIGHUP
, sighandl
);
971 signal(SIGINT
, sighandl
);
972 signal(SIGQUIT
, sighandl
);
973 signal(SIGTERM
, sighandl
);
974 signal(SIGCHLD
, SIG_DFL
);
978 pthread_t readthread
, writethread
;
979 pthread_create(&writethread
, NULL
, writeport
, NULL
);
980 pthread_create(&readthread
, NULL
, readport
, NULL
);
981 pthread_join(writethread
, NULL
);
982 pthread_join(readthread
, NULL
);