/* 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 != ' ') {
}
}
// 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?) */
/***** 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;
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? ");
} 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
utmp_t *u;
struct winsize win;
int header=1, longform=1, from=1, args, maxcmd=80, ch;
+ int userlen = 8;
+ int fromlen = 16;
+ char *env_var;
#ifndef W_SHOWFROM
from = 0;
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"))
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);
if (header) { /* print uptime and headers */
print_uptime();
- printf("USER TTY ");
+ printf("%-*s TTY ",userlen,"USER");
if (from)
printf("FROM ");
if (longform)
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();