mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [PATCH] use perror(3) instead of fprintf(3)
@ 2023-01-13  8:31 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2023-01-13  8:31 UTC (permalink / raw)
  To: mwrap-perl

perror(3) is standardized and reduces our binary text size a bit:

    text           data     bss     dec     hex filename
   80330           1888    4352   86570   1522a before/Mwrap.so
   80043           1896    4352   86291   15113 after/Mwrap.so
---
 httpd.h      | 24 ++++++++++++------------
 mwrap_core.h |  4 ++--
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/httpd.h b/httpd.h
index 9219d36..8a105aa 100644
--- a/httpd.h
+++ b/httpd.h
@@ -221,7 +221,7 @@ static FILE *fbuf_init(struct mw_fbuf *fb)
 {
 	fb->ptr = NULL;
 	fb->fp = open_memstream(&fb->ptr, &fb->len);
-	if (!fb->fp) fprintf(stderr, "open_memstream: %m\n");
+	if (!fb->fp) perror("open_memstream");
 	return fb->fp;
 }
 
@@ -237,7 +237,7 @@ static int fbuf_close(struct mw_fbuf *fb)
 {
 	int e = ferror(fb->fp) | fclose(fb->fp);
 	fb->fp = NULL;
-	if (e) fprintf(stderr, "ferror|fclose: %m\n");
+	if (e) perror("ferror|fclose");
 	return e;
 }
 
@@ -279,7 +279,7 @@ static enum mw_qev h1_200(struct mw_h1 *h1, struct mw_fbuf *fb, const char *ct)
 	 */
 	off_t clen = ftello(fb->fp);
 	if (clen < 0) {
-		fprintf(stderr, "ftello: %m\n");
+		perror("ftello");
 		fbuf_close(fb);
 		return h1_close(h1);
 	}
@@ -468,7 +468,7 @@ static off_t write_loc_name(FILE *fp, const struct src_loc *l)
 	off_t beg = ftello(fp);
 
 	if (beg < 0) {
-		fprintf(stderr, "ftello: %m\n");
+		perror("ftello");
 		return beg;
 	}
 	if (l->f) {
@@ -498,7 +498,7 @@ static off_t write_loc_name(FILE *fp, const struct src_loc *l)
 	}
 	off_t end = ftello(fp);
 	if (end < 0) {
-		fprintf(stderr, "ftello: %m\n");
+		perror("ftello");
 		return end;
 	}
 	return end - beg;
@@ -868,7 +868,7 @@ static enum mw_qev h1_drain_input(struct mw_h1 *h1, struct mw_h1req *h1r,
 				return h1_close(h1);
 			default: /* ENOMEM, ENOBUFS, ... */
 				assert(errno != EBADF);
-				fprintf(stderr, "read: %m\n");
+				perror("read");
 				return h1_close(h1);
 			}
 		}
@@ -1001,7 +1001,7 @@ static enum mw_qev h1_event_step(struct mw_h1 *h1, struct mw_h1d *h1d)
 	if (!h1r) {
 		h1r = h1d->shared_h1r = malloc(sizeof(*h1r));
 		if (!h1r) {
-			fprintf(stderr, "h1r malloc: %m\n");
+			perror("h1r malloc");
 			return h1_close(h1);
 		}
 	}
@@ -1045,7 +1045,7 @@ static enum mw_qev h1_event_step(struct mw_h1 *h1, struct mw_h1d *h1d)
 				return h1_close(h1);
 			default: /* ENOMEM, ENOBUFS, ... */
 				assert(errno != EBADF);
-				fprintf(stderr, "read: %m\n");
+				perror("read");
 				return h1_close(h1);
 			}
 		}
@@ -1153,7 +1153,7 @@ static void h1d_unlink(struct mw_h1d *h1d, bool do_close)
 	if (h1d->lfd < 0 || !h1d->pid_len)
 		return;
 	if (getsockname(h1d->lfd, &sa.any, &len) < 0) {
-		fprintf(stderr, "getsockname: %m\n");
+		perror("getsockname");
 		return;
 	}
 	if (do_close) { /* only safe to close if thread isn't running */
@@ -1219,13 +1219,13 @@ static int h1d_init(struct mw_h1d *h1d, const char *menv)
 		return fprintf(stderr, "unlink(%s): %m\n", sa.un.sun_path);
 	h1d->lfd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
 	if (h1d->lfd < 0)
-		return fprintf(stderr, "socket: %m\n");
+		return perror("socket"), 1;
 	if (bind(h1d->lfd, &sa.any, (socklen_t)sizeof(sa)) < 0) {
-		fprintf(stderr, "bind: %m\n");
+		perror("bind");
 		goto close_fail;
 	}
 	if (listen(h1d->lfd, 1024) < 0) {
-		fprintf(stderr, "listen: %m\n");
+		perror("listen");
 		goto close_fail;
 	}
 	h1d->alive = 1; /* runs in parent, before pthread_create */
diff --git a/mwrap_core.h b/mwrap_core.h
index a246fea..a84cd6d 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -742,7 +742,7 @@ char **bt_syms(void * const *addrlist, uint32_t size)
 #else /* make FreeBSD look like glibc output: */
 	char **s = backtrace_symbols_fmt(addrlist, size, "%f(%n%D) [%a]");
 #endif
-	if (!s) fprintf(stderr, "backtrace_symbols: %m\n");
+	if (!s) perror("backtrace_symbols");
 	return s;
 }
 
@@ -1057,7 +1057,7 @@ __attribute__((constructor)) static void mwrap_ctor(void)
 		h->real = h;
 		call_rcu(&h->as.dead, free_hdr_rcu);
 	} else
-		fprintf(stderr, "malloc: %m\n");
+		perror("malloc");
 
 	h1d_start();
 	CHECK(int, 0, pthread_sigmask(SIG_SETMASK, &old, NULL));

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-13  8:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-13  8:31 [PATCH] use perror(3) instead of fprintf(3) Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).