Merge remote-tracking branch 'sami/2012wk9'

This commit is contained in:
Craig Small 2012-03-16 07:21:37 +11:00
commit 98ebb386c4
2 changed files with 45 additions and 52 deletions

55
pmap.c
View File

@ -272,10 +272,10 @@ static int one_proc(proc_t * p)
&end, flags, &file_offset, &dev_major, &dev_minor,
&inode);
if (start > range_high)
break;
if (end < range_low)
if (end - 1 < range_low)
continue;
if (range_high < start)
break;
tmp = strchr(mapbuf, '\n');
if (tmp)
@ -382,6 +382,28 @@ static int one_proc(proc_t * p)
return 0;
}
static void range_arguments(char *optarg)
{
char *arg1;
char *arg2;
arg1 = xstrdup(optarg);
arg2 = strchr(arg1, ',');
if (arg2)
*arg2 = '\0';
if (arg2)
++arg2;
else
arg2 = arg1;
if (arg1 && *arg1)
range_low = STRTOUKL(arg1, &arg1, 16);
if (*arg2)
range_high = STRTOUKL(arg2, &arg2, 16);
if (arg1 && (*arg1 || *arg2))
xerrx(EXIT_FAILURE, "%s: '%s'", _("failed to parse argument"),
optarg);
}
int main(int argc, char **argv)
{
unsigned *pidlist;
@ -422,32 +444,7 @@ int main(int argc, char **argv)
q_option = 1;
break;
case 'A':
{
/* FIXME: this should be a function. */
char *walk = optarg;
char *arg1;
char *arg2;
if (walk[1]) {
arg1 = walk + 1;
walk += strlen(walk) - 1;
} else {
arg1 = *++argv;
if (!arg1)
usage(stderr);
}
arg2 = strchr(arg1, ',');
if (arg2)
*arg2 = '\0';
if(arg2) ++arg2;
else arg2 = arg1;
if (arg1 && *arg1)
range_low = STRTOUKL(arg1, &arg1, 16);
if (*arg2)
range_high = STRTOUKL(arg2, &arg2, 16);
if (arg1 && (*arg1 || *arg2))
usage(stderr);
}
range_arguments(optarg);
break;
case 'h':
usage(stdout);

42
w.c
View File

@ -67,10 +67,6 @@ typedef struct utmp utmp_t;
# define FROM_STRING "off"
#endif
/* 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
@ -153,14 +149,11 @@ static time_t idletime(const char *restrict const tty)
static void print_logintime(time_t logt, FILE * fout)
{
/* FIXME: make use of locale, remember strftime() */
char weekday[][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
char month[][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
};
/* Abbreviated of weekday can be longer than 3 characters,
* see for instance hu_HU. Using 16 is few bytes more than
* enough. */
char time_str[16];
time_t curt;
struct tm *logtm, *curtm;
int today;
@ -171,12 +164,15 @@ static void print_logintime(time_t logt, FILE * fout)
today = curtm->tm_yday;
logtm = localtime(&logt);
if (curt - logt > 12 * 60 * 60 && logtm->tm_yday != today) {
if (curt - logt > 6 * 24 * 60 * 60)
if (curt - logt > 6 * 24 * 60 * 60) {
strftime(time_str, sizeof(time_str), "%b", logtm);
fprintf(fout, " %02d%3s%02d", logtm->tm_mday,
month[logtm->tm_mon], logtm->tm_year % 100);
else
fprintf(fout, " %3s%02d ", weekday[logtm->tm_wday],
time_str, logtm->tm_year % 100);
} else {
strftime(time_str, sizeof(time_str), "%a", logtm);
fprintf(fout, " %3s%02d ", time_str,
logtm->tm_hour);
}
} else {
fprintf(fout, " %02d:%02d ", logtm->tm_hour, logtm->tm_min);
}
@ -249,7 +245,7 @@ static void showinfo(utmp_t * u, int formtype, int maxcmd, int from,
char uname[UT_NAMESIZE + 1] = "", tty[5 + UT_LINESIZE + 1] = "/dev/";
const proc_t *best;
for (i = 0; i < sizeof(u->ut_line); i++)
for (i = 0; i < UT_LINESIZE; i++)
/* clean up tty if garbled */
if (isalnum(u->ut_line[i]) || (u->ut_line[i] == '/'))
tty[i + 5] = u->ut_line[i];
@ -273,7 +269,7 @@ static void showinfo(utmp_t * u, int formtype, int maxcmd, int from,
if (formtype) {
printf("%-*.*s%-9.8s", userlen + 1, userlen, uname, u->ut_line);
if (from)
print_host(u->ut_host, sizeof u->ut_host, fromlen);
print_host(u->ut_host, UT_HOSTSIZE, fromlen);
print_logintime(u->ut_time, stdout);
if (*u->ut_line == ':')
/* idle unknown for xdm logins */
@ -293,7 +289,7 @@ static void showinfo(utmp_t * u, int formtype, int maxcmd, int from,
printf("%-*.*s%-9.8s", userlen + 1, userlen, u->ut_user,
u->ut_line);
if (from)
print_host(u->ut_host, sizeof u->ut_host, fromlen);
print_host(u->ut_host, UT_HOSTSIZE, fromlen);
if (*u->ut_line == ':')
/* idle unknown for xdm logins */
printf(" ?xdm? ");
@ -401,7 +397,7 @@ int main(int argc, char **argv)
/* Get user field length from environment */
if ((env_var = getenv("PROCPS_USERLEN")) != NULL) {
userlen = atoi(env_var);
if (userlen < 8 || userlen > UT_NAMESIZE) {
if (userlen < 8 || UT_NAMESIZE < userlen) {
xwarnx
(_("User length environment PROCPS_USERLEN must be between 8 and %zu, ignoring.\n"),
UT_NAMESIZE);
@ -411,10 +407,10 @@ int main(int argc, char **argv)
/* Get from field length from environment */
if ((env_var = getenv("PROCPS_FROMLEN")) != NULL) {
fromlen = atoi(env_var);
if (fromlen < 8 || fromlen > HOSTSZ) {
if (fromlen < 8 || UT_HOSTSIZE < fromlen) {
xwarnx
(_("from length environment PROCPS_FROMLEN must be between 8 and %d, ignoring\n"),
HOSTSZ);
UT_HOSTSIZE);
fromlen = 16;
}
}
@ -440,7 +436,7 @@ int main(int argc, char **argv)
* headers. Try to keep alignment intact. */
printf(_("%-*s TTY "), userlen, _("USER"));
if (from)
printf(_("FROM "));
printf("%-*s", fromlen - 1, _("FROM"));
if (longform)
printf(_(" LOGIN@ IDLE JCPU PCPU WHAT\n"));
else