diff options
author | Ron Yorston <rmy@pobox.com> | 2023-07-16 08:49:50 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-07-16 08:49:50 +0100 |
commit | bc0d14ee8eb9720a5df097f5620c4b4c679b6c98 (patch) | |
tree | 8aaac80f79822ac2f590854dfecc4af91c0cedd4 | |
parent | 51ba60c766e010f67dd410ad31a2c4814a8440e4 (diff) | |
download | busybox-w32-bc0d14ee8eb9720a5df097f5620c4b4c679b6c98.tar.gz busybox-w32-bc0d14ee8eb9720a5df097f5620c4b4c679b6c98.tar.bz2 busybox-w32-bc0d14ee8eb9720a5df097f5620c4b4c679b6c98.zip |
date: allow system date to be set
Implement clock_settime(2) and enable the '-s' option to allow
the system time to be set. This requires elevated privileges.
The code in date.c is now identical to upstream BusyBox.
Costs 256-272 bytes.
-rw-r--r-- | coreutils/date.c | 25 | ||||
-rw-r--r-- | include/mingw.h | 1 | ||||
-rw-r--r-- | win32/mingw.c | 23 |
3 files changed, 24 insertions, 25 deletions
diff --git a/coreutils/date.c b/coreutils/date.c index d8e56ef3b..3a89b6caf 100644 --- a/coreutils/date.c +++ b/coreutils/date.c | |||
@@ -97,9 +97,7 @@ | |||
97 | //usage:#define date_full_usage "\n\n" | 97 | //usage:#define date_full_usage "\n\n" |
98 | //usage: "Display time (using +FMT), or set time\n" | 98 | //usage: "Display time (using +FMT), or set time\n" |
99 | //usage: "\n -u Work in UTC (don't convert to local time)" | 99 | //usage: "\n -u Work in UTC (don't convert to local time)" |
100 | //usage: IF_NOT_PLATFORM_MINGW32( | ||
101 | //usage: "\n [-s] TIME Set time to TIME" | 100 | //usage: "\n [-s] TIME Set time to TIME" |
102 | //usage: ) | ||
103 | //usage: "\n -d TIME Display TIME, not 'now'" | 101 | //usage: "\n -d TIME Display TIME, not 'now'" |
104 | //usage: IF_FEATURE_DATE_ISOFMT( | 102 | //usage: IF_FEATURE_DATE_ISOFMT( |
105 | //usage: "\n -D FMT FMT (strptime format) for -s/-d TIME conversion" | 103 | //usage: "\n -D FMT FMT (strptime format) for -s/-d TIME conversion" |
@@ -134,30 +132,19 @@ | |||
134 | 132 | ||
135 | enum { | 133 | enum { |
136 | OPT_RFC2822 = (1 << 0), /* R */ | 134 | OPT_RFC2822 = (1 << 0), /* R */ |
137 | #if !ENABLE_PLATFORM_MINGW32 | ||
138 | OPT_SET = (1 << 1), /* s */ | 135 | OPT_SET = (1 << 1), /* s */ |
139 | OPT_UTC = (1 << 2), /* u */ | 136 | OPT_UTC = (1 << 2), /* u */ |
140 | OPT_DATE = (1 << 3), /* d */ | 137 | OPT_DATE = (1 << 3), /* d */ |
141 | OPT_REFERENCE = (1 << 4), /* r */ | 138 | OPT_REFERENCE = (1 << 4), /* r */ |
142 | OPT_ISO8601 = (1 << 5) * ENABLE_FEATURE_DATE_ISOFMT, /* I */ | 139 | OPT_ISO8601 = (1 << 5) * ENABLE_FEATURE_DATE_ISOFMT, /* I */ |
143 | OPT_STR2DT = (1 << 6) * ENABLE_FEATURE_DATE_ISOFMT, /* D */ | 140 | OPT_STR2DT = (1 << 6) * ENABLE_FEATURE_DATE_ISOFMT, /* D */ |
144 | #else | ||
145 | OPT_SET = (0), /* s */ | ||
146 | OPT_UTC = (1 << 1), /* u */ | ||
147 | OPT_DATE = (1 << 2), /* d */ | ||
148 | OPT_REFERENCE = (1 << 3), /* r */ | ||
149 | OPT_ISO8601 = (1 << 4) * ENABLE_FEATURE_DATE_ISOFMT, /* I */ | ||
150 | OPT_STR2DT = (1 << 5) * ENABLE_FEATURE_DATE_ISOFMT, /* D */ | ||
151 | #endif | ||
152 | }; | 141 | }; |
153 | 142 | ||
154 | #if ENABLE_LONG_OPTS | 143 | #if ENABLE_LONG_OPTS |
155 | static const char date_longopts[] ALIGN1 = | 144 | static const char date_longopts[] ALIGN1 = |
156 | "rfc-822\0" No_argument "R" | 145 | "rfc-822\0" No_argument "R" |
157 | "rfc-2822\0" No_argument "R" | 146 | "rfc-2822\0" No_argument "R" |
158 | #if !ENABLE_PLATFORM_MINGW32 | ||
159 | "set\0" Required_argument "s" | 147 | "set\0" Required_argument "s" |
160 | #endif | ||
161 | "utc\0" No_argument "u" | 148 | "utc\0" No_argument "u" |
162 | /* "universal\0" No_argument "u" */ | 149 | /* "universal\0" No_argument "u" */ |
163 | "date\0" Required_argument "d" | 150 | "date\0" Required_argument "d" |
@@ -187,25 +174,13 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
187 | char *isofmt_arg = NULL; | 174 | char *isofmt_arg = NULL; |
188 | 175 | ||
189 | opt = getopt32long(argv, "^" | 176 | opt = getopt32long(argv, "^" |
190 | #if !ENABLE_PLATFORM_MINGW32 | ||
191 | "Rs:ud:r:" | 177 | "Rs:ud:r:" |
192 | #else | ||
193 | "Rud:r:" | ||
194 | #endif | ||
195 | IF_FEATURE_DATE_ISOFMT("I::D:") | 178 | IF_FEATURE_DATE_ISOFMT("I::D:") |
196 | #if !ENABLE_PLATFORM_MINGW32 | ||
197 | "\0" | 179 | "\0" |
198 | "d--s:s--d" | 180 | "d--s:s--d" |
199 | IF_FEATURE_DATE_ISOFMT(":R--I:I--R"), | 181 | IF_FEATURE_DATE_ISOFMT(":R--I:I--R"), |
200 | #else | ||
201 | IF_FEATURE_DATE_ISOFMT("\0R--I:I--R"), | ||
202 | #endif | ||
203 | date_longopts, | 182 | date_longopts, |
204 | #if !ENABLE_PLATFORM_MINGW32 | ||
205 | &date_str, &date_str, &filename | 183 | &date_str, &date_str, &filename |
206 | #else | ||
207 | &date_str, &filename | ||
208 | #endif | ||
209 | IF_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt) | 184 | IF_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt) |
210 | ); | 185 | ); |
211 | argv += optind; | 186 | argv += optind; |
diff --git a/include/mingw.h b/include/mingw.h index ae34666e8..232ffadd7 100644 --- a/include/mingw.h +++ b/include/mingw.h | |||
@@ -294,6 +294,7 @@ time_t timegm(struct tm *tm); | |||
294 | 294 | ||
295 | int nanosleep(const struct timespec *req, struct timespec *rem); | 295 | int nanosleep(const struct timespec *req, struct timespec *rem); |
296 | int clock_gettime(clockid_t clockid, struct timespec *tp); | 296 | int clock_gettime(clockid_t clockid, struct timespec *tp); |
297 | int clock_settime(clockid_t clockid, const struct timespec *tp); | ||
297 | 298 | ||
298 | /* | 299 | /* |
299 | * sys/stat.h | 300 | * sys/stat.h |
diff --git a/win32/mingw.c b/win32/mingw.c index 86dc75861..5e9c71226 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -1008,6 +1008,29 @@ int clock_gettime(clockid_t clockid, struct timespec *tp) | |||
1008 | return 0; | 1008 | return 0; |
1009 | } | 1009 | } |
1010 | 1010 | ||
1011 | int clock_settime(clockid_t clockid, const struct timespec *tp) | ||
1012 | { | ||
1013 | SYSTEMTIME st; | ||
1014 | FILETIME ft; | ||
1015 | |||
1016 | if (clockid != CLOCK_REALTIME) { | ||
1017 | errno = ENOSYS; | ||
1018 | return -1; | ||
1019 | } | ||
1020 | |||
1021 | timespec_to_filetime(*tp, &ft); | ||
1022 | if (FileTimeToSystemTime(&ft, &st) == 0) { | ||
1023 | errno = EINVAL; | ||
1024 | return -1; | ||
1025 | } | ||
1026 | |||
1027 | if (SetSystemTime(&st) == 0) { | ||
1028 | errno = EPERM; | ||
1029 | return -1; | ||
1030 | } | ||
1031 | return 0; | ||
1032 | } | ||
1033 | |||
1011 | int pipe(int filedes[2]) | 1034 | int pipe(int filedes[2]) |
1012 | { | 1035 | { |
1013 | if (_pipe(filedes, PIPE_BUF, 0) < 0) | 1036 | if (_pipe(filedes, PIPE_BUF, 0) < 0) |