version 1.6, 2022/12/02 22:29:59 |
version 1.7, 2022/12/02 22:36:34 |
|
|
port0 = optarg; |
port0 = optarg; |
else if (port1 == NULL) |
else if (port1 == NULL) |
port1 = optarg; |
port1 = optarg; |
else { |
else |
fputs("too many -q options\n", stderr); |
errx(1, "too many -q options"); |
return 1; |
|
} |
|
break; |
break; |
case 'i': |
case 'i': |
ifile = optarg; |
ifile = optarg; |
|
|
usage(); |
usage(); |
|
|
/* we don't support more than one data flow */ |
/* we don't support more than one data flow */ |
if (ifile != NULL && ofile != NULL) { |
if (ifile != NULL && ofile != NULL) |
fputs("-i and -o are exclusive\n", stderr); |
errx(1, "-i and -o are exclusive"); |
return 1; |
|
} |
|
|
|
/* second port makes sense only for port-to-port transfers */ |
/* second port makes sense only for port-to-port transfers */ |
if (port1 != NULL && !(ifile == NULL && ofile == NULL)) { |
if (port1 != NULL && !(ifile == NULL && ofile == NULL)) |
fputs("too many -q options\n", stderr); |
errx(1, "too many -q options"); |
return 1; |
|
} |
|
|
|
/* if there're neither files nor ports, then we've nothing to do */ |
/* if there're neither files nor ports, then we've nothing to do */ |
if (port0 == NULL && ifile == NULL && ofile == NULL) |
if (port0 == NULL && ifile == NULL && ofile == NULL) |
|
|
ifd = STDIN_FILENO; |
ifd = STDIN_FILENO; |
} else { |
} else { |
ifd = open(ifile, O_RDONLY); |
ifd = open(ifile, O_RDONLY); |
if (ifd == -1) { |
if (ifd == -1) |
perror(ifile); |
err(1, "%s", ifile); |
return 1; |
|
} |
|
} |
} |
} else if (ofile) { |
} else if (ofile) { |
if (strcmp(ofile, "-") == 0) { |
if (strcmp(ofile, "-") == 0) { |
|
|
ofd = STDOUT_FILENO; |
ofd = STDOUT_FILENO; |
} else { |
} else { |
ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
if (ofd == -1) { |
if (ofd == -1) |
perror(ofile); |
err(1, "%s", ofile); |
return 1; |
|
} |
|
} |
} |
} |
} |
|
|
|
|
else |
else |
mode = MIO_IN; |
mode = MIO_IN; |
ih = mio_open(port0, mode, 0); |
ih = mio_open(port0, mode, 0); |
if (ih == NULL) { |
if (ih == NULL) |
fprintf(stderr, "%s: couldn't open port\n", port0); |
errx(1, "%s: couldn't open port", port0); |
return 1; |
|
} |
|
|
|
/* open second port, output only */ |
/* open second port, output only */ |
if (port1 == NULL) |
if (port1 == NULL) |
oh = ih; |
oh = ih; |
else { |
else { |
oh = mio_open(port1, MIO_OUT, 0); |
oh = mio_open(port1, MIO_OUT, 0); |
if (oh == NULL) { |
if (oh == NULL) |
fprintf(stderr, "%s: couldn't open port\n", port1); |
errx(1, "%s: couldn't open port", port1); |
exit(1); |
|
} |
|
} |
} |
|
|
if (pledge("stdio", NULL) == -1) |
if (pledge("stdio", NULL) == -1) |
|
|
if (len == 0) |
if (len == 0) |
break; |
break; |
if (len == -1) { |
if (len == -1) { |
perror("stdin"); |
warn("%s", ifile); |
break; |
break; |
} |
} |
} else { |
} else { |
len = mio_read(ih, buf, sizeof(buf)); |
len = mio_read(ih, buf, sizeof(buf)); |
if (len == 0) { |
if (len == 0) { |
fprintf(stderr, "%s: disconnected\n", port0); |
warnx("%s: disconnected", port0); |
break; |
break; |
} |
} |
} |
} |
if (ofile != NULL) { |
if (ofile != NULL) { |
n = write(ofd, buf, len); |
n = write(ofd, buf, len); |
if (n != len) { |
if (n != len) { |
fprintf(stderr, "%s: short write\n", ofile); |
warn("%s: short write", ofile); |
break; |
break; |
} |
} |
} else { |
} else { |
n = mio_write(oh, buf, len); |
n = mio_write(oh, buf, len); |
if (n != len) { |
if (n != len) { |
fprintf(stderr, "%s: disconnected\n", port1); |
warnx("%s: disconnected", port1); |
break; |
break; |
} |
} |
} |
} |