Module Name:    src
Committed By:   christos
Date:           Tue Aug 18 19:26:29 UTC 2020

Modified Files:
        src/usr.sbin/mdconfig: Makefile mdconfig.c

Log Message:
use opendisk()/dehumanize_number


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/usr.sbin/mdconfig/Makefile
cvs rdiff -u -r1.6 -r1.7 src/usr.sbin/mdconfig/mdconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/mdconfig/Makefile
diff -u src/usr.sbin/mdconfig/Makefile:1.2 src/usr.sbin/mdconfig/Makefile:1.3
--- src/usr.sbin/mdconfig/Makefile:1.2	Wed Jan  1 19:22:43 1997
+++ src/usr.sbin/mdconfig/Makefile	Tue Aug 18 15:26:29 2020
@@ -1,6 +1,10 @@
-#	$NetBSD: Makefile,v 1.2 1997/01/02 00:22:43 pk Exp $
+#	$NetBSD: Makefile,v 1.3 2020/08/18 19:26:29 christos Exp $
 
+WARNS?=6
 PROG=		mdconfig
 MAN=		mdconfig.8
 
+LDADD+=	-lutil
+DPADD+= ${LIBUTIL}
+
 .include <bsd.prog.mk>

Index: src/usr.sbin/mdconfig/mdconfig.c
diff -u src/usr.sbin/mdconfig/mdconfig.c:1.6 src/usr.sbin/mdconfig/mdconfig.c:1.7
--- src/usr.sbin/mdconfig/mdconfig.c:1.6	Tue Jan 23 16:06:25 2018
+++ src/usr.sbin/mdconfig/mdconfig.c	Tue Aug 18 15:26:29 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: mdconfig.c,v 1.6 2018/01/23 21:06:25 sevan Exp $	*/
+/*	$NetBSD: mdconfig.c,v 1.7 2020/08/18 19:26:29 christos Exp $	*/
 
 /*
  * Copyright (c) 1995 Gordon W. Ross
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mdconfig.c,v 1.6 2018/01/23 21:06:25 sevan Exp $");
+__RCSID("$NetBSD: mdconfig.c,v 1.7 2020/08/18 19:26:29 christos Exp $");
 #endif
 
 /*
@@ -37,58 +37,62 @@ __RCSID("$NetBSD: mdconfig.c,v 1.6 2018/
  * (But this design allows any filesystem format!)
  */
 
-#include <sys/types.h>
+#include <sys/errno.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/param.h>
+#include <sys/types.h>
 
 #include <dev/md.h>
 
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <util.h>
 
 int
 main(int argc, char *argv[])
 {
 	struct md_conf md;
-	size_t nblks;
+	char fname[MAXPATHLEN];
+	int64_t num;
+	size_t blks, bytes;
 	int fd;
 
-	if (argc <= 2) {
-		fprintf(stderr, "usage: mdconfig <device> <%d-byte-blocks>\n",
-				DEV_BSIZE);
-		exit(1);
-	}
+	setprogname(argv[0]);
 
-	nblks = (size_t)strtoul(argv[2], NULL, 0);
-	if (nblks == 0) {
-		fprintf(stderr, "invalid number of blocks\n");
-		exit(1);
+	if (argc != 3) {
+		(void)fprintf(stderr, "Usage: %s <device> <%d-byte-blocks>\n",
+		    getprogname(), DEV_BSIZE);
+		return EXIT_FAILURE;
 	}
-	md.md_size = nblks << DEV_BSHIFT;
 
-	fd = open(argv[1], O_RDWR, 0);
-	if (fd < 0) {
-		perror(argv[1]);
-		exit(1);
-	}
+	if (dehumanize_number(argv[2], &num) == -1)
+		goto bad_num;
 
-	md.md_addr = mmap(NULL, md.md_size,
-				PROT_READ | PROT_WRITE,
-				MAP_ANON | MAP_PRIVATE,
-				-1, 0);
-	if (md.md_addr == MAP_FAILED) {
-		perror("mmap");
-		exit(1);
+	blks = (size_t)num;
+	bytes = blks << DEV_BSHIFT;
+	if (num <= 0 || bytes >> DEV_BSHIFT != blks) {
+bad_num:	err(EXIT_FAILURE, "blocks: `%s'", argv[2]);
 	}
+	md.md_size = bytes;
+
+	fd = opendisk(argv[1], O_RDWR, fname, sizeof(fname), 0);
+	if (fd == -1)
+		err(EXIT_FAILURE, "Can't open `%s'", argv[1]);
+
+	md.md_addr = mmap(NULL, md.md_size, PROT_READ | PROT_WRITE,
+	    MAP_ANON | MAP_PRIVATE, -1, 0);
+	if (md.md_addr == MAP_FAILED)
+		err(EXIT_FAILURE, "mmap");
 
 	/* Become server! */
 	md.md_type = MD_UMEM_SERVER;
-	if (ioctl(fd, MD_SETCONF, &md)) {
-		perror("ioctl");
-		exit(1);
-	}
+	if (ioctl(fd, MD_SETCONF, &md) == -1)
+		err(EXIT_FAILURE, "ioctl");
 
-	exit(0);
+	return EXIT_SUCCESS;
 }

Reply via email to