1/* 2 * Copyright (c) 1993 3 * The Regents of the University of California. 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 34#ifndef lint 35static char copyright[] = 36"@(#) Copyright (c) 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93"; 42#endif /* not lint */ 43 44#include <sys/types.h> 45#include <sys/stat.h> 46#include <sys/time.h> 47 48#include <err.h> 49#include <errno.h> 50#include <fcntl.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <time.h> 55#include <unistd.h> 56 57int rw __P((char *, struct stat *, int)); 58void stime_arg1 __P((char *, struct timeval *)); 59void stime_arg2 __P((char *, int, struct timeval *)); 60void stime_file __P((char *, struct timeval *)); 61void usage __P((void)); 62 63int 64main(argc, argv) 65 int argc; 66 char *argv[]; 67{ 68 struct stat sb; 69 struct timeval tv[2]; 70 int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; 71 char *p; 72 73 aflag = cflag = fflag = mflag = timeset = 0; 74 if (gettimeofday(&tv[0], NULL)) 75 err(1, "gettimeofday"); 76 77 while ((ch = getopt(argc, argv, "acfmr:t:")) != -1) 78 switch(ch) { 79 case 'a': 80 aflag = 1; 81 break; 82 case 'c': 83 cflag = 1; 84 break; 85 case 'f': 86 fflag = 1; 87 break; 88 case 'm': 89 mflag = 1; 90 break; 91 case 'r': 92 timeset = 1; 93 stime_file(optarg, tv); 94 break; 95 case 't': 96 timeset = 1; 97 stime_arg1(optarg, tv); 98 break; 99 case '?': 100 default: 101 usage(); 102 } 103 argc -= optind; 104 argv += optind; 105 106 /* Default is both -a and -m. */ 107 if (aflag == 0 && mflag == 0) 108 aflag = mflag = 1; 109 110 /* 111 * If no -r or -t flag, at least two operands, the first of which 112 * is an 8 or 10 digit number, use the obsolete time specification. 113 */ 114 if (!timeset && argc > 1) { 115 (void)strtol(argv[0], &p, 10); 116 len = p - argv[0]; 117 if (*p == '\0' && (len == 8 || len == 10)) { 118 timeset = 1; 119 stime_arg2(*argv++, len == 10, tv); 120 } 121 } 122 123 /* Otherwise use the current time of day. */ 124 if (!timeset) 125 tv[1] = tv[0]; 126 127 if (*argv == NULL) 128 usage(); 129 130 for (rval = 0; *argv; ++argv) { 131 /* See if the file exists. */ 132 if (stat(*argv, &sb)) 133 if (!cflag) { 134 /* Create the file. */ 135 fd = open(*argv, 136 O_WRONLY | O_CREAT, DEFFILEMODE); 137 if (fd == -1 || fstat(fd, &sb) || close(fd)) { 138 rval = 1; 139 warn("%s", *argv); 140 continue; 141 } 142 143 /* If using the current time, we're done. */ 144 if (!timeset) 145 continue; 146 } else 147 continue; 148 149 if (!aflag) 150 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 151 if (!mflag) 152 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 153 154 /* Try utimes(2). */ 155 if (!utimes(*argv, tv)) 156 continue; 157 158 /* If the user specified a time, nothing else we can do. */ 159 if (timeset) { 160 rval = 1; 161 warn("%s", *argv); 162 } 163 164 /* 165 * System V and POSIX 1003.1 require that a NULL argument 166 * set the access/modification times to the current time. 167 * The permission checks are different, too, in that the 168 * ability to write the file is sufficient. Take a shot. 169 */ 170 if (!utimes(*argv, NULL)) 171 continue; 172 173 /* Try reading/writing. */ 174 if (rw(*argv, &sb, fflag)) 175 rval = 1; 176 } 177 exit(rval); 178} 179 180#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 181 182void 183stime_arg1(arg, tvp) 184 char *arg; 185 struct timeval *tvp; 186{ 187 time_t now; 188 struct tm *t; 189 int yearset; 190 char *p; 191 /* Start with the current time. */ 192 now = tvp[0].tv_sec; 193 if ((t = localtime(&now)) == NULL) 194 err(1, "localtime"); 195 /* [[CC]YY]MMDDhhmm[.SS] */ 196 if ((p = strchr(arg, '.')) == NULL) 197 t->tm_sec = 0; /* Seconds defaults to 0. */ 198 else { 199 if (strlen(p + 1) != 2) 200 goto terr; 201 *p++ = '\0'; 202 t->tm_sec = ATOI2(p); 203 } 204 205 yearset = 0; 206 switch(strlen(arg)) { 207 case 12: /* CCYYMMDDhhmm */ 208 t->tm_year = ATOI2(arg); 209 t->tm_year *= 100; 210 yearset = 1; 211 /* FALLTHOUGH */ 212 case 10: /* YYMMDDhhmm */ 213 if (yearset) { 214 yearset = ATOI2(arg); 215 t->tm_year += yearset; 216 } else { 217 yearset = ATOI2(arg); 218 if (yearset < 69) 219 t->tm_year = yearset + 2000; 220 else 221 t->tm_year = yearset + 1900; 222 } 223 t->tm_year -= 1900; /* Convert to UNIX time. */ 224 /* FALLTHROUGH */ 225 case 8: /* MMDDhhmm */ 226 t->tm_mon = ATOI2(arg); 227 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 228 t->tm_mday = ATOI2(arg); 229 t->tm_hour = ATOI2(arg); 230 t->tm_min = ATOI2(arg); 231 break; 232 default: 233 goto terr; 234 } 235 236 t->tm_isdst = -1; /* Figure out DST. */ 237 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 238 if (tvp[0].tv_sec == -1) 239terr: errx(1, 240 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 241 242 tvp[0].tv_usec = tvp[1].tv_usec = 0; 243} 244 245void 246stime_arg2(arg, year, tvp) 247 char *arg; 248 int year; 249 struct timeval *tvp; 250{ 251 time_t now; 252 struct tm *t; 253 /* Start with the current time. */ 254 now = tvp[0].tv_sec; 255 if ((t = localtime(&now)) == NULL) 256 err(1, "localtime"); 257 258 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */ 259 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 260 t->tm_mday = ATOI2(arg); 261 t->tm_hour = ATOI2(arg); 262 t->tm_min = ATOI2(arg);
| 1/* 2 * Copyright (c) 1993 3 * The Regents of the University of California. 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 34#ifndef lint 35static char copyright[] = 36"@(#) Copyright (c) 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93"; 42#endif /* not lint */ 43 44#include <sys/types.h> 45#include <sys/stat.h> 46#include <sys/time.h> 47 48#include <err.h> 49#include <errno.h> 50#include <fcntl.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <time.h> 55#include <unistd.h> 56 57int rw __P((char *, struct stat *, int)); 58void stime_arg1 __P((char *, struct timeval *)); 59void stime_arg2 __P((char *, int, struct timeval *)); 60void stime_file __P((char *, struct timeval *)); 61void usage __P((void)); 62 63int 64main(argc, argv) 65 int argc; 66 char *argv[]; 67{ 68 struct stat sb; 69 struct timeval tv[2]; 70 int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; 71 char *p; 72 73 aflag = cflag = fflag = mflag = timeset = 0; 74 if (gettimeofday(&tv[0], NULL)) 75 err(1, "gettimeofday"); 76 77 while ((ch = getopt(argc, argv, "acfmr:t:")) != -1) 78 switch(ch) { 79 case 'a': 80 aflag = 1; 81 break; 82 case 'c': 83 cflag = 1; 84 break; 85 case 'f': 86 fflag = 1; 87 break; 88 case 'm': 89 mflag = 1; 90 break; 91 case 'r': 92 timeset = 1; 93 stime_file(optarg, tv); 94 break; 95 case 't': 96 timeset = 1; 97 stime_arg1(optarg, tv); 98 break; 99 case '?': 100 default: 101 usage(); 102 } 103 argc -= optind; 104 argv += optind; 105 106 /* Default is both -a and -m. */ 107 if (aflag == 0 && mflag == 0) 108 aflag = mflag = 1; 109 110 /* 111 * If no -r or -t flag, at least two operands, the first of which 112 * is an 8 or 10 digit number, use the obsolete time specification. 113 */ 114 if (!timeset && argc > 1) { 115 (void)strtol(argv[0], &p, 10); 116 len = p - argv[0]; 117 if (*p == '\0' && (len == 8 || len == 10)) { 118 timeset = 1; 119 stime_arg2(*argv++, len == 10, tv); 120 } 121 } 122 123 /* Otherwise use the current time of day. */ 124 if (!timeset) 125 tv[1] = tv[0]; 126 127 if (*argv == NULL) 128 usage(); 129 130 for (rval = 0; *argv; ++argv) { 131 /* See if the file exists. */ 132 if (stat(*argv, &sb)) 133 if (!cflag) { 134 /* Create the file. */ 135 fd = open(*argv, 136 O_WRONLY | O_CREAT, DEFFILEMODE); 137 if (fd == -1 || fstat(fd, &sb) || close(fd)) { 138 rval = 1; 139 warn("%s", *argv); 140 continue; 141 } 142 143 /* If using the current time, we're done. */ 144 if (!timeset) 145 continue; 146 } else 147 continue; 148 149 if (!aflag) 150 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 151 if (!mflag) 152 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 153 154 /* Try utimes(2). */ 155 if (!utimes(*argv, tv)) 156 continue; 157 158 /* If the user specified a time, nothing else we can do. */ 159 if (timeset) { 160 rval = 1; 161 warn("%s", *argv); 162 } 163 164 /* 165 * System V and POSIX 1003.1 require that a NULL argument 166 * set the access/modification times to the current time. 167 * The permission checks are different, too, in that the 168 * ability to write the file is sufficient. Take a shot. 169 */ 170 if (!utimes(*argv, NULL)) 171 continue; 172 173 /* Try reading/writing. */ 174 if (rw(*argv, &sb, fflag)) 175 rval = 1; 176 } 177 exit(rval); 178} 179 180#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 181 182void 183stime_arg1(arg, tvp) 184 char *arg; 185 struct timeval *tvp; 186{ 187 time_t now; 188 struct tm *t; 189 int yearset; 190 char *p; 191 /* Start with the current time. */ 192 now = tvp[0].tv_sec; 193 if ((t = localtime(&now)) == NULL) 194 err(1, "localtime"); 195 /* [[CC]YY]MMDDhhmm[.SS] */ 196 if ((p = strchr(arg, '.')) == NULL) 197 t->tm_sec = 0; /* Seconds defaults to 0. */ 198 else { 199 if (strlen(p + 1) != 2) 200 goto terr; 201 *p++ = '\0'; 202 t->tm_sec = ATOI2(p); 203 } 204 205 yearset = 0; 206 switch(strlen(arg)) { 207 case 12: /* CCYYMMDDhhmm */ 208 t->tm_year = ATOI2(arg); 209 t->tm_year *= 100; 210 yearset = 1; 211 /* FALLTHOUGH */ 212 case 10: /* YYMMDDhhmm */ 213 if (yearset) { 214 yearset = ATOI2(arg); 215 t->tm_year += yearset; 216 } else { 217 yearset = ATOI2(arg); 218 if (yearset < 69) 219 t->tm_year = yearset + 2000; 220 else 221 t->tm_year = yearset + 1900; 222 } 223 t->tm_year -= 1900; /* Convert to UNIX time. */ 224 /* FALLTHROUGH */ 225 case 8: /* MMDDhhmm */ 226 t->tm_mon = ATOI2(arg); 227 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 228 t->tm_mday = ATOI2(arg); 229 t->tm_hour = ATOI2(arg); 230 t->tm_min = ATOI2(arg); 231 break; 232 default: 233 goto terr; 234 } 235 236 t->tm_isdst = -1; /* Figure out DST. */ 237 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 238 if (tvp[0].tv_sec == -1) 239terr: errx(1, 240 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 241 242 tvp[0].tv_usec = tvp[1].tv_usec = 0; 243} 244 245void 246stime_arg2(arg, year, tvp) 247 char *arg; 248 int year; 249 struct timeval *tvp; 250{ 251 time_t now; 252 struct tm *t; 253 /* Start with the current time. */ 254 now = tvp[0].tv_sec; 255 if ((t = localtime(&now)) == NULL) 256 err(1, "localtime"); 257 258 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */ 259 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 260 t->tm_mday = ATOI2(arg); 261 t->tm_hour = ATOI2(arg); 262 t->tm_min = ATOI2(arg);
|
265 266 t->tm_isdst = -1; /* Figure out DST. */ 267 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 268 if (tvp[0].tv_sec == -1) 269 errx(1, 270 "out of range or illegal time specification: MMDDhhmm[yy]"); 271 272 tvp[0].tv_usec = tvp[1].tv_usec = 0; 273} 274 275void 276stime_file(fname, tvp) 277 char *fname; 278 struct timeval *tvp; 279{ 280 struct stat sb; 281 282 if (stat(fname, &sb)) 283 err(1, "%s", fname); 284 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); 285 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); 286} 287 288int 289rw(fname, sbp, force) 290 char *fname; 291 struct stat *sbp; 292 int force; 293{ 294 int fd, needed_chmod, rval; 295 u_char byte; 296 297 /* Try regular files and directories. */ 298 if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) { 299 warnx("%s: %s", fname, strerror(EFTYPE)); 300 return (1); 301 } 302 303 needed_chmod = rval = 0; 304 if ((fd = open(fname, O_RDWR, 0)) == -1) { 305 if (!force || chmod(fname, DEFFILEMODE)) 306 goto err; 307 if ((fd = open(fname, O_RDWR, 0)) == -1) 308 goto err; 309 needed_chmod = 1; 310 } 311 312 if (sbp->st_size != 0) { 313 if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) 314 goto err; 315 if (lseek(fd, (off_t)0, SEEK_SET) == -1) 316 goto err; 317 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) 318 goto err; 319 } else { 320 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { 321err: rval = 1; 322 warn("%s", fname); 323 } else if (ftruncate(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 340void 341usage() 342{ 343 (void)fprintf(stderr, 344 "usage: touch [-acfm] [-r file] [-t time] file ...\n"); 345 exit(1); 346}
| 268 269 t->tm_isdst = -1; /* Figure out DST. */ 270 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 271 if (tvp[0].tv_sec == -1) 272 errx(1, 273 "out of range or illegal time specification: MMDDhhmm[yy]"); 274 275 tvp[0].tv_usec = tvp[1].tv_usec = 0; 276} 277 278void 279stime_file(fname, tvp) 280 char *fname; 281 struct timeval *tvp; 282{ 283 struct stat sb; 284 285 if (stat(fname, &sb)) 286 err(1, "%s", fname); 287 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); 288 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); 289} 290 291int 292rw(fname, sbp, force) 293 char *fname; 294 struct stat *sbp; 295 int force; 296{ 297 int fd, needed_chmod, rval; 298 u_char byte; 299 300 /* Try regular files and directories. */ 301 if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) { 302 warnx("%s: %s", fname, strerror(EFTYPE)); 303 return (1); 304 } 305 306 needed_chmod = rval = 0; 307 if ((fd = open(fname, O_RDWR, 0)) == -1) { 308 if (!force || chmod(fname, DEFFILEMODE)) 309 goto err; 310 if ((fd = open(fname, O_RDWR, 0)) == -1) 311 goto err; 312 needed_chmod = 1; 313 } 314 315 if (sbp->st_size != 0) { 316 if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) 317 goto err; 318 if (lseek(fd, (off_t)0, SEEK_SET) == -1) 319 goto err; 320 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) 321 goto err; 322 } else { 323 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { 324err: rval = 1; 325 warn("%s", fname); 326 } else if (ftruncate(fd, (off_t)0)) { 327 rval = 1; 328 warn("%s: file modified", fname); 329 } 330 } 331 332 if (close(fd) && rval != 1) { 333 rval = 1; 334 warn("%s", fname); 335 } 336 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) { 337 rval = 1; 338 warn("%s: permissions modified", fname); 339 } 340 return (rval); 341} 342 343void 344usage() 345{ 346 (void)fprintf(stderr, 347 "usage: touch [-acfm] [-r file] [-t time] file ...\n"); 348 exit(1); 349}
|