2N/A/* datehook.c - Module to install datetime hooks. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/types.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/env.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/normal.h>
2N/A#include <grub/datetime.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic const char *grub_datetime_names[] =
2N/A{
2N/A "YEAR",
2N/A "MONTH",
2N/A "DAY",
2N/A "HOUR",
2N/A "MINUTE",
2N/A "SECOND",
2N/A "WEEKDAY",
2N/A};
2N/A
2N/Astatic const char *
2N/Agrub_read_hook_datetime (struct grub_env_var *var,
2N/A const char *val __attribute__ ((unused)))
2N/A{
2N/A struct grub_datetime datetime;
2N/A static char buf[6];
2N/A
2N/A buf[0] = 0;
2N/A if (! grub_get_datetime (&datetime))
2N/A {
2N/A int i;
2N/A
2N/A for (i = 0; i < 7; i++)
2N/A if (grub_strcmp (var->name, grub_datetime_names[i]) == 0)
2N/A {
2N/A int n;
2N/A
2N/A switch (i)
2N/A {
2N/A case 0:
2N/A n = datetime.year;
2N/A break;
2N/A case 1:
2N/A n = datetime.month;
2N/A break;
2N/A case 2:
2N/A n = datetime.day;
2N/A break;
2N/A case 3:
2N/A n = datetime.hour;
2N/A break;
2N/A case 4:
2N/A n = datetime.minute;
2N/A break;
2N/A case 5:
2N/A n = datetime.second;
2N/A break;
2N/A default:
2N/A return grub_get_weekday_name (&datetime);
2N/A }
2N/A
2N/A grub_snprintf (buf, sizeof (buf), "%d", n);
2N/A break;
2N/A }
2N/A }
2N/A
2N/A return buf;
2N/A}
2N/A
2N/AGRUB_MOD_INIT(datehook)
2N/A{
2N/A unsigned i;
2N/A
2N/A for (i = 0; i < ARRAY_SIZE (grub_datetime_names); i++)
2N/A {
2N/A grub_register_variable_hook (grub_datetime_names[i],
2N/A grub_read_hook_datetime, 0);
2N/A grub_env_export (grub_datetime_names[i]);
2N/A }
2N/A}
2N/A
2N/AGRUB_MOD_FINI(datehook)
2N/A{
2N/A unsigned i;
2N/A
2N/A for (i = 0; i < ARRAY_SIZE (grub_datetime_names); i++)
2N/A {
2N/A grub_register_variable_hook (grub_datetime_names[i], 0, 0);
2N/A grub_env_unset (grub_datetime_names[i]);
2N/A }
2N/A}