]> granicus.if.org Git - procps-ng/commitdiff
watch: Add hostname to the header
authorJesse Hathaway <jesse@mbuki-mvuki.org>
Sat, 2 Jul 2016 05:45:36 +0000 (15:45 +1000)
committerCraig Small <csmall@enc.com.au>
Sat, 2 Jul 2016 05:47:39 +0000 (15:47 +1000)
watch has the hostname added to the header so you know what device
if you have many it is running on.

Signed-off-by: Craig Small <csmall@enc.com.au>
NEWS
watch.c

diff --git a/NEWS b/NEWS
index ef62e0f2412e16eb3fac4ff7711b39c79763ab3b..c884b15f3afd85d0d4d6a773fb7ac932764f71d5 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,7 @@ procps-ng-NEXT
   * tests: Conditionally add prctl Debian #816237
   * pidof: check cmd if space in argv0. GitLab #4
   * kill: report error if cannot kill process #733172
+  * watch: Add hostname to header
 
 procps-ng-3.3.11
 ----------------
diff --git a/watch.c b/watch.c
index e97a4139a08919180e07d3ceaedc04a80fa4b69c..e3180ce62b48319b6983007bda215217e3e46575 100644 (file)
--- a/watch.c
+++ b/watch.c
@@ -371,40 +371,45 @@ static void output_header(char *restrict command, double interval)
 {
        time_t t = time(NULL);
        char *ts = ctime(&t);
-       int tsl = strlen(ts);
        char *header;
+       char *right_header;
+       char hostname[HOST_NAME_MAX + 1];
+
+       gethostname(hostname, sizeof(hostname));
 
        /*
-        * left justify interval and command, right justify time,
+        * left justify interval and command, right justify hostname and time,
         * clipping all to fit window width
         */
        int hlen = asprintf(&header, _("Every %.1fs: "), interval);
+       int rhlen = asprintf(&right_header, _("%s: %s"), hostname, ts);
 
        /*
         * the rules:
-        *   width < tsl : print nothing
-        *   width < tsl + hlen + 1: print ts
-        *   width = tsl + hlen + 1: print header, ts
-        *   width < tsl + hlen + 4: print header, ..., ts
-        *   width < tsl + hlen +    wcommand_columns: print header,
-        *                           truncated wcommand, ..., ts
-        *   width > "": print header, wcomand, ts
+        *   width < rhlen : print nothing
+        *   width < rhlen + hlen + 1: print hostname, ts
+        *   width = rhlen + hlen + 1: print header, hostname, ts
+        *   width < rhlen + hlen + 4: print header, ..., hostname, ts
+        *   width < rhlen + hlen + wcommand_columns: print header,
+        *                           truncated wcommand, ..., hostname, ts
+        *   width > "": print header, wcomand, hostname, ts
         * this is slightly different from how it used to be
         */
-       if (width < tsl) {
+       if (width < rhlen) {
                free(header);
+               free(right_header);
                return;
        }
-       if (tsl + hlen + 1 <= width) {
+       if (rhlen + hlen + 1 <= width) {
                mvaddstr(0, 0, header);
-               if (tsl + hlen + 2 <= width) {
-                       if (width < tsl + hlen + 4) {
-                               mvaddstr(0, width - tsl - 4, "... ");
+               if (rhlen + hlen + 2 <= width) {
+                       if (width < rhlen + hlen + 4) {
+                               mvaddstr(0, width - rhlen - 4, "... ");
                        } else {
 #ifdef WITH_WATCH8BIT
-                               if (width < tsl + hlen + wcommand_columns) {
+                               if (width < rhlen + hlen + wcommand_columns) {
                                        /* print truncated */
-                                       int available = width - tsl - hlen;
+                                       int available = width - rhlen - hlen;
                                        int in_use = wcommand_columns;
                                        int wcomm_len = wcommand_characters;
                                        while (available - 4 < in_use) {
@@ -412,18 +417,19 @@ static void output_header(char *restrict command, double interval)
                                                in_use = wcswidth(wcommand, wcomm_len);
                                        }
                                        mvaddnwstr(0, hlen, wcommand, wcomm_len);
-                                       mvaddstr(0, width - tsl - 4, "... ");
+                                       mvaddstr(0, width - rhlen - 4, "... ");
                                } else {
                                        mvaddwstr(0, hlen, wcommand);
                                }
 #else
-                               mvaddnstr(0, hlen, command, width - tsl - hlen);
+                               mvaddnstr(0, hlen, command, width - rhlen - hlen);
 #endif /* WITH_WATCH8BIT */
                        }
                }
        }
-       mvaddstr(0, width - tsl + 1, ts);
+       mvaddstr(0, width - rhlen + 1, right_header);
        free(header);
+       free(right_header);
        return;
 }