123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- #if ENABLE_FEATURE_DATE_NANO
- # include <sys/syscall.h>
- #endif
- enum {
- OPT_RFC2822 = (1 << 0),
- OPT_SET = (1 << 1),
- OPT_UTC = (1 << 2),
- OPT_DATE = (1 << 3),
- OPT_REFERENCE = (1 << 4),
- OPT_ISO8601 = (1 << 5) * ENABLE_FEATURE_DATE_ISOFMT,
- OPT_STR2DT = (1 << 6) * ENABLE_FEATURE_DATE_ISOFMT,
- };
- #if ENABLE_LONG_OPTS
- static const char date_longopts[] ALIGN1 =
- "rfc-822\0" No_argument "R"
- "rfc-2822\0" No_argument "R"
- "set\0" Required_argument "s"
- "utc\0" No_argument "u"
-
- "date\0" Required_argument "d"
- "reference\0" Required_argument "r"
- ;
- #endif
- int date_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int date_main(int argc UNUSED_PARAM, char **argv)
- {
- struct timespec ts;
- struct tm tm_time;
- char buf_fmt_dt2str[64];
- unsigned opt;
- int isofmt = -1;
- char *date_str;
- char *fmt_dt2str;
- char *fmt_str2dt;
- char *filename;
- char *isofmt_arg = NULL;
- opt = getopt32long(argv, "^"
- "Rs:ud:r:"
- IF_FEATURE_DATE_ISOFMT("I::D:")
- "\0"
- "d--s:s--d"
- IF_FEATURE_DATE_ISOFMT(":R--I:I--R"),
- date_longopts,
- &date_str, &date_str, &filename
- IF_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt)
- );
- argv += optind;
- if (opt & OPT_UTC)
- putenv((char*)"TZ=UTC0");
- if (ENABLE_FEATURE_DATE_ISOFMT && (opt & OPT_ISO8601)) {
- isofmt = 0;
- if (isofmt_arg) {
- static const char isoformats[] ALIGN1 =
- "date\0""hours\0""minutes\0""seconds\0ns\0";
- isofmt = index_in_substrings(isoformats, isofmt_arg);
- if (isofmt < 0)
- bb_show_usage();
- }
- }
- fmt_dt2str = NULL;
- if (argv[0] && argv[0][0] == '+') {
- fmt_dt2str = &argv[0][1];
- argv++;
- }
- if (!(opt & (OPT_SET | OPT_DATE))) {
- opt |= OPT_SET;
- date_str = argv[0];
- if (date_str) {
- #if ENABLE_FEATURE_DATE_COMPAT
- int len = strspn(date_str, "0123456789");
- if (date_str[len] == '\0'
- || (date_str[len] == '.'
- && isdigit(date_str[len+1])
- && isdigit(date_str[len+2])
- && date_str[len+3] == '\0'
- )
- ) {
-
- len -= 8;
- if (len < 0 || len > 4 || (len & 1))
- bb_error_msg_and_die(bb_msg_invalid_date, date_str);
- if (len != 0) {
- char buf[4];
- memcpy(buf, date_str + 8, len);
- memmove(date_str + len, date_str, 8);
- memcpy(date_str, buf, len);
- }
- }
- #endif
- argv++;
- }
- }
- if (*argv)
- bb_show_usage();
-
- if (opt & OPT_REFERENCE) {
- struct stat statbuf;
- xstat(filename, &statbuf);
- ts.tv_sec = statbuf.st_mtime;
- #if ENABLE_FEATURE_DATE_NANO
- ts.tv_nsec = statbuf.st_mtim.tv_nsec;
-
- #endif
- } else {
- #if ENABLE_FEATURE_DATE_NANO
- clock_gettime(CLOCK_REALTIME, &ts);
- #else
- time(&ts.tv_sec);
- #endif
- }
- #if !ENABLE_FEATURE_DATE_NANO
- ts.tv_nsec = 0;
- #endif
- localtime_r(&ts.tv_sec, &tm_time);
-
- if (date_str != NULL) {
- int check_dst = 1;
-
- tm_time.tm_sec = 0;
- tm_time.tm_min = 0;
- tm_time.tm_hour = 0;
-
- if (ENABLE_FEATURE_DATE_ISOFMT && (opt & OPT_STR2DT)) {
- if (strptime(date_str, fmt_str2dt, &tm_time) == NULL)
- bb_error_msg_and_die(bb_msg_invalid_date, date_str);
- } else {
- check_dst = parse_datestr(date_str, &tm_time);
- }
-
-
- if (check_dst)
- tm_time.tm_isdst = -1;
- ts.tv_sec = validate_tm_time(date_str, &tm_time);
- ts.tv_nsec = 0;
-
- if ((opt & OPT_SET) && clock_settime(CLOCK_REALTIME, &ts) < 0) {
- bb_simple_perror_msg("can't set date");
- }
- }
-
-
- if (fmt_dt2str == NULL) {
- int i;
- fmt_dt2str = buf_fmt_dt2str;
- if (ENABLE_FEATURE_DATE_ISOFMT && isofmt >= 0) {
-
- strcpy(fmt_dt2str, "%Y-%m-%dT%H:%M:%S");
- i = 8 + 3 * isofmt;
- if (isofmt != 0) {
- int n;
- if (isofmt == 4) {
- i -= 3;
- i += sprintf(&fmt_dt2str[i], ",%09u", (unsigned)ts.tv_nsec);
- }
-
-
- n = strftime(&fmt_dt2str[i], 8, "%z", &tm_time);
- i += n;
- if (n == 5 && (fmt_dt2str[i-5] == '+' || fmt_dt2str[i-5] == '-')) {
-
- fmt_dt2str[i ] = fmt_dt2str[i - 1];
- fmt_dt2str[i - 1] = fmt_dt2str[i - 2];
- fmt_dt2str[i - 2] = ':';
- i++;
- }
- }
- fmt_dt2str[i] = '\0';
- } else if (opt & OPT_RFC2822) {
-
- if (ENABLE_LOCALE_SUPPORT)
- setlocale(LC_TIME, "C");
- fmt_dt2str = (char*)"%a, %d %b %Y %H:%M:%S %z";
- } else {
- fmt_dt2str = (char*)"%a %b %e %H:%M:%S %Z %Y";
- }
- }
- #if ENABLE_FEATURE_DATE_NANO
- else {
-
-
- char *p = fmt_dt2str;
- while ((p = strchr(p, '%')) != NULL) {
- int n, m;
- unsigned pres, scale;
- p++;
- if (*p == '%') {
- p++;
- continue;
- }
- n = strspn(p, "0123456789");
- if (p[n] != 'N') {
- p += n;
- continue;
- }
-
- p[-1] = '\0';
- p[n] = '\0';
- scale = 1;
- pres = 9;
- if (n) {
- pres = xatoi_positive(p);
- if (pres == 0)
- pres = 9;
- m = 9 - pres;
- while (--m >= 0)
- scale *= 10;
- }
- m = p - fmt_dt2str;
- p += n + 1;
- fmt_dt2str = xasprintf("%s%0*u%s", fmt_dt2str, pres, (unsigned)ts.tv_nsec / scale, p);
- p = fmt_dt2str + m;
- }
- }
- #endif
- #define date_buf bb_common_bufsiz1
- setup_common_bufsiz();
- if (*fmt_dt2str == '\0') {
-
- date_buf[0] = '\0';
- } else {
-
- strftime(date_buf, COMMON_BUFSIZE, fmt_dt2str, &tm_time);
- }
- puts(date_buf);
- return EXIT_SUCCESS;
- }
|