git: a8d9bd3fa585 - main - bintrans(1): qp switch to getopt_long

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Thu, 09 Jan 2025 09:27:26 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=a8d9bd3fa5855fe7583ed05946296ab6b9937d69

commit a8d9bd3fa5855fe7583ed05946296ab6b9937d69
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2025-01-08 11:13:54 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2025-01-09 09:24:34 +0000

    bintrans(1): qp switch to getopt_long
    
    In preparation for more arguments, switch bintrans qp argument parsing
    to getopt_long, while here make the decodign argument being -d|--decode
    for compatibility with base64 encoding/decoding
    
    MFC After:      1 week
    Reviewed by:    pstef
    Differential Revision:  https://reviews.freebsd.org/D48380
---
 usr.bin/bintrans/bintrans.1 |  4 +--
 usr.bin/bintrans/qp.c       | 61 +++++++++++++++++++++++++--------------------
 2 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/usr.bin/bintrans/bintrans.1 b/usr.bin/bintrans/bintrans.1
index 3376ecd332ed..4177a5c6b9eb 100644
--- a/usr.bin/bintrans/bintrans.1
+++ b/usr.bin/bintrans/bintrans.1
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd January 23, 2024
+.Dd January 8, 2025
 .Dt BINTRANS 1
 .Os
 .Sh NAME
@@ -230,7 +230,7 @@ through a dedicated program:
 is a quoted-printable converter
 and accepts the following options:
 .Bl -tag -width indent
-.It Fl u
+.It Fl d
 Decode.
 .It Fl o Ar output_file
 Output to
diff --git a/usr.bin/bintrans/qp.c b/usr.bin/bintrans/qp.c
index c2c9dfa7a224..3bff47945acf 100644
--- a/usr.bin/bintrans/qp.c
+++ b/usr.bin/bintrans/qp.c
@@ -26,6 +26,7 @@
  */
 
 #include <ctype.h>
+#include <getopt.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
@@ -151,44 +152,50 @@ static void
 usage(void)
 {
 	fprintf(stderr,
-	   "usage: bintrans qp [-u] [-o outputfile] [file name]\n");
+	   "usage: bintrans qp [-d] [-o outputfile] [file name]\n");
 }
 
 int
 main_quotedprintable(int argc, char *argv[])
 {
-	int i;
+	int ch;
 	bool encode = true;
 	FILE *fp = stdin;
 	FILE *fpo = stdout;
 
-	for (i = 1; i < argc; ++i) {
-		if (argv[i][0] == '-') {
-			switch (argv[i][1]) {
-			case 'o':
-				if (++i >= argc) {
-					fprintf(stderr, "qp: -o requires a file name.\n");
-					exit(EXIT_FAILURE);
-				}
-				fpo = fopen(argv[i], "w");
-				if (fpo == NULL) {
-					perror(argv[i]);
-					exit(EXIT_FAILURE);
-				}
-				break;
-			case 'u':
-				encode = false;
-				break;
-			default:
-				usage();
-				exit(EXIT_FAILURE);
-			}
-		} else {
-			fp = fopen(argv[i], "r");
-			if (fp == NULL) {
-				perror(argv[i]);
+	static const struct option opts[] =
+	{
+		{ "decode", no_argument,		NULL, 'd'},
+		{ "output", required_argument,		NULL, 'o'},
+		{NULL,		no_argument,		NULL, 0}
+	};
+
+	while ((ch = getopt_long(argc, argv, "do:u", opts, NULL)) != -1) {
+		switch(ch) {
+		case 'o':
+			fpo = fopen(optarg, "w");
+			if (fpo == NULL) {
+				perror(optarg);
 				exit(EXIT_FAILURE);
 			}
+			break;
+		case 'u':
+			/* FALLTHROUGH for backward compatibility */
+		case 'd':
+			encode = false;
+			break;
+		default:
+			usage();
+			exit(EXIT_FAILURE);
+		}
+	};
+	argc -= optind;
+	argv += optind;
+	if (argc > 0) {
+		fp = fopen(argv[0], "r");
+		if (fp == NULL) {
+			perror(argv[0]);
+			exit(EXIT_FAILURE);
 		}
 	}
 	qp(fp, fpo, encode);