date.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #define GRUB_DATETIME_SET_YEAR 1
  26. #define GRUB_DATETIME_SET_MONTH 2
  27. #define GRUB_DATETIME_SET_DAY 4
  28. #define GRUB_DATETIME_SET_HOUR 8
  29. #define GRUB_DATETIME_SET_MINUTE 16
  30. #define GRUB_DATETIME_SET_SECOND 32
  31. static grub_err_t
  32. grub_cmd_date (grub_command_t cmd __attribute__ ((unused)),
  33. int argc, char **args)
  34. {
  35. struct grub_datetime datetime;
  36. int limit[6][2] = {{1980, 2079}, {1, 12}, {1, 31}, {0, 23}, {0, 59}, {0, 59}};
  37. int value[6], mask;
  38. if (argc == 0)
  39. {
  40. if (grub_get_datetime (&datetime))
  41. return grub_errno;
  42. grub_printf ("%d-%02d-%02d %02d:%02d:%02d %s\n",
  43. datetime.year, datetime.month, datetime.day,
  44. datetime.hour, datetime.minute, datetime.second,
  45. grub_get_weekday_name (&datetime));
  46. return 0;
  47. }
  48. grub_memset (&value, 0, sizeof (value));
  49. mask = 0;
  50. for (; argc; argc--, args++)
  51. {
  52. char *p, c;
  53. int m1, ofs, n, cur_mask;
  54. p = args[0];
  55. m1 = grub_strtoul (p, &p, 10);
  56. c = *p;
  57. if (c == '-')
  58. ofs = 0;
  59. else if (c == ':')
  60. ofs = 3;
  61. else
  62. goto fail;
  63. value[ofs] = m1;
  64. cur_mask = (1 << ofs);
  65. mask &= ~(cur_mask * (1 + 2 + 4));
  66. for (n = 1; (n < 3) && (*p); n++)
  67. {
  68. if (*p != c)
  69. goto fail;
  70. value[ofs + n] = grub_strtoul (p + 1, &p, 10);
  71. cur_mask |= (1 << (ofs + n));
  72. }
  73. if (*p)
  74. goto fail;
  75. if ((ofs == 0) && (n == 2))
  76. {
  77. value[ofs + 2] = value[ofs + 1];
  78. value[ofs + 1] = value[ofs];
  79. ofs++;
  80. cur_mask <<= 1;
  81. }
  82. for (; n; n--, ofs++)
  83. if ((value [ofs] < limit[ofs][0]) ||
  84. (value [ofs] > limit[ofs][1]))
  85. goto fail;
  86. mask |= cur_mask;
  87. }
  88. if (grub_get_datetime (&datetime))
  89. return grub_errno;
  90. if (mask & GRUB_DATETIME_SET_YEAR)
  91. datetime.year = value[0];
  92. if (mask & GRUB_DATETIME_SET_MONTH)
  93. datetime.month = value[1];
  94. if (mask & GRUB_DATETIME_SET_DAY)
  95. datetime.day = value[2];
  96. if (mask & GRUB_DATETIME_SET_HOUR)
  97. datetime.hour = value[3];
  98. if (mask & GRUB_DATETIME_SET_MINUTE)
  99. datetime.minute = value[4];
  100. if (mask & GRUB_DATETIME_SET_SECOND)
  101. datetime.second = value[5];
  102. return grub_set_datetime (&datetime);
  103. fail:
  104. return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid datetime");
  105. }
  106. static grub_command_t cmd;
  107. GRUB_MOD_INIT(date)
  108. {
  109. cmd =
  110. grub_register_command ("date", grub_cmd_date,
  111. N_("[[year-]month-day] [hour:minute[:second]]"),
  112. N_("Command to display/set current datetime."));
  113. }
  114. GRUB_MOD_FINI(date)
  115. {
  116. grub_unregister_command (cmd);
  117. }