datehook.c 2.6 KB

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