midicat(1): use err(3) everywhere
authorcheloha <cheloha@openbsd.org>
Fri, 2 Dec 2022 22:36:34 +0000 (22:36 +0000)
committercheloha <cheloha@openbsd.org>
Fri, 2 Dec 2022 22:36:34 +0000 (22:36 +0000)
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@

usr.bin/midicat/midicat.c

index 7c40dd7..e42294c 100644 (file)
@@ -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>
  *
@@ -49,10 +49,8 @@ main(int argc, char **argv)
                                port0 = optarg;
                        else if (port1 == NULL)
                                port1 = optarg;
-                       else {
-                               fputs("too many -q options\n", stderr);
-                               return 1;
-                       }
+                       else
+                               errx(1, "too many -q options");
                        break;
                case 'i':
                        ifile = optarg;
@@ -71,16 +69,12 @@ main(int argc, char **argv)
                usage();
 
        /* we don't support more than one data flow */
-       if (ifile != NULL && ofile != NULL) {
-               fputs("-i and -o are exclusive\n", stderr);
-               return 1;
-       }
+       if (ifile != NULL && ofile != NULL)
+               errx(1, "-i and -o are exclusive");
 
        /* second port makes sense only for port-to-port transfers */
-       if (port1 != NULL && !(ifile == NULL && ofile == NULL)) {
-               fputs("too many -q options\n", stderr);
-               return 1;
-       }
+       if (port1 != NULL && !(ifile == NULL && ofile == NULL))
+               errx(1, "too many -q options");
 
        /* if there're neither files nor ports, then we've nothing to do */
        if (port0 == NULL && ifile == NULL && ofile == NULL)
@@ -97,10 +91,8 @@ main(int argc, char **argv)
                        ifd = STDIN_FILENO;
                } else {
                        ifd = open(ifile, O_RDONLY);
-                       if (ifd == -1) {
-                               perror(ifile);
-                               return 1;
-                       }
+                       if (ifd == -1)
+                               err(1, "%s", ifile);
                }
        } else if (ofile) {
                if (strcmp(ofile, "-") == 0) {
@@ -108,10 +100,8 @@ main(int argc, char **argv)
                        ofd = STDOUT_FILENO;
                } else {
                        ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-                       if (ofd == -1) {
-                               perror(ofile);
-                               return 1;
-                       }
+                       if (ofd == -1)
+                               err(1, "%s", ofile);
                }
        }
 
@@ -125,20 +115,16 @@ main(int argc, char **argv)
        else
                mode = MIO_IN;
        ih = mio_open(port0, mode, 0);
-       if (ih == NULL) {
-               fprintf(stderr, "%s: couldn't open port\n", port0);
-               return 1;
-       }
+       if (ih == NULL)
+               errx(1, "%s: couldn't open port", port0);
 
        /* open second port, output only */
        if (port1 == NULL)
                oh = ih;
        else {
                oh = mio_open(port1, MIO_OUT, 0);
-               if (oh == NULL) {
-                       fprintf(stderr, "%s: couldn't open port\n", port1);
-                       exit(1);
-               }
+               if (oh == NULL)
+                       errx(1, "%s: couldn't open port", port1);
        }
 
        if (pledge("stdio", NULL) == -1)
@@ -151,26 +137,26 @@ main(int argc, char **argv)
                        if (len == 0)
                                break;
                        if (len == -1) {
-                               perror("stdin");
+                               warn("%s", ifile);
                                break;
                        }
                } else {
                        len = mio_read(ih, buf, sizeof(buf));
                        if (len == 0) {
-                               fprintf(stderr, "%s: disconnected\n", port0);
+                               warnx("%s: disconnected", port0);
                                break;
                        }
                }
                if (ofile != NULL) {
                        n = write(ofd, buf, len);
                        if (n != len) {
-                               fprintf(stderr, "%s: short write\n", ofile);
+                               warn("%s: short write", ofile);
                                break;
                        }
                } else {
                        n = mio_write(oh, buf, len);
                        if (n != len) {
-                               fprintf(stderr, "%s: disconnected\n", port1);
+                               warnx("%s: disconnected", port1);
                                break;
                        }
                }