Add #ifdef __MINGW32__ in two places
[git/dscho.git] / test-chmtime.c
blob8a1e6b84792d2bed2f21966b354e980d26d02648
1 #include "git-compat-util.h"
2 #include <utime.h>
4 static const char usage_str[] = "(+|=|=+|=-|-)<seconds> <file>...";
6 #ifdef __MINGW32__
7 static inline void time_t_to_filetime(time_t t, FILETIME *ft)
9 long long winTime = t * 10000000LL + 116444736000000000LL;
10 ft->dwLowDateTime = winTime;
11 ft->dwHighDateTime = winTime >> 32;
14 int git_utime (const char *file_name, const struct utimbuf *times)
16 FILETIME mft, aft;
17 int fh, rc;
19 /* must have write permission */
20 if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0)
21 return -1;
23 time_t_to_filetime(times->modtime, &mft);
24 time_t_to_filetime(times->actime, &aft);
25 if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) {
26 errno = EINVAL;
27 rc = -1;
28 } else
29 rc = 0;
30 close(fh);
31 return rc;
34 int git_utime(const char *file_name, const struct utimbuf *times);
35 #define utime git_utime
36 #endif /* __MINGW32__ */
38 int main(int argc, const char *argv[])
40 int i;
41 int set_eq;
42 long int set_time;
43 char *test;
44 const char *timespec;
46 if (argc < 3)
47 goto usage;
49 timespec = argv[1];
50 set_eq = (*timespec == '=') ? 1 : 0;
51 if (set_eq) {
52 timespec++;
53 if (*timespec == '+') {
54 set_eq = 2; /* relative "in the future" */
55 timespec++;
58 set_time = strtol(timespec, &test, 10);
59 if (*test) {
60 fprintf(stderr, "Not a base-10 integer: %s\n", argv[1] + 1);
61 goto usage;
63 if ((set_eq && set_time < 0) || set_eq == 2) {
64 time_t now = time(NULL);
65 set_time += now;
68 for (i = 2; i < argc; i++) {
69 struct stat sb;
70 struct utimbuf utb;
72 if (stat(argv[i], &sb) < 0) {
73 fprintf(stderr, "Failed to stat %s: %s\n",
74 argv[i], strerror(errno));
75 return -1;
78 utb.actime = sb.st_atime;
79 utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
81 if (utime(argv[i], &utb) < 0) {
82 fprintf(stderr, "Failed to modify time on %s: %s\n",
83 argv[i], strerror(errno));
84 return -1;
88 return 0;
90 usage:
91 fprintf(stderr, "Usage: %s %s\n", argv[0], usage_str);
92 return -1;