date.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* date.c - command to display/set current datetime. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/err.h>
  21. #include <grub/misc.h>
  22. #include <grub/datetime.h>
  23. #include <grub/command.h>
  24. #include <grub/i18n.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. #define GRUB_DATETIME_SET_YEAR 1
  27. #define GRUB_DATETIME_SET_MONTH 2
  28. #define GRUB_DATETIME_SET_DAY 4
  29. #define GRUB_DATETIME_SET_HOUR 8
  30. #define GRUB_DATETIME_SET_MINUTE 16
  31. #define GRUB_DATETIME_SET_SECOND 32
  32. static grub_err_t
  33. grub_cmd_date (grub_command_t cmd __attribute__ ((unused)),
  34. int argc, char **args)
  35. {
  36. struct grub_datetime datetime;
  37. int limit[6][2] = {{1980, 2079}, {1, 12}, {1, 31}, {0, 23}, {0, 59}, {0, 59}};
  38. int value[6], mask;
  39. if (argc == 0)
  40. {
  41. if (grub_get_datetime (&datetime))
  42. return grub_errno;
  43. grub_printf ("%d-%02d-%02d %02d:%02d:%02d %s\n",
  44. datetime.year, datetime.month, datetime.day,
  45. datetime.hour, datetime.minute, datetime.second,
  46. grub_get_weekday_name (&datetime));
  47. return 0;
  48. }
  49. grub_memset (&value, 0, sizeof (value));
  50. mask = 0;
  51. for (; argc; argc--, args++)
  52. {
  53. char *p, c;
  54. int m1, ofs, n, cur_mask;
  55. p = args[0];
  56. m1 = grub_strtoul (p, &p, 10);
  57. c = *p;
  58. if (c == '-')
  59. ofs = 0;
  60. else if (c == ':')
  61. ofs = 3;
  62. else
  63. goto fail;
  64. value[ofs] = m1;
  65. cur_mask = (1 << ofs);
  66. mask &= ~(cur_mask * (1 + 2 + 4));
  67. for (n = 1; (n < 3) && (*p); n++)
  68. {
  69. if (*p != c)
  70. goto fail;
  71. value[ofs + n] = grub_strtoul (p + 1, &p, 10);
  72. cur_mask |= (1 << (ofs + n));
  73. }
  74. if (*p)
  75. goto fail;
  76. if ((ofs == 0) && (n == 2))
  77. {
  78. value[ofs + 2] = value[ofs + 1];
  79. value[ofs + 1] = value[ofs];
  80. ofs++;
  81. cur_mask <<= 1;
  82. }
  83. for (; n; n--, ofs++)
  84. if ((value [ofs] < limit[ofs][0]) ||
  85. (value [ofs] > limit[ofs][1]))
  86. goto fail;
  87. mask |= cur_mask;
  88. }
  89. if (grub_get_datetime (&datetime))
  90. return grub_errno;
  91. if (mask & GRUB_DATETIME_SET_YEAR)
  92. datetime.year = value[0];
  93. if (mask & GRUB_DATETIME_SET_MONTH)
  94. datetime.month = value[1];
  95. if (mask & GRUB_DATETIME_SET_DAY)
  96. datetime.day = value[2];
  97. if (mask & GRUB_DATETIME_SET_HOUR)
  98. datetime.hour = value[3];
  99. if (mask & GRUB_DATETIME_SET_MINUTE)
  100. datetime.minute = value[4];
  101. if (mask & GRUB_DATETIME_SET_SECOND)
  102. datetime.second = value[5];
  103. return grub_set_datetime (&datetime);
  104. fail:
  105. return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid datetime");
  106. }
  107. static grub_command_t cmd;
  108. GRUB_MOD_INIT(date)
  109. {
  110. cmd =
  111. grub_register_command ("date", grub_cmd_date,
  112. N_("[[year-]month-day] [hour:minute[:second]]"),
  113. N_("Display/set current datetime."));
  114. }
  115. GRUB_MOD_FINI(date)
  116. {
  117. grub_unregister_command (cmd);
  118. }