1
Description: Use environment to set user and from/host column widths
2
Author: Craig Small <csmall@debian.org>
3
Bug-Debian: http://bugs.debian.org/396423
4
Bug-Debian: http://bugs.debian.org/341439
6
===================================================================
7
--- a/w.1 2009-11-24 21:00:42.000000000 +1100
8
+++ b/w.1 2009-11-24 21:00:44.000000000 +1100
12
-.TH W 1 "8 Dec 1993 " " " "Linux User's Manual"
13
+.TH W 1 "5 October 2009 " " " "Linux User's Manual"
15
w \- Show who is logged on and what they are doing.
19
Show information about the specified user only.
24
+Override the default width of the username column. Defaults to 8.
27
+Override the default width of the from column. Defaults to 16.
33
===================================================================
34
--- a/w.c 2009-11-24 21:00:43.000000000 +1100
35
+++ b/w.c 2009-11-24 21:00:44.000000000 +1100
37
/* Uh... same thing as UT_NAMESIZE */
38
#define USERSZ (sizeof u->ut_user)
40
+/* Arbitary setting, not too big for the screen, max host size */
44
/* This routine is careful since some programs leave utmp strings
45
- * unprintable. Always outputs at least 16 chars padded with spaces
46
+ * unprintable. Always outputs at least fromlen chars padded with spaces
47
* on the right if necessary.
49
-static void print_host(const char *restrict host, int len) {
50
+static void print_host(const char *restrict host, int len, const int fromlen) {
54
- /* FIXME: there should really be a way to configure this... */
55
- /* for now, we'll just limit it to the 16 that the libc5 version
58
- if (len > 16) len = 16;
59
+ if (len > fromlen) len = fromlen;
61
for ( ; host < last ; host++){
62
if (isprint(*host) && *host != ' ') {
66
// space-fill, and a '-' too if needed to ensure the column exists
67
- if(width < 16) fputs("- "+width, stdout);
68
+ while(width++ < fromlen)
72
/***** compact 7 char format for time intervals (belongs in libproc?) */
77
-static void showinfo(utmp_t *u, int formtype, int maxcmd, int from) {
78
+static void showinfo(utmp_t *u, int formtype, int maxcmd, int from, const int userlen, const int fromlen) {
79
unsigned long long jcpu;
84
strncpy(uname, u->ut_user, USERSZ); /* force NUL term for printf */
86
- printf("%-9.8s%-9.8s", uname, u->ut_line);
87
+ printf("%-*.*s%-9.8s", userlen+1, userlen, uname, u->ut_line);
89
- print_host(u->ut_host, sizeof u->ut_host);
90
+ print_host(u->ut_host, sizeof u->ut_host, fromlen);
91
print_logintime(u->ut_time, stdout);
92
if (*u->ut_line == ':') /* idle unknown for xdm logins */
98
- printf("%-9.8s%-9.8s", u->ut_user, u->ut_line);
99
+ printf("%-*.*s%-9.8s", userlen+1, userlen, u->ut_user, u->ut_line);
101
- print_host(u->ut_host, sizeof u->ut_host);
102
+ print_host(u->ut_host, sizeof u->ut_host, fromlen);
103
if (*u->ut_line == ':') /* idle unknown for xdm logins */
109
int header=1, longform=1, from=1, args, maxcmd, ch;
118
user = (argv[optind]);
120
+ /* Get user field length from environment */
121
+ if ( (env_var = getenv("PROCPS_USERLEN")) != NULL) {
122
+ userlen = atoi(env_var);
123
+ if (userlen < 8 || userlen > USERSZ) {
124
+ fprintf(stderr, "User length environment PROCPS_USERLEN must be between 8 and %d, ignoring.\n", USERSZ);
128
+ /* Get from field length from environment */
129
+ if ( (env_var = getenv("PROCPS_FROMLEN")) != NULL) {
130
+ fromlen = atoi(env_var);
131
+ if (fromlen < 8 || fromlen > HOSTSZ) {
132
+ fprintf(stderr, "From length environment PROCPS_FROMLEN must be between 8 and %d, ignoring.\n", HOSTSZ);
136
if (ioctl(1, TIOCGWINSZ, &win) != -1 && win.ws_col > 0)
138
else if (p = getenv("COLUMNS"))
140
fprintf(stderr, "%d column window is too narrow\n", maxcmd);
143
- maxcmd -= 29 + (from ? 16 : 0) + (longform ? 20 : 0);
144
+ maxcmd -= 21 + userlen + (from ? fromlen : 0) + (longform ? 20 : 0);
146
fprintf(stderr, "warning: screen width %d suboptimal.\n", win.ws_col);
150
if (header) { /* print uptime and headers */
152
- printf("USER TTY ");
153
+ printf("%-*s TTY ",userlen,"USER");
157
@@ -309,14 +328,14 @@
159
if (unlikely(!u)) break;
160
if (u->ut_type != USER_PROCESS) continue;
161
- if (!strncmp(u->ut_user, user, USERSZ)) showinfo(u, longform, maxcmd, from);
162
+ if (!strncmp(u->ut_user, user, USERSZ)) showinfo(u, longform, maxcmd, from, userlen, fromlen);
167
if (unlikely(!u)) break;
168
if (u->ut_type != USER_PROCESS) continue;
169
- if (*u->ut_user) showinfo(u, longform, maxcmd, from);
170
+ if (*u->ut_user) showinfo(u, longform, maxcmd, from, userlen, fromlen);