summaryrefslogtreecommitdiff
path: root/usr/src/ucbcmd/touch/touch.c
blob: d60e6067e1beae6f3d8091db0b67ab983be79847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef lint
#pragma ident	"%Z%%M%	%I%	%E% SMI"
#endif

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>

#define	isleap(y) (((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0)

struct	stat	stbuf;
int	status;
#ifdef S5EMUL
int dmsize[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
#endif /* S5EMUL */

static char usage[] =
#ifdef S5EMUL
		"[-amc] [mmddhhmm[yy]]";
#else /*!S5EMUL*/
		"[-amcf]";
int	force = 0;
int	nowrite;
#endif /*!S5EMUL*/

int	mflg=1, aflg=1, cflg=0, nflg=0;
char	*prog;

#ifdef S5EMUL
char	*cbp;
#endif /* S5EMUL */
time_t	time();
off_t	lseek();
time_t	timelocal(), timegm();
struct timeval timbuf;
static void timestruc_to_timeval(timestruc_t *, struct timeval *);

#ifdef S5EMUL
struct tm *
gtime()
{
	static struct tm newtime;
	long nt;

	newtime.tm_mon = gpair() - 1;
	newtime.tm_mday = gpair();
	newtime.tm_hour = gpair();
	if (newtime.tm_hour == 24) {
		newtime.tm_hour = 0;
		newtime.tm_mday++;
	}
	newtime.tm_min = gpair();
	newtime.tm_sec = 0;
	newtime.tm_year = gpair();
	if (newtime.tm_year < 0) {
		(void) time(&nt);
		newtime.tm_year = localtime(&nt)->tm_year;
	}
	return (&newtime);
}

gpair()
{
	int c, d;
	char *cp;

	cp = cbp;
	if (*cp == 0)
		return (-1);
	c = (*cp++ - '0') * 10;
	if (c<0 || c>100)
		return (-1);
	if (*cp == 0)
		return (-1);
	if ((d = *cp++ - '0') < 0 || d > 9)
		return (-1);
	cbp = cp;
	return (c+d);
}
#endif /*S5EMUL*/

int
main(int argc, char *argv[])
{
	int c;
#ifdef S5EMUL
	int days_in_month;
	struct tm *tp;
#endif /* S5EMUL */

	int errflg=0, optc;
	extern char *optarg;
	extern int optind;
	extern int opterr;

	prog = argv[0];
	opterr = 0;			/* disable getopt() error msgs */
	while ((optc=getopt(argc, argv, "amcf")) != EOF)
		switch (optc) {
		case 'm':
			mflg++;
			aflg--;
			break;
		case 'a':
			aflg++;
			mflg--;
			break;
		case 'c':
			cflg++;
			break;
#ifndef S5EMUL
		case 'f':
			force++;	/* SysV version ignores -f */
			break;
#endif /*!S5EMUL*/
		case '?':
			errflg++;
		}

	if (((argc-optind) < 1) || errflg) {
		(void) fprintf(stderr, "usage: %s %s file ...\n", prog, usage);
		exit(2);
	}
	status = 0;

#ifdef S5EMUL
	if (!isnumber(argv[optind])) {	/* BSD version only sets Present */
#endif /*S5EMUL*/
		if ((aflg <= 0) || (mflg <= 0))
			(void) gettimeofday(&timbuf, NULL);
		else
			nflg++;		/* no -a, -m, or date seen */
#ifdef S5EMUL
	} else {			/* SysV version sets arbitrary date */
		cbp = (char *)argv[optind++];
		if ((tp = gtime()) == NULL) {
			(void) fprintf(stderr, "%s: bad date conversion\n",
			    prog);
			exit(2);
		}
		days_in_month = dmsize[tp->tm_mon];
		if (tp->tm_mon == 1 && isleap(tp->tm_year + 1900))
			days_in_month = 29;	/* February in leap year */
		if (tp->tm_mon < 0 || tp->tm_mon > 11 ||
		    tp->tm_mday < 1 || tp->tm_mday > days_in_month ||
		    tp->tm_hour < 0 || tp->tm_hour > 23 ||
		    tp->tm_min < 0 || tp->tm_min > 59 ||
		    tp->tm_sec < 0 || tp->tm_sec > 59) {
			(void) fprintf(stderr, "%s: bad date conversion\n",
			    prog);
			exit(2);
		}
		timbuf = timelocal(tp);
	}
#endif /*S5EMUL*/

	for (c = optind; c < argc; c++) {
		if (touch(argv[c]) < 0)
			status++;
	}
	return (status);
}

int
touch(filename)
	char *filename;
{
	struct timeval times[2];
	int fd;

	if (stat(filename, &stbuf)) {
		/*
		 * if stat failed for reasons other than ENOENT,
		 * the file should not be created, since this
		 * can clobber the contents of an existing file
		 * (for example, a large file that results in overflow).
		 */
		if (errno != ENOENT) {
			(void) fprintf(stderr,"%s: cannot stat ", prog);
			perror(filename);
			return (-1);
		} else if (cflg) {
			return (-1);
		}
		else if ((fd = creat(filename, 0666)) < 0) {
			(void) fprintf(stderr, "%s: cannot create ", prog);
			perror(filename);
			return (-1);
		}
		else {
			(void) close(fd);
			if (stat(filename, &stbuf)) {
				(void) fprintf(stderr,"%s: cannot stat ", prog);
				perror(filename);
				return (-1);
			}
		}
		if (nflg)
			return (0);
	}

	times[0] = times[1] = timbuf;
	if (mflg <= 0)
		timestruc_to_timeval(&stbuf.st_mtim, times + 1);
	if (aflg <= 0)
		timestruc_to_timeval(&stbuf.st_atim, times);

#ifndef S5EMUL
	/*
	 * Since utime() allows the owner to change file times without
	 * regard to access permission, enforce BSD semantics here
	 * (cannot touch if read-only and not -f).
	 */
	nowrite = access(filename, R_OK|W_OK);
	if (nowrite && !force) {
		(void) fprintf(stderr,
		    "%s: cannot touch %s: no write permission\n",
		    prog, filename);
		return (-1);
	}
#endif /*!S5EMUL*/

	if (utimes(filename, nflg ? NULL : times)) {
		if (nflg && (errno != EROFS) && (errno != EACCES)) {
			/*
			 * If utime() failed to set the Present, it
			 * could be a BSD server that is complaining.
			 * If that's the case, try the old read/write trick.
			 */
			return (oldtouch(filename, &stbuf));
		}
		(void) fprintf(stderr,"%s: cannot change times on ", prog);
		perror(filename);
		return (-1);
	}
	return (0);
}

int
oldtouch(filename, statp)
	char *filename;
	struct stat *statp;
{
	int rwstatus;

	if ((statp->st_mode & S_IFMT) != S_IFREG) {
		(void) fprintf(stderr,
		    "%s: %s: only owner may touch special files on this filesystem\n",
		    prog, filename);
		return (-1);
	}

#ifndef S5EMUL
	if (nowrite && force) {
		if (chmod(filename, 0666)) {
			fprintf(stderr, "%s: could not chmod ", prog);
			perror(filename);
			return (-1);
		}
		rwstatus = readwrite(filename, statp->st_size);
		if (chmod(filename, (int)statp->st_mode)) {
			fprintf(stderr, "%s: could not chmod back ", prog);
			perror(filename);
			return (-1);
		}
		return (rwstatus);
	} else
#endif /*!S5EMUL*/
		return (readwrite(filename, statp->st_size));
}

int
readwrite(filename, size)
	char	*filename;
	off_t	size;
{
	int fd;
	char first;

	if (size) {
		if ((fd = open(filename, 2)) < 0)
			goto error;
		if (read(fd, &first, 1) != 1)
			goto closeerror;
		if (lseek(fd, 0L, 0) == -1)
			goto closeerror;
		if (write(fd, &first, 1) != 1)
			goto closeerror;
	} else {
		if ((fd = creat(filename, 0666)) < 0)
			goto error;
	}
	if (close(fd) < 0)
		goto error;
	return (0);

closeerror:
	(void) close(fd);
error:
	(void) fprintf(stderr, "%s: could not touch ", prog);
	perror(filename);
	return (-1);
}

#ifdef S5EMUL
isnumber(s)
	char *s;
{
	char c;

	while (c = *s++)
		if (!isdigit(c))
			return (0);
	return (1);
}
#endif /* S5EMUL */

/*
 * nanoseconds are rounded off to microseconds by flooring.
 */
static void
timestruc_to_timeval(timestruc_t *ts, struct timeval *tv)
{
	tv->tv_sec = ts->tv_sec;
	tv->tv_usec = ts->tv_nsec / 1000;
}