Make interrupt.h names more regular.
[pintos-anon] / src / lib / lib.c
index 79dff9ab0725753bb89321c25f3ffa892691e8b0..c88c4a6b40f5c2b97dfb8485cc462f5e92edc1b2 100644 (file)
@@ -9,44 +9,52 @@
 #include "vga.h"
 
 void *
-memset (void *dst_, int value, size_t cnt
+memset (void *dst_, int value, size_t size
 {
   unsigned char *dst = dst_;
+
+  ASSERT (dst != NULL || size == 0);
   
-  while (cnt-- > 0)
+  while (size-- > 0)
     *dst++ = value;
 
   return dst_;
 }
 
 void *
-memcpy (void *dst_, const void *src_, size_t cnt
+memcpy (void *dst_, const void *src_, size_t size
 {
   unsigned char *dst = dst_;
   const unsigned char *src = src_;
 
-  while (cnt-- > 0)
+  ASSERT (dst != NULL || size == 0);
+  ASSERT (src != NULL || size == 0);
+
+  while (size-- > 0)
     *dst++ = *src++;
 
   return dst_;
 }
 
 void *
-memmove (void *dst_, const void *src_, size_t cnt
+memmove (void *dst_, const void *src_, size_t size
 {
   unsigned char *dst = dst_;
   const unsigned char *src = src_;
 
+  ASSERT (dst != NULL || size == 0);
+  ASSERT (src != NULL || size == 0);
+
   if (dst < src) 
     {
-      while (cnt-- > 0)
+      while (size-- > 0)
         *dst++ = *src++;
     }
   else 
     {
-      dst += cnt;
-      src += cnt;
-      while (cnt-- > 0)
+      dst += size;
+      src += size;
+      while (size-- > 0)
         *--dst = *--src;
     }
 
@@ -59,6 +67,8 @@ memchr (const void *block_, int ch_, size_t size)
   const unsigned char *block = block_;
   unsigned char ch = ch_;
 
+  ASSERT (block != NULL || size == 0);
+
   for (; size-- > 0; block++)
     if (*block == ch)
       return (void *) block;
@@ -72,6 +82,9 @@ memcmp (const void *a_, const void *b_, size_t size)
   const unsigned char *a = a_;
   const unsigned char *b = b_;
 
+  ASSERT (a != NULL || size == 0);
+  ASSERT (b != NULL || size == 0);
+
   for (; size-- > 0; a++, b++)
     if (*a != *b)
       return *a > *b ? +1 : -1;
@@ -81,7 +94,12 @@ memcmp (const void *a_, const void *b_, size_t size)
 size_t
 strlcpy (char *dst, const char *src, size_t size) 
 {
-  size_t src_len = strlen (src);
+  size_t src_len;
+
+  ASSERT (dst != NULL);
+  ASSERT (src != NULL);
+
+  src_len = strlen (src);
   if (size > 0) 
     {
       size_t dst_len_max = size - 1;
@@ -97,6 +115,8 @@ strlen (const char *string)
 {
   const char *p;
 
+  ASSERT (string != NULL);
+
   for (p = string; *p != '\0'; p++)
     continue;
   return p - string;
@@ -107,12 +127,108 @@ strchr (const char *string, int c_)
 {
   char c = c_;
 
+  ASSERT (string != NULL);
+
   for (;;) 
     if (*string == c)
       return (char *) string;
     else if (*string == '\0')
       return NULL;
-    else string++;
+    else
+      string++;
+}
+
+int
+strcmp (const char *a_, const char *b_) 
+{
+  const unsigned char *a = (const unsigned char *) a_;
+  const unsigned char *b = (const unsigned char *) b_;
+
+  ASSERT (a != NULL);
+  ASSERT (b != NULL);
+
+  while (*a != '\0' && *a == *b) 
+    {
+      a++;
+      b++;
+    }
+
+  return *a < *b ? -1 : *a > *b;
+}
+
+char *
+strtok_r (char *s, const char *delimiters, char **save_ptr) 
+{
+  char *token;
+  
+  ASSERT (delimiters != NULL);
+  ASSERT (save_ptr != NULL);
+
+  /* If S is nonnull, start from it.
+     If S is null, start from saved position. */
+  if (s == NULL)
+    s = *save_ptr;
+  ASSERT (s != NULL);
+
+  /* Skip any DELIMITERS at our current position. */
+  while (strchr (delimiters, *s) != NULL) 
+    {
+      /* strchr() will always return nonnull if we're searching
+         for a null byte, because every string contains a null
+         byte (at the end). */
+      if (*s == '\0')
+        {
+          *save_ptr = s;
+          return NULL;
+        }
+
+      s++;
+    }
+
+  /* Skip any non-DELIMITERS up to the end of the string. */
+  token = s;
+  while (strchr (delimiters, *s) == NULL)
+    s++;
+  if (*s != '\0') 
+    {
+      *s = '\0';
+      *save_ptr = s + 1;
+    }
+  else 
+    *save_ptr = s;
+  return token;
+}
+
+int
+atoi (const char *s) 
+{
+  bool negative;
+  int value;
+
+  /* Skip white space. */
+  while (isspace (*s))
+    s++;
+
+  /* Parse sign. */
+  negative = false;
+  if (*s == '+')
+    s++;
+  else if (*s == '-')
+    {
+      negative = true;
+      s++;
+    }
+
+  /* Parse digits.  We always initially parse the value as
+     negative, and then make it positive later, because the
+     negative range of an int is bigger than the positive range
+     on a 2's complement system. */
+  for (value = 0; isdigit (*s); s++)
+    value = value * 10 - (*s - '0');
+  if (!negative)
+    value = -value;
+
+  return value;
 }
 \f
 static void
@@ -120,7 +236,7 @@ vprintf_core (const char *format, va_list args,
               void (*output) (char, void *), void *aux);
 
 static void
-vprintk_helper (char ch, void *aux __attribute__ ((unused))
+vprintk_helper (char ch, void *aux UNUSED
 {
   vga_putc (ch);
   serial_outb (ch);
@@ -129,7 +245,7 @@ vprintk_helper (char ch, void *aux __attribute__ ((unused)))
 void
 vprintk (const char *format, va_list args) 
 {
-  enum if_level old_level = intr_disable ();
+  enum intr_level old_level = intr_disable ();
   vprintf_core (format, args, vprintk_helper, NULL);
   intr_set_level (old_level);
 }
@@ -182,10 +298,13 @@ snprintf (char *buffer, size_t buf_size,
           const char *format, ...) 
 {
   va_list args;
+  int retval;
 
   va_start (args, format);
-  vsnprintf (buffer, buf_size, format, args);
+  retval = vsnprintf (buffer, buf_size, format, args);
   va_end (args);
+
+  return retval;
 }
 \f
 /* printf() and friends internals.  You do not need to understand
@@ -199,7 +318,8 @@ struct printf_conversion
         PLUS = 1 << 1,
         SPACE = 1 << 2,
         POUND = 1 << 3,
-        ZERO = 1 << 4
+        ZERO = 1 << 4,
+        GROUP = 1 << 5
       }
     flags;
 
@@ -245,6 +365,9 @@ parse_conversion (const char *format, struct printf_conversion *c,
         case '0':
           c->flags |= ZERO;
           break;
+        case '\'':
+          c->flags |= GROUP;
+          break;
         default:
           format--;
           goto not_a_flag;
@@ -362,7 +485,7 @@ printf_integer (uintmax_t value, bool negative, const char *digits,
   char buf[64], *cp;
   int base;
   const char *base_name;
-  int pad_cnt;
+  int pad_cnt, group_cnt;
 
   base = strlen (digits);
 
@@ -371,8 +494,14 @@ printf_integer (uintmax_t value, bool negative, const char *digits,
      will output the buffer's content in reverse.  This is also
      the reason that later we append zeros and the sign. */
   cp = buf;
+  group_cnt = 0;
   while (value > 0) 
     {
+      if ((c->flags & GROUP) && group_cnt++ == 3) 
+        {
+          *cp++ = ',';
+          group_cnt = 0; 
+        }
       *cp++ = digits[value % base];
       value /= base;
     }
@@ -635,3 +764,38 @@ vprintf_core (const char *format, va_list args,
         }
     }
 }
+\f
+void
+hex_dump (const void *buffer, size_t size, bool ascii)
+{
+  const size_t n_per_line = 16;
+  const uint8_t *p = buffer;
+  size_t ofs = 0;
+
+  while (size > 0) 
+    {
+      size_t n, i;
+
+      printk ("%08zx", ofs);
+      n = size >= n_per_line ? n_per_line : size;
+      for (i = 0; i < n; i++) 
+        printk ("%c%02x", i == n_per_line / 2 ? '-' : ' ', (unsigned) p[i]);
+
+      if (ascii) 
+        {
+          for (; i < n_per_line; i++)
+            printk ("   ");
+          printk (" |");
+          for (i = 0; i < n; i++)
+            printk ("%c", isprint (p[i]) ? p[i] : '.');
+          for (; i < n_per_line; i++)
+            printk (" ");
+          printk ("|");
+        }
+      printk ("\n");
+
+      p += n;
+      ofs += n;
+      size -= n;
+    }
+}