diff --git a/ChangeLog b/ChangeLog index ceeb1081b..17b770968 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2009-11-23 Robert Millan + + * util/hostdisk.c: Include `'. + (find_grub_drive): Use ARRAY_SIZE for map size calculation. + (make_device_name): Rewrite using asprintf. + (convert_system_partition_to_system_disk): Replace 0 with NULL. + (find_system_device): If a device is not found, generate one just + by reusing the OS path name. + (read_device_map): Make it permissible for device.map not to exist. + 2009-11-23 Robert Millan * script/sh/execute.c: Move from here ... diff --git a/util/hostdisk.c b/util/hostdisk.c index e1058a100..04c412300 100644 --- a/util/hostdisk.c +++ b/util/hostdisk.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -127,7 +128,7 @@ find_grub_drive (const char *name) if (name) { - for (i = 0; i < sizeof (map) / sizeof (map[0]); i++) + for (i = 0; i < ARRAY_SIZE (map); i++) if (map[i].drive && ! strcmp (map[i].drive, name)) return i; } @@ -565,7 +566,10 @@ read_device_map (const char *dev_map) fp = fopen (dev_map, "r"); if (! fp) - grub_util_error ("Cannot open `%s'", dev_map); + { + grub_util_info (_("Cannot open `%s'"), dev_map); + return; + } while (fgets (buf, sizeof (buf), fp)) { @@ -670,34 +674,27 @@ grub_util_biosdisk_fini (void) static char * make_device_name (int drive, int dos_part, int bsd_part) { - int len = strlen(map[drive].drive); - char *p; + char *ret; + char *dos_part_str = NULL; + char *bsd_part_str = NULL; if (dos_part >= 0) - { - /* Add in char length of dos_part+1 */ - int tmp = dos_part + 1; - len++; - while ((tmp /= 10) != 0) - len++; - } - if (bsd_part >= 0) - len += 2; - - /* Length to alloc is: char length of map[drive].drive, plus - * char length of (dos_part+1) or of bsd_part, plus - * 2 for the comma and a null/end of string (\0) - */ - p = xmalloc (len + 2); - sprintf (p, "%s", map[drive].drive); - - if (dos_part >= 0) - sprintf (p + strlen (p), ",%d", dos_part + 1); + asprintf (&dos_part_str, ",%d", dos_part + 1); if (bsd_part >= 0) - sprintf (p + strlen (p), ",%c", bsd_part + 'a'); + asprintf (&bsd_part_str, ",%c", dos_part + 'a'); - return p; + asprintf (&ret, "%s%s%s", map[drive].drive, + dos_part_str ? : "", + bsd_part_str ? : ""); + + if (dos_part_str) + free (dos_part_str); + + if (bsd_part_str) + free (bsd_part_str); + + return ret; } static char * @@ -706,7 +703,7 @@ convert_system_partition_to_system_disk (const char *os_dev) #if defined(__linux__) char *path = xmalloc (PATH_MAX); if (! realpath (os_dev, path)) - return 0; + return NULL; if (strncmp ("/dev/", path, 5) == 0) { @@ -876,22 +873,29 @@ device_is_wholedisk (const char *os_dev) static int find_system_device (const char *os_dev) { - int i; + unsigned int i; char *os_disk; os_disk = convert_system_partition_to_system_disk (os_dev); if (! os_disk) return -1; - for (i = 0; i < (int) (sizeof (map) / sizeof (map[0])); i++) - if (map[i].device && strcmp (map[i].device, os_disk) == 0) + for (i = 0; i < ARRAY_SIZE (map); i++) + if (! map[i].device) + break; + else if (strcmp (map[i].device, os_disk) == 0) { free (os_disk); return i; } - free (os_disk); - return -1; + if (i == ARRAY_SIZE (map)) + grub_util_error (_("device count exceeds limit")); + + map[i].device = os_disk; + map[i].drive = xstrdup (os_disk); + + return i; } char *