summary refs log tree commit diff
path: root/bin/pngo.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2024-05-22 12:16:41 -0400
committerJune McEnroe <june@causal.agency>2024-05-22 12:16:41 -0400
commit625662a5c1cbb71cf37653df1f31cd60c48b18ae (patch)
tree7a04a48f7007b30f086b353c0c03002c861383b9 /bin/pngo.c
parentAdd photo descriptions from 05-03 and 05-06 (diff)
downloadsrc-master.tar.gz
src-master.zip
Remove use of sysexits.h HEAD master
Diffstat (limited to '')
-rw-r--r--bin/pngo.c83
1 files changed, 33 insertions, 50 deletions
diff --git a/bin/pngo.c b/bin/pngo.c
index eb51ccc2..e2ad3837 100644
--- a/bin/pngo.c
+++ b/bin/pngo.c
@@ -22,7 +22,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sysexits.h>
 #include <unistd.h>
 #include <zlib.h>
 
@@ -35,14 +34,14 @@ static uint32_t crc;
 
 static void pngRead(void *ptr, size_t len, const char *desc) {
 	size_t n = fread(ptr, len, 1, file);
-	if (!n && ferror(file)) err(EX_IOERR, "%s", path);
-	if (!n) errx(EX_DATAERR, "%s: missing %s", path, desc);
+	if (!n && ferror(file)) err(1, "%s", path);
+	if (!n) errx(1, "%s: missing %s", path, desc);
 	crc = crc32(crc, ptr, len);
 }
 
 static void pngWrite(const void *ptr, size_t len) {
 	size_t n = fwrite(ptr, len, 1, file);
-	if (!n) err(EX_IOERR, "%s", path);
+	if (!n) err(1, "%s", path);
 	crc = crc32(crc, ptr, len);
 }
 
@@ -52,7 +51,7 @@ static void sigRead(void) {
 	uint8_t sig[sizeof(Sig)];
 	pngRead(sig, sizeof(sig), "signature");
 	if (memcmp(sig, Sig, sizeof(sig))) {
-		errx(EX_DATAERR, "%s: invalid signature", path);
+		errx(1, "%s: invalid signature", path);
 	}
 }
 
@@ -96,10 +95,7 @@ static void crcRead(void) {
 	uint32_t expect = crc;
 	uint32_t actual = u32Read("CRC32");
 	if (actual == expect) return;
-	errx(
-		EX_DATAERR, "%s: expected CRC32 %08X, found %08X",
-		path, expect, actual
-	);
+	errx(1, "%s: expected CRC32 %08X, found %08X", path, expect, actual);
 }
 
 static void crcWrite(void) {
@@ -108,7 +104,7 @@ static void crcWrite(void) {
 
 static void chunkSkip(struct Chunk chunk) {
 	if (!(chunk.type[0] & 0x20)) {
-		errx(EX_CONFIG, "%s: unsupported critical chunk %s", path, chunk.type);
+		errx(1, "%s: unsupported critical chunk %s", path, chunk.type);
 	}
 	uint8_t buf[4096];
 	while (chunk.len > sizeof(buf)) {
@@ -181,7 +177,7 @@ static void headerPrint(void) {
 static void headerRead(struct Chunk chunk) {
 	if (chunk.len != HeaderLen) {
 		errx(
-			EX_DATAERR, "%s: expected %s length %" PRIu32 ", found %" PRIu32,
+			1, "%s: expected %s length %" PRIu32 ", found %" PRIu32,
 			path, chunk.type, (uint32_t)HeaderLen, chunk.len
 		);
 	}
@@ -195,8 +191,8 @@ static void headerRead(struct Chunk chunk) {
 	crcRead();
 	recalc();
 
-	if (!header.width) errx(EX_DATAERR, "%s: invalid width 0", path);
-	if (!header.height) errx(EX_DATAERR, "%s: invalid height 0", path);
+	if (!header.width) errx(1, "%s: invalid width 0", path);
+	if (!header.height) errx(1, "%s: invalid height 0", path);
 	static const struct {
 		uint8_t color;
 		uint8_t depth;
@@ -228,28 +224,21 @@ static void headerRead(struct Chunk chunk) {
 	}
 	if (!valid) {
 		errx(
-			EX_DATAERR,
-			"%s: invalid color type %" PRIu8 " and bit depth %" PRIu8,
+			1, "%s: invalid color type %" PRIu8 " and bit depth %" PRIu8,
 			path, header.color, header.depth
 		);
 	}
 	if (header.compression != Deflate) {
 		errx(
-			EX_DATAERR, "%s: invalid compression method %" PRIu8,
+			1, "%s: invalid compression method %" PRIu8,
 			path, header.compression
 		);
 	}
 	if (header.filter != Adaptive) {
-		errx(
-			EX_DATAERR, "%s: invalid filter method %" PRIu8,
-			path, header.filter
-		);
+		errx(1, "%s: invalid filter method %" PRIu8, path, header.filter);
 	}
 	if (header.interlace > Adam7) {
-		errx(
-			EX_DATAERR, "%s: invalid interlace method %" PRIu8,
-			path, header.interlace
-		);
+		errx(1, "%s: invalid interlace method %" PRIu8, path, header.interlace);
 	}
 
 	if (verbose) headerPrint();
@@ -331,16 +320,13 @@ static void transCompact(void) {
 static void palRead(struct Chunk chunk) {
 	if (chunk.len % 3) {
 		errx(
-			EX_DATAERR, "%s: %s length %" PRIu32 " not divisible by 3",
+			1, "%s: %s length %" PRIu32 " not divisible by 3",
 			path, chunk.type, chunk.len
 		);
 	}
 	pal.len = chunk.len / 3;
 	if (pal.len > 256) {
-		errx(
-			EX_DATAERR, "%s: %s length %" PRIu32 " > 256",
-			path, chunk.type, pal.len
-		);
+		errx(1, "%s: %s length %" PRIu32 " > 256", path, chunk.type, pal.len);
 	}
 	pngRead(pal.rgb, chunk.len, "palette data");
 	crcRead();
@@ -362,10 +348,7 @@ static void palWrite(void) {
 static void transRead(struct Chunk chunk) {
 	trans.len = chunk.len;
 	if (trans.len > 256) {
-		errx(
-			EX_DATAERR, "%s: %s length %" PRIu32 " > 256",
-			path, chunk.type, trans.len
-		);
+		errx(1, "%s: %s length %" PRIu32 " > 256", path, chunk.type, trans.len);
 	}
 	pngRead(trans.a, chunk.len, "transparency data");
 	crcRead();
@@ -388,7 +371,7 @@ static uint8_t *data;
 
 static void dataAlloc(void) {
 	data = malloc(dataLen);
-	if (!data) err(EX_OSERR, "malloc");
+	if (!data) err(1, "malloc");
 }
 
 static const char *humanize(size_t n) {
@@ -408,15 +391,15 @@ static void dataRead(struct Chunk chunk) {
 
 	z_stream stream = { .next_out = data, .avail_out = dataLen };
 	int error = inflateInit(&stream);
-	if (error != Z_OK) errx(EX_SOFTWARE, "inflateInit: %s", stream.msg);
+	if (error != Z_OK) errx(1, "inflateInit: %s", stream.msg);
 
 	for (;;) {
 		if (strcmp(chunk.type, "IDAT")) {
-			errx(EX_DATAERR, "%s: missing IDAT chunk", path);
+			errx(1, "%s: missing IDAT chunk", path);
 		}
 
 		uint8_t *idat = malloc(chunk.len);
-		if (!idat) err(EX_OSERR, "malloc");
+		if (!idat) err(1, "malloc");
 
 		pngRead(idat, chunk.len, "image data");
 		crcRead();
@@ -428,7 +411,7 @@ static void dataRead(struct Chunk chunk) {
 
 		if (error == Z_STREAM_END) break;
 		if (error != Z_OK) {
-			errx(EX_DATAERR, "%s: inflate: %s", path, stream.msg);
+			errx(1, "%s: inflate: %s", path, stream.msg);
 		}
 
 		chunk = chunkRead();
@@ -436,7 +419,7 @@ static void dataRead(struct Chunk chunk) {
 	inflateEnd(&stream);
 	if ((size_t)stream.total_out != dataLen) {
 		errx(
-			EX_DATAERR, "%s: expected data length %zu, found %zu",
+			1, "%s: expected data length %zu, found %zu",
 			path, dataLen, (size_t)stream.total_out
 		);
 	}
@@ -461,11 +444,11 @@ static void dataWrite(void) {
 	int error = deflateInit2(
 		&stream, Z_BEST_COMPRESSION, Z_DEFLATED, 15, 8, Z_FILTERED
 	);
-	if (error != Z_OK) errx(EX_SOFTWARE, "deflateInit2: %s", stream.msg);
+	if (error != Z_OK) errx(1, "deflateInit2: %s", stream.msg);
 
 	uLong bound = deflateBound(&stream, dataLen);
 	uint8_t *buf = malloc(bound);
-	if (!buf) err(EX_OSERR, "malloc");
+	if (!buf) err(1, "malloc");
 
 	stream.next_out = buf;
 	stream.avail_out = bound;
@@ -566,7 +549,7 @@ static void dataFilter(void) {
 	uint8_t *filter[FilterCap];
 	for (enum Filter i = None; i < FilterCap; ++i) {
 		filter[i] = malloc(lineLen);
-		if (!filter[i]) err(EX_OSERR, "malloc");
+		if (!filter[i]) err(1, "malloc");
 	}
 	for (uint32_t y = header.height-1; y < header.height; --y) {
 		uint32_t heuristic[FilterCap] = {0};
@@ -838,7 +821,7 @@ static void optimize(const char *inPath, const char *outPath) {
 	if (inPath) {
 		path = inPath;
 		file = fopen(path, "r");
-		if (!file) err(EX_NOINPUT, "%s", path);
+		if (!file) err(1, "%s", path);
 	} else {
 		path = "stdin";
 		file = stdin;
@@ -847,11 +830,11 @@ static void optimize(const char *inPath, const char *outPath) {
 	sigRead();
 	struct Chunk ihdr = chunkRead();
 	if (strcmp(ihdr.type, "IHDR")) {
-		errx(EX_DATAERR, "%s: expected IHDR, found %s", path, ihdr.type);
+		errx(1, "%s: expected IHDR, found %s", path, ihdr.type);
 	}
 	headerRead(ihdr);
 	if (header.interlace != Progressive) {
-		errx(EX_CONFIG, "%s: unsupported interlacing", path);
+		errx(1, "%s: unsupported interlacing", path);
 	}
 
 	palClear();
@@ -888,10 +871,10 @@ static void optimize(const char *inPath, const char *outPath) {
 		if (outPath == inPath) {
 			snprintf(buf, sizeof(buf), "%so", outPath);
 			file = fopen(buf, "wx");
-			if (!file) err(EX_CANTCREAT, "%s", buf);
+			if (!file) err(1, "%s", buf);
 		} else {
 			file = fopen(path, "w");
-			if (!file) err(EX_CANTCREAT, "%s", outPath);
+			if (!file) err(1, "%s", outPath);
 		}
 	} else {
 		path = "stdout";
@@ -907,11 +890,11 @@ static void optimize(const char *inPath, const char *outPath) {
 	dataWrite();
 	free(data);
 	int error = fclose(file);
-	if (error) err(EX_IOERR, "%s", path);
+	if (error) err(1, "%s", path);
 
 	if (outPath && outPath == inPath) {
 		error = rename(buf, outPath);
-		if (error) err(EX_CANTCREAT, "%s", outPath);
+		if (error) err(1, "%s", outPath);
 	}
 }
 
@@ -927,7 +910,7 @@ int main(int argc, char *argv[]) {
 			break; case 'g': discardColor = true;
 			break; case 'o': outPath = optarg;
 			break; case 'v': verbose = true;
-			break; default:  return EX_USAGE;
+			break; default:  return 1;
 		}
 	}