Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 13 Feb 1999 20:31:20 +0900
From:      Jun Kuriyama <kuriyama@sky.rim.or.jp>
To:        FreeBSD-current <FreeBSD-current@FreeBSD.ORG>
Subject:   Error handling for src/usr.sbin/pccard/pccardc/*
Message-ID:  <36C56288.D80AAC1E@sky.rim.or.jp>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------842F4005FDBECD640ACC0BB9
Content-Type: text/plain; charset=iso-2022-jp
Content-Transfer-Encoding: 7bit

This is a patch for better (I think) error handling for pccardc.
(obtained from PAO3)

Please review.


-- 
Jun Kuriyama // kuriyama@sky.rim.or.jp
            // kuriyama@FreeBSD.ORG
--------------842F4005FDBECD640ACC0BB9
Content-Type: text/plain; charset=iso-2022-jp; name="err.diff.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="err.diff.txt"

Index: pccardc/dumpcis.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/dumpcis.c,v
retrieving revision 1.10
diff -u -r1.10 dumpcis.c
--- dumpcis.c	1999/02/05 16:00:15	1.10
+++ dumpcis.c	1999/02/13 04:33:28
@@ -43,7 +43,7 @@
 
 int     nocards;
 
-void
+static void
 scan(slot)
 	int     slot;
 {
@@ -57,7 +57,8 @@
 	if (fd < 0)
 		return;
 	nocards++;
-	ioctl(fd, PIOCGSTATE, &st);
+	if (ioctl(fd, PIOCGSTATE, &st))
+		err(1, "ioctl (PIOCGSTATE)");
 	if (st.state == filled) {
 		cp = readcis(fd);
 		if (cp) {
Index: pccardc/enabler.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/enabler.c,v
retrieving revision 1.11
diff -u -r1.11 enabler.c
--- enabler.c	1999/02/05 16:00:15	1.11
+++ enabler.c	1999/02/13 03:50:51
@@ -126,7 +126,7 @@
 			err(1, "set I/O context");
 	}
 	if (ioctl(fd, PIOCSDRV, &drv))
-		warn("set driver");
+		err(1, "set driver");
 	close(fd);
 	return 0;
 }
@@ -138,9 +138,9 @@
 usage(msg)
 	char   *msg;
 {
-	warnx("enabler: %s", msg);
+	fprintf(stderr, "enabler: %s\n", msg);
 	fprintf(stderr,
-"usage: pccardc enabler slot driver [-m addr size] [-a iobase] [-i irq]\n");
+"Usage: enabler slot driver [-m addr size] [-a iobase] [-i irq]\n");
 	fprintf(stderr,
 "    -m card addr size : card address (hex), host address (hex) & size (Kb)\n");
 	fprintf(stderr,
Index: pccardc/pccardc.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/pccardc.c,v
retrieving revision 1.7
diff -u -r1.7 pccardc.c
--- pccardc.c	1998/02/26 14:36:01	1.7
+++ pccardc.c	1999/02/13 03:51:35
@@ -84,10 +84,11 @@
 {
 	int     i;
 
-	fprintf(stderr, "usage: pccardc <subcommand> <arg> ...\n");
-	fprintf(stderr, "subcommands:\n");
+	fprintf(stderr, "Usage:\n");
+	fprintf(stderr, "\t%s <subcommand> <arg> ...\n", argv[0]);
+	fprintf(stderr, "Subcommands:\n");
 	for (i = 0; subcommands[i].name; i++)
-		fprintf(stderr, "\t%s\n\t\t%s\n",
+		fprintf(stderr, "\t%s\t: %s\n",
 		    subcommands[i].name, subcommands[i].help);
 	return 1;
 }
Index: pccardc/pccardmem.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/pccardmem.c,v
retrieving revision 1.10
diff -u -r1.10 pccardmem.c
--- pccardmem.c	1999/02/05 16:00:15	1.10
+++ pccardmem.c	1999/02/13 04:02:59
@@ -37,13 +37,6 @@
 
 #include <pccard/cardinfo.h>
 
-static void
-usage()
-{
-	fprintf(stderr, "usage: pccardc pccardmem [memory-address]\n");
-	exit(1);
-}
-
 int
 pccardmem_main(argc, argv)
 	int     argc;
@@ -54,7 +47,8 @@
 	int     fd;
 
 	if (argc > 2)
-		usage();
+		errx(1, "Usage: %s pccardmem [ memory-address ]", argv[0]);
+
 	sprintf(name, CARD_DEVICE, 0);
 	fd = open(name, O_RDONLY);
 	if (fd < 0)
@@ -64,8 +58,8 @@
 			errx(1, "arg error");
 	}
 	if (ioctl(fd, PIOCRWMEM, &addr))
-		warn("ioctl");
+		err(1, "ioctl (PIOCRWMEM)");
 	else
 		printf("PCCARD Memory address set to 0x%x\n", addr);
-	exit(0);
+	return 0;
 }
Index: pccardc/rdattr.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/rdattr.c,v
retrieving revision 1.4
diff -u -r1.4 rdattr.c
--- rdattr.c	1998/02/27 08:00:18	1.4
+++ rdattr.c	1999/02/13 04:06:35
@@ -24,11 +24,13 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
+
 #include <pccard/cardinfo.h>
 
 int
@@ -41,37 +43,31 @@
 	u_char *buf;
 	int     fd;
 	off_t   offs;
+
+	if (argc != 4)
+		errx(1, "Usage: %s rdattr slot offs length", argv[0]);
 
-	if (argc != 4) {
-		fprintf(stderr, "usage: %s rdattr slot offs length\n", argv[0]);
-		exit(1);
-	}
 	sprintf(name, CARD_DEVICE, atoi(argv[1]));
 	fd = open(name, O_RDONLY);
-	if (fd < 0) {
-		perror(name);
-		exit(1);
-	}
+	if (fd < 0)
+		err(1, "%s", name);
+
 	reg = MDF_ATTR;
-	if (ioctl(fd, PIOCRWFLAG, &reg)) {
-		perror("ioctl (PIOCRWFLAG)");
-		exit(1);
-	}
+	if (ioctl(fd, PIOCRWFLAG, &reg))
+		err(1, "ioctl (PIOCRWFLAG)");
+
 	if (sscanf(argv[2], "%x", &reg) != 1 ||
-	    sscanf(argv[3], "%x", &length) != 1) {
-		fprintf(stderr, "arg error\n");
-		exit(1);
-	}
+	    sscanf(argv[3], "%x", &length) != 1)
+		errx(1, "arg error");
+
 	offs = reg;
-	if ((buf = malloc(length)) == 0) {
-		perror(name);
-		exit(1);
-	}
+	if ((buf = malloc(length)) == 0)
+		err(1, "%s", name);
+
 	lseek(fd, offs, SEEK_SET);
-	if (read(fd, buf, length) != length) {
-		perror(name);
-		exit(1);
-	}
+	if (read(fd, buf, length) != length)
+		err(1, "%s", name);
+
 	for (i = 0; i < length; i++) {
 		if (i % 16 == 0) {
 			printf("%04x: ", (int) offs + i);
Index: pccardc/rdmap.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/rdmap.c,v
retrieving revision 1.10
diff -u -r1.10 rdmap.c
--- rdmap.c	1999/02/05 16:00:16	1.10
+++ rdmap.c	1999/02/13 04:09:23
@@ -29,6 +29,7 @@
 	"$Id: rdmap.c,v 1.10 1999/02/05 16:00:16 kuriyama Exp $";
 #endif /* not lint */
 
+#include <err.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -39,7 +40,7 @@
 #include <pccard/cardinfo.h>
 #include <pccard/cis.h>
 
-void
+static void
 dump_io(fd, nio)
 	int     fd, nio;
 {
@@ -48,13 +49,14 @@
 
 	for (i = 0; i < nio; i++) {
 		io.window = i;
-		ioctl(fd, PIOCGIO, &io);
+		if (ioctl(fd, PIOCGIO, &io))
+			err(1, "ioctl (PIOCGIO)");
 		printf("I/O %d: flags 0x%03x port 0x%3x size %d bytes\n",
 		    io.window, io.flags, io.start, io.size);
 	}
 }
 
-void
+static void
 dump_mem(fd, nmem)
 	int     fd, nmem;
 {
@@ -63,7 +65,8 @@
 
 	for (i = 0; i < nmem; i++) {
 		mem.window = i;
-		ioctl(fd, PIOCGMEM, &mem);
+		if (ioctl(fd, PIOCGMEM, &mem))
+			err(1, "ioctl (PIOCGMEM)");
 		printf("Mem %d: flags 0x%03x host %p card %04lx size %d bytes\n",
 		    mem.window, mem.flags, mem.start, mem.card, mem.size);
 	}
@@ -81,7 +84,8 @@
 	fd = open(name, O_RDONLY);
 	if (fd < 0)
 		return;
-	ioctl(fd, PIOCGSTATE, &st);
+	if (ioctl(fd, PIOCGSTATE, &st))
+		err(1, "ioctl (PIOCGSTATE)");
 /*
 	if (st.state == filled)
  */
Index: pccardc/rdreg.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/rdreg.c,v
retrieving revision 1.10
diff -u -r1.10 rdreg.c
--- rdreg.c	1999/02/05 16:00:16	1.10
+++ rdreg.c	1999/02/13 04:10:54
@@ -38,6 +38,7 @@
 #include <sys/ioctl.h>
 
 #include <pccard/cardinfo.h>
+
 void
 dumpslot(sl)
 	int     sl;
@@ -47,7 +48,7 @@
 	struct pcic_reg r;
 
 	sprintf(name, CARD_DEVICE, sl);
-	fd = open(name, O_RDWR);
+	fd = open(name, O_RDONLY);
 	if (fd < 0) {
 		warn("%s", name);
 		return;
@@ -55,7 +56,7 @@
 	printf("Registers for slot %d\n", sl);
 	for (r.reg = 0; r.reg < 0x40; r.reg++) {
 		if (ioctl(fd, PIOCGREG, &r)) {
-			warn("ioctl");
+			warn("ioctl (PIOCGREG)");
 			break;
 		}
 		if ((r.reg % 16) == 0)
Index: pccardc/wrattr.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/wrattr.c,v
retrieving revision 1.11
diff -u -r1.11 wrattr.c
--- wrattr.c	1999/02/05 16:00:16	1.11
+++ wrattr.c	1999/02/13 04:12:02
@@ -39,13 +39,6 @@
 
 #include <pccard/cardinfo.h>
 
-static void
-usage()
-{
-	fprintf(stderr, "usage: pccardc wrattr slot offs value\n");
-	exit(1);
-}
-
 int
 wrattr_main(argc, argv)
 	int     argc;
@@ -57,21 +50,25 @@
 	off_t   offs;
 
 	if (argc != 4)
-		usage();
+		errx(1, "Usage: %s wrattr slot offs value", argv[0]);
+
 	sprintf(name, CARD_DEVICE, atoi(argv[1]));
 	fd = open(name, O_RDWR);
 	if (fd < 0)
 		err(1, "%s", name);
+
 	reg = MDF_ATTR;
 	if (ioctl(fd, PIOCRWFLAG, &reg))
 		err(1, "ioctl (PIOCRWFLAG)");
+
 	if (sscanf(argv[2], "%x", &reg) != 1 ||
 	    sscanf(argv[3], "%x", &value) != 1)
 		errx(1, "arg error");
+
 	offs = reg;
 	c = value;
 	lseek(fd, offs, SEEK_SET);
 	if (write(fd, &c, 1) != 1)
-		warn("%s", name);
+		err(1, "%s", name);
 	return 0;
 }
Index: pccardc/wrreg.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pccard/pccardc/wrreg.c,v
retrieving revision 1.10
diff -u -r1.10 wrreg.c
--- wrreg.c	1999/02/05 16:00:16	1.10
+++ wrreg.c	1999/02/13 04:12:35
@@ -39,13 +39,6 @@
 
 #include <pccard/cardinfo.h>
 
-static void
-usage()
-{
-	fprintf(stderr, "usage: pccardc wrreg slot reg value\n");
-	exit(1);
-}
-
 int
 wrreg_main(argc, argv)
 	int     argc;
@@ -57,7 +50,8 @@
 	struct pcic_reg r;
 
 	if (argc != 4)
-		usage();
+		errx(1, "Usage: %s wrreg slot reg value", argv[0]);
+
 	sprintf(name, CARD_DEVICE, atoi(argv[1]));
 	fd = open(name, O_RDWR);
 	if (fd < 0)
@@ -68,6 +62,6 @@
 	r.reg = reg;
 	r.value = value;
 	if (ioctl(fd, PIOCSREG, &r))
-		warn("ioctl");
+		err(1, "ioctl (PIOCSREG)");
 	return 0;
 }


--------------842F4005FDBECD640ACC0BB9--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?36C56288.D80AAC1E>