mirror of
https://github.com/openbsd/src.git
synced 2024-12-22 16:42:56 -08:00
midicat(1): use err(3) everywhere
This is consistent with style(9) and makes the program shorter, too. Tweaked by ratchov@. Link: https://marc.info/?l=openbsd-tech&m=166982129428027&w=2 ok millert@ kn@ ratchov@
This commit is contained in:
parent
7671136a57
commit
dbbc8c3f84
@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: midicat.c,v 1.6 2022/12/02 22:29:59 cheloha Exp $ */
|
/* $OpenBSD: midicat.c,v 1.7 2022/12/02 22:36:34 cheloha Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2015 Alexandre Ratchov <alex@caoua.org>
|
* Copyright (c) 2015 Alexandre Ratchov <alex@caoua.org>
|
||||||
*
|
*
|
||||||
@ -49,10 +49,8 @@ main(int argc, char **argv)
|
|||||||
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;
|
||||||
@ -71,16 +69,12 @@ main(int argc, char **argv)
|
|||||||
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)
|
||||||
@ -97,10 +91,8 @@ main(int argc, char **argv)
|
|||||||
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) {
|
||||||
@ -108,10 +100,8 @@ main(int argc, char **argv)
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,20 +115,16 @@ main(int argc, char **argv)
|
|||||||
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)
|
||||||
@ -151,26 +137,26 @@ main(int argc, char **argv)
|
|||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user