datehook.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* datehook.c - Module to install datetime hooks. */
  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/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/env.h>
  22. #include <grub/misc.h>
  23. #include <grub/normal.h>
  24. #include <grub/datetime.h>
  25. static char *grub_datetime_names[] =
  26. {
  27. "YEAR",
  28. "MONTH",
  29. "DAY",
  30. "HOUR",
  31. "MINUTE",
  32. "SECOND",
  33. "WEEKDAY",
  34. };
  35. static char *
  36. grub_read_hook_datetime (struct grub_env_var *var,
  37. const char *val __attribute__ ((unused)))
  38. {
  39. struct grub_datetime datetime;
  40. static char buf[6];
  41. buf[0] = 0;
  42. if (! grub_get_datetime (&datetime))
  43. {
  44. int i;
  45. for (i = 0; i < 7; i++)
  46. if (! grub_strcmp (var->name, grub_datetime_names[i]))
  47. {
  48. int n;
  49. switch (i)
  50. {
  51. case 0:
  52. n = datetime.year;
  53. break;
  54. case 1:
  55. n = datetime.month;
  56. break;
  57. case 2:
  58. n = datetime.day;
  59. break;
  60. case 3:
  61. n = datetime.hour;
  62. break;
  63. case 4:
  64. n = datetime.minute;
  65. break;
  66. case 5:
  67. n = datetime.second;
  68. break;
  69. default:
  70. return grub_get_weekday_name (&datetime);
  71. }
  72. grub_snprintf (buf, sizeof (buf), "%d", n);
  73. break;
  74. }
  75. }
  76. return buf;
  77. }
  78. GRUB_MOD_INIT(datehook)
  79. {
  80. int i;
  81. for (i = 0; i < 7; i++)
  82. grub_register_variable_hook (grub_datetime_names[i],
  83. grub_read_hook_datetime, 0);
  84. }
  85. GRUB_MOD_FINI(datehook)
  86. {
  87. int i;
  88. for (i = 0; i < 7; i++)
  89. {
  90. grub_register_variable_hook (grub_datetime_names[i], 0, 0);
  91. grub_env_unset (grub_datetime_names[i]);
  92. }
  93. }