blob: 980f0d35392d04ac5d66bd2815aca0753102cb33 [file] [log] [blame]
Ken Sumrall795165b2011-04-05 20:46:30 -07001#include <stdio.h>
2#include <string.h>
3#include <sys/types.h>
4#include <fcntl.h>
5#include <sys/stat.h>
6#include <stdlib.h>
7#include <fcntl.h>
Ken Sumrall13495a12013-06-25 21:42:23 -07008#include <time.h>
Elliott Hughes0badbd62014-12-29 12:24:25 -08009#include <unistd.h>
Ken Sumrall795165b2011-04-05 20:46:30 -070010
11static void usage(void)
12{
Ken Sumrall13495a12013-06-25 21:42:23 -070013 fprintf(stderr, "touch: usage: touch [-alm] [-t YYYYMMDD[.hhmmss]] <file>\n");
Ken Sumrall795165b2011-04-05 20:46:30 -070014 exit(1);
15}
16
Ken Sumrall13495a12013-06-25 21:42:23 -070017static time_t parse_time(char *s)
18{
19 struct tm tm;
20 int day = atoi(s);
21 int hour = 0;
22
23 while (*s && *s != '.') {
24 s++;
25 }
26
27 if (*s) {
28 s++;
29 hour = atoi(s);
30 }
31
32 tm.tm_year = day / 10000 - 1900;
33 tm.tm_mon = (day % 10000) / 100 - 1;
34 tm.tm_mday = day % 100;
35 tm.tm_hour = hour / 10000;
36 tm.tm_min = (hour % 10000) / 100;
37 tm.tm_sec = hour % 100;
38 tm.tm_isdst = -1;
39
40 return mktime(&tm);
41}
42
Ken Sumrall795165b2011-04-05 20:46:30 -070043int touch_main(int argc, char *argv[])
44{
45 int i, fd, aflag = 0, mflag = 0, debug = 0, flags = 0;
46 struct timespec specified_time, times[2];
47 char *file = 0;
48
49 specified_time.tv_nsec = UTIME_NOW;
50
51 for (i = 1; i < argc; i++) {
52 if (argv[i][0] == '-') {
53 /* an option */
54 const char *arg = argv[i]+1;
55 while (arg[0]) {
56 switch (arg[0]) {
57 case 'a': aflag = 1; break;
58 case 'm': mflag = 1; break;
59 case 't':
60 if ((i+1) >= argc)
61 usage();
Ken Sumrall13495a12013-06-25 21:42:23 -070062 specified_time.tv_sec = parse_time(argv[++i]);
63 if (specified_time.tv_sec == -1) {
64 fprintf(stderr, "touch: invalid timestamp specified\n");
Ken Sumrall795165b2011-04-05 20:46:30 -070065 exit(1);
66 }
67 specified_time.tv_nsec = 0;
68 break;
69 case 'l': flags |= AT_SYMLINK_NOFOLLOW; break;
70 case 'd': debug = 1; break;
71 default:
72 usage();
73 }
74 arg++;
75 }
76 } else {
77 /* not an option, and only accept one filename */
78 if (i+1 != argc)
79 usage();
80 file = argv[i];
81 }
82 }
83
84 if (! file) {
85 fprintf(stderr, "touch: no file specified\n");
86 exit(1);
87 }
88
89 if (access(file, F_OK))
90 if ((fd=creat(file, 0666)) != -1)
91 close(fd);
92
93 if ((mflag == 0) && (aflag == 0))
94 aflag = mflag = 1;
95
96 if (aflag)
97 times[0] = specified_time;
98 else
99 times[0].tv_nsec = UTIME_OMIT;
100
101 if (mflag)
102 times[1] = specified_time;
103 else
104 times[1].tv_nsec = UTIME_OMIT;
105
106 if (debug) {
107 fprintf(stderr, "file = %s\n", file);
108 fprintf(stderr, "times[0].tv_sec = %ld, times[0].tv_nsec = %ld\n", times[0].tv_sec, times[0].tv_nsec);
109 fprintf(stderr, "times[1].tv_sec = %ld, times[1].tv_nsec = %ld\n", times[1].tv_sec, times[1].tv_nsec);
110 fprintf(stderr, "flags = 0x%8.8x\n", flags);
111 }
112
113 return utimensat(AT_FDCWD, file, times, flags);
114}
115