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
This commit is contained in:
Baptiste Daroussin 2025-01-08 12:13:54 +01:00
parent ee233742a5
commit a8d9bd3fa5
2 changed files with 36 additions and 29 deletions

View file

@ -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

View file

@ -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);