uu{encode,decode}: improve style

This commit is contained in:
Piotr Pawel Stefaniak 2021-11-08 15:31:01 +01:00
parent 68700941c7
commit d5d3f5dab2
2 changed files with 23 additions and 21 deletions

View File

@ -62,6 +62,7 @@ __FBSDID("$FreeBSD$");
#include <libgen.h> #include <libgen.h>
#include <pwd.h> #include <pwd.h>
#include <resolv.h> #include <resolv.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -69,7 +70,7 @@ __FBSDID("$FreeBSD$");
static const char *infile, *outfile; static const char *infile, *outfile;
static FILE *infp, *outfp; static FILE *infp, *outfp;
static int base64, cflag, iflag, oflag, pflag, rflag, sflag; static bool base64, cflag, iflag, oflag, pflag, rflag, sflag;
static void usage(void); static void usage(void);
static int decode(void); static int decode(void);
@ -83,42 +84,42 @@ main(int argc, char *argv[])
int rval, ch; int rval, ch;
if (strcmp(basename(argv[0]), "b64decode") == 0) if (strcmp(basename(argv[0]), "b64decode") == 0)
base64 = 1; base64 = true;
while ((ch = getopt(argc, argv, "cimo:prs")) != -1) { while ((ch = getopt(argc, argv, "cimo:prs")) != -1) {
switch (ch) { switch (ch) {
case 'c': case 'c':
if (oflag || rflag) if (oflag || rflag)
usage(); usage();
cflag = 1; /* multiple uudecode'd files */ cflag = true; /* multiple uudecode'd files */
break; break;
case 'i': case 'i':
iflag = 1; /* ask before override files */ iflag = true; /* ask before override files */
break; break;
case 'm': case 'm':
base64 = 1; base64 = true;
break; break;
case 'o': case 'o':
if (cflag || pflag || rflag || sflag) if (cflag || pflag || rflag || sflag)
usage(); usage();
oflag = 1; /* output to the specified file */ oflag = true; /* output to the specified file */
sflag = 1; /* do not strip pathnames for output */ sflag = true; /* do not strip pathnames for output */
outfile = optarg; /* set the output filename */ outfile = optarg; /* set the output filename */
break; break;
case 'p': case 'p':
if (oflag) if (oflag)
usage(); usage();
pflag = 1; /* print output to stdout */ pflag = true; /* print output to stdout */
break; break;
case 'r': case 'r':
if (cflag || oflag) if (cflag || oflag)
usage(); usage();
rflag = 1; /* decode raw data */ rflag = true; /* decode raw data */
break; break;
case 's': case 's':
if (oflag) if (oflag)
usage(); usage();
sflag = 1; /* do not strip pathnames for output */ sflag = true; /* do not strip pathnames for output */
break; break;
default: default:
usage(); usage();
@ -185,14 +186,14 @@ decode2(void)
struct stat st; struct stat st;
char buf[MAXPATHLEN + 1]; char buf[MAXPATHLEN + 1];
base64 = 0; base64 = false;
/* search for header line */ /* search for header line */
for (;;) { for (;;) {
if (fgets(buf, sizeof(buf), infp) == NULL) if (fgets(buf, sizeof(buf), infp) == NULL)
return (EOF); return (EOF);
p = buf; p = buf;
if (strncmp(p, "begin-base64 ", 13) == 0) { if (strncmp(p, "begin-base64 ", 13) == 0) {
base64 = 1; base64 = true;
p += 13; p += 13;
} else if (strncmp(p, "begin ", 6) == 0) } else if (strncmp(p, "begin ", 6) == 0)
p += 6; p += 6;
@ -378,7 +379,7 @@ uu_decode(void)
} else { } else {
if (i >= 1) { if (i >= 1) {
if (!(IS_DEC(*p) && IS_DEC(*(p + 1)))) if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
OUT_OF_RANGE; OUT_OF_RANGE;
ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
putc(ch, outfp); putc(ch, outfp);
} }

View File

@ -58,6 +58,7 @@ __FBSDID("$FreeBSD$");
#include <libgen.h> #include <libgen.h>
#include <resolv.h> #include <resolv.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -68,18 +69,18 @@ static void usage(void);
static FILE *output; static FILE *output;
static int mode; static int mode;
static char raw = 0; static bool raw;
static char **av; static char **av;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct stat sb; struct stat sb;
int base64; bool base64;
int ch; int ch;
const char *outfile; const char *outfile;
base64 = 0; base64 = false;
outfile = NULL; outfile = NULL;
if (strcmp(basename(argv[0]), "b64encode") == 0) if (strcmp(basename(argv[0]), "b64encode") == 0)
@ -88,13 +89,13 @@ main(int argc, char *argv[])
while ((ch = getopt(argc, argv, "mo:r")) != -1) { while ((ch = getopt(argc, argv, "mo:r")) != -1) {
switch (ch) { switch (ch) {
case 'm': case 'm':
base64 = 1; base64 = true;
break; break;
case 'o': case 'o':
outfile = optarg; outfile = optarg;
break; break;
case 'r': case 'r':
raw = 1; raw = true;
break; break;
case '?': case '?':
default: default:
@ -104,7 +105,7 @@ main(int argc, char *argv[])
argv += optind; argv += optind;
argc -= optind; argc -= optind;
switch(argc) { switch (argc) {
case 2: /* optional first argument is input file */ case 2: /* optional first argument is input file */
if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb)) if (!freopen(*argv, "r", stdin) || fstat(fileno(stdin), &sb))
err(1, "%s", *argv); err(1, "%s", *argv);
@ -179,8 +180,8 @@ base64_encode(void)
static void static void
encode(void) encode(void)
{ {
register int ch, n; int ch, n;
register char *p; char *p;
char buf[80]; char buf[80];
if (!raw) if (!raw)