17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*eaacacc0SMilan Jurik * Common Development and Distribution License (the "License"). 6*eaacacc0SMilan Jurik * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*eaacacc0SMilan Jurik * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 307c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/stat.h> 347c478bd9Sstevel@tonic-gate #include <stdio.h> 357c478bd9Sstevel@tonic-gate #include <string.h> 367c478bd9Sstevel@tonic-gate #include <strings.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <ctype.h> 397c478bd9Sstevel@tonic-gate #include <libgen.h> 407c478bd9Sstevel@tonic-gate #include <fcntl.h> 417c478bd9Sstevel@tonic-gate #include <pwd.h> 427c478bd9Sstevel@tonic-gate #include <time.h> 437c478bd9Sstevel@tonic-gate #include <unistd.h> 447c478bd9Sstevel@tonic-gate #include <locale.h> 457c478bd9Sstevel@tonic-gate #include <sys/time.h> 467c478bd9Sstevel@tonic-gate #include <errno.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #define BADTIME "bad time specification" 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate static char *myname; 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate static int isnumber(char *); 537c478bd9Sstevel@tonic-gate static int atoi_for2(char *); 547c478bd9Sstevel@tonic-gate static void usage(const int); 557c478bd9Sstevel@tonic-gate static void touchabort(const char *); 567c478bd9Sstevel@tonic-gate static void parse_time(char *, timestruc_t *); 577c478bd9Sstevel@tonic-gate static void parse_datetime(char *, timestruc_t *); 587c478bd9Sstevel@tonic-gate static void timestruc_to_timeval(timestruc_t *, struct timeval *); 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate int 617c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 627c478bd9Sstevel@tonic-gate { 637c478bd9Sstevel@tonic-gate int c; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate int aflag = 0; 667c478bd9Sstevel@tonic-gate int cflag = 0; 677c478bd9Sstevel@tonic-gate int rflag = 0; 687c478bd9Sstevel@tonic-gate int mflag = 0; 697c478bd9Sstevel@tonic-gate int tflag = 0; 707c478bd9Sstevel@tonic-gate int stflag = 0; 717c478bd9Sstevel@tonic-gate int status = 0; 727c478bd9Sstevel@tonic-gate int usecurrenttime = 1; 737c478bd9Sstevel@tonic-gate int timespecified; 747c478bd9Sstevel@tonic-gate int optc; 757c478bd9Sstevel@tonic-gate int fd; 767c478bd9Sstevel@tonic-gate struct stat stbuf; 777c478bd9Sstevel@tonic-gate struct stat prstbuf; 787c478bd9Sstevel@tonic-gate struct timeval times[2]; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 817c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 827c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 837c478bd9Sstevel@tonic-gate #endif 847c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate myname = basename(argv[0]); 877c478bd9Sstevel@tonic-gate if (strcmp(myname, "settime") == NULL) { 887c478bd9Sstevel@tonic-gate cflag++; 897c478bd9Sstevel@tonic-gate stflag++; 907c478bd9Sstevel@tonic-gate while ((optc = getopt(argc, argv, "f:")) != EOF) 917c478bd9Sstevel@tonic-gate switch (optc) { 927c478bd9Sstevel@tonic-gate case 'f': 937c478bd9Sstevel@tonic-gate rflag++; 947c478bd9Sstevel@tonic-gate usecurrenttime = 0; 957c478bd9Sstevel@tonic-gate if (stat(optarg, &prstbuf) == -1) { 967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", myname); 977c478bd9Sstevel@tonic-gate perror(optarg); 987c478bd9Sstevel@tonic-gate return (2); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate break; 1017c478bd9Sstevel@tonic-gate case '?': 1027c478bd9Sstevel@tonic-gate usage(stflag); 1037c478bd9Sstevel@tonic-gate break; 1047c478bd9Sstevel@tonic-gate }; 1057c478bd9Sstevel@tonic-gate } else 1067c478bd9Sstevel@tonic-gate while ((optc = getopt(argc, argv, "acfmr:t:")) != EOF) 1077c478bd9Sstevel@tonic-gate switch (optc) { 1087c478bd9Sstevel@tonic-gate case 'a': 1097c478bd9Sstevel@tonic-gate aflag++; 1107c478bd9Sstevel@tonic-gate usecurrenttime = 0; 1117c478bd9Sstevel@tonic-gate break; 1127c478bd9Sstevel@tonic-gate case 'c': 1137c478bd9Sstevel@tonic-gate cflag++; 1147c478bd9Sstevel@tonic-gate break; 1157c478bd9Sstevel@tonic-gate case 'f': /* silently ignore for UCB compat */ 1167c478bd9Sstevel@tonic-gate break; 1177c478bd9Sstevel@tonic-gate case 'm': 1187c478bd9Sstevel@tonic-gate mflag++; 1197c478bd9Sstevel@tonic-gate usecurrenttime = 0; 1207c478bd9Sstevel@tonic-gate break; 1217c478bd9Sstevel@tonic-gate case 'r': /* same as settime's -f option */ 1227c478bd9Sstevel@tonic-gate rflag++; 1237c478bd9Sstevel@tonic-gate usecurrenttime = 0; 1247c478bd9Sstevel@tonic-gate if (stat(optarg, &prstbuf) == -1) { 1257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", myname); 1267c478bd9Sstevel@tonic-gate perror(optarg); 1277c478bd9Sstevel@tonic-gate return (2); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate break; 1307c478bd9Sstevel@tonic-gate case 't': 1317c478bd9Sstevel@tonic-gate tflag++; 1327c478bd9Sstevel@tonic-gate usecurrenttime = 0; 1337c478bd9Sstevel@tonic-gate parse_time(optarg, &prstbuf.st_mtim); 1347c478bd9Sstevel@tonic-gate prstbuf.st_atim = prstbuf.st_mtim; 1357c478bd9Sstevel@tonic-gate break; 1367c478bd9Sstevel@tonic-gate case '?': 1377c478bd9Sstevel@tonic-gate usage(stflag); 1387c478bd9Sstevel@tonic-gate break; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate argc -= optind; 1427c478bd9Sstevel@tonic-gate argv += optind; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate if ((argc < 1) || (rflag + tflag > 1)) 1457c478bd9Sstevel@tonic-gate usage(stflag); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if ((aflag == 0) && (mflag == 0)) { 1487c478bd9Sstevel@tonic-gate aflag = 1; 1497c478bd9Sstevel@tonic-gate mflag = 1; 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * If either -r or -t has been specified, 1547c478bd9Sstevel@tonic-gate * use the specified time. 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate timespecified = rflag || tflag; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (timespecified == 0) { 1597c478bd9Sstevel@tonic-gate if (argc >= 2 && isnumber(*argv) && (strlen(*argv) == 8 || 1607c478bd9Sstevel@tonic-gate strlen(*argv) == 10)) { 1617c478bd9Sstevel@tonic-gate /* 1627c478bd9Sstevel@tonic-gate * time is specified as an operand. 1637c478bd9Sstevel@tonic-gate * use it. 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate parse_datetime(*argv++, &prstbuf.st_mtim); 1667c478bd9Sstevel@tonic-gate prstbuf.st_atim = prstbuf.st_mtim; 1677c478bd9Sstevel@tonic-gate usecurrenttime = 0; 1687c478bd9Sstevel@tonic-gate timespecified = 1; 1697c478bd9Sstevel@tonic-gate argc--; 1707c478bd9Sstevel@tonic-gate } else { 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * no time information is specified. 1737c478bd9Sstevel@tonic-gate * use the current time. 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate (void) gettimeofday(times, NULL); 1767c478bd9Sstevel@tonic-gate times[1] = times[0]; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate for (c = 0; c < argc; c++) { 1807c478bd9Sstevel@tonic-gate if (stat(argv[c], &stbuf)) { 1817c478bd9Sstevel@tonic-gate /* 182*eaacacc0SMilan Jurik * If stat failed for reasons other than EOVERFLOW or 183*eaacacc0SMilan Jurik * ENOENT, the file should not be created, since this 184*eaacacc0SMilan Jurik * can clobber the contents of an existing file. 1857c478bd9Sstevel@tonic-gate */ 186*eaacacc0SMilan Jurik if (errno == EOVERFLOW) { 187*eaacacc0SMilan Jurik if (aflag == 0 || mflag == 0) { 1887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 189*eaacacc0SMilan Jurik gettext("%s: %s: current timestamps" 190*eaacacc0SMilan Jurik " unavailable:\n%s"), 191*eaacacc0SMilan Jurik myname, argv[c], aflag > 0 ? 192*eaacacc0SMilan Jurik gettext("consider trying again" 193*eaacacc0SMilan Jurik " without -a option\n") : 194*eaacacc0SMilan Jurik gettext("consider trying again" 195*eaacacc0SMilan Jurik " without -m option\n")); 196*eaacacc0SMilan Jurik status++; 197*eaacacc0SMilan Jurik continue; 198*eaacacc0SMilan Jurik } 199*eaacacc0SMilan Jurik /* 200*eaacacc0SMilan Jurik * Since we have EOVERFLOW, we know the file 201*eaacacc0SMilan Jurik * exists. Since both atime and mtime are being 202*eaacacc0SMilan Jurik * changed to known values, we don't care that 203*eaacacc0SMilan Jurik * st_atime and st_mtime from the file aren't 204*eaacacc0SMilan Jurik * available. 205*eaacacc0SMilan Jurik */ 206*eaacacc0SMilan Jurik } else if (errno != ENOENT) { 207*eaacacc0SMilan Jurik (void) fprintf(stderr, 208*eaacacc0SMilan Jurik gettext("%s: cannot stat %s: %s\n"), 209*eaacacc0SMilan Jurik myname, argv[c], strerror(errno)); 2107c478bd9Sstevel@tonic-gate status++; 2117c478bd9Sstevel@tonic-gate continue; 2127c478bd9Sstevel@tonic-gate } else if (cflag) { 2137c478bd9Sstevel@tonic-gate continue; 2147c478bd9Sstevel@tonic-gate } else if ((fd = creat(argv[c], 2157c478bd9Sstevel@tonic-gate (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH))) 2167c478bd9Sstevel@tonic-gate < 0) { 2177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 218*eaacacc0SMilan Jurik gettext("%s: cannot create %s: %s\n"), 219*eaacacc0SMilan Jurik myname, argv[c], strerror(errno)); 2207c478bd9Sstevel@tonic-gate status++; 2217c478bd9Sstevel@tonic-gate continue; 2227c478bd9Sstevel@tonic-gate } else { 2237c478bd9Sstevel@tonic-gate (void) close(fd); 2247c478bd9Sstevel@tonic-gate if (stat(argv[c], &stbuf)) { 2257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 226*eaacacc0SMilan Jurik gettext("%s: cannot stat %s: %s\n"), 227*eaacacc0SMilan Jurik myname, argv[c], strerror(errno)); 2287c478bd9Sstevel@tonic-gate status++; 2297c478bd9Sstevel@tonic-gate continue; 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if (mflag == 0) { 2357c478bd9Sstevel@tonic-gate /* Keep the mtime of the file */ 2367c478bd9Sstevel@tonic-gate timestruc_to_timeval(&stbuf.st_mtim, times + 1); 2377c478bd9Sstevel@tonic-gate } else { 2387c478bd9Sstevel@tonic-gate if (timespecified) { 2397c478bd9Sstevel@tonic-gate /* Set the specified time */ 2407c478bd9Sstevel@tonic-gate timestruc_to_timeval(&prstbuf.st_mtim, 2417c478bd9Sstevel@tonic-gate times + 1); 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate /* Otherwise, use the current time by gettimeofday */ 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate if (aflag == 0) { 2477c478bd9Sstevel@tonic-gate /* Keep the atime of the file */ 2487c478bd9Sstevel@tonic-gate timestruc_to_timeval(&stbuf.st_atim, times); 2497c478bd9Sstevel@tonic-gate } else { 2507c478bd9Sstevel@tonic-gate if (timespecified) { 2517c478bd9Sstevel@tonic-gate /* Set the specified time */ 2527c478bd9Sstevel@tonic-gate timestruc_to_timeval(&prstbuf.st_atim, times); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate /* Otherwise, use the current time by gettimeofday */ 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if (utimes(argv[c], (usecurrenttime) ? NULL : times)) { 2587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 259*eaacacc0SMilan Jurik gettext("%s: cannot change times on %s: %s\n"), 260*eaacacc0SMilan Jurik myname, argv[c], strerror(errno)); 2617c478bd9Sstevel@tonic-gate status++; 2627c478bd9Sstevel@tonic-gate continue; 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate return (status); 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate static int 2697c478bd9Sstevel@tonic-gate isnumber(char *s) 2707c478bd9Sstevel@tonic-gate { 2717c478bd9Sstevel@tonic-gate int c; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate while ((c = *s++) != '\0') 2747c478bd9Sstevel@tonic-gate if (!isdigit(c)) 2757c478bd9Sstevel@tonic-gate return (0); 2767c478bd9Sstevel@tonic-gate return (1); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate static void 2817c478bd9Sstevel@tonic-gate parse_time(char *t, timestruc_t *ts) 2827c478bd9Sstevel@tonic-gate { 2837c478bd9Sstevel@tonic-gate int century = 0; 2847c478bd9Sstevel@tonic-gate int seconds = 0; 2857c478bd9Sstevel@tonic-gate char *p; 2867c478bd9Sstevel@tonic-gate time_t when; 2877c478bd9Sstevel@tonic-gate struct tm tm; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /* 2907c478bd9Sstevel@tonic-gate * time in the following format (defined by the touch(1) spec): 2917c478bd9Sstevel@tonic-gate * [[CC]YY]MMDDhhmm[.SS] 2927c478bd9Sstevel@tonic-gate */ 2937c478bd9Sstevel@tonic-gate if ((p = strchr(t, '.')) != NULL) { 2947c478bd9Sstevel@tonic-gate if (strchr(p+1, '.') != NULL) 2957c478bd9Sstevel@tonic-gate touchabort(BADTIME); 2967c478bd9Sstevel@tonic-gate seconds = atoi_for2(p+1); 2977c478bd9Sstevel@tonic-gate *p = '\0'; 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate (void) memset(&tm, 0, sizeof (struct tm)); 3017c478bd9Sstevel@tonic-gate when = time(0); 3027c478bd9Sstevel@tonic-gate tm.tm_year = localtime(&when)->tm_year; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate switch (strlen(t)) { 3057c478bd9Sstevel@tonic-gate case 12: /* CCYYMMDDhhmm */ 3067c478bd9Sstevel@tonic-gate century = atoi_for2(t); 3077c478bd9Sstevel@tonic-gate t += 2; 3087c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 3097c478bd9Sstevel@tonic-gate case 10: /* YYMMDDhhmm */ 3107c478bd9Sstevel@tonic-gate tm.tm_year = atoi_for2(t); 3117c478bd9Sstevel@tonic-gate t += 2; 3127c478bd9Sstevel@tonic-gate if (century == 0) { 3137c478bd9Sstevel@tonic-gate if (tm.tm_year < 69) 3147c478bd9Sstevel@tonic-gate tm.tm_year += 100; 3157c478bd9Sstevel@tonic-gate } else 3167c478bd9Sstevel@tonic-gate tm.tm_year += (century - 19) * 100; 3177c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 3187c478bd9Sstevel@tonic-gate case 8: /* MMDDhhmm */ 3197c478bd9Sstevel@tonic-gate tm.tm_mon = atoi_for2(t) - 1; 3207c478bd9Sstevel@tonic-gate t += 2; 3217c478bd9Sstevel@tonic-gate tm.tm_mday = atoi_for2(t); 3227c478bd9Sstevel@tonic-gate t += 2; 3237c478bd9Sstevel@tonic-gate tm.tm_hour = atoi_for2(t); 3247c478bd9Sstevel@tonic-gate t += 2; 3257c478bd9Sstevel@tonic-gate tm.tm_min = atoi_for2(t); 3267c478bd9Sstevel@tonic-gate tm.tm_sec = seconds; 3277c478bd9Sstevel@tonic-gate break; 3287c478bd9Sstevel@tonic-gate default: 3297c478bd9Sstevel@tonic-gate touchabort(BADTIME); 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate if ((when = mktime(&tm)) == -1) 3337c478bd9Sstevel@tonic-gate touchabort(BADTIME); 3347c478bd9Sstevel@tonic-gate if (tm.tm_isdst) 3357c478bd9Sstevel@tonic-gate when -= (timezone-altzone); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate ts->tv_sec = when; 3387c478bd9Sstevel@tonic-gate ts->tv_nsec = 0; 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate static void 3427c478bd9Sstevel@tonic-gate parse_datetime(char *t, timestruc_t *ts) 3437c478bd9Sstevel@tonic-gate { 3447c478bd9Sstevel@tonic-gate time_t when; 3457c478bd9Sstevel@tonic-gate struct tm tm; 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * time in the following format (defined by the touch(1) spec): 3497c478bd9Sstevel@tonic-gate * MMDDhhmm[yy] 3507c478bd9Sstevel@tonic-gate */ 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate (void) memset(&tm, 0, sizeof (struct tm)); 3537c478bd9Sstevel@tonic-gate when = time(0); 3547c478bd9Sstevel@tonic-gate tm.tm_year = localtime(&when)->tm_year; 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate switch (strlen(t)) { 3577c478bd9Sstevel@tonic-gate case 10: /* MMDDhhmmyy */ 3587c478bd9Sstevel@tonic-gate tm.tm_year = atoi_for2(t+8); 3597c478bd9Sstevel@tonic-gate if (tm.tm_year < 69) 3607c478bd9Sstevel@tonic-gate tm.tm_year += 100; 3617c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 3627c478bd9Sstevel@tonic-gate case 8: /* MMDDhhmm */ 3637c478bd9Sstevel@tonic-gate tm.tm_mon = atoi_for2(t) - 1; 3647c478bd9Sstevel@tonic-gate t += 2; 3657c478bd9Sstevel@tonic-gate tm.tm_mday = atoi_for2(t); 3667c478bd9Sstevel@tonic-gate t += 2; 3677c478bd9Sstevel@tonic-gate tm.tm_hour = atoi_for2(t); 3687c478bd9Sstevel@tonic-gate t += 2; 3697c478bd9Sstevel@tonic-gate tm.tm_min = atoi_for2(t); 3707c478bd9Sstevel@tonic-gate break; 3717c478bd9Sstevel@tonic-gate default: 3727c478bd9Sstevel@tonic-gate touchabort(BADTIME); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate if ((when = mktime(&tm)) == -1) 3767c478bd9Sstevel@tonic-gate touchabort(BADTIME); 3777c478bd9Sstevel@tonic-gate if (tm.tm_isdst) 3787c478bd9Sstevel@tonic-gate when -= (timezone - altzone); 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate ts->tv_sec = when; 3817c478bd9Sstevel@tonic-gate ts->tv_nsec = 0; 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate static int 3857c478bd9Sstevel@tonic-gate atoi_for2(char *p) 3867c478bd9Sstevel@tonic-gate { 3877c478bd9Sstevel@tonic-gate int value; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate value = (*p - '0') * 10 + *(p+1) - '0'; 3907c478bd9Sstevel@tonic-gate if ((value < 0) || (value > 99)) 3917c478bd9Sstevel@tonic-gate touchabort(BADTIME); 3927c478bd9Sstevel@tonic-gate return (value); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate static void 3967c478bd9Sstevel@tonic-gate touchabort(const char *message) 3977c478bd9Sstevel@tonic-gate { 3987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", myname, gettext(message)); 3997c478bd9Sstevel@tonic-gate exit(1); 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate static void 4037c478bd9Sstevel@tonic-gate usage(const int settime) 4047c478bd9Sstevel@tonic-gate { 4057c478bd9Sstevel@tonic-gate if (settime) 4067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 407*eaacacc0SMilan Jurik "usage: %s [-f file] [mmddhhmm[yy]] file...\n"), myname); 4087c478bd9Sstevel@tonic-gate else 4097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4107c478bd9Sstevel@tonic-gate "usage: %s [-acm] [-r ref_file] file...\n" 4117c478bd9Sstevel@tonic-gate " %s [-acm] [MMDDhhmm[yy]] file...\n" 4127c478bd9Sstevel@tonic-gate " %s [-acm] [-t [[CC]YY]MMDDhhmm[.SS]] file...\n"), 4137c478bd9Sstevel@tonic-gate myname, myname, myname); 4147c478bd9Sstevel@tonic-gate exit(2); 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate /* 4187c478bd9Sstevel@tonic-gate * nanoseconds are rounded off to microseconds by flooring. 4197c478bd9Sstevel@tonic-gate */ 4207c478bd9Sstevel@tonic-gate static void 4217c478bd9Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv) 4227c478bd9Sstevel@tonic-gate { 4237c478bd9Sstevel@tonic-gate tv->tv_sec = ts->tv_sec; 4247c478bd9Sstevel@tonic-gate tv->tv_usec = ts->tv_nsec / 1000; 4257c478bd9Sstevel@tonic-gate } 426