[procps] Re: A rainy days effort

On Sat, Mar 3, 2012 at 22:30, Craig Small <csmall-procps@xxxxxxxxxx> wrote:
> On Sat, Mar 03, 2012 at 10:10:06AM +0100, Sami Kerola wrote:
>> Another patch which is missing without reason is 'w: use utmp
>> definitions to determine array sizes'. This patch, though, is
>> unimportant clean up. I attached both patches to this email to ease
>> merging.
> Not sure what happened there, i remember seeing that go in at one stage
> because I was curious about %zu anyhow the fixes are in.

Uh, oh. I should have said ignore the previous patch, I made better &
more complete to my git. Well, now the change is in two commits but
that's how stuff sometimes go. Below you can find pull request which
has all changes I'd like to get to upstream before release v3.3.3.
From my behalf the project is now in release candidate phase. If I
write some nice to have changes I'll keep them in my git, and request
pull after the release. Possible bug fixes are, of course, different
story.


The following changes since commit ddd15211e0895478f9b6008ff9c26c99d45a1753:

  w: use utmp definitions to determine array sizes (2012-03-04 08:20:39 +1100)

are available in the git repository at:

  git://gitorious.org/~kerolasa/procps/sami-procps-ng.git 2012wk9

for you to fetch changes up to d5c760ee07d5f2f7d6e5bd78c79e48e74ed276b3:

  pmap: use only address start to range determination (2012-03-04
12:13:18 +0100)

----------------------------------------------------------------
Sami Kerola (5):
      w: use utmp headers to determine host size as well
      w: make date string abbreviations nls aware
      w: align header when from lenght is defined
      pmap: add range arguments parsing function
      pmap: use only address start to range determination

 pmap.c |   55 ++++++++++++++++++++++++++-----------------------------
 w.c    |   42 +++++++++++++++++++-----------------------
 2 files changed, 45 insertions(+), 52 deletions(-)

diff --git a/pmap.c b/pmap.c
index 1f8b0a0..3ba1bfe 100644
--- a/pmap.c
+++ b/pmap.c
@@ -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);
diff --git a/w.c b/w.c
index da948ba..db7d75c 100644
--- a/w.c
+++ b/w.c
@@ -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

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/

Other related posts: