Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/base64 Provide MacOS/X compatible flags (where possi...



details:   https://anonhg.NetBSD.org/src/rev/44cd382cb99d
branches:  trunk
changeset: 991661:44cd382cb99d
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Jul 25 03:45:34 2018 +0000

description:
Provide MacOS/X compatible flags (where possible)
Propagate errno

diffstat:

 usr.bin/base64/base64.c |  57 ++++++++++++++++++++++++++----------------------
 1 files changed, 31 insertions(+), 26 deletions(-)

diffs (182 lines):

diff -r 76ae510fa092 -r 44cd382cb99d usr.bin/base64/base64.c
--- a/usr.bin/base64/base64.c   Wed Jul 25 03:28:17 2018 +0000
+++ b/usr.bin/base64/base64.c   Wed Jul 25 03:45:34 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: base64.c,v 1.1 2018/07/24 15:26:16 christos Exp $      */
+/*     $NetBSD: base64.c,v 1.2 2018/07/25 03:45:34 christos Exp $      */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: base64.c,v 1.1 2018/07/24 15:26:16 christos Exp $");
+__RCSID("$NetBSD: base64.c,v 1.2 2018/07/25 03:45:34 christos Exp $");
 
 #include <ctype.h>
 #include <errno.h>
@@ -66,23 +66,22 @@
 
        for (i = 0; i < len + 1; i++) {
                if (out[i] >= 64) {
-                       errno = EINVAL;
-                       return -1;
+                       return EINVAL;
                }
                if (fputc(B64[out[i]], fout) == -1)
-                       return -1;
+                       return errno;
                if (++(*pos) == wrap) {
                        if (fputc('\n', fout) == -1)
-                               return -1;
+                               return errno;
                        *pos = 0;
                }
        }
        for (; i < 4; i++) {
                if (fputc('=', fout) == -1)
-                       return -1;
+                       return errno;
                if (++(*pos) == wrap) {
                        if (fputc('\n', fout) == -1)
-                               return -1;
+                               return errno;
                        *pos = 0;
                }
        }
@@ -106,22 +105,23 @@
        uint8_t out[4];
        size_t ilen;
        size_t pos = 0;
+       int e;
 
        while ((ilen = getinput(fin, in)) > 2) {
                encode(out, in);
-               if (putoutput(fout, out, ilen, wrap, &pos) == -1)
-                       return -1;
+               if ((e = putoutput(fout, out, ilen, wrap, &pos)) != 0)
+                       return e;
        }
 
        if (ilen != 0) {
                encode(out, in);
-               if (putoutput(fout, out, ilen, wrap, &pos) == -1)
-                       return -1;
+               if ((e = putoutput(fout, out, ilen, wrap, &pos)) != 0)
+                       return e;
        }
 
        if (pos && wrap) {
                if (fputc('\n', fout) == -1)
-                       return -1;
+                       return errno;
        }
        return 0;
 }
@@ -146,7 +146,7 @@
 
                pos = strchr(B64, c);
                if (pos == NULL)
-                       return -1;
+                       return EFTYPE;
 
                b = (uint8_t)(pos - B64);
 
@@ -157,19 +157,19 @@
                case 1:
                        out |= b >> 4;
                        if (fputc(out, fout) == -1)
-                               return -1;
+                               return errno;
                        out = (uint8_t)((b & 0xf) << 4);
                        break;
                case 2:
                        out |= b >> 2;
                        if (fputc(out, fout) == -1)
-                               return -1;
+                               return errno;
                        out = (uint8_t)((b & 0x3) << 6);
                        break;
                case 3:
                        out |= b;
                        if (fputc(out, fout) == -1)
-                               return -1;
+                               return errno;
                        out = 0;
                        break;
                default:
@@ -182,7 +182,7 @@
                switch (state) {
                case 0:
                case 1:
-                       return -1;
+                       return EFTYPE;
                case 2:
                        while ((c = getc(fin)) != -1) {
                                if (ignore && isspace(c))
@@ -190,7 +190,7 @@
                                break;
                        }
                        if (c != '=')
-                               return -1;
+                               return EFTYPE;
                        /*FALLTHROUGH*/
                case 3:
                        while ((c = getc(fin)) != -1) {
@@ -199,7 +199,7 @@
                                break;
                        }
                        if (c != -1)
-                               return -1;
+                               return EFTYPE;
                        return 0;
                default:
                        abort();
@@ -207,7 +207,7 @@
        }
 
        if (c != -1 || state != 0)
-               return -1;
+               return EFTYPE;
 
        return 0;
 }
@@ -226,12 +226,13 @@
        int e;
 
        if (decode)
-               e = b64_decode(stdout, stdin, ignore) != 0;
+               e = b64_decode(stdout, stdin, ignore);
        else
-               e = b64_encode(stdout, stdin, wrap) != 0;
+               e = b64_encode(stdout, stdin, wrap);
 
-       if (e != 0)
-               errx(EXIT_FAILURE, "%scoding failed", decode ? "De": "En");
+       if (e == 0)
+               return;
+       errc(EXIT_FAILURE, e, "%scoding failed", decode ? "De": "En");
 }
 
 int
@@ -242,14 +243,18 @@
        bool ignore = false;
        int c;
 
-       while ((c = getopt(argc, argv, "diw:")) != -1) {
+       while ((c = getopt(argc, argv, "b:Ddiw:")) != -1) {
                switch (c) {
+               case 'D':
+                       decode = ignore = true;
+                       break;
                case 'd':
                        decode = true;
                        break;
                case 'i':
                        ignore = true;
                        break;
+               case 'b':
                case 'w':
                        wrap = (size_t)atoi(optarg);
                        break;



Home | Main Index | Thread Index | Old Index