editenv.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* editenv.c - tool to edit environment block. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009,2010,2013 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 <config.h>
  20. #include <grub/types.h>
  21. #include <grub/emu/misc.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/util/install.h>
  24. #include <grub/lib/envblk.h>
  25. #include <grub/i18n.h>
  26. #include <grub/emu/hostfile.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #define DEFAULT_ENVBLK_SIZE 1024
  30. void
  31. grub_util_create_envblk_file (const char *name)
  32. {
  33. FILE *fp;
  34. char *buf;
  35. char *namenew;
  36. buf = xmalloc (DEFAULT_ENVBLK_SIZE);
  37. namenew = xasprintf ("%s.new", name);
  38. fp = grub_util_fopen (namenew, "wb");
  39. if (! fp)
  40. grub_util_error (_("cannot open `%s': %s"), namenew,
  41. strerror (errno));
  42. memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
  43. memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#',
  44. DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
  45. if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
  46. grub_util_error (_("cannot write to `%s': %s"), namenew,
  47. strerror (errno));
  48. grub_util_file_sync (fp);
  49. free (buf);
  50. fclose (fp);
  51. if (grub_util_rename (namenew, name) < 0)
  52. grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
  53. free (namenew);
  54. }