ReactOS 0.4.15-dev-7934-g1dc8d80
touch.c File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/utime.h>
#include <io.h>
#include <fcntl.h>
#include <getopt.h>
#include <various.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Include dependency graph for touch.c:

Go to the source code of this file.

Macros

#define DEFFILEMODE   S_IWRITE
 
#define ATOI2(ar)   ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
 

Functions

int rw __P ((char *, struct stat *, int))
 
void stime_arg1 __P ((char *, time_t *))
 
void stime_arg2 __P ((char *, int, time_t *))
 
void usage __P ((void))
 
int main (int argc, char *argv[])
 
void stime_arg1 (char *arg, time_t *tvp)
 
void stime_arg2 (char *arg, int year, time_t *tvp)
 
void stime_file (char *fname, time_t *tvp)
 
int rw (char *fname, struct stat *sbp, int force)
 
void usage ()
 

Variables

char copyright []
 
char__progname
 

Macro Definition Documentation

◆ ATOI2

#define ATOI2 (   ar)    ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;

Definition at line 197 of file touch.c.

◆ DEFFILEMODE

#define DEFFILEMODE   S_IWRITE

Definition at line 58 of file touch.c.

Function Documentation

◆ __P() [1/4]

void stime_arg2 __P ( (char *, int, time_t *)  )

◆ __P() [2/4]

int rw __P ( (char *, struct stat *, int )

◆ __P() [3/4]

void stime_file __P ( (char *, time_t *)  )

◆ __P() [4/4]

void usage __P ( (void )

◆ main()

int main ( int argc  ,
char argv[] 
)

Definition at line 78 of file touch.c.

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}
static int argc
Definition: ServiceArgs.c:12
#define O_WRONLY
Definition: acwin.h:111
#define stat
Definition: acwin.h:99
#define O_CREAT
Definition: acwin.h:110
#define fstat
Definition: acwin.h:100
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:55
const char * optarg
Definition: getopt.c:49
int optind
Definition: getopt.c:47
float rval
Definition: cylfrac.c:48
#define NULL
Definition: types.h:112
superblock * sb
Definition: btrfs.c:4261
__kernel_time_t time_t
Definition: linux.h:252
int mflag
Definition: finger.c:66
#define utime
Definition: getline.c:47
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
#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)
__u16 time
Definition: mkdosfs.c:8
#define argv
Definition: mplay32.c:18
#define warn(...)
#define rw
Definition: rosglue.h:38
_Check_return_opt_ _CRTIMP int __cdecl _close(_In_ int _FileHandle)
_CRTIMP int __cdecl _open(const char *_Filename, int _OpenFlag,...)
Definition: file.c:2001
static int fd
Definition: io.c:51
Definition: stat.h:55
void usage()
Definition: touch.c:342
void stime_arg1(char *arg, time_t *tvp)
Definition: touch.c:200
void stime_file(char *fname, time_t *tvp)
Definition: touch.c:280
char * __progname
Definition: touch.c:68
void stime_arg2(char *arg, int year, time_t *tvp)
Definition: touch.c:257
#define DEFFILEMODE
Definition: touch.c:58

◆ rw()

int rw ( char fname,
struct stat sbp,
int  force 
)

Definition at line 291 of file touch.c.

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)) {
320err: 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}
#define S_ISDIR(mode)
Definition: various.h:18
#define S_ISREG(mode)
Definition: various.h:17
UCHAR u_char
Definition: types.h:80
#define byte(x, n)
Definition: tomcrypt.h:118
__kernel_off_t off_t
Definition: linux.h:201
#define O_RDWR
Definition: fcntl.h:36
#define SEEK_SET
Definition: jmemansi.c:26
const char * strerror(int err)
Definition: compat_str.c:23
#define err(...)
#define warnx(...)
_Check_return_opt_ _CRTIMP long __cdecl _lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
_Check_return_ _CRTIMP int __cdecl _chmod(_In_z_ const char *_Filename, _In_ int _Mode)
_CRTIMP int __cdecl _write(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount)
_Check_return_ _CRTIMP int __cdecl _read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
_Check_return_ _CRTIMP int __cdecl _chsize(_In_ int _FileHandle, _In_ long _Size)
unsigned short st_mode
Definition: stat.h:58
_off_t st_size
Definition: stat.h:63

◆ stime_arg1()

void stime_arg1 ( char arg,
time_t tvp 
)

Definition at line 200 of file touch.c.

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)
252terr: errx(1,
253 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
254}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strchr(const char *String, int ch)
Definition: utclib.c:501
GLdouble GLdouble t
Definition: gl.h:2047
#define errx(...)
_CRTIMP time_t __cdecl mktime(struct tm *_Tm)
Definition: time.h:418
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:416
Definition: time.h:68
#define ATOI2(ar)
Definition: touch.c:197

Referenced by main().

◆ stime_arg2()

void stime_arg2 ( char arg,
int  year,
time_t tvp 
)

Definition at line 257 of file touch.c.

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}

Referenced by main().

◆ stime_file()

void stime_file ( char fname,
time_t tvp 
)

Definition at line 280 of file touch.c.

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}

Referenced by main().

◆ usage()

void usage ( void  )

Definition at line 342 of file touch.c.

343{
345 "usage: touch [-acfm] [-r file] [-t time] file ...\n");
346 exit(1);
347}
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define exit(n)
Definition: config.h:202

Referenced by main().

Variable Documentation

◆ __progname

char* __progname

Definition at line 68 of file touch.c.

Referenced by main(), verr(), verrx(), vwarn(), and vwarnx().

◆ copyright

char copyright[]
Initial value:
=
"@(#) Copyright (c) 1993 Regents of the University of California.\n\
All rights reserved.\n"

Definition at line 40 of file touch.c.