GRUB-1.98 changes
[grub2/jjazz.git] / util / grub-mkdevicemap.c
blobc68482af1d8ac4ef855705637084b3bbd1487103
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,2009,2010 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>
34 #include <grub/i18n.h>
36 #define _GNU_SOURCE 1
37 #include <getopt.h>
39 #include "progname.h"
41 static void
42 make_device_map (const char *device_map, int floppy_disks)
44 int num_hd = 0;
45 int num_fd = 0;
46 FILE *fp;
48 auto int NESTED_FUNC_ATTR process_device (const char *name, int is_floppy);
50 int NESTED_FUNC_ATTR process_device (const char *name, int is_floppy)
52 grub_util_emit_devicemap_entry (fp, (char *) name,
53 is_floppy, &num_fd, &num_hd);
54 return 0;
57 if (strcmp (device_map, "-") == 0)
58 fp = stdout;
59 else
60 fp = fopen (device_map, "w");
62 if (! fp)
63 grub_util_error ("cannot open %s", device_map);
65 grub_util_iterate_devices (process_device, floppy_disks);
67 if (fp != stdout)
68 fclose (fp);
71 static struct option options[] =
73 {"device-map", required_argument, 0, 'm'},
74 {"probe-second-floppy", no_argument, 0, 's'},
75 {"no-floppy", no_argument, 0, 'n'},
76 {"help", no_argument, 0, 'h'},
77 {"version", no_argument, 0, 'V'},
78 {"verbose", no_argument, 0, 'v'},
79 {0, 0, 0, 0}
82 static void
83 usage (int status)
85 if (status)
86 fprintf (stderr,
87 "Try `%s --help' for more information.\n", program_name);
88 else
89 printf ("\
90 Usage: %s [OPTION]...\n\
91 \n\
92 Generate a device map file automatically.\n\
93 \n\
94 -n, --no-floppy do not probe any floppy drive\n\
95 -s, --probe-second-floppy probe the second floppy drive\n\
96 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
97 -h, --help display this message and exit\n\
98 -V, --version print version information and exit\n\
99 -v, --verbose print verbose messages\n\
101 Report bugs to <%s>.\n\
102 ", program_name,
103 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
105 exit (status);
109 main (int argc, char *argv[])
111 char *dev_map = 0;
112 int floppy_disks = 1;
114 set_program_name (argv[0]);
116 grub_util_init_nls ();
118 /* Check for options. */
119 while (1)
121 int c = getopt_long (argc, argv, "snm:r:hVv", options, 0);
123 if (c == -1)
124 break;
125 else
126 switch (c)
128 case 'm':
129 if (dev_map)
130 free (dev_map);
132 dev_map = xstrdup (optarg);
133 break;
135 case 'n':
136 floppy_disks = 0;
137 break;
139 case 's':
140 floppy_disks = 2;
141 break;
143 case 'h':
144 usage (0);
145 break;
147 case 'V':
148 printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
149 return 0;
151 case 'v':
152 verbosity++;
153 break;
155 default:
156 usage (1);
157 break;
161 make_device_map (dev_map ? : DEFAULT_DEVICE_MAP, floppy_disks);
163 free (dev_map);
165 return 0;