Am Dienstag, den 02.09.2008, 17:59 +0300 schrieb Vesa Jääskeläinen: 
> Felix Zielcke wrote:
> 
> sizeof returns type of size_t so it would be good that char k uses that.
> I am a bit surprised that this didn't generate compiler warning?

Well somewhere hidden in a mail from me, I think I already wrote that I
already got the idea to make these warnings more visible :)
I didn't look yet further into it, maybe even a [RFC] topic should be
made for this.
I'm thinking about how linux kernel compiling works.

Actually I haven't checked that code ;)

> And there is no reason to define integers as constants unless you really
> want to make sure they don't change :)

This is just a thing left from my previous coding experience.
As I started to code I personally just prefer that, but yeah gcc is so
nice and optimizes (almost) everything for us and this code is so little
anyway :)

> > +     p = strchr (os_dev, 'p');
> > +     if (p)
> > +       *p = ',';
> 
> It is usually a bad idea to modify source string.

Urm right this is util/ no need to save a few bytes :)

Thanks for your suggestions Vesa.
It really looks now better compared to the first code I sent about this
problem and it only uses now 3 variables :)

-- 
Felix Zielcke
2008-09-02  Felix Zielcke  <[EMAIL PROTECTED]>

        * util/getroot.c: Include <config.h>.
        (grub_util_get_grub_dev): Rewritten to use asprintf for mdraid devices,
        add support for /dev/md/N devices and handle LVM double dash escaping.

Index: util/getroot.c
===================================================================
--- util/getroot.c      (Revision 1845)
+++ util/getroot.c      (Arbeitskopie)
@@ -17,6 +17,7 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>
@@ -417,62 +418,52 @@ grub_util_get_grub_dev (const char *os_d
   switch (grub_util_get_dev_abstraction (os_dev))
     {
     case GRUB_DEV_ABSTRACTION_LVM:
-      grub_dev = xmalloc (strlen (os_dev) - 12 + 1);
-
-      strcpy (grub_dev, os_dev + 12);
 
+      {
+       unsigned short i, len;
+       grub_size_t offset = sizeof ("/dev/mapper/") - 1;
+
+       len = strlen (os_dev) - offset;
+       grub_dev = xmalloc (len);
+
+       for (i = 0; i <= len; i++, offset++)
+         {
+           grub_dev[i] = os_dev[offset];
+           if (os_dev[offset] == '-' && os_dev[offset + 1] == '-')
+             offset++;
+         }
+      }
+      
       break;
 
     case GRUB_DEV_ABSTRACTION_RAID:
-      grub_dev = xmalloc (20);
 
       if (os_dev[7] == '_' && os_dev[8] == 'd')
        {
-         const char *p;
-
          /* This a partitionable RAID device of the form /dev/md_dNNpMM. */
-         int i;
-
-         grub_dev[0] = 'm';
-         grub_dev[1] = 'd';
-         i = 2;
          
-         p = os_dev + 9;
-         while (*p >= '0' && *p <= '9')
-           {
-             grub_dev[i] = *p;
-             i++;
-             p++;
-           }
+         char *p , *q;
 
-         if (*p == '\0')
-           grub_dev[i] = '\0';
-         else if (*p == 'p')
-           {
-             p++;
-             grub_dev[i] = ',';
-             i++;
-
-             while (*p >= '0' && *p <= '9')
-               {
-                 grub_dev[i] = *p;
-                 i++;
-                 p++;
-               }
+         p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
 
-             grub_dev[i] = '\0';
-           }
-         else
-           grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
+         q = strchr (p, 'p');
+         if (q)
+           *q = ',';
+
+         asprintf (&grub_dev, "md%s", p);
+         free (p);
        }
       else if (os_dev[7] >= '0' && os_dev[7] <= '9')
        {
-         memcpy (grub_dev, os_dev + 5, 7);
-         grub_dev[7] = '\0';
+         asprintf (&grub_dev, "md%s", os_dev + sizeof ("/dev/md") - 1);
+       }
+      else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+       {
+         asprintf (&grub_dev, "md%s", os_dev + sizeof ("/dev/md/") - 1);
        }
       else
        grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
-
+      
       break;
 
     default:  /* GRUB_DEV_ABSTRACTION_NONE */
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to