malloc->zalloc
[grub2/phcoder.git] / util / grub-mkdevicemap.c
blobec43b34b409747d9fa57278774ef0b39887ac034
1 /* grub-mkdevicemap.c - make a device map file automatically */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <config.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <limits.h>
32 #include <grub/util/misc.h>
33 #include <grub/util/deviceiter.h>
35 #define _GNU_SOURCE 1
36 #include <getopt.h>
38 static void
39 make_device_map (const char *device_map, int floppy_disks)
41 int num_hd = 0;
42 int num_fd = 0;
43 FILE *fp;
45 auto int NESTED_FUNC_ATTR process_device (const char *name, int is_floppy);
47 int NESTED_FUNC_ATTR process_device (const char *name, int is_floppy)
49 grub_util_emit_devicemap_entry (fp, (char *) name,
50 is_floppy, &num_fd, &num_hd);
51 return 0;
54 if (strcmp (device_map, "-") == 0)
55 fp = stdout;
56 else
57 fp = fopen (device_map, "w");
59 if (! fp)
60 grub_util_error ("cannot open %s", device_map);
62 grub_util_iterate_devices (process_device, floppy_disks);
64 if (fp != stdout)
65 fclose (fp);
68 static struct option options[] =
70 {"device-map", required_argument, 0, 'm'},
71 {"probe-second-floppy", no_argument, 0, 's'},
72 {"no-floppy", no_argument, 0, 'n'},
73 {"help", no_argument, 0, 'h'},
74 {"version", no_argument, 0, 'V'},
75 {"verbose", no_argument, 0, 'v'},
76 {0, 0, 0, 0}
79 static void
80 usage (int status)
82 if (status)
83 fprintf (stderr,
84 "Try ``grub-mkdevicemap --help'' for more information.\n");
85 else
86 printf ("\
87 Usage: grub-mkdevicemap [OPTION]...\n\
88 \n\
89 Generate a device map file automatically.\n\
90 \n\
91 -n, --no-floppy do not probe any floppy drive\n\
92 -s, --probe-second-floppy probe the second floppy drive\n\
93 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
94 -h, --help display this message and exit\n\
95 -V, --version print version information and exit\n\
96 -v, --verbose print verbose messages\n\
97 \n\
98 Report bugs to <%s>.\n\
100 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
102 exit (status);
106 main (int argc, char *argv[])
108 char *dev_map = 0;
109 int floppy_disks = 1;
111 progname = "grub-mkdevicemap";
113 /* Check for options. */
114 while (1)
116 int c = getopt_long (argc, argv, "snm:r:hVv", options, 0);
118 if (c == -1)
119 break;
120 else
121 switch (c)
123 case 'm':
124 if (dev_map)
125 free (dev_map);
127 dev_map = xstrdup (optarg);
128 break;
130 case 'n':
131 floppy_disks = 0;
132 break;
134 case 's':
135 floppy_disks = 2;
136 break;
138 case 'h':
139 usage (0);
140 break;
142 case 'V':
143 printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
144 return 0;
146 case 'v':
147 verbosity++;
148 break;
150 default:
151 usage (1);
152 break;
156 make_device_map (dev_map ? : DEFAULT_DEVICE_MAP, floppy_disks);
158 free (dev_map);
160 return 0;