touch.c (3731B)
1 /* see LICENSE file for copyright and license details */ 2 3 #if FEATURE_TOUCH_EXT 4 #ifndef _DEFAULT_SOURCE 5 #define _DEFAULT_SOURCE 6 #endif 7 #ifndef _XOPEN_SOURCE 8 #define _XOPEN_SOURCE 700 9 #endif 10 #endif 11 #include <sys/stat.h> 12 13 #include <errno.h> 14 #include <fcntl.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <time.h> 18 #include <unistd.h> 19 20 #include "util.h" 21 22 static int aflag; 23 static int cflag; 24 static int mflag; 25 static struct timespec times[2] = {{.tv_nsec = UTIME_NOW}}; 26 27 static void 28 touch(const char *file) 29 { 30 int fd, ret; 31 32 if (utimensat(AT_FDCWD, file, times, 0) == 0) 33 return; 34 if (errno != ENOENT) 35 eprintf("utimensat %s:", file); 36 if (cflag) 37 return; 38 if ((fd = open(file, O_WRONLY | O_CREAT, 0666)) < 0) 39 eprintf("open %s:", file); 40 ret = futimens(fd, times); 41 close(fd); 42 if (ret < 0) 43 eprintf("futimens %s:", file); 44 } 45 46 static time_t 47 parsetime(char *str) 48 { 49 time_t now; 50 struct tm *cur, t = {0}; 51 int zulu = 0; 52 char *format; 53 size_t len = strlen(str); 54 55 if ((now = time(NULL)) == -1) 56 eprintf("time:"); 57 if (!(cur = localtime(&now))) 58 eprintf("localtime:"); 59 t.tm_isdst = -1; 60 61 switch (len) { 62 /* -t flag argument */ 63 case 8: 64 t.tm_year = cur->tm_year; 65 format = "%m%d%H%M"; 66 break; 67 case 10: 68 format = "%y%m%d%H%M"; 69 break; 70 case 11: 71 t.tm_year = cur->tm_year; 72 format = "%m%d%H%M.%S"; 73 break; 74 case 12: 75 format = "%Y%m%d%H%M"; 76 break; 77 case 13: 78 format = "%y%m%d%H%M.%S"; 79 break; 80 case 15: 81 format = "%Y%m%d%H%M.%S"; 82 break; 83 /* -d flag argument */ 84 case 19: 85 format = "%Y-%m-%dT%H:%M:%S"; 86 break; 87 case 20: 88 /* only zulu-timezone supported */ 89 if (str[19] != 'Z') 90 eprintf("Invalid time zone\n"); 91 str[19] = 0; 92 zulu = 1; 93 format = "%Y-%m-%dT%H:%M:%S"; 94 break; 95 default: 96 eprintf("Invalid date format length\n", str); 97 } 98 99 if (!strptime(str, format, &t)) 100 eprintf("strptime %s: Invalid date format\n", str); 101 if (zulu) { 102 t.tm_hour += t.tm_gmtoff / 60; 103 t.tm_gmtoff = 0; 104 t.tm_zone = "Z"; 105 } 106 107 return mktime(&t); 108 } 109 110 static void __attribute__((noreturn)) 111 usage(void) 112 { 113 eprintf( 114 "usage: %s [-acm] [-d time | -r ref_file | -t time | -T time] " 115 "file ...\n", 116 argv0 117 ); 118 } 119 120 // ?man touch: change file timestamps 121 // ?man update the access and modification times of files 122 int 123 main(int argc, char *argv[]) 124 { 125 struct stat st; 126 char *ref = NULL; 127 128 ARGBEGIN 129 { 130 // ?man -a: print or show all entries 131 case 'a': 132 aflag = 1; 133 break; 134 // ?man -c: print count or perform stdout action 135 case 'c': 136 cflag = 1; 137 break; 138 // ?man -d: specify directory 139 case 'd': 140 // ?man -t:str: sort or specify timestamp 141 case 't': 142 times[0].tv_sec = parsetime(EARGF(usage())); 143 times[0].tv_nsec = 0; 144 break; 145 // ?man -m: specify mode or limit 146 case 'm': 147 mflag = 1; 148 break; 149 // ?man -r:str: operate recursively 150 case 'r': 151 ref = EARGF(usage()); 152 if (stat(ref, &st) < 0) 153 eprintf("stat '%s':", ref); 154 times[0] = st.st_atim; 155 times[1] = st.st_mtim; 156 break; 157 // ?man -T:num: specify option flag 158 case 'T': 159 times[0].tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX); 160 times[0].tv_nsec = 0; 161 break; 162 default: 163 usage(); 164 } 165 ARGEND 166 167 if (!argc) 168 usage(); 169 if (!aflag && !mflag) 170 aflag = mflag = 1; 171 if (!ref) 172 times[1] = times[0]; 173 if (!aflag) 174 times[0].tv_nsec = UTIME_OMIT; 175 if (!mflag) 176 times[1].tv_nsec = UTIME_OMIT; 177 178 for (; *argv; argc--, argv++) 179 touch(*argv); 180 181 return 0; 182 }