/*
* Copyright (c) 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.
* 3. 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
/*
* If no -r or -t flag, at least two operands, the first of which
* is an 8 or 10 digit number, use the obsolete time specification.
*/
if (!timeset && argc > 1) {
(void)strtol(argv[0], &p, 10);
len = p - argv[0];
if (*p == '\0' && (len == 8 || len == 10)) {
timeset = 1;
stime_arg2(*argv++, len == 10, ts);
}
}
/* Otherwise use the current time of day. */
if (!timeset)
ts[1] = ts[0];
if (*argv == NULL)
usage();
for (rval = EXIT_SUCCESS; *argv; ++argv) {
/* See if the file exists. */
if ((*get_file_status)(*argv, &sb)) {
if (!cflag) {
/* Create the file. */
fd = open(*argv,
O_WRONLY | O_CREAT, DEFFILEMODE);
if (fd == -1 || fstat(fd, &sb) || close(fd)) {
rval = EXIT_FAILURE;
warn("%s", *argv);
continue;
}
/* If using the current time, we're done. */
if (!timeset)
continue;
} else
continue;
}
if (!aflag)
ts[0] = sb.st_atimespec;
if (!mflag)
ts[1] = sb.st_mtimespec;
/* Try utimes(2). */
if (!(*change_file_times)(*argv, ts))
continue;
/* If the user specified a time, nothing else we can do. */
if (timeset) {
rval = EXIT_FAILURE;
warn("%s", *argv);
}
/*
* System V and POSIX 1003.1 require that a NULL argument
* set the access/modification times to the current time.
* The permission checks are different, too, in that the
* ability to write the file is sufficient. Take a shot.
*/
if (!(*change_file_times)(*argv, NULL))
continue;
t->tm_isdst = -1; /* Figure out DST. */
tm = *t;
tsp[0].tv_sec = tsp[1].tv_sec = mktime(t);
if (tsp[0].tv_sec == NO_TIME || difftm(t, &tm))
terr: errx(EXIT_FAILURE, "out of range or bad time specification:\n"
"\t'%s' should be [[CC]YY]MMDDhhmm[.ss]", initarg);
tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
}
static void
stime_arg2(const char *arg, int year, struct timespec *tsp)
{
struct tm *t, tm;
time_t tmptime;
/* Start with the current time. */
tmptime = tsp[0].tv_sec;
if ((t = localtime(&tmptime)) == NULL)
err(EXIT_FAILURE, "localtime");
t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
--t->tm_mon; /* Convert from 01-12 to 00-11 */
t->tm_mday = ATOI2(arg);
t->tm_hour = ATOI2(arg);
t->tm_min = ATOI2(arg);
if (year) {
year = ATOI2(arg);
if (year < YEAR_BOUNDARY)
t->tm_year = year + LOW_YEAR_CENTURY - TM_YEAR_BASE;
else
t->tm_year = year + HIGH_YEAR_CENTURY - TM_YEAR_BASE;
}
t->tm_sec = 0;
t->tm_isdst = -1; /* Figure out DST. */
tm = *t;
tsp[0].tv_sec = tsp[1].tv_sec = mktime(t);
if (tsp[0].tv_sec == NO_TIME || difftm(t, &tm))
errx(EXIT_FAILURE,
"out of range or bad time specification: MMDDhhmm[YY]");
tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
}
static void
stime_file(const char *fname, struct timespec *tsp,
int statfunc(const char *, struct stat *))
{
struct stat sb;
if ((p = strchr(arg, '-')) == NULL)
return 0;
if (p - arg < 4) /* at least 4 year digits required */
return 0;
if (!isdigch(arg[0])) /* and the first must be a digit! */
return 0;
(void)memset(&tm, 0, sizeof tm);
errno = 0;
val = strtol(arg, &ep, 10); /* YYYY */
if (val < 0 || val > INT_MAX)
return 0;
if (*ep != '-')
return 0;
tm.tm_year = (int)val - 1900;
p = ep + 1;
if (!isdigch(*p))
return 0;
val = strtol(p, &ep, 10); /* MM */
if (val < 1 || val > 12)
return 0;
if (*ep != '-' || ep != p + 2)
return 0;
tm.tm_mon = (int)val - 1;
p = ep + 1;
if (!isdigch(*p))
return 0;
val = strtol(p, &ep, 10); /* DD */
if (val < 1 || val > 31)
return 0;
if ((*ep != 'T' && *ep != ' ') || ep != p + 2)
return 0;
tm.tm_mday = (int)val;
p = ep + 1;
if (!isdigch(*p))
return 0;
val = strtol(p, &ep, 10); /* hh */
if (val < 0 || val > 23)
return 0;
if (*ep != ':' || ep != p + 2)
return 0;
tm.tm_hour = (int)val;
p = ep + 1;
if (!isdigch(*p))
return 0;
val = strtol(p, &ep, 10); /* mm */
if (val < 0 || val > 59)
return 0;
if (*ep != ':' || ep != p + 2)
return 0;
tm.tm_min = (int)val;
p = ep + 1;
if (!isdigch(*p))
return 0;
val = strtol(p, &ep, 10); /* ss (or in POSIX, SS) */
if (val < 0 || val > 60)
return 0;
if ((*ep != '.' && *ep != ',' && *ep != 'Z' && *ep != '\0') ||
ep != p + 2)
return 0;
tm.tm_sec = (int)val;
p = ep + 1;
if (!isdigch(*p))
return 0;
val = strtol(p, &ep, 10);
if (val < 0)
return 0;
if (ep == p) /* at least 1 digit required */
return 0;
if (*ep != 'Z' && *ep != '\0')
return 0;
if (errno != 0)
return 0;
fdigs = ep - p;
if (fdigs > 15) {
/* avoid being absurd */
/* don't want to risk 10^fdigs being INF */
if (val == 0)
fdigs = 1;
else while (fdigs > 15) {
val = (val + 5) / 10;
fdigs--;
}
}
/*
* Determine whether 2 struct tn's are different
* return true (1) if theu are, false (0) otherwise.
*
* Note that we only consider the fields that are set
* for mktime() to use - if mktime() returns them
* differently than was set, then there was a problem
* with the setting.
*/
static int
difftm(const struct tm *t1, const struct tm *t2)
{
#define CHK(fld) do { \
if (t1->tm_##fld != t2->tm_##fld) { \
return 1; \
} \
} while(/*CONSTCOND*/0)