shithub: util

Download patch

ref: 3716520dbca0010ad5456de51365b79261054968
parent: c77b44ee5997cb85d2eeee3faef6fd3eabc4df0a
author: glenda <glenda@kingship>
date: Sun Oct 19 23:16:37 EDT 2025

cleanup

--- a/mcp7940x.c
+++ b/mcp7940x.c
@@ -179,6 +179,23 @@
 	return (n & 0xF) + (10 * (n >> 4));
 }
 
+int
+datafd(void)
+{
+	char *data;
+	int fd;
+
+	data = smprint(DATA, i2cdir);
+	if (data == nil)
+		sysfatal("smprint: %r");
+	fd = open(data, ORDWR);
+	if (fd < 0)
+		sysfatal("open: %r");
+	free(data);
+
+	return fd;
+}
+
 void
 fsread(Req *r)
 {
@@ -187,7 +204,6 @@
 	Rtc rtc;
 	char buf[256];
 	ulong t;
-	char *data;
 	int sign;
 	int trim;
 	int offs;
@@ -197,13 +213,8 @@
 	clk[0] = 0;
 
 	if ((intptr)(r->fid->file->aux) == 1) {
-		data = smprint(DATA, i2cdir);
-		if (data == nil)
-			sysfatal("smprint: %r");
-		fd = open(data, ORDWR);
-		if (fd < 0)
-			sysfatal("open: %r");
-		free(data);
+		fd = datafd();
+
 		write(fd, clk, 1);
 		read(fd, clk, Nbcd);
 		close(fd);
@@ -230,13 +241,7 @@
 		readstr(r, buf);
 		respond(r, nil);
 	} else if ((intptr)(r->fid->file->aux) == 2) {
-		data = smprint(DATA, i2cdir);
-		if (data == nil)
-			sysfatal("smprint: %r");
-		fd = open(data, ORDWR);
-		if (fd < 0)
-			sysfatal("open: %r");
-		free(data);
+		fd = datafd();
 
 		clk[0] = OscTrim;
 		write(fd, clk, 1);
@@ -270,13 +275,7 @@
 
 		len -= offs;
 
-		data = smprint(DATA, i2cdir);
-		if (data == nil)
-			sysfatal("smprint: %r");
-		fd = open(data, ORDWR);
-		if (fd < 0)
-			sysfatal("open: %r");
-		free(data);
+		fd = datafd();
 
 		for (i = 0; i < len; i++) {
 			clk[0] = i + (uchar)offs + 0x20;
@@ -305,7 +304,6 @@
 	uchar bcdclock[1+Nbcd];
 	char *p, *ep;
 	int fd;
-	char *data;
 	long trim;
 	uchar reg[2];
 	int offs;
@@ -337,13 +335,8 @@
 		bcdclock[1+Seconds] |= Start;
 		bcdclock[1+Weekday] |= Vbaten;
 
-		data = smprint(DATA, i2cdir);
-		if (data == nil)
-			sysfatal("smprint: %r");
-		fd = open(data, OWRITE);
-		if (fd < 0)
-			sysfatal("open: %r");
-		free(data);
+		fd = datafd();
+
 		write(fd, bcdclock, 1+Nbcd);
 		close(fd);
 
@@ -375,13 +368,7 @@
 			reg[1] = 0x80;
 		reg[1] |= (uchar)trim;
 
-		data = smprint(DATA, i2cdir);
-		if (data == nil)
-			sysfatal("smprint: %r");
-		fd = open(data, ORDWR);
-		if (fd < 0)
-			sysfatal("open: %r");
-		free(data);
+		fd = datafd();
 
 		if (write(fd, reg, 2) != 2)
 			sysfatal("write: %r");
@@ -401,13 +388,7 @@
 			offs = 64;
 		len -= offs;
 
-		data = smprint(DATA, i2cdir);
-		if (data == nil)
-			sysfatal("smprint: %r");
-		fd = open(data, ORDWR);
-		if (fd < 0)
-			sysfatal("open: %r");
-		free(data);
+		fd = datafd();
 
 		for (i = 0; i < len; i++) {
 			reg[0] = i + offs + 0x20;
@@ -443,7 +424,6 @@
 void
 main()
 {
-	char *data;
 	int fd;
 	uchar reg;
 	uchar buf[2];
@@ -451,13 +431,8 @@
 	ctl("size 256");
 	ctl("subaddress 0");
 
-	data = smprint(DATA, i2cdir);
-	if (data == nil)
-		sysfatal("smprint: %r");
-	fd = open(data, ORDWR);
-	if (fd < 0)
-		sysfatal("open: %r");
-	free(data);
+	fd = datafd();
+
 	reg = Control;
 	if (write(fd, &reg, 1) != 1)
 		sysfatal("write: %r");
@@ -468,6 +443,7 @@
 	buf[1] = reg;
 	if (write(fd, buf, 2) != 2)
 		sysfatal("write: %r");
+
 	close(fd);
 
 	fs.tree = alloctree("mcp7940x", "mcp7940x", DMDIR|0555, nil);
--