Skip to content

Commit a3d0338

Browse files
committed
Use libast tvgettime() for better portability
This replaces usage of the obsolescent timeofday() macro with the more portable tvgettime() function in libast, which itself uses the modern clock_gettime internally. This commit uses C99 variable declarations unabashedly for better struct initialization; no need to keep up a false C89 pretense anymore.
1 parent feb5aac commit a3d0338

File tree

6 files changed

+27
-46
lines changed

6 files changed

+27
-46
lines changed

src/cmd/ksh93/Mamfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ make install virtual
347347
prev include/shlex.h
348348
prev include/fcin.h
349349
prev include/builtins.h
350+
prev %{INCLUDE_AST}/tv.h
350351
prev %{INCLUDE_AST}/tmx.h
351352
prev %{INCLUDE_AST}/error.h
352353
prev include/defs.h
@@ -695,6 +696,7 @@ make install virtual
695696
prev include/variables.h
696697
prev %{INCLUDE_AST}/ast_random.h
697698
prev %{INCLUDE_AST}/regex.h
699+
prev %{INCLUDE_AST}/tv.h
698700
prev %{INCLUDE_AST}/tmx.h
699701
prev include/defs.h
700702
prev shopt.h
@@ -841,6 +843,7 @@ make install virtual
841843
prev %{INCLUDE_AST}/error.h
842844
prev %{INCLUDE_AST}/sig.h
843845
prev %{INCLUDE_AST}/ast.h
846+
prev %{INCLUDE_AST}/tv.h
844847
prev shopt.h
845848
done
846849

@@ -872,6 +875,7 @@ make install virtual
872875
prev include/variables.h
873876
prev include/fcin.h
874877
prev include/defs.h
878+
prev %{INCLUDE_AST}/tv.h
875879
prev shopt.h
876880
done
877881

src/cmd/ksh93/bltins/alarm.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "defs.h"
2828
#include <error.h>
2929
#include <tmx.h>
30+
#include <tv.h>
3031
#include "builtins.h"
3132
#include "fcin.h"
3233
#include "shlex.h"
@@ -96,9 +97,9 @@ static void *time_delete(struct tevent *item, void *list)
9697

9798
static Time_t getnow(void)
9899
{
99-
struct timeval tmp;
100-
timeofday(&tmp);
101-
return tmp.tv_sec + 1.e-6 * tmp.tv_usec;
100+
Tv_t tmp;
101+
tvgettime(&tmp);
102+
return tmp.tv_sec + 1.e-9 * tmp.tv_nsec;
102103
}
103104

104105
static void print_alarms(void *list)

src/cmd/ksh93/features/time

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,10 @@
11
hdr utime,sys/resource
2-
lib getrusage,gettimeofday,setitimer
3-
mem timeval.tv_usec sys/time.h
4-
tst lib_2_timeofday note{ 2 arg gettimeofday() }end link{
5-
#include <sys/types.h>
6-
#include <sys/time.h>
7-
int main(void)
8-
{
9-
struct timeval tv;
10-
struct timezone tz;
11-
return gettimeofday(&tv, &tz);
12-
}
13-
}end
14-
tst lib_1_timeofday note{ 1 arg gettimeofday() }end link{
15-
#include <sys/types.h>
16-
#include <sys/time.h>
17-
int main(void)
18-
{
19-
struct timeval tv;
20-
return gettimeofday(&tv);
21-
}
22-
}end
2+
lib getrusage,setitimer
233
cat{
244
#undef _def_time
255
#include <times.h>
266
#include <sys/time.h>
277
#define _def_time 1
28-
#undef timeofday
29-
#if _lib_2_timeofday
30-
#define timeofday(p) gettimeofday(p,NULL)
31-
#else
32-
#if _lib_1_timeofday
33-
#define timeofday(p) gettimeofday(p)
34-
#endif
35-
#endif
368
/* BSD timeradd and timersub macros */
379
#if !defined(timeradd)
3810
#define timeradd(tvp, uvp, vvp) \

src/cmd/ksh93/sh/init.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "defs.h"
3333
#include <pwd.h>
3434
#include <tmx.h>
35+
#include <tv.h>
3536
#include <regex.h>
3637
#include <math.h>
3738
#include <ast_random.h>
@@ -553,14 +554,13 @@ static char* get_ifs(Namval_t *np, Namfun_t *fp)
553554
/*
554555
* these functions are used to get and set the SECONDS variable
555556
*/
556-
#define dtime(tp) ((double)((tp)->tv_sec)+1e-6*((double)((tp)->tv_usec)))
557-
#define tms timeval
557+
#define dtime(tp) ((double)((tp)->tv_sec)+1e-9*((double)((tp)->tv_nsec)))
558558

559559
static void put_seconds(Namval_t* np,const char *val,nvflag_t flags,Namfun_t *fp)
560560
{
561561
double d;
562562
double *dp = np->nvalue;
563-
struct tms tp;
563+
Tv_t tp;
564564
if(!val)
565565
{
566566
nv_putv(np, val, flags, fp);
@@ -577,31 +577,31 @@ static void put_seconds(Namval_t* np,const char *val,nvflag_t flags,Namfun_t *fp
577577
}
578578
nv_putv(np, val, flags, fp);
579579
d = *dp;
580-
timeofday(&tp);
580+
tvgettime(&tp);
581581
*dp = dtime(&tp)-d;
582582
}
583583

584584
static char* get_seconds(Namval_t *np, Namfun_t *fp)
585585
{
586586
size_t places = nv_size(np);
587-
struct tms tp;
587+
Tv_t tp;
588588
double d;
589589
double *dp = np->nvalue;
590590
double offset = dp ? *dp : 0;
591591
NOT_USED(fp);
592-
timeofday(&tp);
592+
tvgettime(&tp);
593593
d = dtime(&tp)- offset;
594594
sfprintf(sh.strbuf,"%.*f",places,d);
595595
return sfstruse(sh.strbuf);
596596
}
597597

598598
static Sfdouble_t nget_seconds(Namval_t *np, Namfun_t *fp)
599599
{
600-
struct tms tp;
600+
Tv_t tp;
601601
double *dp = np->nvalue;
602602
double offset = dp ? *dp : 0;
603603
NOT_USED(fp);
604-
timeofday(&tp);
604+
tvgettime(&tp);
605605
return dtime(&tp) - offset;
606606
}
607607

src/cmd/ksh93/sh/timers.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <ast.h>
2121
#include <sig.h>
2222
#include <error.h>
23+
#include <tv.h>
2324
#include "fault.h"
2425
#include "defs.h"
2526
#include "FEATURE/time"
@@ -44,9 +45,9 @@ static char time_state;
4445
static Sfdouble_t getnow(void)
4546
{
4647
Sfdouble_t now;
47-
struct timeval tp;
48-
timeofday(&tp);
49-
now = tp.tv_sec + 1.e-6*tp.tv_usec;
48+
Tv_t tp;
49+
tvgettime(&tp);
50+
now = tp.tv_sec + 1.e-6*(tp.tv_nsec/1000);
5051
return now+.001;
5152
}
5253

src/cmd/ksh93/sh/xec.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "jobs.h"
3636
#include "test.h"
3737
#include "builtins.h"
38+
#include <tv.h>
3839
#include "FEATURE/time"
3940
#include "FEATURE/externs"
4041
#include "FEATURE/locale"
@@ -2240,7 +2241,7 @@ int sh_exec(const Shnode_t *t, int flags)
22402241
case TTIME:
22412242
{
22422243
const char *format = e_timeformat;
2243-
struct timeval ta, tb;
2244+
Tv_t tva, tvb;
22442245
struct timeval before_usr, before_sys, after_usr, after_sys, tm[3];
22452246
if(type!=TTIME)
22462247
{
@@ -2252,7 +2253,7 @@ int sh_exec(const Shnode_t *t, int flags)
22522253
{
22532254
int timer_on = sh_isstate(SH_TIMING);
22542255
/* must be run after forking a subshell */
2255-
timeofday(&tb);
2256+
tvgettime(&tvb);
22562257
get_cpu_times(&before_usr, &before_sys);
22572258
sh_onstate(SH_TIMING);
22582259
sh_exec(t->par.partre,sh_isstate(SH_ERREXIT)|(flags & ARG_OPTIMIZE));
@@ -2265,7 +2266,9 @@ int sh_exec(const Shnode_t *t, int flags)
22652266
before_sys.tv_sec = before_sys.tv_usec = 0;
22662267
}
22672268
get_cpu_times(&after_usr, &after_sys);
2268-
timeofday(&ta);
2269+
tvgettime(&tva);
2270+
struct timeval ta = { .tv_sec = tva.tv_sec, .tv_usec = (suseconds_t)(tva.tv_nsec / 1000) };
2271+
struct timeval tb = { .tv_sec = tvb.tv_sec, .tv_usec = (suseconds_t)(tvb.tv_nsec / 1000) };
22692272
timersub(&ta, &tb, &tm[TM_REAL_IDX]); /* calculate elapsed real-time */
22702273
timersub(&after_usr, &before_usr, &tm[TM_USR_IDX]);
22712274
timersub(&after_sys, &before_sys, &tm[TM_SYS_IDX]);

0 commit comments

Comments
 (0)