From 791b6de78673f005e9748983231f7260f6b69e99 Mon Sep 17 00:00:00 2001
From: Marc Kleine-Budde <mkl@pengutronix.de>
Date: Wed, 30 Oct 2019 11:59:49 +0100
Subject: [PATCH] treewide: Fix compilation on libmusl, replace error() by
 err()

Reported-by: Brandon Ros <brandonros1@gmail.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 jacd.c      | 56 ++++++++++++++++++++++++++---------------------------
 jspy.c      | 26 ++++++++++++-------------
 jsr.c       | 28 +++++++++++++--------------
 libj1939.c  |  4 ++--
 testj1939.c | 23 +++++++++++-----------
 5 files changed, 68 insertions(+), 69 deletions(-)

diff --git a/jacd.c b/jacd.c
index 83275e2..0b7469b 100644
--- a/jacd.c
+++ b/jacd.c
@@ -19,7 +19,7 @@
 
 #include <unistd.h>
 #include <getopt.h>
-#include <error.h>
+#include <err.h>
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <net/if.h>
@@ -154,12 +154,12 @@ static int parse_range(char *str)
 	for (tok = strtok(str, ",;"); tok; tok = strtok(NULL, ",;")) {
 		a0 = ae = strtoul(tok, &endp, 0);
 		if (endp <= tok)
-			error(1, 0, "parsing range '%s'", tok);
+			err(1, "parsing range '%s'", tok);
 		if (*endp == '-') {
 			tok = endp+1;
 			ae = strtoul(tok, &endp, 0);
 			if (endp <= tok)
-				error(1, 0, "parsing addr '%s'", tok);
+				err(1, "parsing addr '%s'", tok);
 			if (ae < a0)
 				ae = a0;
 		}
@@ -204,21 +204,21 @@ static int open_socket(const char *device, uint64_t name)
 		fprintf(stderr, "- socket(PF_CAN, SOCK_DGRAM, CAN_J1939);\n");
 	sock = ret = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
 	if (ret < 0)
-		error(1, errno, "socket(j1939)");
+		err(1, "socket(j1939)");
 
 	if (s.verbose)
 		fprintf(stderr, "- setsockopt(, SOL_SOCKET, SO_BINDTODEVICE, %s, %zd);\n", device, strlen(device));
 	ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
 			device, strlen(device));
 	if (ret < 0)
-		error(1, errno, "bindtodevice %s", device);
+		err(1, "bindtodevice %s", device);
 
 	if (s.verbose)
 		fprintf(stderr, "- setsockopt(, SOL_CAN_J1939, SO_J1939_FILTER, <filter>, %zd);\n", sizeof(filt));
 	ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER,
 			&filt, sizeof(filt));
 	if (ret < 0)
-		error(1, errno, "setsockopt filter");
+		err(1, "setsockopt filter");
 
 	value = 1;
 	if (s.verbose)
@@ -226,13 +226,13 @@ static int open_socket(const char *device, uint64_t name)
 	ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_RECV_OWN,
 			&value, sizeof(value));
 	if (ret < 0)
-		error(1, errno, "setsockopt receive own msgs");
+		err(1, "setsockopt receive own msgs");
 
 	if (s.verbose)
 		fprintf(stderr, "- bind(, %s, %zi);\n", libj1939_addr2str(&saddr), sizeof(saddr));
 	ret = bind(sock, (void *)&saddr, sizeof(saddr));
 	if (ret < 0)
-		error(1, errno, "bind()");
+		err(1, "bind()");
 	return sock;
 }
 
@@ -249,7 +249,7 @@ static int repeat_address(int sock, uint64_t name)
 		fprintf(stderr, "- send(, %" PRId64 ", 8, 0);\n", name);
 	ret = send(sock, dat, 8, 0);
 	if (must_warn(ret))
-		error(1, errno, "send address claim for 0x%02x", s.last_sa);
+		err(1, "send address claim for 0x%02x", s.last_sa);
 	return ret;
 }
 static int claim_address(int sock, uint64_t name, int sa)
@@ -269,7 +269,7 @@ static int claim_address(int sock, uint64_t name, int sa)
 		fprintf(stderr, "- bind(, %s, %zi);\n", libj1939_addr2str(&saddr), sizeof(saddr));
 	ret = bind(sock, (void *)&saddr, sizeof(saddr));
 	if (ret < 0)
-		error(1, errno, "rebind with sa 0x%02x", sa);
+		err(1, "rebind with sa 0x%02x", sa);
 	s.last_sa = sa;
 	return repeat_address(sock, name);
 }
@@ -288,7 +288,7 @@ static int request_addresses(int sock)
 		fprintf(stderr, "- sendto(, { 0, 0xee, 0, }, %zi, 0, %s, %zi);\n", sizeof(dat), libj1939_addr2str(&saddr), sizeof(saddr));
 	ret = sendto(sock, dat, sizeof(dat), 0, (void *)&saddr, sizeof(saddr));
 	if (must_warn(ret))
-		error(1, errno, "send request for address claims");
+		err(1, "send request for address claims");
 	return ret;
 }
 
@@ -356,7 +356,7 @@ static void install_signal(int sig)
 	sigfillset(&sigact.sa_mask);
 	ret = sigaction(sig, &sigact, NULL);
 	if (ret < 0)
-		error(1, errno, "sigaction for signal %i", sig);
+		err(1, "sigaction for signal %i", sig);
 }
 
 static void schedule_itimer(int msec)
@@ -372,7 +372,7 @@ static void schedule_itimer(int msec)
 		ret = setitimer(ITIMER_REAL, &val, NULL);
 	} while ((ret < 0) && (errno == EINTR));
 	if (ret < 0)
-		error(1, errno, "setitimer %i msec", msec);
+		err(1, "setitimer %i msec", msec);
 }
 
 /* dump status */
@@ -413,7 +413,7 @@ static void save_cache(void)
 		return;
 	fp = fopen(s.cachefile, "w");
 	if (!fp)
-		error(1, errno, "fopen %s, w", s.cachefile);
+		err(1, "fopen %s, w", s.cachefile);
 
 	time(&t);
 	fprintf(fp, "# saved on %s\n", ctime(&t));
@@ -436,7 +436,7 @@ static void restore_cache(void)
 	if (!fp) {
 		if (ENOENT == errno)
 			return;
-		error(1, errno, "fopen %s, r", s.cachefile);
+		err(1, "fopen %s, r", s.cachefile);
 	}
 	while (!feof(fp)) {
 		ret = getline(&line, &sz, fp);
@@ -486,7 +486,7 @@ int main(int argc, char *argv[])
 #ifdef _GNU_SOURCE
 		asprintf(&program_invocation_name, "%s.%s", program_invocation_short_name, optarg);
 #else
-		error(0, 0, "compile with -D_GNU_SOURCE to use -p");
+		err(0, "compile with -D_GNU_SOURCE to use -p");
 #endif
 		break;
 	default:
@@ -505,18 +505,18 @@ int main(int argc, char *argv[])
 
 	ret = parse_range(s.ranges);
 	if (!ret)
-		error(1, 0, "no addresses in range");
+		err(1, "no addresses in range");
 
 	if ((s.current_sa < J1939_IDLE_ADDR) && !(addr[s.current_sa].flags & F_USE)) {
 		if (s.verbose)
-			error(0, 0, "forget saved address 0x%02x", s.current_sa);
+			err(0, "forget saved address 0x%02x", s.current_sa);
 		s.current_sa = J1939_IDLE_ADDR;
 	}
 
 	if (s.verbose)
-		error(0, 0, "ready for %s:%016llx", s.intf, (long long)s.name);
+		err(0, "ready for %s:%016llx", s.intf, (long long)s.name);
 	if (!s.intf || !s.name)
-		error(1, 0, "bad arguments");
+		err(1, "bad arguments");
 	ret = sock = open_socket(s.intf, s.name);
 
 	install_signal(SIGTERM);
@@ -534,7 +534,7 @@ int main(int argc, char *argv[])
 		case STATE_INITIAL:
 			ret = request_addresses(sock);
 			if (ret < 0)
-				error(1, errno, "could not sent initial request");
+				err(1, "could not sent initial request");
 			s.state = STATE_REQ_SENT;
 			break;
 		case STATE_REQ_PENDING:
@@ -544,7 +544,7 @@ int main(int argc, char *argv[])
 			/* claim addr */
 			sa = choose_new_sa(s.name, s.current_sa);
 			if (sa == J1939_IDLE_ADDR)
-				error(1, 0, "no free address to use");
+				err(1, "no free address to use");
 			ret = claim_address(sock, s.name, sa);
 			if (ret < 0)
 				schedule_itimer(50);
@@ -565,7 +565,7 @@ int main(int argc, char *argv[])
 		if (ret < 0) {
 			if (EINTR == errno)
 				continue;
-			error(1, errno, "recvfrom()");
+			err(1, "recvfrom()");
 		}
 		switch (saddr.can_addr.j1939.pgn) {
 		case 0x0ea00:
@@ -577,7 +577,7 @@ int main(int argc, char *argv[])
 				break;
 			if (s.state == STATE_REQ_SENT) {
 				if (s.verbose)
-					error(0, 0, "request sent, pending for 1250 ms");
+					err(0, "request sent, pending for 1250 ms");
 				schedule_itimer(1250);
 				s.state = STATE_REQ_PENDING;
 			} else if (s.state == STATE_OPERATIONAL) {
@@ -607,14 +607,14 @@ int main(int argc, char *argv[])
 				/* ourselve, disable itimer */
 				s.current_sa = sa;
 				if (s.verbose)
-					error(0, 0, "claimed 0x%02x", sa);
+					err(0, "claimed 0x%02x", sa);
 			} else if (sa == s.current_sa) {
 				if (s.verbose)
-					error(0, 0, "address collision for 0x%02x", sa);
+					err(0, "address collision for 0x%02x", sa);
 				if (s.name > saddr.can_addr.j1939.name) {
 					sa = choose_new_sa(s.name, sa);
 					if (sa == J1939_IDLE_ADDR) {
-						error(0, 0, "no address left");
+						err(0, "no address left");
 						/* put J1939_IDLE_ADDR in cache file */
 						s.current_sa = sa;
 						goto done;
@@ -639,7 +639,7 @@ int main(int argc, char *argv[])
 	}
 done:
 	if (s.verbose)
-		error(0, 0, "shutdown");
+		err(0, "shutdown");
 	claim_address(sock, s.name, J1939_IDLE_ADDR);
 	save_cache();
 	return 0;
diff --git a/jspy.c b/jspy.c
index 6ae34d8..64bef2a 100644
--- a/jspy.c
+++ b/jspy.c
@@ -18,7 +18,7 @@
 
 #include <unistd.h>
 #include <getopt.h>
-#include <error.h>
+#include <err.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/time.h>
@@ -122,7 +122,7 @@ int main(int argc, char **argv)
 	case 't':
 		if (optarg) {
 			if (!strchr("adzA", optarg[0]))
-				error(1, 0, "unknown time option '%c'", optarg[0]);
+				err(1, "unknown time option '%c'", optarg[0]);
 			s.time = optarg[0];
 		} else {
 			s.time = 'z';
@@ -137,19 +137,19 @@ int main(int argc, char **argv)
 		optarg = argv[optind];
 		ret = libj1939_str2addr(optarg, 0, &s.addr);
 		if (ret < 0) {
-			error(0, 0, "bad URI %s", optarg);
+			err(0, "bad URI %s", optarg);
 			return 1;
 		}
 	}
 
 	buf = malloc(s.pkt_len);
 	if (!buf)
-		error(1, errno, "malloc %u", s.pkt_len);
+		err(1, "malloc %u", s.pkt_len);
 
 	/* setup socket */
 	sock = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
 	if (sock < 0)
-		error(1, errno, "socket(can, dgram, j1939)");
+		err(1, "socket(can, dgram, j1939)");
 
 	memset(&filt, 0, sizeof(filt));
 	if (s.addr.can_addr.j1939.name) {
@@ -170,23 +170,23 @@ int main(int argc, char **argv)
 	if (filter) {
 		ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_FILTER, &filt, sizeof(filt));
 		if (ret < 0)
-			error(1, errno, "setsockopt filter");
+			err(1, "setsockopt filter");
 	}
 
 	if (s.promisc) {
 		ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_PROMISC, &ival_1, sizeof(ival_1));
 		if (ret < 0)
-			error(1, errno, "setsockopt promisc");
+			err(1, "setsockopt promisc");
 	}
 
 	if (s.time) {
 		ret = setsockopt(sock, SOL_SOCKET, SO_TIMESTAMP, &ival_1, sizeof(ival_1));
 		if (ret < 0)
-			error(1, errno, "setsockopt timestamp");
+			err(1, "setsockopt timestamp");
 	}
 	ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &s.pkt_len, sizeof(s.pkt_len));
 		if (ret < 0)
-			error(1, errno, "setsockopt rcvbuf %u", s.pkt_len);
+			err(1, "setsockopt rcvbuf %u", s.pkt_len);
 
 	/* bind(): to default, only ifindex is used. */
 	memset(&src, 0, sizeof(src));
@@ -197,7 +197,7 @@ int main(int argc, char **argv)
 	src.can_addr.j1939.pgn = J1939_NO_PGN;
 	ret = bind(sock, (void *)&src, sizeof(src));
 	if (ret < 0)
-		error(1, errno, "bind(%s)", argv[1]);
+		err(1, "bind(%s)", argv[1]);
 
 	/* these settings are static and can be held out of the hot path */
 	iov.iov_base = &buf[0];
@@ -208,7 +208,7 @@ int main(int argc, char **argv)
 
 	memset(&tref, 0, sizeof(tref));
 	if (s.verbose)
-		error(0, 0, "listening");
+		err(0, "listening");
 	while (1) {
 		/* these settings may be modified by recvmsg() */
 		iov.iov_len = s.pkt_len;
@@ -221,12 +221,12 @@ int main(int argc, char **argv)
 		if (ret < 0) {
 			switch (errno) {
 			case ENETDOWN:
-				error(0, errno, "ifindex %i", s.addr.can_ifindex);
+				err(0, "ifindex %i", s.addr.can_ifindex);
 				continue;
 			case EINTR:
 				continue;
 			default:
-				error(1, errno, "recvmsg(ifindex %i)", s.addr.can_ifindex);
+				err(1, "recvmsg(ifindex %i)", s.addr.can_ifindex);
 				break;
 			}
 		}
diff --git a/jsr.c b/jsr.c
index 20ea99c..3b84f0d 100644
--- a/jsr.c
+++ b/jsr.c
@@ -17,7 +17,7 @@
 
 #include <unistd.h>
 #include <getopt.h>
-#include <error.h>
+#include <err.h>
 #include <poll.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -105,7 +105,7 @@ int main(int argc, char **argv)
 	case 's':
 		s.pkt_len = strtoul(optarg, 0, 0);
 		if (!s.pkt_len)
-			error(1, EINVAL, "packet size of %s", optarg);
+			err(1, "packet size of %s", optarg);
 		break;
 	case 'p':
 		s.priority = strtoul(optarg, 0, 0);
@@ -124,14 +124,14 @@ int main(int argc, char **argv)
 		optarg = argv[optind++];
 		ret = libj1939_str2addr(optarg, 0, &s.src);
 		if (ret < 0)
-			error(1, 0, "bad address spec [%s]", optarg);
+			err(1, "bad address spec [%s]", optarg);
 		s.defined |= DEF_SRC;
 	}
 	if (argv[optind]) {
 		optarg = argv[optind++];
 		ret = libj1939_str2addr(optarg, 0, &s.dst);
 		if (ret < 0)
-			error(1, 0, "bad address spec [%s]", optarg);
+			err(1, "bad address spec [%s]", optarg);
 		s.defined |= DEF_DST;
 	}
 
@@ -139,36 +139,36 @@ int main(int argc, char **argv)
 		struct stat st;
 
 		if (fstat(STDIN_FILENO, &st) < 0)
-			error(1, errno, "stat stdin, could not determine buffer size");
+			err(1, "stat stdin, could not determine buffer size");
 		s.pkt_len = st.st_size ?: 1024;
 	}
 
 	/* prepare */
 	buf = malloc(s.pkt_len);
 	if (!buf)
-		error(1, errno, "malloc %u", s.pkt_len);
+		err(1, "malloc %u", s.pkt_len);
 
 	sock = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
 	if (sock < 0)
-		error(1, errno, "socket(can, dgram, j1939)");
+		err(1, "socket(can, dgram, j1939)");
 
 	if (s.defined & DEF_PRIO) {
 		ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_SEND_PRIO, &s.priority, sizeof(s.priority));
 		if (ret < 0)
-			error(1, errno, "setsockopt priority");
+			err(1, "setsockopt priority");
 	}
 	if (s.defined & DEF_SRC) {
 		s.src.can_family = AF_CAN;
 		ret = bind(sock, (void *)&s.src, sizeof(s.src));
 		if (ret < 0)
-			error(1, errno, "bind(%s), %i", libj1939_addr2str(&s.src), -errno);
+			err(1, "bind(%s), %i", libj1939_addr2str(&s.src), -errno);
 	}
 
 	if (s.defined & DEF_DST) {
 		s.dst.can_family = AF_CAN;
 		ret = connect(sock, (void *)&s.dst, sizeof(s.dst));
 		if (ret < 0)
-			error(1, errno, "connect(%s), %i", libj1939_addr2str(&s.dst), -errno);
+			err(1, "connect(%s), %i", libj1939_addr2str(&s.dst), -errno);
 	}
 
 	pfd[0].fd = STDIN_FILENO;
@@ -182,19 +182,19 @@ int main(int argc, char **argv)
 		if (ret < 0) {
 			if (errno == EINTR)
 				continue;
-			error(1, errno, "poll()");
+			err(1, "poll()");
 		}
 		if (pfd[0].revents) {
 			ret = read(pfd[0].fd, buf, s.pkt_len);
 			if (ret < 0)
-				error(1, errno, "read(stdin)");
+				err(1, "read(stdin)");
 			if (!ret)
 				break;
 			len = ret;
 			do {
 				ret = send(pfd[1].fd, buf, len, s.sendflags);
 				if (ret < 0)
-					error(errno != ENOBUFS, errno, "write(%s)",
+					err(errno != ENOBUFS, "write(%s)",
 							libj1939_addr2str(&s.src));
 			} while (ret < 0);
 		}
@@ -202,7 +202,7 @@ int main(int argc, char **argv)
 			ret = read(pfd[1].fd, buf, s.pkt_len);
 			if (ret < 0) {
 				ret = errno;
-				error(0, errno, "read(%s)", libj1939_addr2str(&s.dst));
+				err(0, "read(%s)", libj1939_addr2str(&s.dst));
 				switch (ret) {
 				case EHOSTDOWN:
 					break;
diff --git a/libj1939.c b/libj1939.c
index 5970d02..a710b73 100644
--- a/libj1939.c
+++ b/libj1939.c
@@ -15,7 +15,7 @@
 #include <errno.h>
 #include <inttypes.h>
 
-#include <error.h>
+#include <err.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <net/if.h>
@@ -41,7 +41,7 @@ static inline void fetch_names(void)
 	if (!saved) {
 		saved = if_nameindex();
 		if (!saved)
-			error(1, errno, "if_nameindex()");
+			err(1, "if_nameindex()");
 	}
 }
 
diff --git a/testj1939.c b/testj1939.c
index 83df1d2..82d3266 100644
--- a/testj1939.c
+++ b/testj1939.c
@@ -19,7 +19,7 @@
 
 #include <unistd.h>
 #include <getopt.h>
-#include <error.h>
+#include <err.h>
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <net/if.h>
@@ -71,7 +71,7 @@ static void parse_canaddr(char *spec, struct sockaddr_can *paddr)
 
 static void onsigalrm(int sig)
 {
-	error(0, 0, "exit as requested");
+	err(0, "exit as requested");
 	exit(0);
 }
 
@@ -82,7 +82,7 @@ static void schedule_oneshot_itimer(double delay)
 	it.it_value.tv_sec = delay;
 	it.it_value.tv_usec = (long)(delay * 1e6) % 1000000;
 	if (setitimer(ITIMER_REAL, &it, NULL) < 0)
-		error(1, errno, "schedule itimer %.3lfs", delay);
+		err(1, "schedule itimer %.3lfs", delay);
 }
 
 /* main */
@@ -165,7 +165,7 @@ int main(int argc, char *argv[])
 		fprintf(stderr, "- socket(PF_CAN, SOCK_DGRAM, CAN_J1939);\n");
 	sock = ret = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
 	if (ret < 0)
-		error(1, errno, "socket(j1939)");
+		err(1, "socket(j1939)");
 
 	if (todo_prio >= 0) {
 		if (verbose)
@@ -173,23 +173,22 @@ int main(int argc, char *argv[])
 		ret = setsockopt(sock, SOL_CAN_J1939, SO_J1939_SEND_PRIO,
 				&todo_prio, sizeof(todo_prio));
 		if (ret < 0)
-			error(1, errno, "set priority %i", todo_prio);
+			err(1, "set priority %i", todo_prio);
 	}
 
 	if (verbose)
 		fprintf(stderr, "- bind(, %s, %zi);\n", libj1939_addr2str(&sockname), sizeof(sockname));
 	ret = bind(sock, (void *)&sockname, sizeof(sockname));
 	if (ret < 0)
-		error(1, errno, "bind()");
-
+		err(1, "bind()");
 	if (todo_connect) {
 		if (!valid_peername)
-			error(1, 0, "no peername supplied");
+			err(1, "no peername supplied");
 		if (verbose)
 			fprintf(stderr, "- connect(, %s, %zi);\n", libj1939_addr2str(&peername), sizeof(peername));
 		ret = connect(sock, (void *)&peername, sizeof(peername));
 		if (ret < 0)
-			error(1, errno, "connect()");
+			err(1, "connect()");
 	}
 
 	if (todo_send) {
@@ -218,7 +217,7 @@ int main(int argc, char *argv[])
 		}
 
 		if (ret < 0)
-			error(1, errno, "sendto");
+			err(1, "sendto");
 	}
 
 	/* main loop */
@@ -240,7 +239,7 @@ int main(int argc, char *argv[])
 					fprintf(stderr, "-\t<interrupted>\n");
 				continue;
 			}
-			error(1, errno, "recvfrom()");
+			err(1, "recvfrom()");
 		}
 
 		if (todo_echo) {
@@ -249,7 +248,7 @@ int main(int argc, char *argv[])
 			ret = sendto(sock, dat, ret, 0,
 					(void *)&peername, peernamelen);
 			if (ret < 0)
-				error(1, errno, "sendto");
+				err(1, "sendto");
 		}
 		if (todo_recv) {
 			if (todo_names && peername.can_addr.j1939.name)

