Re: ps -o etime

2016-09-08 Thread Todd C. Miller
On Thu, 08 Sep 2016 23:15:01 +1200, Carlin Bingham wrote:

> The "etime" keyword is currently an alias for "start". posix says it
> should be the amount of time since the program started running, in the
> format [[dd-]hh:]mm:ss, I've encountered some code that doesn't work on
> openbsd because that's what it expects. The commit that added this in
> '97 says it was for XPG4 compat, but XPG4 gives the same definition of
> etime as posix does now.
> 
> Given that the alias is old enough to vote, too late to change this?

Yes, we should fix this, diff looks good.

 - todd



Re: ps -o etime

2016-09-08 Thread Ted Unangst
Carlin Bingham wrote:
> The "etime" keyword is currently an alias for "start". posix says it
> should be the amount of time since the program started running, in the
> format [[dd-]hh:]mm:ss, I've encountered some code that doesn't work on
> openbsd because that's what it expects. The commit that added this in
> '97 says it was for XPG4 compat, but XPG4 gives the same definition of
> etime as posix does now.
> 
> Given that the alias is old enough to vote, too late to change this?

I don't think so. The alias was added in a half-hearted effort at compliance,
but we should make the output do the right thing. That's obviously what people
and scripts expect.

The traditional output of 'start' shouldn't be changed, but you've got that
covered.



ps -o etime

2016-09-08 Thread Carlin Bingham
The "etime" keyword is currently an alias for "start". posix says it
should be the amount of time since the program started running, in the
format [[dd-]hh:]mm:ss, I've encountered some code that doesn't work on
openbsd because that's what it expects. The commit that added this in
'97 says it was for XPG4 compat, but XPG4 gives the same definition of
etime as posix does now.

Given that the alias is old enough to vote, too late to change this?

-- 
Carlin



Index: bin/ps/extern.h
===
RCS file: /cvs/src/bin/ps/extern.h,v
retrieving revision 1.19
diff -u -p -u -r1.19 extern.h
--- bin/ps/extern.h 10 Jan 2016 14:04:16 -  1.19
+++ bin/ps/extern.h 8 Sep 2016 10:59:32 -
@@ -48,6 +48,7 @@ void   command(const struct kinfo_proc *,
 voidcputime(const struct kinfo_proc *, VARENT *);
 int donlist(void);
 voidemulname(const struct kinfo_proc *, VARENT *);
+voidelapsed(const struct kinfo_proc *, VARENT *);
 double  getpcpu(const struct kinfo_proc *);
 double  getpmem(const struct kinfo_proc *);
 voidgname(const struct kinfo_proc *, VARENT *);
Index: bin/ps/keyword.c
===
RCS file: /cvs/src/bin/ps/keyword.c,v
retrieving revision 1.43
diff -u -p -u -r1.43 keyword.c
--- bin/ps/keyword.c30 Dec 2015 14:59:10 -  1.43
+++ bin/ps/keyword.c8 Sep 2016 10:59:32 -
@@ -101,7 +101,7 @@ VAR var[] = {
{"cwd", "CWD", NULL, LJUST, curwd, CWDLEN},
{"dsiz", "DSIZ", NULL, 0, dsize, 4},
{"emul", "EMUL", NULL, LJUST, emulname, KI_EMULNAMELEN - 1},
-   {"etime", "ELAPSED", "start"},
+   {"etime", "ELAPSED", NULL, USER, elapsed, 12},
{"f", "F", NULL, 0, pvar, 7, 0, POFF(p_flag), INT32, "x"},
{"flags", "", "f"},
GID("gid", "GID", pvar, POFF(p_gid)),
Index: bin/ps/print.c
===
RCS file: /cvs/src/bin/ps/print.c,v
retrieving revision 1.68
diff -u -p -u -r1.68 print.c
--- bin/ps/print.c  1 Sep 2016 09:44:06 -   1.68
+++ bin/ps/print.c  8 Sep 2016 10:59:32 -
@@ -439,6 +439,50 @@ lstarted(const struct kinfo_proc *kp, VA
(void)printf("%-*s", v->width, buf);
 }
 
+void elapsed(const struct kinfo_proc *kp, VARENT *ve)
+{
+   VAR *v;
+   static time_t now;
+   time_t secs;
+   char buf[64];
+   long days, hours, minutes, seconds;
+
+   v = ve->var;
+   if (!kp->p_uvalid) {
+   (void)printf("%*s", v->width, "-");
+   return;
+   }
+
+   if (!now)
+   (void)time();
+   secs = now - kp->p_ustart_sec;
+
+   if (secs < 0) {
+   (void)printf("%*s", v->width, "-");
+   return;
+   }
+
+   days = secs / SECSPERDAY;
+   secs %= SECSPERDAY;
+
+   hours = secs / SECSPERHOUR;
+   secs %= SECSPERHOUR;
+
+   minutes = secs / 60;
+   seconds = secs % 60;
+
+   if (days > 0)
+   (void)snprintf(buf, sizeof(buf), "%ld-%02ld:%02ld:%02ld",
+   days, hours, minutes, seconds);
+   else if (hours > 0)
+   (void)snprintf(buf, sizeof(buf), "%02ld:%02ld:%02ld",
+   hours, minutes, seconds);
+   else
+   (void)snprintf(buf, sizeof(buf), "%02ld:%02ld",
+   minutes, seconds);
+   (void)printf("%*s", v->width, buf);
+}
+
 void
 wchan(const struct kinfo_proc *kp, VARENT *ve)
 {
Index: bin/ps/ps.1
===
RCS file: /cvs/src/bin/ps/ps.1,v
retrieving revision 1.106
diff -u -p -u -r1.106 ps.1
--- bin/ps/ps.1 25 Apr 2016 20:34:55 -  1.106
+++ bin/ps/ps.1 8 Sep 2016 10:59:32 -
@@ -211,6 +211,8 @@ Current working directory.
 Data size, in Kilobytes.
 .It Cm emul
 Name of system call emulation environment.
+.It Cm etime
+Elapsed time since the process was started.
 .It Cm flags
 Alias:
 .Cm f .
@@ -386,8 +388,6 @@ Sleep time (in seconds; 127 = infinity).
 .It Cm ssiz
 Stack size, in Kilobytes.
 .It Cm start
-Alias:
-.Cm etime .
 The time the command started.
 If the command started less than 24 hours ago, the start time is
 displayed using the