Subject: bin/26336: Implement -q and -s in who(1)
To: None <gnats-bugs@gnats.NetBSD.org>
From: Peter Postma <peter@pointless.nl>
List: netbsd-bugs
Date: 07/15/2004 15:33:59
>Number:         26336
>Category:       bin
>Synopsis:       Implement -q and -s in who(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Thu Jul 15 13:35:00 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     Peter Postma
>Release:        NetBSD 2.0G
>Organization:
>Environment:
System: NetBSD mercury.pointless.nl 2.0G NetBSD 2.0G (mercury) #11: Fri Jul 2 17:57:06 CEST 2004 peter@mercury.pointless.nl:/usr/obj/sys/arch/sparc64/compile/mercury sparc64
Architecture: sparc64
Machine: sparc64
>Description:
POSIX.2 specifies options -q and -s for who(1) as XSI extentions.
I've implemented them in the diff below.

-q is quick mode, list only the names and the number of users currently
logged on.

-s is default mode, list only the name, line and time fields.
>How-To-Repeat:
>Fix:
Index: who.c
===================================================================
RCS file: /cvsroot/src/usr.bin/who/who.c,v
retrieving revision 1.13
diff -u -r1.13 who.c
--- who.c	5 Jan 2004 23:23:37 -0000	1.13
+++ who.c	15 Jul 2004 12:58:20 -0000
@@ -56,6 +56,7 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <utmp.h>
 #include "utmpentry.h"
 
 static void output_labels(void);
@@ -63,6 +64,7 @@
 static void usage(void);
 static void process(const char *, int);
 static void print(const char *, const char *, time_t, const char *);
+static void quick(const char *);
 
 int main(int, char **);
 
@@ -74,25 +76,33 @@
 int
 main(int argc, char **argv)
 {
-	int c, only_current_term, show_labels;
+	int c, only_current_term, show_labels, quick_mode, default_mode;
 
 	setlocale(LC_ALL, "");
 
 	only_current_term = show_term = show_idle = show_labels = 0;
-	while ((c = getopt(argc, argv, "mTuH")) != -1) {
+	quick_mode = default_mode = 0;
+
+	while ((c = getopt(argc, argv, "HmqsTu")) != -1) {
 		switch (c) {
+		case 'H':
+			show_labels = 1;
+			break;
 		case 'm':
 			only_current_term = 1;
 			break;
+		case 'q':
+			quick_mode = 1;
+			break;
+		case 's':
+			default_mode = 1;
+			break;
 		case 'T':
 			show_term = 1;
 			break;
 		case 'u':
 			show_idle = 1;
 			break;
-		case 'H':
-			show_labels = 1;
-			break;
 		default:
 			usage();
 			/* NOTREACHED */
@@ -106,10 +116,15 @@
 		/* NOTREACHED */
 	}
 
+	if (default_mode)
+		only_current_term = show_term = show_idle = show_labels = 0;
+
 	switch (argc) {
 	case 0:					/* who */
 		if (only_current_term) {
 			who_am_i(NULL, show_labels);
+		} else if (quick_mode) {
+			quick(NULL);
 		} else {
 			process(NULL, show_labels);
 		}
@@ -117,6 +132,8 @@
 	case 1:					/* who utmp_file */
 		if (only_current_term) {
 			who_am_i(*argv, show_labels);
+		} else if (quick_mode) {
+			quick(*argv);
 		} else {
 			process(*argv, show_labels);
 		}
@@ -244,9 +261,27 @@
 }
 
 static void
+quick(const char *fname)
+{
+	struct utmpentry *ehead, *ep;
+	int num = 0;
+
+	(void)getutentries(fname, &ehead);
+	for (ep = ehead; ep != NULL; ep = ep->next) {
+		printf("%-*.*s ", UT_NAMESIZE, UT_NAMESIZE, ep->name);
+		if ((++num % 8) == 0)
+			(void)putchar('\n');
+	}
+	if (num % 8)
+		(void)putchar('\n');
+
+	printf("# users = %d\n", num);
+}
+
+static void
 usage()
 {
-	(void)fprintf(stderr, "usage: %s [-mTuH] [ file ]\n       %s am i\n",
+	(void)fprintf(stderr, "usage: %s [-HmqsTu] [file]\n       %s am i\n",
 	    getprogname(), getprogname());
 	exit(1);
 }
Index: who.1
===================================================================
RCS file: /cvsroot/src/usr.bin/who/who.1,v
retrieving revision 1.14
diff -u -r1.14 who.1
--- who.1	7 Aug 2003 11:17:18 -0000	1.14
+++ who.1	15 Jul 2004 12:58:20 -0000
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)who.1	8.2 (Berkeley) 12/30/93
 .\"
-.Dd December 6, 1994
+.Dd July 15, 2004
 .Dt WHO 1
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd display who is logged in
 .Sh SYNOPSIS
 .Nm
-.Op Fl mTuH
+.Op Fl HmqsTu
 .Op Ar file
 .Nm
 .Ar am i
@@ -51,6 +51,8 @@
 Available options:
 .Pp
 .Bl -tag -width file
+.It Fl H
+Write column headings above the regular output.
 .It Fl m
 Only print information about the current terminal.
 This is the
@@ -58,6 +60,13 @@
 way of saying
 .Nm
 .Ar am i .
+.It Fl q
+.Dq Quick mode :
+List only the names and the number of users currently logged on.
+When this option is used, all other options are ignored.
+.It Fl s
+List only the name, line and time fields.
+This is the default.
 .It Fl T
 Print a character after the user name indicating the state of the
 terminal line:
@@ -70,8 +79,6 @@
 if a bad line is encountered.
 .It Fl u
 Print the idle time for each user.
-.It Fl H
-Write column headings above the regular output.
 .It Ar \&am I
 Returns the invoker's real user name.
 .It Ar file
@@ -119,7 +126,8 @@
 .Xr mesg 1 ,
 .Xr users 1 ,
 .Xr getuid 2 ,
-.Xr utmp 5
+.Xr utmp 5 ,
+.Xr utmpx 5
 .Sh STANDARDS
 The
 .Nm
>Release-Note:
>Audit-Trail:
>Unformatted: