shithub: fnt

Download patch

ref: 95df0a4352792cb83be7100c19ad0e815a492a52
parent: 6b622e92c2098ed15c550aed5e5242f5c11aa71d
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Jul 16 19:40:23 EDT 2024

don't dumpmap if redirected to /dev/null

--- a/plan9/test.c
+++ b/plan9/test.c
@@ -2,6 +2,14 @@
 #include <libc.h>
 #include <bio.h>
 #include "otf.h"
+
+static int
+otfdiscard(void *aux, void *dst, int sz)
+{
+	USED(aux, dst);
+	return sz;
+}
+
 #include "test.h"
 
 static int
@@ -32,6 +40,7 @@
 void
 main(int argc, char **argv)
 {
+	char buf[16];
 	Otfile in, out;
 
 	in.seek = otfseek;
@@ -40,6 +49,9 @@
 	out.write = otfwrite;
 	out.aux = Bfdopen(1, OWRITE);
 	parseoptions();
+
+	if(fd2path(1, buf, sizeof(buf)) == 0 && strcmp(buf, "/dev/null") == 0)
+		out.write = otfdiscard;
 
 	if((in.aux = Bopen(*argv, OREAD)) == nil || process(&in, &out) != 0)
 		sysfatal("%r");
--- a/test.h
+++ b/test.h
@@ -135,7 +135,7 @@
 			free(g);
 		}
 		if(ppem > 0){
-			if(dumpmap(out, im, n) != 0)
+			if(out->write != otfdiscard && dumpmap(out, im, n) != 0)
 				return -1;
 			for(i = 0; i < n; i++)
 				free(im[i].b);
@@ -150,7 +150,7 @@
 			GlyfImage im;
 			if(otfdrawglyf(o, g, ppem, gap, &im) != 0)
 				goto glypherr;
-			if(dumpmap(out, &im, 1) != 0)
+			if(out->write != otfdiscard && dumpmap(out, &im, 1) != 0)
 				return -1;
 			free(im.b);
 		}else{
--- a/unix/test.c
+++ b/unix/test.c
@@ -4,9 +4,18 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include "otf.h"
 #include "otfsys.h"
 #include "argbegin.h"
+
+static int
+otfdiscard(void *aux, const void *src, int sz)
+{
+	USED(aux); USED(src);
+	return sz;
+}
+
 #include "test.h"
 
 static int
@@ -39,6 +48,7 @@
 int
 main(int argc, char **argv)
 {
+	char buf[16];
 	Otfile in, out;
 
 	out.print = (void*)fprintf;
@@ -47,6 +57,9 @@
 	in.seek = otfseek;
 	in.read = otfread;
 	parseoptions();
+
+	if(readlink("/proc/self/fd/1", buf, sizeof(buf)) == 9 && strcmp(buf, "/dev/null") == 0)
+		out.write = otfdiscard;
 
 	if((in.aux = fopen(*argv, "rb")) == NULL)
 		err(1, nil);