12 vprintf_core (const char *format, va_list args,
13 void (*output) (char, void *), void *aux);
17 /* Sets the SIZE bytes in DST to VALUE. */
19 memset (void *dst_, int value, size_t size)
21 unsigned char *dst = dst_;
23 ASSERT (dst != NULL || size == 0);
31 /* Copies SIZE bytes from SRC to DST, which must not overlap.
34 memcpy (void *dst_, const void *src_, size_t size)
36 unsigned char *dst = dst_;
37 const unsigned char *src = src_;
39 ASSERT (dst != NULL || size == 0);
40 ASSERT (src != NULL || size == 0);
48 /* Copies SIZE bytes from SRC to DST, which are allowed to
49 overlap. Returns DST. */
51 memmove (void *dst_, const void *src_, size_t size)
53 unsigned char *dst = dst_;
54 const unsigned char *src = src_;
56 ASSERT (dst != NULL || size == 0);
57 ASSERT (src != NULL || size == 0);
75 /* Returns a pointer to the first occurrence of CH in the first
76 SIZE bytes starting at BLOCK. Returns a null pointer if CH
77 does not occur in BLOCK. */
79 memchr (const void *block_, int ch_, size_t size)
81 const unsigned char *block = block_;
82 unsigned char ch = ch_;
84 ASSERT (block != NULL || size == 0);
86 for (; size-- > 0; block++)
88 return (void *) block;
93 /* Find the first differing byte in the two blocks of SIZE bytes
94 at A and B. Returns a positive value if the byte in A is
95 greater, a negative value if the byte in B is greater, or zero
96 if blocks A and B are equal. */
98 memcmp (const void *a_, const void *b_, size_t size)
100 const unsigned char *a = a_;
101 const unsigned char *b = b_;
103 ASSERT (a != NULL || size == 0);
104 ASSERT (b != NULL || size == 0);
106 for (; size-- > 0; a++, b++)
108 return *a > *b ? +1 : -1;
112 /* Finds and returns the first occurrence of C in STRING, or a
113 null pointer if C does not appear in STRING. If C == '\0'
114 then returns a pointer to the null terminator at the end of
117 strchr (const char *string, int c_)
121 ASSERT (string != NULL);
125 return (char *) string;
126 else if (*string == '\0')
132 /* Copies string SRC to DST. If SRC is longer than SIZE - 1
133 characters, only SIZE - 1 characters are copied. A null
134 terminator is always written to DST, unless SIZE is 0.
135 Returns the length of SRC.
137 strlcpy() is not in the standard C library, but it is an
138 increasingly popular extension. See
139 http://www.courtesan.com/todd/papers/strlcpy.html for
140 information on strlcpy(). */
142 strlcpy (char *dst, const char *src, size_t size)
146 ASSERT (dst != NULL);
147 ASSERT (src != NULL);
149 src_len = strlen (src);
152 size_t dst_len = size - 1;
153 if (src_len < dst_len)
155 memcpy (dst, src, dst_len);
161 /* Returns the length of STRING. */
163 strlen (const char *string)
167 ASSERT (string != NULL);
169 for (p = string; *p != '\0'; p++)
174 /* If STRING is less than MAXLEN characters in length, returns
175 its actual length. Otherwise, returns MAXLEN. */
177 strnlen (const char *string, size_t maxlen)
181 for (length = 0; string[length] != '\0' && length < maxlen; length++)
186 /* Finds the first differing characters in strings A and B.
187 Returns a positive value if the character in A (as an unsigned
188 char) is greater, a negative value if the character in B (as
189 an unsigned char) is greater, or zero if strings A and B are
192 strcmp (const char *a_, const char *b_)
194 const unsigned char *a = (const unsigned char *) a_;
195 const unsigned char *b = (const unsigned char *) b_;
200 while (*a != '\0' && *a == *b)
206 return *a < *b ? -1 : *a > *b;
209 /* Breaks a string into tokens separated by DELIMITERS. The
210 first time this function is called, S should be the string to
211 tokenize, and in subsequent calls it must be a null pointer.
212 SAVE_PTR is the address of a `char *' variable used to keep
213 track of the tokenizer's position. The return value each time
214 is the next token in the string, or a null pointer if no
217 This function treats multiple adjacent delimiters as a single
218 delimiter. The returned tokens will never be length 0.
219 DELIMITERS may change from one call to the next within a
222 strtok_r() modifies the string S, changing delimiters to null
223 bytes. Thus, S must be a modifiable string.
227 char s[] = " String to tokenize. ";
228 char *token, *save_ptr;
230 for (token = strtok_r (s, " ", &save_ptr); token != NULL;
231 token = strtok_r (NULL, " ", &save_ptr))
232 printf ("'%s'\n", token);
241 strtok_r (char *s, const char *delimiters, char **save_ptr)
245 ASSERT (delimiters != NULL);
246 ASSERT (save_ptr != NULL);
248 /* If S is nonnull, start from it.
249 If S is null, start from saved position. */
254 /* Skip any DELIMITERS at our current position. */
255 while (strchr (delimiters, *s) != NULL)
257 /* strchr() will always return nonnull if we're searching
258 for a null byte, because every string contains a null
259 byte (at the end). */
269 /* Skip any non-DELIMITERS up to the end of the string. */
271 while (strchr (delimiters, *s) == NULL)
285 /* Converts a string representation of a signed decimal integer
286 in S into an `int', which is returned. */
293 /* Skip white space. */
307 /* Parse digits. We always initially parse the value as
308 negative, and then make it positive later, because the
309 negative range of an int is bigger than the positive range
310 on a 2's complement system. */
311 for (value = 0; isdigit (*s); s++)
312 value = value * 10 - (*s - '0');
321 /* Auxiliary data for vsnprintf_helper(). */
324 char *p; /* Current output position. */
325 int length; /* Length of output string. */
326 int max_length; /* Max length of output string. */
329 static void vsnprintf_helper (char, void *);
331 /* Like vprintf(), except that output is stored into BUFFER,
332 which must have space for BUF_SIZE characters. Writes at most
333 BUF_SIZE - 1 characters to BUFFER, followed by a null
334 terminator. BUFFER will always be null-terminated unless
335 BUF_SIZE is zero. Returns the number of characters that would
336 have been written to BUFFER, not including a null terminator,
337 had there been enough room. */
339 vsnprintf (char *buffer, size_t buf_size, const char *format, va_list args)
341 /* Set up aux data for vsnprintf_helper(). */
342 struct vsnprintf_aux aux;
345 aux.max_length = buf_size > 0 ? buf_size - 1 : 0;
347 /* Do most of the work. */
348 vprintf_core (format, args, vsnprintf_helper, &aux);
350 /* Add null terminator. */
357 /* Helper function for vsnprintf(). */
359 vsnprintf_helper (char ch, void *aux_)
361 struct vsnprintf_aux *aux = aux_;
363 if (aux->length++ < aux->max_length)
367 /* Like printf(), except that output is stored into BUFFER,
368 which must have space for BUF_SIZE characters. Writes at most
369 BUF_SIZE - 1 characters to BUFFER, followed by a null
370 terminator. BUFFER will always be null-terminated unless
371 BUF_SIZE is zero. Returns the number of characters that would
372 have been written to BUFFER, not including a null terminator,
373 had there been enough room. */
375 snprintf (char *buffer, size_t buf_size, const char *format, ...)
380 va_start (args, format);
381 retval = vsnprintf (buffer, buf_size, format, args);
387 /* Nonstandard functions. */
389 static void vprintk_helper (char, void *);
391 /* Like vprintf(), except that output is written to the system
392 console, which is defined as the video display and the first
393 serial port (at the same time). */
395 vprintk (const char *format, va_list args)
397 enum intr_level old_level = intr_disable ();
398 vprintf_core (format, args, vprintk_helper, NULL);
399 intr_set_level (old_level);
402 /* Helper function for vprintk(). */
404 vprintk_helper (char ch, void *aux UNUSED)
410 /* Like printf(), except that output is written to the system
411 console, which is defined as the video display and the first
412 serial port (at the same time). */
414 printk (const char *format, ...)
418 va_start (args, format);
419 vprintk (format, args);
423 /* printf() formatting internals. */
425 /* A printf() conversion. */
426 struct printf_conversion
431 MINUS = 1 << 0, /* '-' */
432 PLUS = 1 << 1, /* '+' */
433 SPACE = 1 << 2, /* ' ' */
434 POUND = 1 << 3, /* '#' */
435 ZERO = 1 << 4, /* '0' */
436 GROUP = 1 << 5 /* '\'' */
440 /* Minimum field width. */
443 /* Numeric precision.
444 -1 indicates no precision was specified. */
447 /* Type of argument to format. */
452 INT = 3, /* (none) */
455 LONGLONG = 6, /* ll */
456 PTRDIFFT = 7, /* t */
464 int base; /* Base. */
465 const char *digits; /* Collection of digits. */
466 const char *signifier; /* Prefix used with # flag. */
467 int group; /* Number of digits to group with ' flag. */
470 static const struct integer_base base_d = {10, "0123456789", "", 3};
471 static const struct integer_base base_o = {8, "01234567", "0", 3};
472 static const struct integer_base base_x = {16, "0123456789abcdef", "0x", 4};
473 static const struct integer_base base_X = {16, "0123456789ABCDEF", "0x", 4};
475 static const char *parse_conversion (const char *format,
476 struct printf_conversion *,
478 static void format_integer (uintmax_t value, bool negative,
479 const struct integer_base *,
480 const struct printf_conversion *,
481 void (*output) (char, void *), void *aux);
482 static void output_dup (char ch, size_t cnt,
483 void (*output) (char, void *), void *aux);
484 static void format_string (const char *string, size_t length,
485 struct printf_conversion *,
486 void (*output) (char, void *), void *aux);
487 static void printf_core (const char *format,
488 void (*output) (char, void *), void *aux, ...);
491 vprintf_core (const char *format, va_list args,
492 void (*output) (char, void *), void *aux)
494 for (; *format != '\0'; format++)
496 struct printf_conversion c;
498 /* Literally copy non-conversions to output. */
501 output (*format, aux);
513 /* Parse conversion specifiers. */
514 format = parse_conversion (format, &c, &args);
522 /* Signed integer conversions. */
528 value = (signed char) va_arg (args, int);
531 value = (short) va_arg (args, int);
534 value = va_arg (args, int);
537 value = va_arg (args, long);
540 value = va_arg (args, long long);
543 value = va_arg (args, ptrdiff_t);
546 value = va_arg (args, size_t);
552 format_integer (value < 0 ? -value : value,
553 value < 0, &base_d, &c, output, aux);
562 /* Unsigned integer conversions. */
564 const struct integer_base *b;
569 value = (unsigned char) va_arg (args, unsigned);
572 value = (unsigned short) va_arg (args, unsigned);
575 value = va_arg (args, unsigned);
578 value = va_arg (args, unsigned long);
581 value = va_arg (args, unsigned long long);
584 value = va_arg (args, ptrdiff_t);
587 value = va_arg (args, size_t);
595 case 'o': b = &base_o; break;
596 case 'u': b = &base_d; break;
597 case 'x': b = &base_x; break;
598 case 'X': b = &base_X; break;
599 default: NOT_REACHED ();
602 format_integer (value, false, b, &c, output, aux);
608 /* Treat character as single-character string. */
609 char ch = va_arg (args, int);
610 format_string (&ch, 1, &c, output, aux);
616 /* String conversion. */
617 const char *s = va_arg (args, char *);
621 /* Limit string length according to precision.
622 Note: if c.precision == -1 then strnlen() will get
623 SIZE_MAX for MAXLEN, which is just what we want. */
624 format_string (s, strnlen (s, c.precision), &c, output, aux);
630 /* Pointer conversion.
631 Format non-null pointers as %#x. */
632 void *p = va_arg (args, void *);
636 format_integer ((uintptr_t) p, false, &base_x, &c, output, aux);
638 format_string ("(nil)", 5, &c, output, aux);
648 /* We don't support floating-point arithmetic,
649 and %n can be part of a security hole. */
650 printf_core ("<<no %%%c in kernel>>", output, aux, *format);
654 printf_core ("<<no %%%c conversion>>", output, aux, *format);
660 /* Parses conversion option characters starting at FORMAT and
661 initializes C appropriately. Returns the character in FORMAT
662 that indicates the conversion (e.g. the `d' in `%d'). Uses
663 *ARGS for `*' field widths and precisions. */
665 parse_conversion (const char *format, struct printf_conversion *c,
668 /* Parse flag characters. */
698 if (c->flags & MINUS)
703 /* Parse field width. */
708 c->width = va_arg (*args, int);
712 for (; isdigit (*format); format++)
713 c->width = c->width * 10 + *format - '0';
717 c->width = -c->width;
721 /* Parse precision. */
729 c->precision = va_arg (*args, int);
734 for (; isdigit (*format); format++)
735 c->precision = c->precision * 10 + *format - '0';
737 if (c->precision < 0)
740 if (c->precision >= 0)
787 /* Performs an integer conversion, writing output to OUTPUT with
788 auxiliary data AUX. The integer converted has absolute value
789 VALUE. If NEGATIVE is true the value is negative, otherwise
790 positive. The output will use the given DIGITS, with
791 strlen(DIGITS) indicating the output base. Details of the
792 conversion are in C. */
794 format_integer (uintmax_t value, bool negative, const struct integer_base *b,
795 const struct printf_conversion *c,
796 void (*output) (char, void *), void *aux)
798 char buf[64], *cp; /* Buffer and current position. */
799 const char *signifier; /* b->signifier or "". */
800 int precision; /* Rendered precision. */
801 int pad_cnt; /* # of pad characters to fill field width. */
802 int group_cnt; /* # of digits grouped so far. */
804 /* Accumulate digits into buffer.
805 This algorithm produces digits in reverse order, so later we
806 will output the buffer's content in reverse. This is also
807 the reason that later we append zeros and the sign. */
812 if ((c->flags & GROUP) && group_cnt++ == b->group)
817 *cp++ = b->digits[value % b->base];
821 /* Append enough zeros to match precision.
822 If requested precision is 0, then a value of zero is
823 rendered as a null string, otherwise as "0". */
824 precision = c->precision < 0 ? 1 : c->precision;
827 while (cp - buf < precision && cp - buf < (int) sizeof buf - 8)
832 *cp++ = negative ? '-' : '+';
833 else if (c->flags & SPACE)
834 *cp++ = negative ? '-' : ' ';
838 /* Calculate number of pad characters to fill field width. */
839 signifier = c->flags & POUND ? b->signifier : "";
840 pad_cnt = c->width - (cp - buf) - strlen (signifier);
845 if ((c->flags & (MINUS | ZERO)) == 0)
846 output_dup (' ', pad_cnt, output, aux);
847 while (*signifier != '\0')
848 output (*signifier++, aux);
850 output_dup ('0', pad_cnt, output, aux);
853 if (c->flags & MINUS)
854 output_dup (' ', pad_cnt, output, aux);
857 /* Writes CH to OUTPUT with auxiliary data AUX, CNT times. */
859 output_dup (char ch, size_t cnt, void (*output) (char, void *), void *aux)
865 /* Formats the LENGTH characters starting at STRING according to
866 the conversion specified in C. Writes output to OUTPUT with
867 auxiliary data AUX. */
869 format_string (const char *string, size_t length,
870 struct printf_conversion *c,
871 void (*output) (char, void *), void *aux)
873 if (c->width > 1 && (c->flags & MINUS) == 0)
874 output_dup (' ', c->width - 1, output, aux);
876 output (*string++, aux);
877 if (c->width > 1 && (c->flags & MINUS) != 0)
878 output_dup (' ', c->width - 1, output, aux);
881 /* Wrapper for vprintf_core() that converts varargs into a
884 printf_core (const char *format,
885 void (*output) (char, void *), void *aux, ...)
889 va_start (args, aux);
890 vprintf_core (format, args, output, aux);
894 /* Dumps the SIZE bytes in BUFFER to the console as hex bytes
895 arranged 16 per line. If ASCII is true then the corresponding
896 ASCII characters are also rendered alongside. */
898 hex_dump (const void *buffer, size_t size, bool ascii)
900 const size_t n_per_line = 16; /* Maximum bytes per line. */
901 size_t n; /* Number of bytes in this line. */
902 const uint8_t *p; /* Start of current line in buffer. */
904 for (p = buffer; p < (uint8_t *) buffer + size; p += n)
908 /* Number of bytes on this line. */
909 n = (uint8_t *) (buffer + size) - p;
914 for (i = 0; i < n; i++)
915 printk ("%c%02x", i == n_per_line / 2 ? '-' : ' ', (unsigned) p[i]);
918 for (; i < n_per_line; i++)
921 for (i = 0; i < n; i++)
922 printk ("%c", isprint (p[i]) ? p[i] : '.');
923 for (; i < n_per_line; i++)