2002-04-03 03:57:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1993 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 7/20/97 - Ted Felix <tfelix@fred.net>
|
|
|
|
* Ported to Win32 from bsd4.3-reno at wuarchive.
|
|
|
|
* Biggest problems were err() routines, utines, and
|
|
|
|
* gettimeofday(). All easily fixed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
char copyright[] =
|
|
|
|
"@(#) Copyright (c) 1993 Regents of the University of California.\n\
|
|
|
|
All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2008-12-21 18:01:57 +00:00
|
|
|
#include <time.h>
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
//#ifdef __STDC__
|
|
|
|
//#error "__STDC__ defined"
|
|
|
|
//#endif
|
|
|
|
|
2002-09-03 18:44:19 +00:00
|
|
|
#include <sys/utime.h>
|
2002-04-03 03:57:56 +00:00
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <various.h>
|
|
|
|
#define DEFFILEMODE S_IWRITE
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
char *__progname; /* Program name, from crt0. */
|
|
|
|
|
|
|
|
/* Prototypes */
|
|
|
|
int rw __P((char *, struct stat *, int));
|
|
|
|
void stime_arg1 __P((char *, time_t *));
|
|
|
|
void stime_arg2 __P((char *, int, time_t *));
|
|
|
|
void stime_file __P((char *, time_t *));
|
|
|
|
void usage __P((void));
|
|
|
|
|
2005-01-25 05:11:17 +00:00
|
|
|
int
|
2002-04-03 03:57:56 +00:00
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
time_t tv[2];
|
|
|
|
int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
|
|
|
|
char *p;
|
|
|
|
struct utimbuf utb;
|
|
|
|
|
|
|
|
__progname = argv[0];
|
|
|
|
|
|
|
|
aflag = cflag = fflag = mflag = timeset = 0;
|
|
|
|
time(&tv[0]);
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "acfmr:t:")) != EOF)
|
|
|
|
switch(ch) {
|
|
|
|
case 'a':
|
2005-09-02 21:11:17 +00:00
|
|
|
aflag = 1;
|
2002-04-03 03:57:56 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2005-09-02 21:11:17 +00:00
|
|
|
cflag = 1;
|
2002-04-03 03:57:56 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2005-09-02 21:11:17 +00:00
|
|
|
fflag = 1;
|
2002-04-03 03:57:56 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
2005-09-02 21:11:17 +00:00
|
|
|
mflag = 1;
|
2002-04-03 03:57:56 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
timeset = 1;
|
2005-09-02 21:11:17 +00:00
|
|
|
stime_file(optarg, tv);
|
2002-04-03 03:57:56 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
timeset = 1;
|
2005-09-02 21:11:17 +00:00
|
|
|
stime_arg1(optarg, tv);
|
2002-04-03 03:57:56 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2005-09-02 21:11:17 +00:00
|
|
|
usage();
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* Default is both -a and -m. */
|
|
|
|
if (aflag == 0 && mflag == 0)
|
2005-09-02 21:11:17 +00:00
|
|
|
aflag = mflag = 1;
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2005-09-02 21:11:17 +00:00
|
|
|
stime_arg2(argv[0], len == 10, tv);
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise use the current time of day. */
|
|
|
|
if (!timeset)
|
2005-09-02 21:11:17 +00:00
|
|
|
tv[1] = tv[0];
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
if (*argv == NULL)
|
2005-09-02 21:11:17 +00:00
|
|
|
usage();
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
for (rval = 0; *argv; ++argv) {
|
|
|
|
/* See if the file exists. */
|
2005-09-02 21:11:17 +00:00
|
|
|
if (stat(*argv, &sb)) {
|
2002-04-03 03:57:56 +00:00
|
|
|
if (!cflag) {
|
|
|
|
/* Create the file. */
|
2009-11-11 18:19:42 +00:00
|
|
|
fd = _open(*argv,
|
2002-04-03 03:57:56 +00:00
|
|
|
O_WRONLY | O_CREAT, DEFFILEMODE);
|
2009-11-11 18:19:42 +00:00
|
|
|
if (fd == -1 || fstat(fd, &sb) || _close(fd)) {
|
2002-04-03 03:57:56 +00:00
|
|
|
rval = 1;
|
2005-09-02 21:11:17 +00:00
|
|
|
warn("%s", *argv);
|
|
|
|
continue;
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If using the current time, we're done. */
|
|
|
|
if (!timeset)
|
2005-09-02 21:11:17 +00:00
|
|
|
continue;
|
2002-04-03 03:57:56 +00:00
|
|
|
} else
|
2005-09-02 21:11:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
if (!aflag)
|
2005-09-02 21:11:17 +00:00
|
|
|
tv[0] = sb.st_atime;
|
2002-04-03 03:57:56 +00:00
|
|
|
if (!mflag)
|
2005-09-02 21:11:17 +00:00
|
|
|
tv[1] = sb.st_mtime;
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
/* Try utime. */
|
2005-09-02 21:11:17 +00:00
|
|
|
utb.actime = tv[0];
|
|
|
|
utb.modtime = tv[1];
|
2002-04-03 03:57:56 +00:00
|
|
|
if (!utime(*argv, &utb))
|
2005-09-02 21:11:17 +00:00
|
|
|
continue;
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
/* If the user specified a time, nothing else we can do. */
|
|
|
|
if (timeset) {
|
2005-09-02 21:11:17 +00:00
|
|
|
rval = 1;
|
|
|
|
warn("%s", *argv);
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 (!utime(*argv, NULL))
|
2005-09-02 21:11:17 +00:00
|
|
|
continue;
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
/* Try reading/writing. */
|
|
|
|
if (rw(*argv, &sb, fflag))
|
2005-09-02 21:11:17 +00:00
|
|
|
rval = 1;
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
2005-09-02 21:11:17 +00:00
|
|
|
return rval;
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
|
|
|
|
|
|
|
|
void
|
|
|
|
stime_arg1(char *arg, time_t *tvp)
|
|
|
|
{
|
|
|
|
struct tm *t;
|
|
|
|
int yearset;
|
2005-05-07 21:24:31 +00:00
|
|
|
char *p;
|
2002-04-03 03:57:56 +00:00
|
|
|
/* Start with the current time. */
|
|
|
|
if ((t = localtime(&tvp[0])) == NULL)
|
2005-09-02 21:11:17 +00:00
|
|
|
err(1, "localtime");
|
2002-04-03 03:57:56 +00:00
|
|
|
/* [[CC]YY]MMDDhhmm[.SS] */
|
|
|
|
if ((p = strchr(arg, '.')) == NULL)
|
|
|
|
t->tm_sec = 0; /* Seconds defaults to 0. */
|
|
|
|
else {
|
|
|
|
if (strlen(p + 1) != 2)
|
|
|
|
goto terr;
|
|
|
|
*p++ = '\0';
|
|
|
|
t->tm_sec = ATOI2(p);
|
|
|
|
}
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2002-04-03 03:57:56 +00:00
|
|
|
yearset = 0;
|
|
|
|
switch(strlen(arg)) {
|
|
|
|
case 12: /* CCYYMMDDhhmm */
|
|
|
|
t->tm_year = ATOI2(arg);
|
|
|
|
t->tm_year *= 1000;
|
|
|
|
yearset = 1;
|
|
|
|
/* FALLTHOUGH */
|
|
|
|
case 10: /* YYMMDDhhmm */
|
|
|
|
if (yearset) {
|
|
|
|
yearset = ATOI2(arg);
|
|
|
|
t->tm_year += yearset;
|
|
|
|
} else {
|
|
|
|
yearset = ATOI2(arg);
|
|
|
|
if (yearset < 69)
|
|
|
|
t->tm_year = yearset + 2000;
|
|
|
|
else
|
|
|
|
t->tm_year = yearset + 1900;
|
|
|
|
}
|
|
|
|
t->tm_year -= 1900; /* Convert to UNIX time. */
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 8: /* MMDDhhmm */
|
|
|
|
t->tm_mon = ATOI2(arg);
|
|
|
|
--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);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto terr;
|
|
|
|
}
|
|
|
|
|
|
|
|
t->tm_isdst = -1; /* Figure out DST. */
|
|
|
|
tvp[0] = tvp[1] = mktime(t);
|
|
|
|
if (tvp[0] == -1)
|
|
|
|
terr: errx(1,
|
|
|
|
"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stime_arg2(char *arg, int year, time_t *tvp)
|
|
|
|
{
|
|
|
|
struct tm *t;
|
|
|
|
/* Start with the current time. */
|
|
|
|
if ((t = localtime(&tvp[0])) == NULL)
|
|
|
|
err(1, "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)
|
|
|
|
t->tm_year = ATOI2(arg);
|
|
|
|
|
|
|
|
t->tm_isdst = -1; /* Figure out DST. */
|
|
|
|
tvp[0] = tvp[1] = mktime(t);
|
|
|
|
if (tvp[0] == -1)
|
|
|
|
errx(1,
|
|
|
|
"out of range or illegal time specification: MMDDhhmm[yy]");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stime_file(char *fname, time_t *tvp)
|
|
|
|
{
|
2005-09-02 21:11:17 +00:00
|
|
|
struct stat sb;
|
2002-04-03 03:57:56 +00:00
|
|
|
|
|
|
|
if (stat(fname, &sb))
|
2005-09-02 21:11:17 +00:00
|
|
|
err(1, "%s", fname);
|
|
|
|
tvp[0] = sb.st_atime;
|
|
|
|
tvp[1] = sb.st_mtime;
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rw(char *fname, struct stat *sbp, int force)
|
|
|
|
{
|
|
|
|
int fd, needed_chmod, rval;
|
|
|
|
u_char byte;
|
|
|
|
|
|
|
|
/* Try regular files and directories. */
|
|
|
|
if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
|
2005-09-02 21:11:17 +00:00
|
|
|
warnx("%s: %s", fname, strerror(0));
|
2002-04-03 03:57:56 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
needed_chmod = rval = 0;
|
2009-11-11 18:19:42 +00:00
|
|
|
if ((fd = _open(fname, O_RDWR, 0)) == -1) {
|
|
|
|
if (!force || _chmod(fname, DEFFILEMODE))
|
2002-04-03 03:57:56 +00:00
|
|
|
goto err;
|
2009-11-11 18:19:42 +00:00
|
|
|
if ((fd = _open(fname, O_RDWR, 0)) == -1)
|
2005-09-02 21:11:17 +00:00
|
|
|
goto err;
|
|
|
|
needed_chmod = 1;
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sbp->st_size != 0) {
|
2009-11-11 18:19:42 +00:00
|
|
|
if (_read(fd, &byte, sizeof(byte)) != sizeof(byte))
|
2005-09-02 21:11:17 +00:00
|
|
|
goto err;
|
2009-11-11 18:19:42 +00:00
|
|
|
if (_lseek(fd, (off_t)0, SEEK_SET) == -1)
|
2005-09-02 21:11:17 +00:00
|
|
|
goto err;
|
2009-11-11 18:19:42 +00:00
|
|
|
if (_write(fd, &byte, sizeof(byte)) != sizeof(byte))
|
2005-09-02 21:11:17 +00:00
|
|
|
goto err;
|
2002-04-03 03:57:56 +00:00
|
|
|
} else {
|
2009-11-11 18:19:42 +00:00
|
|
|
if (_write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
|
2005-09-02 21:11:17 +00:00
|
|
|
err: rval = 1;
|
2002-04-03 03:57:56 +00:00
|
|
|
warn("%s", fname);
|
|
|
|
/* } else if (ftruncate(fd, (off_t)0)) {*/
|
2009-11-11 18:19:42 +00:00
|
|
|
} else if (_chsize(fd, (off_t)0)) {
|
2005-09-02 21:11:17 +00:00
|
|
|
rval = 1;
|
|
|
|
warn("%s: file modified", fname);
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-11 18:19:42 +00:00
|
|
|
if (_close(fd) && rval != 1) {
|
2005-09-02 21:11:17 +00:00
|
|
|
rval = 1;
|
|
|
|
warn("%s", fname);
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
2009-11-11 18:19:42 +00:00
|
|
|
if (needed_chmod && _chmod(fname, sbp->st_mode) && rval != 1) {
|
2002-04-03 03:57:56 +00:00
|
|
|
rval = 1;
|
2005-09-02 21:11:17 +00:00
|
|
|
warn("%s: permissions modified", fname);
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
2005-09-02 21:11:17 +00:00
|
|
|
return (rval);
|
2002-04-03 03:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*__dead void*/
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"usage: touch [-acfm] [-r file] [-t time] file ...\n");
|
|
|
|
exit(1);
|
|
|
|
}
|