+/*
+ * Regarding the macro timespeccmp:
+ *
+ * Copyright (c) 1982, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)time.h 8.5 (Berkeley) 5/4/95
+ * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
+ */
+#ifndef timespeccmp
+#define timespeccmp(tvp, uvp, cmp) \
+ (((tvp)->tv_sec == (uvp)->tv_sec) ? \
+ ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
+ ((tvp)->tv_sec cmp (uvp)->tv_sec))
+#endif /*timespeccmp*/
+
+
+
+#ifdef st_mtime
+
+static bool stab_isnewer(const struct stat *a, const struct stat *b) {
+ if (debugmode)
+ fprintf(stderr,"stab_isnewer mtim %lu.%06lu %lu.06%lu\n",
+ (unsigned long)a->st_mtim.tv_sec,
+ (unsigned long)a->st_mtim.tv_nsec,
+ (unsigned long)b->st_mtim.tv_sec,
+ (unsigned long)b->st_mtim.tv_nsec);
+ return timespeccmp(&a->st_mtim, &b->st_mtim, >);
+}
+
+static void stab_mtimenow(struct stat *out) {
+ int r = clock_gettime(CLOCK_REALTIME, &out->st_mtim);
+ if (r) err(127,"(stage2) clock_gettime");
+ if (debugmode)
+ fprintf(stderr,"stab_mtimenow mtim %lu.%06lu\n",
+ (unsigned long)out->st_mtim.tv_sec,
+ (unsigned long)out->st_mtim.tv_nsec);
+}
+
+#else /* !defined(st_mtime) */
+
+static bool stab_isnewer(const struct stat *a, const struct stat *b) {
+ if (debugmode)
+ fprintf(stderr,"stab_isnewer mtime %lu %lu\n",
+ (unsigned long)a->st_mtime,
+ (unsigned long)b->st_mtime);
+ return a->st_mtime > &b->st_mtime;
+}
+
+static void stab_mtimenow(struct stat *out) {
+ out->st_mtime = time(NULL);
+ if (baseline_time.st_mtime == (time_t)-1) err(127,"(stage2) time()");
+ if (debugmode)
+ fprintf(stderr,"stab_mtimenow mtime %lu\n",
+ (unsigned long)out->st_mtime);
+}
+
+#endif /* !defined(st_mtime) */
+
+static bool check_garbage_vs(const struct stat *started) {
+ struct stat script_stab;
+ int r;
+
+ r = lstat(script, &script_stab);
+ if (r) err(127,"lstat script (%s)",script);
+
+ if (stab_isnewer(&script_stab, started))
+ return 1;
+
+ if (S_ISLNK(script_stab.st_mode)) {
+ r = stat(script, &script_stab);
+ if (r) err(127,"stat script (%s0",script);
+
+ if (stab_isnewer(&script_stab, started))
+ return 1;
+ }
+
+ return 0;
+}
+