From: brynet Date: Fri, 24 Apr 2015 21:48:31 +0000 (+0000) Subject: struct timespec/clock_gettime(3) conversion for vi(1). X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=e5f693684f9ee461d64ceeee8484d7299521db84;p=openbsd struct timespec/clock_gettime(3) conversion for vi(1). ok guenther@ --- diff --git a/usr.bin/vi/common/exf.c b/usr.bin/vi/common/exf.c index 43d14008859..5a6436123fc 100644 --- a/usr.bin/vi/common/exf.c +++ b/usr.bin/vi/common/exf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: exf.c,v 1.35 2015/04/19 01:10:59 millert Exp $ */ +/* $OpenBSD: exf.c,v 1.36 2015/04/24 21:48:31 brynet Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -13,6 +13,7 @@ #include #include +#include /* * We include , because the flock(2) and open(2) #defines @@ -30,6 +31,7 @@ #include #include #include +#include #include #include "common.h" @@ -209,7 +211,7 @@ file_init(SCR *sp, FREF *frp, char *rcv_name, int flags) if (!LF_ISSET(FS_OPENERR)) F_SET(frp, FR_NEWFILE); - time(&ep->mtime); + (void)clock_gettime(CLOCK_REALTIME, &ep->mtim); } else { /* * XXX @@ -228,7 +230,7 @@ file_init(SCR *sp, FREF *frp, char *rcv_name, int flags) ep->mdev = sb.st_dev; ep->minode = sb.st_ino; - ep->mtime = sb.st_mtime; + ep->mtim = sb.st_mtim; if (!S_ISREG(sb.st_mode)) msgq_str(sp, M_ERR, oname, @@ -800,7 +802,7 @@ file_write(SCR *sp, MARK *fm, MARK *tm, char *name, int flags) if (noname && !LF_ISSET(FS_FORCE | FS_APPEND) && ((F_ISSET(ep, F_DEVSET) && (sb.st_dev != ep->mdev || sb.st_ino != ep->minode)) || - sb.st_mtime != ep->mtime)) { + timespeccmp(&sb.st_mtim, &ep->mtim, !=))) { msgq_str(sp, M_ERR, name, LF_ISSET(FS_POSSIBLE) ? "250|%s: file modified more recently than this copy; use ! to override" : "251|%s: file modified more recently than this copy"); @@ -864,13 +866,13 @@ file_write(SCR *sp, MARK *fm, MARK *tm, char *name, int flags) */ if (noname) { if (stat(name, &sb)) - time(&ep->mtime); + (void)clock_gettime(CLOCK_REALTIME, &ep->mtim); else { F_SET(ep, F_DEVSET); ep->mdev = sb.st_dev; ep->minode = sb.st_ino; - ep->mtime = sb.st_mtime; + ep->mtim = sb.st_mtim; } } diff --git a/usr.bin/vi/common/exf.h b/usr.bin/vi/common/exf.h index d89768427f8..3ae13e27440 100644 --- a/usr.bin/vi/common/exf.h +++ b/usr.bin/vi/common/exf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: exf.h,v 1.4 2001/01/29 01:58:29 niklas Exp $ */ +/* $OpenBSD: exf.h,v 1.5 2015/04/24 21:48:31 brynet Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -35,13 +35,9 @@ struct _exf { LIST_HEAD(_markh, _lmark) marks;/* Linked list of file MARK's. */ - /* - * XXX - * Mtime should be a struct timespec, but time_t is more portable. - */ dev_t mdev; /* Device. */ ino_t minode; /* Inode. */ - time_t mtime; /* Last modification time. */ + struct timespec mtim; /* Last modification time. */ int fcntl_fd; /* Fcntl locking fd; see exf.c. */ diff --git a/usr.bin/vi/common/recover.c b/usr.bin/vi/common/recover.c index 33c7192f070..3e13d89df39 100644 --- a/usr.bin/vi/common/recover.c +++ b/usr.bin/vi/common/recover.c @@ -1,4 +1,4 @@ -/* $OpenBSD: recover.c,v 1.21 2015/03/27 04:11:25 brynet Exp $ */ +/* $OpenBSD: recover.c,v 1.22 2015/04/24 21:48:31 brynet Exp $ */ /*- * Copyright (c) 1993, 1994 @@ -13,6 +13,7 @@ #include #include +#include /* * We include , because the open #defines were found there @@ -572,7 +573,7 @@ rcv_read(SCR *sp, FREF *frp) struct stat sb; DIR *dirp; EXF *ep; - time_t rec_mtime; + struct timespec rec_mtim; int fd, found, locked, requested, sv_fd; char *name, *p, *t, *rp, *recp, *pathp; char file[PATH_MAX], path[PATH_MAX], recpath[PATH_MAX]; @@ -587,7 +588,7 @@ rcv_read(SCR *sp, FREF *frp) name = frp->name; sv_fd = -1; - rec_mtime = 0; + rec_mtim.tv_sec = rec_mtim.tv_nsec = 0; recp = pathp = NULL; for (found = requested = 0; (dp = readdir(dirp)) != NULL;) { if (strncmp(dp->d_name, "recover.", 8)) @@ -667,14 +668,10 @@ rcv_read(SCR *sp, FREF *frp) /* * If we've found more than one, take the most recent. - * - * XXX - * Since we're using st_mtime, for portability reasons, - * we only get a single second granularity, instead of - * getting it right. */ (void)fstat(fd, &sb); - if (recp == NULL || rec_mtime < sb.st_mtime) { + if (recp == NULL || + timespeccmp(&rec_mtim, &sb.st_mtim, <)) { p = recp; t = pathp; if ((recp = strdup(recpath)) == NULL) { @@ -693,7 +690,7 @@ rcv_read(SCR *sp, FREF *frp) free(p); free(t); } - rec_mtime = sb.st_mtime; + rec_mtim = sb.st_mtim; if (sv_fd != -1) (void)close(sv_fd); sv_fd = fd; diff --git a/usr.bin/vi/ex/ex_cscope.c b/usr.bin/vi/ex/ex_cscope.c index 4e00e6e76a4..df26cba3d74 100644 --- a/usr.bin/vi/ex/ex_cscope.c +++ b/usr.bin/vi/ex/ex_cscope.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ex_cscope.c,v 1.25 2015/04/21 01:41:42 jsg Exp $ */ +/* $OpenBSD: ex_cscope.c,v 1.26 2015/04/24 21:48:31 brynet Exp $ */ /*- * Copyright (c) 1994, 1996 @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -244,7 +245,7 @@ cscope_add(SCR *sp, EXCMD *cmdp, char *dname) csc->dname = csc->buf; csc->dlen = len; memcpy(csc->dname, dname, len); - csc->mtime = sb.st_mtime; + csc->mtim = sb.st_mtim; /* Get the search paths for the cscope. */ if (get_paths(sp, csc)) @@ -765,7 +766,8 @@ csc_file(SCR *sp, CSC *csc, char *name, char **dirp, size_t *dlenp, if (stat(buf, &sb) == 0) { *dirp = *pp; *dlenp = strlen(*pp); - *isolderp = sb.st_mtime < csc->mtime; + *isolderp = timespeccmp( + &sb.st_mtim, &csc->mtim, <); return; } } diff --git a/usr.bin/vi/ex/tag.h b/usr.bin/vi/ex/tag.h index 81b1d20c6f0..6af6ebb8843 100644 --- a/usr.bin/vi/ex/tag.h +++ b/usr.bin/vi/ex/tag.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tag.h,v 1.5 2013/12/01 19:26:37 krw Exp $ */ +/* $OpenBSD: tag.h,v 1.6 2015/04/24 21:48:31 brynet Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -23,7 +23,7 @@ struct _csc { char *dname; /* Base directory of this cscope connection. */ size_t dlen; /* Length of base directory. */ pid_t pid; /* PID of the connected cscope process. */ - time_t mtime; /* Last modification time of cscope database. */ + struct timespec mtim; /* Last modification time of cscope database. */ FILE *from_fp; /* from cscope: FILE. */ int from_fd; /* from cscope: file descriptor. */ diff --git a/usr.bin/vi/vi/vi.h b/usr.bin/vi/vi/vi.h index bcf928d8347..6426f2b63a3 100644 --- a/usr.bin/vi/vi/vi.h +++ b/usr.bin/vi/vi/vi.h @@ -1,4 +1,4 @@ -/* $OpenBSD: vi.h,v 1.7 2013/11/28 22:12:40 krw Exp $ */ +/* $OpenBSD: vi.h,v 1.8 2015/04/24 21:48:31 brynet Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -255,7 +255,7 @@ typedef struct _vi_private { size_t busy_fx; /* Busy character x coordinate. */ size_t busy_oldy; /* Saved y coordinate. */ size_t busy_oldx; /* Saved x coordinate. */ - struct timeval busy_tv; /* Busy timer. */ + struct timespec busy_ts;/* Busy timer. */ char *ps; /* Paragraph plus section list. */ diff --git a/usr.bin/vi/vi/vs_msg.c b/usr.bin/vi/vi/vs_msg.c index f5d2db3d3d4..23ccf5eabc2 100644 --- a/usr.bin/vi/vi/vs_msg.c +++ b/usr.bin/vi/vi/vs_msg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vs_msg.c,v 1.13 2014/11/12 04:28:41 bentley Exp $ */ +/* $OpenBSD: vs_msg.c,v 1.14 2015/04/24 21:48:31 brynet Exp $ */ /*- * Copyright (c) 1993, 1994 @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "../common/common.h" @@ -59,7 +60,7 @@ vs_busy(SCR *sp, const char *msg, busy_t btype) GS *gp; VI_PRIVATE *vip; static const char flagc[] = "|/-\\"; - struct timeval tv; + struct timespec ts, ts_diff; size_t len, notused; const char *p; @@ -84,7 +85,7 @@ vs_busy(SCR *sp, const char *msg, busy_t btype) /* Initialize state for updates. */ vip->busy_ch = 0; - (void)gettimeofday(&vip->busy_tv, NULL); + (void)clock_gettime(CLOCK_MONOTONIC, &vip->busy_ts); /* Save the current cursor. */ (void)gp->scr_cursor(sp, &vip->busy_oldy, &vip->busy_oldx); @@ -117,11 +118,18 @@ vs_busy(SCR *sp, const char *msg, busy_t btype) break; /* Update no more than every 1/8 of a second. */ - (void)gettimeofday(&tv, NULL); - if (((tv.tv_sec - vip->busy_tv.tv_sec) * 1000000 + - (tv.tv_usec - vip->busy_tv.tv_usec)) < 125000) + (void)clock_gettime(CLOCK_MONOTONIC, &ts); + ts_diff = ts; + ts_diff.tv_sec -= vip->busy_ts.tv_sec; + ts_diff.tv_nsec -= vip->busy_ts.tv_nsec; + if (ts_diff.tv_nsec < 0) { + ts_diff.tv_sec--; + ts_diff.tv_nsec += 1000000000; + } + if ((ts_diff.tv_sec == 0 && ts_diff.tv_nsec < 125000000) || + ts_diff.tv_sec < 0) return; - vip->busy_tv = tv; + vip->busy_ts = ts; /* Display the update. */ if (vip->busy_ch == sizeof(flagc) - 1)