Re: ksh: count $SECONDS with monotonic clock

2018-04-13 Thread Todd C. Miller
On Fri, 13 Apr 2018 19:42:51 +0200, Jeremie Courreges-Anglas wrote:

> The diff uses timespecsub:

Ah, OK.  That's fine then.

 - todd



Re: ksh: count $SECONDS with monotonic clock

2018-04-13 Thread Jeremie Courreges-Anglas
On Fri, Apr 13 2018, "Todd C. Miller"  wrote:
> On Fri, 13 Apr 2018 11:11:04 -0500, Scott Cheloha wrote:
>
>> So that $SECONDS advances uniformly, independent of the system clock.
>
> Why are you including sys/time.h?  For struct timespect you only
> need time.h which is already included.
>
> In general, you only need sys/time.h for struct timeval or for some
> of the time-related macros.

The diff uses timespecsub:

@@ -921,8 +922,13 @@ getspec(struct tbl *vp)
 * has been set - don't do anything in this case
 * (see initcoms[] in main.c).
 */
-   if (vp->flag & ISSET)
-   setint(vp, (int64_t)(time(NULL) - seconds));
+   if (vp->flag & ISSET) {
+   struct timespec difference, now;
+
+   clock_gettime(CLOCK_MONOTONIC, &now);
+   timespecsub(&now, &seconds, &difference);
+   setint(vp, (int64_t)difference.tv_sec);
+   }
vp->flag |= SPECIAL;
break;
case V_RANDOM:


-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE



Re: ksh: count $SECONDS with monotonic clock

2018-04-13 Thread Todd C. Miller
On Fri, 13 Apr 2018 11:11:04 -0500, Scott Cheloha wrote:

> So that $SECONDS advances uniformly, independent of the system clock.

Why are you including sys/time.h?  For struct timespect you only
need time.h which is already included.

In general, you only need sys/time.h for struct timeval or for some
of the time-related macros.

 - todd



Re: ksh: count $SECONDS with monotonic clock

2018-04-13 Thread Jeremie Courreges-Anglas
On Fri, Apr 13 2018, Scott Cheloha  wrote:
> So that $SECONDS advances uniformly, independent of the system clock.
>
> ok?

ok jca@

> --
> Scott Cheloha
>
> P.S. Similar change forthcoming for $MAILCHECK.
>
> Index: bin/ksh/var.c
> ===
> RCS file: /cvs/src/bin/ksh/var.c,v
> retrieving revision 1.67
> diff -u -p -r1.67 var.c
> --- bin/ksh/var.c 9 Apr 2018 17:53:36 -   1.67
> +++ bin/ksh/var.c 13 Apr 2018 16:01:08 -
> @@ -1,6 +1,7 @@
>  /*   $OpenBSD: var.c,v 1.67 2018/04/09 17:53:36 tobias Exp $ */
>  
>  #include 
> +#include 
>  
>  #include 
>  #include 
> @@ -908,7 +909,7 @@ unspecial(const char *name)
>   ktdelete(tp);
>  }
>  
> -static   time_t  seconds;/* time SECONDS last set */
> +static   struct  timespec seconds;   /* time SECONDS last set */
>  static   int user_lineno;/* what user set $LINENO to */
>  
>  static void
> @@ -921,8 +922,13 @@ getspec(struct tbl *vp)
>* has been set - don't do anything in this case
>* (see initcoms[] in main.c).
>*/
> - if (vp->flag & ISSET)
> - setint(vp, (int64_t)(time(NULL) - seconds));
> + if (vp->flag & ISSET) {
> + struct timespec difference, now;
> +
> + clock_gettime(CLOCK_MONOTONIC, &now);
> + timespecsub(&now, &seconds, &difference);
> + setint(vp, (int64_t)difference.tv_sec);
> + }
>   vp->flag |= SPECIAL;
>   break;
>   case V_RANDOM:
> @@ -1036,7 +1042,8 @@ setspec(struct tbl *vp)
>   break;
>   case V_SECONDS:
>   vp->flag &= ~SPECIAL;
> - seconds = time(NULL) - intval(vp);
> + clock_gettime(CLOCK_MONOTONIC, &seconds);
> + seconds.tv_sec -= intval(vp);
>   vp->flag |= SPECIAL;
>   break;
>   case V_TMOUT:
>

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE