]> git.proxmox.com Git - grub2.git/blob - util/editenv.c
Import grub2_2.02+dfsg1.orig.tar.xz
[grub2.git] / util / editenv.c
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
20 #include <config.h>
21 #include <grub/types.h>
22 #include <grub/emu/misc.h>
23 #include <grub/util/misc.h>
24 #include <grub/util/install.h>
25 #include <grub/lib/envblk.h>
26 #include <grub/i18n.h>
27 #include <grub/emu/hostfile.h>
28
29 #include <errno.h>
30 #include <string.h>
31
32 #define DEFAULT_ENVBLK_SIZE 1024
33
34 void
35 grub_util_create_envblk_file (const char *name)
36 {
37 FILE *fp;
38 char *buf;
39 char *namenew;
40
41 buf = xmalloc (DEFAULT_ENVBLK_SIZE);
42
43 namenew = xasprintf ("%s.new", name);
44 fp = grub_util_fopen (namenew, "wb");
45 if (! fp)
46 grub_util_error (_("cannot open `%s': %s"), namenew,
47 strerror (errno));
48
49 memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
50 memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#',
51 DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
52
53 if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
54 grub_util_error (_("cannot write to `%s': %s"), namenew,
55 strerror (errno));
56
57
58 grub_util_file_sync (fp);
59 free (buf);
60 fclose (fp);
61
62 if (grub_util_rename (namenew, name) < 0)
63 grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
64 free (namenew);
65 }