Module Name:    src
Committed By:   ryo
Date:           Mon Sep 19 18:46:39 UTC 2016

Modified Files:
        src/usr.sbin/intrctl: intrctl.c

Log Message:
"intrctl list" auto fit to minimum columns

Reviewed by nonaka@ and knakahara@


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/usr.sbin/intrctl/intrctl.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/intrctl/intrctl.c
diff -u src/usr.sbin/intrctl/intrctl.c:1.2 src/usr.sbin/intrctl/intrctl.c:1.3
--- src/usr.sbin/intrctl/intrctl.c:1.2	Wed Aug  3 08:34:21 2016
+++ src/usr.sbin/intrctl/intrctl.c	Mon Sep 19 18:46:39 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: intrctl.c,v 1.2 2016/08/03 08:34:21 knakahara Exp $	*/
+/*	$NetBSD: intrctl.c,v 1.3 2016/09/19 18:46:39 ryo Exp $	*/
 
 /*
  * Copyright (c) 2015 Internet Initiative Japan Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: intrctl.c,v 1.2 2016/08/03 08:34:21 knakahara Exp $");
+__RCSID("$NetBSD: intrctl.c,v 1.3 2016/09/19 18:46:39 ryo Exp $");
 
 #include <sys/param.h>
 #include <sys/sysctl.h>
@@ -112,8 +112,9 @@ static int intrctl_io_alloc_retry_count 
 static void
 intrctl_list(int argc, char **argv)
 {
+	char buf[64];
 	struct intrio_list_line *illine;
-	int i, ncpus;
+	int i, ncpus, *cpucol;
 	void *handle;
 	size_t intridlen;
 
@@ -124,34 +125,53 @@ intrctl_list(int argc, char **argv)
 	/* calc columns */
 	ncpus = intrctl_io_ncpus(handle);
 	intridlen = strlen("interrupt id");
-	illine = intrctl_io_firstline(handle);
-	for (; illine != NULL; illine = intrctl_io_nextline(handle, illine)) {
+	for (illine = intrctl_io_firstline(handle); illine != NULL;
+	    illine = intrctl_io_nextline(handle, illine)) {
 		size_t len = strlen(illine->ill_intrid);
 		if (intridlen < len)
 			intridlen = len;
 	}
 
+	cpucol = malloc(sizeof(*cpucol) * (size_t)ncpus);
+	if (cpucol == NULL)
+		err(EXIT_FAILURE, "malloc");
+	for (i = 0; i < ncpus; i++) {
+		snprintf(buf, sizeof(buf), "CPU%u", i);
+		cpucol[i] = strlen(buf);
+	}
+	for (illine = intrctl_io_firstline(handle); illine != NULL;
+	    illine = intrctl_io_nextline(handle, illine)) {
+		for (i = 0; i < ncpus; i++) {
+			int len;
+			snprintf(buf, sizeof(buf), "%" PRIu64,
+			    illine->ill_cpu[i].illc_count);
+			len = (int)strlen(buf);
+			if (cpucol[i] < len)
+				cpucol[i] = len;
+		}
+	}
+
 	/* header */
-	printf("%-*s", (int)intridlen, "interrupt id");
+	printf("%-*s  ", (int)intridlen, "interrupt id");
 	for (i = 0; i < ncpus; i++) {
-		char buf[64];
 		snprintf(buf, sizeof(buf), "CPU%u", i);
-		printf(" %20s ", buf);
+		printf("%*s  ", cpucol[i], buf);
 	}
-	printf(" device name(s)\n");
+	printf("device name(s)\n");
 
 	/* body */
-	illine = intrctl_io_firstline(handle);
-	for (; illine != NULL; illine = intrctl_io_nextline(handle, illine)) {
-		printf("%-*s ", (int)intridlen, illine->ill_intrid);
+	for (illine = intrctl_io_firstline(handle); illine != NULL;
+	    illine = intrctl_io_nextline(handle, illine)) {
+		printf("%-*s  ", (int)intridlen, illine->ill_intrid);
 		for (i = 0; i < ncpus; i++) {
 			struct intrio_list_line_cpu *illc = &illine->ill_cpu[i];
-			printf("%20" PRIu64 "%c ", illc->illc_count,
+			printf("%*" PRIu64 "%c ", cpucol[i], illc->illc_count,
 			    illc->illc_assigned ? '*' : ' ');
 		}
 		printf("%s\n", illine->ill_xname);
 	}
 
+	free(cpucol);
 	intrctl_io_free(handle);
 }
 

Reply via email to