summaryrefslogtreecommitdiffstats
path: root/patches/procps-3.2.8/0038-w_envlength.patch
blob: 501e86a69d2890cf759028c4e915d5311eca6e33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
From fcc63581dff12b8d5603c0787091af1a1f1d7c89 Mon Sep 17 00:00:00 2001
From: http://ftp.de.debian.org/debian/pool/main/p/procps/procps_3.2.8-8.debian.tar.gz <info@debian.org>
Date: Fri, 19 Mar 2010 21:58:46 +0100
Subject: [PATCH 38/70] w_envlength

===================================================================
---
 w.1 |   10 +++++++++-
 w.c |   53 ++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/w.1 b/w.1
index a13cc81..d51e490 100644
--- a/w.1
+++ b/w.1
@@ -1,6 +1,6 @@
 .\"             -*-Nroff-*-
 .\"
-.TH W 1 "8 Dec 1993 " " " "Linux User's Manual"
+.TH W 1 "5 October 2009 " " " "Linux User's Manual"
 .SH NAME
 w \- Show who is logged on and what they are doing.
 .SH SYNOPSIS
@@ -61,6 +61,14 @@ Old style output. Prints blank space for idle times less than one minute.
 .B "user "
 Show information about the specified user only.
 
+.SH ENVIRONMENT
+.TP
+PROCPS_USERLEN
+Override the default width of the username column. Defaults to 8.
+.TP
+PROCPS_FROMLEN
+Override the default width of the from column. Defaults to 16.
+
 .SH FILES
 .TP
 .I /var/run/utmp
diff --git a/w.c b/w.c
index 2077338..48a02be 100644
--- a/w.c
+++ b/w.c
@@ -44,20 +44,19 @@ typedef struct utmp utmp_t;
 /* Uh... same thing as UT_NAMESIZE */
 #define USERSZ (sizeof u->ut_user)
 
+/* Arbitary setting, not too big for the screen, max host size */
+#define HOSTSZ 40
+
 
 /* This routine is careful since some programs leave utmp strings
- * unprintable.  Always outputs at least 16 chars padded with spaces
+ * unprintable.  Always outputs at least fromlen chars padded with spaces
  * on the right if necessary.
  */
-static void print_host(const char *restrict host, int len) {
+static void print_host(const char *restrict host, int len, const int fromlen) {
     const char *last;
     int width = 0;
 
-    /* FIXME: there should really be a way to configure this... */
-    /* for now, we'll just limit it to the 16 that the libc5 version
-     * of utmp uses.
-     */
-    if (len > 16) len = 16;
+    if (len > fromlen) len = fromlen;
     last = host + len;
     for ( ; host < last ; host++){
         if (isprint(*host) && *host != ' ') {
@@ -68,7 +67,8 @@ static void print_host(const char *restrict host, int len) {
 	}
     }
     // space-fill, and a '-' too if needed to ensure the column exists
-    if(width < 16) fputs("-               "+width, stdout);
+	while(width++ < fromlen)
+	  fputc(' ',stdout);
 }
 
 /***** compact 7 char format for time intervals (belongs in libproc?) */
@@ -180,7 +180,7 @@ static const proc_t *getproc(const utmp_t *restrict const u, const char *restric
 
 
 /***** showinfo */
-static void showinfo(utmp_t *u, int formtype, int maxcmd, int from) {
+static void showinfo(utmp_t *u, int formtype, int maxcmd, int from, const int userlen, const int fromlen) {
     unsigned long long jcpu;
     int ut_pid_found;
     unsigned i;
@@ -205,9 +205,9 @@ static void showinfo(utmp_t *u, int formtype, int maxcmd, int from) {
 
     strncpy(uname, u->ut_user, USERSZ);		/* force NUL term for printf */
     if (formtype) {
-	printf("%-9.8s%-9.8s", uname, u->ut_line);
+	printf("%-*.*s%-9.8s", userlen+1, userlen, uname, u->ut_line);
 	if (from)
-	    print_host(u->ut_host, sizeof u->ut_host);
+	    print_host(u->ut_host, sizeof u->ut_host, fromlen);
 	print_logintime(u->ut_time, stdout);
 	if (*u->ut_line == ':')			/* idle unknown for xdm logins */
 	    printf(" ?xdm? ");
@@ -220,9 +220,9 @@ static void showinfo(utmp_t *u, int formtype, int maxcmd, int from) {
 	} else
 	    printf("   ?   ");
     } else {
-	printf("%-9.8s%-9.8s", u->ut_user, u->ut_line);
+	printf("%-*.*s%-9.8s", userlen+1, userlen, u->ut_user, u->ut_line);
 	if (from)
-	    print_host(u->ut_host, sizeof u->ut_host);
+	    print_host(u->ut_host, sizeof u->ut_host, fromlen);
 	if (*u->ut_line == ':')			/* idle unknown for xdm logins */
 	    printf(" ?xdm? ");
 	else
@@ -245,6 +245,9 @@ int main(int argc, char **argv) {
     utmp_t *u;
     struct winsize win;
     int header=1, longform=1, from=1, args, maxcmd, ch;
+    int userlen = 8;
+    int fromlen = 16;
+	char *env_var;
 
 #ifndef W_SHOWFROM
     from = 0;
@@ -275,6 +278,22 @@ int main(int argc, char **argv) {
     if ((argv[optind]))
 	user = (argv[optind]);
 
+	/* Get user field length from environment */
+	if ( (env_var = getenv("PROCPS_USERLEN")) != NULL) {
+        userlen = atoi(env_var);
+        if (userlen < 8 || userlen > USERSZ) {
+            fprintf(stderr, "User length environment PROCPS_USERLEN must be between 8 and %d, ignoring.\n", USERSZ);
+			userlen=8;
+        }
+	}
+	/* Get from field length from environment */
+	if ( (env_var = getenv("PROCPS_FROMLEN")) != NULL) {
+        fromlen = atoi(env_var);
+        if (fromlen < 8 || fromlen > HOSTSZ) {
+            fprintf(stderr, "From length environment PROCPS_FROMLEN must be between 8 and %d, ignoring.\n", HOSTSZ);
+			fromlen=16;
+        }
+	}
     if (ioctl(1, TIOCGWINSZ, &win) != -1 && win.ws_col > 0)
 	maxcmd = win.ws_col;
     else if (p = getenv("COLUMNS"))
@@ -285,7 +304,7 @@ int main(int argc, char **argv) {
 	fprintf(stderr, "%d column window is too narrow\n", maxcmd);
 	exit(1);
     }
-    maxcmd -= 29 + (from ? 16 : 0) + (longform ? 20 : 0);
+    maxcmd -= 21 + userlen + (from ? fromlen : 0) + (longform ? 20 : 0);
     if (maxcmd < 3)
 	fprintf(stderr, "warning: screen width %d suboptimal.\n", win.ws_col);
 
@@ -293,7 +312,7 @@ int main(int argc, char **argv) {
 
     if (header) {				/* print uptime and headers */
 	print_uptime();
-	printf("USER     TTY      ");
+	printf("%-*s TTY      ",userlen,"USER");
 	if (from)
 	    printf("FROM            ");
 	if (longform)
@@ -309,14 +328,14 @@ int main(int argc, char **argv) {
 	    u = getutent();
 	    if (unlikely(!u)) break;
 	    if (u->ut_type != USER_PROCESS) continue;
- 	    if (!strncmp(u->ut_user, user, USERSZ)) showinfo(u, longform, maxcmd, from);
+ 	    if (!strncmp(u->ut_user, user, USERSZ)) showinfo(u, longform, maxcmd, from, userlen, fromlen);
 	}
     } else {
 	for (;;) {
 	    u = getutent();
 	    if (unlikely(!u)) break;
 	    if (u->ut_type != USER_PROCESS) continue;
- 	    if (*u->ut_user) showinfo(u, longform, maxcmd, from);
+ 	    if (*u->ut_user) showinfo(u, longform, maxcmd, from, userlen, fromlen);
 	}
     }
     endutent();
-- 
1.7.0