Ken Sumrall | 795165b | 2011-04-05 20:46:30 -0700 | [diff] [blame] | 1 | #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 Sumrall | 13495a1 | 2013-06-25 21:42:23 -0700 | [diff] [blame] | 8 | #include <time.h> |
Elliott Hughes | 0badbd6 | 2014-12-29 12:24:25 -0800 | [diff] [blame^] | 9 | #include <unistd.h> |
Ken Sumrall | 795165b | 2011-04-05 20:46:30 -0700 | [diff] [blame] | 10 | |
| 11 | static void usage(void) |
| 12 | { |
Ken Sumrall | 13495a1 | 2013-06-25 21:42:23 -0700 | [diff] [blame] | 13 | fprintf(stderr, "touch: usage: touch [-alm] [-t YYYYMMDD[.hhmmss]] <file>\n"); |
Ken Sumrall | 795165b | 2011-04-05 20:46:30 -0700 | [diff] [blame] | 14 | exit(1); |
| 15 | } |
| 16 | |
Ken Sumrall | 13495a1 | 2013-06-25 21:42:23 -0700 | [diff] [blame] | 17 | static 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 Sumrall | 795165b | 2011-04-05 20:46:30 -0700 | [diff] [blame] | 43 | int 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 Sumrall | 13495a1 | 2013-06-25 21:42:23 -0700 | [diff] [blame] | 62 | specified_time.tv_sec = parse_time(argv[++i]); |
| 63 | if (specified_time.tv_sec == -1) { |
| 64 | fprintf(stderr, "touch: invalid timestamp specified\n"); |
Ken Sumrall | 795165b | 2011-04-05 20:46:30 -0700 | [diff] [blame] | 65 | 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 | |