ReactOS  0.4.14-dev-317-g96040ec
touch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1993 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  * This product includes software developed by the University of
16  * California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * 7/20/97 - Ted Felix <tfelix@fred.net>
34  * Ported to Win32 from bsd4.3-reno at wuarchive.
35  * Biggest problems were err() routines, utines, and
36  * gettimeofday(). All easily fixed.
37  */
38 
39 #ifndef lint
40 char copyright[] =
41 "@(#) Copyright (c) 1993 Regents of the University of California.\n\
42  All rights reserved.\n";
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <time.h>
48 
49 //#ifdef __STDC__
50 //#error "__STDC__ defined"
51 //#endif
52 
53 #include <sys/utime.h>
54 #include <io.h>
55 #include <fcntl.h>
56 #include <getopt.h>
57 #include <various.h>
58 #define DEFFILEMODE S_IWRITE
59 
60 #include <err.h>
61 #include <errno.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <time.h>
66 #include <unistd.h>
67 
68 char *__progname; /* Program name, from crt0. */
69 
70 /* Prototypes */
71 int rw __P((char *, struct stat *, int));
72 void stime_arg1 __P((char *, time_t *));
73 void stime_arg2 __P((char *, int, time_t *));
74 void stime_file __P((char *, time_t *));
75 void usage __P((void));
76 
77 int
78 main(int argc, char *argv[])
79 {
80  struct stat sb;
81  time_t tv[2];
82  int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
83  char *p;
84  struct utimbuf utb;
85 
86  __progname = argv[0];
87 
88  aflag = cflag = fflag = mflag = timeset = 0;
89  time(&tv[0]);
90 
91  while ((ch = getopt(argc, argv, "acfmr:t:")) != EOF)
92  switch(ch) {
93  case 'a':
94  aflag = 1;
95  break;
96  case 'c':
97  cflag = 1;
98  break;
99  case 'f':
100  fflag = 1;
101  break;
102  case 'm':
103  mflag = 1;
104  break;
105  case 'r':
106  timeset = 1;
107  stime_file(optarg, tv);
108  break;
109  case 't':
110  timeset = 1;
111  stime_arg1(optarg, tv);
112  break;
113  case '?':
114  default:
115  usage();
116  }
117  argc -= optind;
118  argv += optind;
119 
120  /* Default is both -a and -m. */
121  if (aflag == 0 && mflag == 0)
122  aflag = mflag = 1;
123 
124  /*
125  * If no -r or -t flag, at least two operands, the first of which
126  * is an 8 or 10 digit number, use the obsolete time specification.
127  */
128  if (!timeset && argc > 1) {
129  (void)strtol(argv[0], &p, 10);
130  len = p - argv[0];
131  if (*p == '\0' && (len == 8 || len == 10)) {
132  timeset = 1;
133  stime_arg2(argv[0], len == 10, tv);
134  }
135  }
136 
137  /* Otherwise use the current time of day. */
138  if (!timeset)
139  tv[1] = tv[0];
140 
141  if (*argv == NULL)
142  usage();
143 
144  for (rval = 0; *argv; ++argv) {
145  /* See if the file exists. */
146  if (stat(*argv, &sb)) {
147  if (!cflag) {
148  /* Create the file. */
149  fd = _open(*argv,
151  if (fd == -1 || fstat(fd, &sb) || _close(fd)) {
152  rval = 1;
153  warn("%s", *argv);
154  continue;
155  }
156 
157  /* If using the current time, we're done. */
158  if (!timeset)
159  continue;
160  } else
161  continue;
162  }
163 
164  if (!aflag)
165  tv[0] = sb.st_atime;
166  if (!mflag)
167  tv[1] = sb.st_mtime;
168 
169  /* Try utime. */
170  utb.actime = tv[0];
171  utb.modtime = tv[1];
172  if (!utime(*argv, &utb))
173  continue;
174 
175  /* If the user specified a time, nothing else we can do. */
176  if (timeset) {
177  rval = 1;
178  warn("%s", *argv);
179  }
180 
181  /*
182  * System V and POSIX 1003.1 require that a NULL argument
183  * set the access/modification times to the current time.
184  * The permission checks are different, too, in that the
185  * ability to write the file is sufficient. Take a shot.
186  */
187  if (!utime(*argv, NULL))
188  continue;
189 
190  /* Try reading/writing. */
191  if (rw(*argv, &sb, fflag))
192  rval = 1;
193  }
194  return rval;
195 }
196 
197 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
198 
199 void
200 stime_arg1(char *arg, time_t *tvp)
201 {
202  struct tm *t;
203  int yearset;
204  char *p;
205  /* Start with the current time. */
206  if ((t = localtime(&tvp[0])) == NULL)
207  err(1, "localtime");
208  /* [[CC]YY]MMDDhhmm[.SS] */
209  if ((p = strchr(arg, '.')) == NULL)
210  t->tm_sec = 0; /* Seconds defaults to 0. */
211  else {
212  if (strlen(p + 1) != 2)
213  goto terr;
214  *p++ = '\0';
215  t->tm_sec = ATOI2(p);
216  }
217 
218  yearset = 0;
219  switch(strlen(arg)) {
220  case 12: /* CCYYMMDDhhmm */
221  t->tm_year = ATOI2(arg);
222  t->tm_year *= 1000;
223  yearset = 1;
224  /* FALLTHOUGH */
225  case 10: /* YYMMDDhhmm */
226  if (yearset) {
227  yearset = ATOI2(arg);
228  t->tm_year += yearset;
229  } else {
230  yearset = ATOI2(arg);
231  if (yearset < 69)
232  t->tm_year = yearset + 2000;
233  else
234  t->tm_year = yearset + 1900;
235  }
236  t->tm_year -= 1900; /* Convert to UNIX time. */
237  /* FALLTHROUGH */
238  case 8: /* MMDDhhmm */
239  t->tm_mon = ATOI2(arg);
240  --t->tm_mon; /* Convert from 01-12 to 00-11 */
241  t->tm_mday = ATOI2(arg);
242  t->tm_hour = ATOI2(arg);
243  t->tm_min = ATOI2(arg);
244  break;
245  default:
246  goto terr;
247  }
248 
249  t->tm_isdst = -1; /* Figure out DST. */
250  tvp[0] = tvp[1] = mktime(t);
251  if (tvp[0] == -1)
252 terr: errx(1,
253  "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
254 }
255 
256 void
257 stime_arg2(char *arg, int year, time_t *tvp)
258 {
259  struct tm *t;
260  /* Start with the current time. */
261  if ((t = localtime(&tvp[0])) == NULL)
262  err(1, "localtime");
263 
264  t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
265  --t->tm_mon; /* Convert from 01-12 to 00-11 */
266  t->tm_mday = ATOI2(arg);
267  t->tm_hour = ATOI2(arg);
268  t->tm_min = ATOI2(arg);
269  if (year)
270  t->tm_year = ATOI2(arg);
271 
272  t->tm_isdst = -1; /* Figure out DST. */
273  tvp[0] = tvp[1] = mktime(t);
274  if (tvp[0] == -1)
275  errx(1,
276  "out of range or illegal time specification: MMDDhhmm[yy]");
277 }
278 
279 void
280 stime_file(char *fname, time_t *tvp)
281 {
282  struct stat sb;
283 
284  if (stat(fname, &sb))
285  err(1, "%s", fname);
286  tvp[0] = sb.st_atime;
287  tvp[1] = sb.st_mtime;
288 }
289 
290 int
291 rw(char *fname, struct stat *sbp, int force)
292 {
293  int fd, needed_chmod, rval;
294  u_char byte;
295 
296  /* Try regular files and directories. */
297  if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
298  warnx("%s: %s", fname, strerror(0));
299  return (1);
300  }
301 
302  needed_chmod = rval = 0;
303  if ((fd = _open(fname, O_RDWR, 0)) == -1) {
304  if (!force || _chmod(fname, DEFFILEMODE))
305  goto err;
306  if ((fd = _open(fname, O_RDWR, 0)) == -1)
307  goto err;
308  needed_chmod = 1;
309  }
310 
311  if (sbp->st_size != 0) {
312  if (_read(fd, &byte, sizeof(byte)) != sizeof(byte))
313  goto err;
314  if (_lseek(fd, (off_t)0, SEEK_SET) == -1)
315  goto err;
316  if (_write(fd, &byte, sizeof(byte)) != sizeof(byte))
317  goto err;
318  } else {
319  if (_write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
320 err: rval = 1;
321  warn("%s", fname);
322 /* } else if (ftruncate(fd, (off_t)0)) {*/
323  } else if (_chsize(fd, (off_t)0)) {
324  rval = 1;
325  warn("%s: file modified", fname);
326  }
327  }
328 
329  if (_close(fd) && rval != 1) {
330  rval = 1;
331  warn("%s", fname);
332  }
333  if (needed_chmod && _chmod(fname, sbp->st_mode) && rval != 1) {
334  rval = 1;
335  warn("%s: permissions modified", fname);
336  }
337  return (rval);
338 }
339 
340 /*__dead void*/
341 void
342 usage()
343 {
345  "usage: touch [-acfm] [-r file] [-t time] file ...\n");
346  exit(1);
347 }
static int argc
Definition: ServiceArgs.c:12
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define warn(...)
_CRTIMP int __cdecl _write(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount)
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
superblock * sb
Definition: btrfs.c:4137
int optind
Definition: getopt.c:47
__kernel_off_t off_t
Definition: linux.h:201
GLdouble GLdouble t
Definition: gl.h:2047
#define warnx(...)
static int fd
Definition: io.c:51
__u16 time
Definition: mkdosfs.c:366
int mflag
Definition: finger.c:66
#define argv
Definition: mplay32.c:18
const char * strerror(int err)
Definition: compat_str.c:23
int rw __P((char *, struct stat *, int))
__CRT_INLINE int __cdecl utime(const char *_Filename, struct utimbuf *_Utimbuf)
Definition: utime.h:136
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define O_WRONLY
Definition: acwin.h:111
smooth NULL
Definition: ftsmooth.c:416
char copyright[]
Definition: touch.c:40
time_t actime
Definition: syshdrs.h:132
_Check_return_opt_ _CRTIMP int __cdecl _close(_In_ int _FileHandle)
_Check_return_ _CRTIMP int __cdecl _chmod(_In_z_ const char *_Filename, _In_ int _Mode)
int main(int argc, char *argv[])
Definition: touch.c:78
#define SEEK_SET
Definition: jmemansi.c:26
_Check_return_ _CRTIMP int __cdecl _chsize(_In_ int _FileHandle, _In_ long _Size)
#define S_ISDIR(mode)
Definition: various.h:18
#define errx(...)
_Check_return_ _CRTIMP int __cdecl _read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:55
int rw
float rval
Definition: cylfrac.c:48
unsigned short st_mode
Definition: stat.h:58
GLsizeiptr const GLvoid GLenum usage
Definition: glext.h:5919
Definition: stat.h:55
GLenum GLsizei len
Definition: glext.h:6722
time_t modtime
Definition: syshdrs.h:132
Definition: time.h:76
#define err(...)
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:424
#define ATOI2(ar)
Definition: touch.c:197
_CRTIMP int __cdecl stat(const char *_Filename, struct stat *_Stat)
Definition: stat.h:345
void stime_arg1(char *arg, time_t *tvp)
Definition: touch.c:200
__kernel_time_t time_t
Definition: linux.h:252
#define byte(x, n)
Definition: tomcrypt.h:118
#define O_RDWR
Definition: fcntl.h:36
const char * optarg
Definition: getopt.c:49
_CRTIMP int __cdecl _open(const char *_Filename, int _OpenFlag,...)
Definition: file.c:2001
_CRTIMP time_t __cdecl mktime(struct tm *_Tm)
Definition: time.h:426
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define EOF
Definition: stdio.h:24
_Check_return_ long __cdecl strtol(_In_z_ const char *_Str, _Out_opt_ _Deref_post_z_ char **_EndPtr, _In_ int _Radix)
_CRTIMP int __cdecl fstat(int _Desc, struct stat *_Stat)
Definition: stat.h:341
FILE * stderr
_Check_return_opt_ _CRTIMP long __cdecl _lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
void exit(int exitcode)
Definition: _exit.c:33
GLfloat GLfloat p
Definition: glext.h:8902
char * __progname
Definition: touch.c:68
UCHAR u_char
Definition: types.h:80
#define DEFFILEMODE
Definition: touch.c:58
#define S_ISREG(mode)
Definition: various.h:17
#define O_CREAT
Definition: acwin.h:110
_off_t st_size
Definition: stat.h:63