Remove some dead code

This commit is contained in:
Justine Tunney 2023-07-03 02:47:05 -07:00
parent 168d1c157e
commit 73c0faa1b5
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
66 changed files with 324 additions and 7705 deletions

View file

@ -28,17 +28,15 @@
#include "third_party/getopt/getopt.internal.h"
#include "third_party/mbedtls/sha256.h"
#define PROG "sha256sum"
#define USAGE \
"\
Usage: " PROG " [-?hbctw] [PATH...]\n\
"[-?hbctw] [PATH...]\n\
-h help\n\
-c check mode\n\
-b binary mode\n\
-t textual mode\n\
-w warning mode\n\
\n\
cosmopolitan " PROG " v1.0\n\
cosmopolitan sha256sum v1.1\n\
copyright 2022 justine alexandra roberts tunney\n\
notice licenses are embedded in the binary\n\
https://twitter.com/justinetunney\n\
@ -56,11 +54,17 @@ static bool g_warn;
static char g_mode;
static bool g_check;
static int g_mismatches;
static const char *prog;
static wontreturn void PrintUsage(int rc, int fd) {
tinyprint(fd, "Usage: ", prog, USAGE, NULL);
exit(rc);
}
static void GetOpts(int argc, char *argv[]) {
int opt;
g_mode = ' ';
while ((opt = getopt(argc, argv, "?hbctw")) != -1) {
while ((opt = getopt(argc, argv, "hbctw")) != -1) {
switch (opt) {
case 'w':
g_warn = true;
@ -75,28 +79,13 @@ static void GetOpts(int argc, char *argv[]) {
g_mode = '*';
break;
case 'h':
case '?':
write(1, USAGE, sizeof(USAGE) - 1);
exit(0);
PrintUsage(0, 1);
default:
write(2, USAGE, sizeof(USAGE) - 1);
exit(64);
PrintUsage(1, 2);
}
}
}
static ssize_t Write(int fd, const char *s, ...) {
va_list va;
char buf[512];
buf[0] = 0;
va_start(va, s);
do {
strlcat(buf, s, sizeof(buf));
} while ((s = va_arg(va, const char *)));
va_end(va);
return write(fd, buf, strlen(buf));
}
static bool IsModeCharacter(char c) {
switch (c) {
case ' ':
@ -117,7 +106,7 @@ static bool IsSupportedPath(const char *path) {
case '\r':
case '\n':
case '\\':
Write(2, PROG, ": ", path, ": unsupported path\n", NULL);
tinyprint(2, prog, ": ", path, ": unsupported path\n", NULL);
return false;
default:
break;
@ -135,7 +124,7 @@ static bool GetDigest(const char *path, FILE *f, unsigned char digest[32]) {
_unassert(!mbedtls_sha256_update_ret(&ctx, buf, got));
}
if (ferror(f)) {
Write(2, PROG, ": ", path, ": ", _strerdoc(errno), "\n", NULL);
tinyprint(2, prog, ": ", path, ": ", strerror(errno), "\n", NULL);
return false;
}
_unassert(!mbedtls_sha256_finish_ret(&ctx, digest));
@ -150,7 +139,7 @@ static bool ProduceDigest(const char *path, FILE *f) {
if (!IsSupportedPath(path)) return false;
if (!GetDigest(path, f, digest)) return false;
hexpcpy(hexdigest, digest, 32);
Write(1, hexdigest, " ", mode, path, "\n", NULL);
tinyprint(1, hexdigest, " ", mode, path, "\n", NULL);
return true;
}
@ -182,13 +171,13 @@ static bool CheckDigests(const char *path, FILE *f) {
++g_mismatches;
k = false;
}
Write(1, path2, ": ", status, "\n", NULL);
tinyprint(1, path2, ": ", status, "\n", NULL);
} else {
k = false;
}
fclose(f2);
} else {
Write(2, PROG, ": ", path2, ": ", _strerdoc(errno), "\n", NULL);
tinyprint(2, prog, ": ", path2, ": ", strerror(errno), "\n", NULL);
k = false;
}
continue;
@ -196,12 +185,12 @@ static bool CheckDigests(const char *path, FILE *f) {
if (g_warn) {
char linestr[12];
FormatInt32(linestr, line + 1);
Write(2, PROG, ": ", path, ":", linestr, ": ",
"improperly formatted checksum line", "\n", NULL);
tinyprint(2, prog, ": ", path, ":", linestr, ": ",
"improperly formatted checksum line", "\n", NULL);
}
}
if (ferror(f)) {
Write(2, PROG, ": ", path, ": ", _strerdoc(errno), "\n", NULL);
tinyprint(2, prog, ": ", path, ": ", strerror(errno), "\n", NULL);
k = false;
}
return k;
@ -219,6 +208,8 @@ int main(int argc, char *argv[]) {
int i;
FILE *f;
bool k = true;
prog = argv[0];
if (!prog) prog = "sha256sum";
GetOpts(argc, argv);
if (optind == argc) {
f = stdin;
@ -229,7 +220,7 @@ int main(int argc, char *argv[]) {
k &= Process(argv[i], f);
fclose(f);
} else {
Write(2, PROG, ": ", argv[i], ": ", _strerdoc(errno), "\n", NULL);
tinyprint(2, prog, ": ", argv[i], ": ", strerror(errno), "\n", NULL);
k = false;
}
}
@ -237,8 +228,8 @@ int main(int argc, char *argv[]) {
if (g_mismatches) {
char ibuf[12];
FormatInt32(ibuf, g_mismatches);
Write(2, PROG, ": WARNING: ", ibuf, " computed checksum did NOT match\n",
NULL);
tinyprint(2, prog, ": WARNING: ", ibuf,
" computed checksum did NOT match\n", NULL);
}
return !k;
}