grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] Various cleanups in kern/misc.c


From: Vincent Pelletier
Subject: [PATCH 1/2] Various cleanups in kern/misc.c
Date: Wed, 29 Jun 2005 16:30:52 +0200
User-agent: Debian Thunderbird 1.0.2 (X11/20050602)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi ! I did some grub hacking again yesterday, and here is the patch
resuming my changes. I only did a systematic read of misc.c to see if I
could find bugs & make improvements.

Don't check it in, as I haven't updated the places where grub_strchr and
grub_strrchr are used (so there are lots of warnings at build). Please
tell me if the patch is ok, and I'll make those updates (which will be
in [PATCH 2/2]).

I haven't read yet the 2 biggest functions : grub_vsprintf and
grub_split_cmdline, so there might be another patch if I find enough
courage to read them :).

2005-06-28  Vincent Pelletier  <address@hidden>

    * kern/misc.c
      (grub_strncpy, grub_strncat, grub_strncmp, grub_strncasecmp):
      Changed argument type from int to grub_size_t.
      (grub_strcat, grub_strlen): Make "while" notation uniform.
      (grub_strncat, grub_strncmp, grub_strncasecmp, grub_strlen):
      Make maximum length check uniform.
      (grub_strncasecmp): Make return value to also ignore case when we
      reach the end of one string.
      (grub_printf, grub_vprintf, grub_vsprintf, grub_sprintf): Changed
      return type from int to grub_size_t.
      (grub_strchr, grub_strrchr): Changed return type from char * to
      const char *.
      (grub_isupper, grub_islower, grub_toupper): New functions.
      (grub_isalpha, grub_tolower): Use new functions.
      (grub_itoa): Changed unsigned to unsigned int.
      (grub_ftoa): intp can be negative, while fractp can't.  Use a
      float argument type.
    * include/grub/mish.h
      (grub_isupper, grub_islower, grub_toupper): New prototypes.
      (grub_strncpy, grub_strncat, grub_strncmp,
      grub_strncasecmp, grub_strchr, grub_strrchr, grub_printf,
      grub_vprintf, grub_sprintf, grub_vsprintf): Updated prototypes to
      match changes in kern/misc.c.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFCwrCcFEQoKRQyjtURArDNAJ9HPmv3nyMUBZkId1paaU8ASVQRCwCggW4J
TuWKLeApkSyYZv8hl0vAXKE=
=K7Co
-----END PGP SIGNATURE-----
Index: kern/misc.c
===================================================================
RCS file: /cvsroot/grub/grub2/kern/misc.c,v
retrieving revision 1.20
diff -u -p -r1.20 misc.c
--- kern/misc.c 23 Jun 2005 23:13:57 -0000      1.20
+++ kern/misc.c 29 Jun 2005 13:59:30 -0000
@@ -63,7 +63,7 @@ grub_strcpy (char *dest, const char *src
 }
 
 char *
-grub_strncpy (char *dest, const char *src, int c)
+grub_strncpy (char *dest, const char *src, grub_size_t c)
 {
   char *p = dest;
   
@@ -91,8 +91,8 @@ grub_strcat (char *dest, const char *src
 {
   char *p = dest;
 
-  while (*p)
-    p++;
+  while (*p++)
+    ;
 
   while ((*p++ = *src++) != '\0')
     ;
@@ -101,12 +101,12 @@ grub_strcat (char *dest, const char *src
 }
 
 char *
-grub_strncat (char *dest, const char *src, int c)
+grub_strncat (char *dest, const char *src, grub_size_t c)
 {
   char *p = dest;
 
-  while (*p)
-    p++;
+  while (*p++)
+    ;
 
   while ((*p++ = *src++) != '\0' && --c)
     ;
@@ -115,11 +115,11 @@ grub_strncat (char *dest, const char *sr
   return dest;
 }
 
-int
+grub_size_t
 grub_printf (const char *fmt, ...)
 {
   va_list ap;
-  int ret;
+  grub_size_t ret;
   
   va_start (ap, fmt);
   ret = grub_vprintf (fmt, ap);
@@ -145,10 +145,10 @@ grub_real_dprintf(const char *file, cons
     }
 }
 
-int
+grub_size_t
 grub_vprintf (const char *fmt, va_list args)
 {
-  int ret;
+  grub_size_t ret;
 
   ret = grub_vsprintf (0, fmt, args);
   grub_refresh ();
@@ -191,63 +191,57 @@ grub_strcmp (const char *s1, const char 
 }
 
 int
-grub_strncmp (const char *s1, const char *s2, int c)
+grub_strncmp (const char *s1, const char *s2, grub_size_t c)
 {
-  int p = 1;
-
-  while (*s1 && *s2 && p < c)
+  while (*s1 && *s2 && c--)
     {
       if (*s1 != *s2)
        return (int) *s1 - (int) *s2;
       
       s1++;
       s2++;
-      p++;
     }
 
   return (int) *s1 - (int) *s2;
 }
 
 int
-grub_strncasecmp (const char *s1, const char *s2, int c)
+grub_strncasecmp (const char *s1, const char *s2, grub_size_t c)
 {
-  int p = 1;
-
-  while (grub_tolower (*s1) && grub_tolower (*s2) && p < c)
+  while (grub_tolower (*s1) && grub_tolower (*s2) && c--)
     {
       if (grub_tolower (*s1) != grub_tolower (*s2))
        return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
       
       s1++;
       s2++;
-      p++;
     }
 
-  return (int) *s1 - (int) *s2;
+  return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
 }
 
-char *
+const char *
 grub_strchr (const char *s, int c)
 {
   while (*s)
     {
       if (*s == c)
-       return (char *) s;
+       return s;
       s++;
     }
 
   return 0;
 }
 
-char *
+const char *
 grub_strrchr (const char *s, int c)
 {
-  char *p = 0;
+  const char *p = 0;
 
   while (*s)
     {
       if (*s == c)
-       p = (char *) s;
+       p = s;
       s++;
     }
 
@@ -310,9 +304,21 @@ grub_isprint (int c)
 }
 
 int
+grub_isupper (int c)
+{
+  return (c >= 'A' && c <= 'Z');
+}
+
+int
+grub_islower (int c)
+{
+  return (c >= 'a' && c <= 'z');
+}
+
+int
 grub_isalpha (int c)
 {
-  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+  return grub_islower (c) || grub_isupper (c);
 }
 
 int
@@ -330,12 +336,21 @@ grub_isgraph (int c)
 int
 grub_tolower (int c)
 {
-  if (c >= 'A' && c <= 'Z')
+  if (grub_isupper (c))
     return c - 'A' + 'a';
 
   return c;
 }
 
+int
+grub_toupper (int c)
+{
+  if (grub_islower (c))
+    return c - 'a' + 'A';
+
+  return c;
+}
+
 unsigned long
 grub_strtoul (const char *str, char **end, int base)
 {
@@ -451,8 +466,8 @@ grub_strlen (const char *s)
 {
   const char *p = s;
 
-  while (*p)
-    p++;
+  while (*p++)
+    ;
 
   return p - s;
 }
@@ -475,21 +490,21 @@ grub_reverse (char *str)
 }
 
 static char *
-grub_itoa (char *str, int c, unsigned n)
+grub_itoa (char *str, int c, unsigned int n)
 {
-  unsigned base = (c == 'x') ? 16 : 10;
+  unsigned int base = (c == 'x') ? 16 : 10, d;
   char *p;
   
   if ((int) n < 0 && c == 'd')
     {
-      n = (unsigned) (-((int) n));
+      n = (unsigned int) (-((int) n));
       *str++ = '-';
     }
 
   p = str;
   do
     {
-      unsigned d = n % base;
+      d = n % base;
       *p++ = (d > 9) ? d + 'a' - 10 : d + '0';
     }
   while (n /= base);
@@ -500,9 +515,9 @@ grub_itoa (char *str, int c, unsigned n)
 }
 
 static char *
-grub_ftoa (char *str, double f, int round)
+grub_ftoa (char *str, float f, int round)
 {
-  unsigned int intp;
+  int intp;
   unsigned int fractp;
   unsigned int power = 1;
   int i;
@@ -513,15 +528,15 @@ grub_ftoa (char *str, double f, int roun
   intp = f;
   fractp = (f - (float) intp) * power;
 
-  grub_sprintf (str, "%d.%d", intp, fractp);
+  grub_sprintf (str, "%d.%u", intp, fractp);
   return str;
 }
 
-int
+grub_size_t
 grub_vsprintf (char *str, const char *fmt, va_list args)
 {
   char c;
-  int count = 0;
+  grub_size_t count = 0;
   auto void write_char (unsigned char ch);
   auto void write_str (const char *s);
   auto void write_fill (const char ch, int n);
@@ -729,11 +744,11 @@ grub_vsprintf (char *str, const char *fm
   return count;
 }
 
-int
+grub_size_t
 grub_sprintf (char *str, const char *fmt, ...)
 {
   va_list ap;
-  int ret;
+  grub_size_t ret;
   
   va_start (ap, fmt);
   ret = grub_vsprintf (str, fmt, ap);
Index: include/grub/misc.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/misc.h,v
retrieving revision 1.13
diff -u -p -r1.13 misc.h
--- include/grub/misc.h 9 May 2005 01:47:37 -0000       1.13
+++ include/grub/misc.h 29 Jun 2005 13:59:30 -0000
@@ -32,10 +32,10 @@
 
 void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
 char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
-char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c);
+char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, grub_size_t c);
 char *EXPORT_FUNC(grub_stpcpy) (char *dest, const char *src);
 char *EXPORT_FUNC(grub_strcat) (char *dest, const char *src);
-char *EXPORT_FUNC(grub_strncat) (char *dest, const char *src, int c);
+char *EXPORT_FUNC(grub_strncat) (char *dest, const char *src, grub_size_t c);
 
 /* Prototypes for aliases.  */
 void *EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
@@ -43,31 +43,34 @@ void *EXPORT_FUNC(memcpy) (void *dest, c
 
 int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
 int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
-int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, int c);
-int EXPORT_FUNC(grub_strncasecmp) (const char *s1, const char *s2, int c);
-char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
-char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
+int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t c);
+int EXPORT_FUNC(grub_strncasecmp) (const char *s1, const char *s2, grub_size_t 
c);
+const char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
+const char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
 int EXPORT_FUNC(grub_iswordseparator) (int c);
 int EXPORT_FUNC(grub_isspace) (int c);
 int EXPORT_FUNC(grub_isprint) (int c);
+int EXPORT_FUNC(grub_isupper) (int c);
+int EXPORT_FUNC(grub_islower) (int c);
 int EXPORT_FUNC(grub_isalpha) (int c);
 int EXPORT_FUNC(grub_isgraph) (int c);
 int EXPORT_FUNC(grub_isdigit) (int c);
 int EXPORT_FUNC(grub_tolower) (int c);
+int EXPORT_FUNC(grub_toupper) (int c);
 unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int 
base);
 char *EXPORT_FUNC(grub_strdup) (const char *s);
 char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n);
 void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n);
 grub_size_t EXPORT_FUNC(grub_strlen) (const char *s);
-int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format 
(printf, 1, 2)));
+grub_size_t EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ 
((format (printf, 1, 2)));
 void EXPORT_FUNC(grub_real_dprintf) (const char *file,
                                      const int line,
                                      const char *condition,
                                      const char *fmt, ...) __attribute__ 
((format (printf, 4, 5)));
-int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
-int EXPORT_FUNC(grub_sprintf) (char *str, const char *fmt, ...) __attribute__ 
((format (printf, 2, 3)));
-int EXPORT_FUNC(grub_vsprintf) (char *str, const char *fmt, va_list args);
+grub_size_t EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
+grub_size_t EXPORT_FUNC(grub_sprintf) (char *str, const char *fmt, ...) 
__attribute__ ((format (printf, 2, 3)));
+grub_size_t EXPORT_FUNC(grub_vsprintf) (char *str, const char *fmt, va_list 
args);
 void EXPORT_FUNC(grub_stop) (void) __attribute__ ((noreturn));
 grub_uint8_t *EXPORT_FUNC(grub_utf16_to_utf8) (grub_uint8_t *dest,
                                               grub_uint16_t *src,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]