Skip to content
Snippets Groups Projects
vsprintf.c 46.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	if (*fmt == '.') {
    		++fmt;
    		if (isdigit(*fmt)) {
    			spec->precision = skip_atoi(&fmt);
    			if (spec->precision < 0)
    				spec->precision = 0;
    		} else if (*fmt == '*') {
    			/* it's the next argument */
    
    			return ++fmt - start;
    		}
    	}
    
    qualifier:
    	/* get the conversion qualifier */
    	spec->qualifier = -1;
    
    	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
    	    _tolower(*fmt) == 'z' || *fmt == 't') {
    
    		spec->qualifier = *fmt++;
    		if (unlikely(spec->qualifier == *fmt)) {
    			if (spec->qualifier == 'l') {
    				spec->qualifier = 'L';
    				++fmt;
    			} else if (spec->qualifier == 'h') {
    				spec->qualifier = 'H';
    				++fmt;
    			}
    
    		}
    	}
    
    	/* default base */
    	spec->base = 10;
    	switch (*fmt) {
    	case 'c':
    		spec->type = FORMAT_TYPE_CHAR;
    		return ++fmt - start;
    
    	case 's':
    		spec->type = FORMAT_TYPE_STR;
    		return ++fmt - start;
    
    	case 'p':
    		spec->type = FORMAT_TYPE_PTR;
    		return fmt - start;
    		/* skip alnum */
    
    	case 'n':
    		spec->type = FORMAT_TYPE_NRCHARS;
    		return ++fmt - start;
    
    	case '%':
    		spec->type = FORMAT_TYPE_PERCENT_CHAR;
    		return ++fmt - start;
    
    	/* integer number formats - set up the flags and "break" */
    	case 'o':
    		spec->base = 8;
    		break;
    
    	case 'x':
    		spec->flags |= SMALL;
    
    	case 'X':
    		spec->base = 16;
    		break;
    
    	case 'd':
    	case 'i':
    
    
    	default:
    		spec->type = FORMAT_TYPE_INVALID;
    		return fmt - start;
    
    
    	if (spec->qualifier == 'L')
    		spec->type = FORMAT_TYPE_LONG_LONG;
    	else if (spec->qualifier == 'l') {
    
    		if (spec->flags & SIGN)
    
    			spec->type = FORMAT_TYPE_LONG;
    		else
    			spec->type = FORMAT_TYPE_ULONG;
    
    	} else if (_tolower(spec->qualifier) == 'z') {
    
    		spec->type = FORMAT_TYPE_SIZE_T;
    	} else if (spec->qualifier == 't') {
    		spec->type = FORMAT_TYPE_PTRDIFF;
    
    	} else if (spec->qualifier == 'H') {
    		if (spec->flags & SIGN)
    			spec->type = FORMAT_TYPE_BYTE;
    		else
    			spec->type = FORMAT_TYPE_UBYTE;
    
    	} else if (spec->qualifier == 'h') {
    
    		if (spec->flags & SIGN)
    
    			spec->type = FORMAT_TYPE_SHORT;
    		else
    			spec->type = FORMAT_TYPE_USHORT;
    	} else {
    
    		if (spec->flags & SIGN)
    
    			spec->type = FORMAT_TYPE_INT;
    		else
    			spec->type = FORMAT_TYPE_UINT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     * vsnprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @args: Arguments for the format string
     *
    
     * This function follows C99 vsnprintf, but has some extensions:
    
     * %pS output the name of a text symbol with offset
     * %ps output the name of a text symbol without offset
    
     * %pF output the name of a function pointer with its offset
     * %pf output the name of a function pointer without its offset
    
     * %pB output the name of a backtrace symbol with its offset
    
     * %pR output the address range in a struct resource with decoded flags
     * %pr output the address range in a struct resource with raw flags
     * %pM output a 6-byte MAC address with colons
     * %pm output a 6-byte MAC address without colons
     * %pI4 print an IPv4 address without leading zeros
     * %pi4 print an IPv4 address with leading zeros
     * %pI6 print an IPv6 address with colons
     * %pi6 print an IPv6 address without colons
    
     * %pI6c print an IPv6 address as specified by RFC 5952
    
     * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
     *   case.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * The return value is the number of characters which would
     * be generated for the given input, excluding the trailing
     * '\0', as per ISO C99. If you want to have the exact
     * number of characters written into @buf as return value
    
     * (not including the trailing '\0'), use vscnprintf(). If the
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * return is greater than or equal to @size, the resulting
     * string is truncated.
     *
    
     * If you're not already dealing with a va_list consider using snprintf().
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
    {
    	unsigned long long num;
    
    	struct printf_spec spec = {0};
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* Reject out-of-range values early.  Large positive sizes are
    	   used for unknown buffer sizes. */
    
    	if (WARN_ON_ONCE((int) size < 0))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return 0;
    
    	str = buf;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* Make sure end is always >= buf */
    	if (end < buf) {
    		end = ((void *)-1);
    		size = end - buf;
    
    	while (*fmt) {
    		const char *old_fmt = fmt;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		switch (spec.type) {
    		case FORMAT_TYPE_NONE: {
    			int copy = read;
    			if (str < end) {
    				if (copy > end - str)
    					copy = end - str;
    				memcpy(str, old_fmt, copy);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    
    			spec.field_width = va_arg(args, int);
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_PRECISION:
    			spec.precision = va_arg(args, int);
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (!(spec.flags & LEFT)) {
    				while (--spec.field_width > 0) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    						*str = ' ';
    					++str;
    
    
    				}
    			}
    			c = (unsigned char) va_arg(args, int);
    			if (str < end)
    				*str = c;
    			++str;
    			while (--spec.field_width > 0) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				++str;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_STR:
    			str = string(str, end, va_arg(args, char *), spec);
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_PTR:
    			str = pointer(fmt+1, str, end, va_arg(args, void *),
    				      spec);
    			while (isalnum(*fmt))
    				fmt++;
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_PERCENT_CHAR:
    			if (str < end)
    				*str = '%';
    			++str;
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		case FORMAT_TYPE_INVALID:
    			if (str < end)
    				*str = '%';
    			++str;
    			break;
    
    		case FORMAT_TYPE_NRCHARS: {
    
    			u8 qualifier = spec.qualifier;
    
    
    			if (qualifier == 'l') {
    				long *ip = va_arg(args, long *);
    				*ip = (str - buf);
    
    			} else if (_tolower(qualifier) == 'z') {
    
    				size_t *ip = va_arg(args, size_t *);
    				*ip = (str - buf);
    			} else {
    				int *ip = va_arg(args, int *);
    				*ip = (str - buf);
    			}
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    
    		default:
    			switch (spec.type) {
    			case FORMAT_TYPE_LONG_LONG:
    				num = va_arg(args, long long);
    				break;
    			case FORMAT_TYPE_ULONG:
    				num = va_arg(args, unsigned long);
    				break;
    			case FORMAT_TYPE_LONG:
    				num = va_arg(args, long);
    				break;
    			case FORMAT_TYPE_SIZE_T:
    				num = va_arg(args, size_t);
    				break;
    			case FORMAT_TYPE_PTRDIFF:
    				num = va_arg(args, ptrdiff_t);
    				break;
    
    			case FORMAT_TYPE_UBYTE:
    				num = (unsigned char) va_arg(args, int);
    				break;
    			case FORMAT_TYPE_BYTE:
    				num = (signed char) va_arg(args, int);
    				break;
    
    			case FORMAT_TYPE_USHORT:
    				num = (unsigned short) va_arg(args, int);
    				break;
    			case FORMAT_TYPE_SHORT:
    				num = (short) va_arg(args, int);
    				break;
    
    			case FORMAT_TYPE_INT:
    				num = (int) va_arg(args, int);
    
    				break;
    			default:
    				num = va_arg(args, unsigned int);
    			}
    
    			str = number(str, end, num, spec);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    	}
    
    			end[-1] = '\0';
    
    	/* the trailing null byte doesn't count towards the total */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return str-buf;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(vsnprintf);
    
    /**
     * vscnprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @args: Arguments for the format string
     *
     * The return value is the number of characters which have been written into
    
     * the @buf not including the trailing '\0'. If @size is == 0 the function
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * returns 0.
     *
    
     * If you're not already dealing with a va_list consider using scnprintf().
    
     *
     * See the vsnprintf() documentation for format string extensions over C99.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
    {
    	int i;
    
    
    	i = vsnprintf(buf, size, fmt, args);
    
    
    	if (likely(i < size))
    		return i;
    	if (size != 0)
    		return size - 1;
    	return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(vscnprintf);
    
    /**
     * snprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @...: Arguments for the format string
     *
     * The return value is the number of characters which would be
     * generated for the given input, excluding the trailing null,
     * as per ISO C99.  If the return is greater than or equal to
     * @size, the resulting string is truncated.
    
     *
     * See the vsnprintf() documentation for format string extensions over C99.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    int snprintf(char *buf, size_t size, const char *fmt, ...)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	va_list args;
    	int i;
    
    	va_start(args, fmt);
    
    	i = vsnprintf(buf, size, fmt, args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	va_end(args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return i;
    }
    EXPORT_SYMBOL(snprintf);
    
    /**
     * scnprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @...: Arguments for the format string
     *
     * The return value is the number of characters written into @buf not including
    
     * the trailing '\0'. If @size is == 0 the function returns 0.
    
    int scnprintf(char *buf, size_t size, const char *fmt, ...)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	va_list args;
    	int i;
    
    	va_start(args, fmt);
    
    	i = vscnprintf(buf, size, fmt, args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	va_end(args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(scnprintf);
    
    /**
     * vsprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @fmt: The format string to use
     * @args: Arguments for the format string
     *
     * The function returns the number of characters written
    
     * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * buffer overflows.
     *
    
     * If you're not already dealing with a va_list consider using sprintf().
    
     *
     * See the vsnprintf() documentation for format string extensions over C99.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    int vsprintf(char *buf, const char *fmt, va_list args)
    {
    	return vsnprintf(buf, INT_MAX, fmt, args);
    }
    EXPORT_SYMBOL(vsprintf);
    
    /**
     * sprintf - Format a string and place it in a buffer
     * @buf: The buffer to place the result into
     * @fmt: The format string to use
     * @...: Arguments for the format string
     *
     * The function returns the number of characters written
    
     * into @buf. Use snprintf() or scnprintf() in order to avoid
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * buffer overflows.
    
     *
     * See the vsnprintf() documentation for format string extensions over C99.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    int sprintf(char *buf, const char *fmt, ...)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	va_list args;
    	int i;
    
    	va_start(args, fmt);
    
    	i = vsnprintf(buf, INT_MAX, fmt, args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	va_end(args);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return i;
    }
    EXPORT_SYMBOL(sprintf);
    
    
    #ifdef CONFIG_BINARY_PRINTF
    /*
     * bprintf service:
     * vbin_printf() - VA arguments to binary data
     * bstr_printf() - Binary data to text string
     */
    
    /**
     * vbin_printf - Parse a format string and place args' binary value in a buffer
     * @bin_buf: The buffer to place args' binary value
     * @size: The size of the buffer(by words(32bits), not characters)
     * @fmt: The format string to use
     * @args: Arguments for the format string
     *
     * The format follows C99 vsnprintf, except %n is ignored, and its argument
     * is skiped.
     *
     * The return value is the number of words(32bits) which would be generated for
     * the given input.
     *
     * NOTE:
     * If the return value is greater than @size, the resulting bin_buf is NOT
     * valid for bstr_printf().
     */
    int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
    {
    
    	struct printf_spec spec = {0};
    
    	char *str, *end;
    
    	str = (char *)bin_buf;
    	end = (char *)(bin_buf + size);
    
    #define save_arg(type)							\
    do {									\
    	if (sizeof(type) == 8) {					\
    		unsigned long long value;				\
    		str = PTR_ALIGN(str, sizeof(u32));			\
    		value = va_arg(args, unsigned long long);		\
    		if (str + sizeof(type) <= end) {			\
    			*(u32 *)str = *(u32 *)&value;			\
    			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
    		}							\
    	} else {							\
    		unsigned long value;					\
    		str = PTR_ALIGN(str, sizeof(type));			\
    		value = va_arg(args, int);				\
    		if (str + sizeof(type) <= end)				\
    			*(typeof(type) *)str = (type)value;		\
    	}								\
    	str += sizeof(type);						\
    } while (0)
    
    
    		switch (spec.type) {
    		case FORMAT_TYPE_NONE:
    
    		case FORMAT_TYPE_INVALID:
    		case FORMAT_TYPE_PERCENT_CHAR:
    
    		case FORMAT_TYPE_PRECISION:
    			save_arg(int);
    			break;
    
    		case FORMAT_TYPE_CHAR:
    
    			save_arg(char);
    
    			const char *save_str = va_arg(args, char *);
    			size_t len;
    
    			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
    					|| (unsigned long)save_str < PAGE_SIZE)
    
    				save_str = "(null)";
    
    			len = strlen(save_str) + 1;
    			if (str + len < end)
    				memcpy(str, save_str, len);
    			str += len;
    
    			save_arg(void *);
    			/* skip all alphanumeric pointer suffixes */
    
    				fmt++;
    
    			/* skip %n 's argument */
    
    			u8 qualifier = spec.qualifier;
    
    			void *skip_arg;
    			if (qualifier == 'l')
    				skip_arg = va_arg(args, long *);
    
    			else if (_tolower(qualifier) == 'z')
    
    				skip_arg = va_arg(args, size_t *);
    			else
    				skip_arg = va_arg(args, int *);
    
    
    		default:
    			switch (spec.type) {
    
    			case FORMAT_TYPE_LONG_LONG:
    
    				save_arg(long long);
    
    				break;
    			case FORMAT_TYPE_ULONG:
    			case FORMAT_TYPE_LONG:
    
    				save_arg(unsigned long);
    
    				save_arg(size_t);
    
    				save_arg(ptrdiff_t);
    
    			case FORMAT_TYPE_UBYTE:
    			case FORMAT_TYPE_BYTE:
    				save_arg(char);
    				break;
    
    			case FORMAT_TYPE_USHORT:
    			case FORMAT_TYPE_SHORT:
    
    				save_arg(short);
    
    				save_arg(int);
    
    	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
    
    }
    EXPORT_SYMBOL_GPL(vbin_printf);
    
    /**
     * bstr_printf - Format a string from binary arguments and place it in a buffer
     * @buf: The buffer to place the result into
     * @size: The size of the buffer, including the trailing null space
     * @fmt: The format string to use
     * @bin_buf: Binary arguments for the format string
     *
     * This function like C99 vsnprintf, but the difference is that vsnprintf gets
     * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
     * a binary buffer that generated by vbin_printf.
     *
     * The format follows C99 vsnprintf, but has some extensions:
    
     *  see vsnprintf comment for details.
    
     *
     * The return value is the number of characters which would
     * be generated for the given input, excluding the trailing
     * '\0', as per ISO C99. If you want to have the exact
     * number of characters written into @buf as return value
     * (not including the trailing '\0'), use vscnprintf(). If the
     * return is greater than or equal to @size, the resulting
     * string is truncated.
     */
    int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
    {
    
    	struct printf_spec spec = {0};
    
    	char *str, *end;
    	const char *args = (const char *)bin_buf;
    
    	if (WARN_ON_ONCE((int) size < 0))
    
    		return 0;
    
    	str = buf;
    	end = buf + size;
    
    #define get_arg(type)							\
    ({									\
    	typeof(type) value;						\
    	if (sizeof(type) == 8) {					\
    		args = PTR_ALIGN(args, sizeof(u32));			\
    		*(u32 *)&value = *(u32 *)args;				\
    		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
    	} else {							\
    		args = PTR_ALIGN(args, sizeof(type));			\
    		value = *(typeof(type) *)args;				\
    	}								\
    	args += sizeof(type);						\
    	value;								\
    })
    
    	/* Make sure end is always >= buf */
    	if (end < buf) {
    		end = ((void *)-1);
    		size = end - buf;
    	}
    
    
    	while (*fmt) {
    		const char *old_fmt = fmt;
    
    		switch (spec.type) {
    		case FORMAT_TYPE_NONE: {
    			int copy = read;
    			if (str < end) {
    				if (copy > end - str)
    					copy = end - str;
    				memcpy(str, old_fmt, copy);
    
    			spec.field_width = get_arg(int);
    			break;
    
    		case FORMAT_TYPE_PRECISION:
    			spec.precision = get_arg(int);
    			break;
    
    			if (!(spec.flags & LEFT)) {
    				while (--spec.field_width > 0) {
    
    					if (str < end)
    						*str = ' ';
    					++str;
    				}
    			}
    			c = (unsigned char) get_arg(char);
    			if (str < end)
    				*str = c;
    			++str;
    
    			while (--spec.field_width > 0) {
    
    				if (str < end)
    					*str = ' ';
    				++str;
    			}
    
    			const char *str_arg = args;
    
    			str = string(str, end, (char *)str_arg, spec);
    			break;
    
    		case FORMAT_TYPE_PTR:
    			str = pointer(fmt+1, str, end, get_arg(void *), spec);
    			while (isalnum(*fmt))
    
    				fmt++;
    
    		case FORMAT_TYPE_PERCENT_CHAR:
    		case FORMAT_TYPE_INVALID:
    
    			if (str < end)
    				*str = '%';
    			++str;
    
    			switch (spec.type) {
    
    			case FORMAT_TYPE_LONG_LONG:
    				num = get_arg(long long);
    				break;
    			case FORMAT_TYPE_ULONG:
    			case FORMAT_TYPE_LONG:
    				num = get_arg(unsigned long);
    				break;
    			case FORMAT_TYPE_SIZE_T:
    				num = get_arg(size_t);
    				break;
    			case FORMAT_TYPE_PTRDIFF:
    				num = get_arg(ptrdiff_t);
    				break;
    
    			case FORMAT_TYPE_UBYTE:
    				num = get_arg(unsigned char);
    				break;
    			case FORMAT_TYPE_BYTE:
    				num = get_arg(signed char);
    				break;
    
    			case FORMAT_TYPE_USHORT:
    				num = get_arg(unsigned short);
    				break;
    			case FORMAT_TYPE_SHORT:
    				num = get_arg(short);
    				break;
    			case FORMAT_TYPE_UINT:
    				num = get_arg(unsigned int);
    				break;
    			default:
    				num = get_arg(int);
    			}
    
    			str = number(str, end, num, spec);
    
    		} /* default: */
    		} /* switch(spec.type) */
    	} /* while(*fmt) */
    
    	if (size > 0) {
    		if (str < end)
    			*str = '\0';
    		else
    			end[-1] = '\0';
    	}
    
    #undef get_arg
    
    	/* the trailing null byte doesn't count towards the total */
    	return str - buf;
    }
    EXPORT_SYMBOL_GPL(bstr_printf);
    
    /**
     * bprintf - Parse a format string and place args' binary value in a buffer
     * @bin_buf: The buffer to place args' binary value
     * @size: The size of the buffer(by words(32bits), not characters)
     * @fmt: The format string to use
     * @...: Arguments for the format string
     *
     * The function returns the number of words(u32) written
     * into @bin_buf.
     */
    int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
    {
    	va_list args;
    	int ret;
    
    	va_start(args, fmt);
    	ret = vbin_printf(bin_buf, size, fmt, args);
    	va_end(args);
    
    	return ret;
    }
    EXPORT_SYMBOL_GPL(bprintf);
    
    #endif /* CONFIG_BINARY_PRINTF */
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     * vsscanf - Unformat a buffer into a list of arguments
     * @buf:	input buffer
     * @fmt:	format of buffer
     * @args:	arguments
     */
    
    int vsscanf(const char *buf, const char *fmt, va_list args)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	const char *str = buf;
    	char *next;
    	char digit;
    	int num = 0;
    
    	u8 qualifier;
    	u8 base;
    	s16 field_width;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		/* skip any white space in format */
    		/* white space in format matchs any amount of
    		 * white space, including none, in the input.
    		 */
    		if (isspace(*fmt)) {
    
    			fmt = skip_spaces(++fmt);
    			str = skip_spaces(str);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    		/* anything that is not a conversion must match exactly */
    		if (*fmt != '%' && *fmt) {
    			if (*fmt++ != *str++)
    				break;
    			continue;
    		}
    
    		if (!*fmt)
    			break;
    		++fmt;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		/* skip this conversion.
    		 * advance both strings to next white space
    		 */
    		if (*fmt == '*') {
    
    Andy Spencer's avatar
    Andy Spencer committed
    			while (!isspace(*fmt) && *fmt != '%' && *fmt)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				fmt++;
    			while (!isspace(*str) && *str)
    				str++;
    			continue;
    		}
    
    		/* get field width */
    		field_width = -1;
    		if (isdigit(*fmt))
    			field_width = skip_atoi(&fmt);
    
    		/* get conversion qualifier */
    		qualifier = -1;
    
    		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
    		    _tolower(*fmt) == 'z') {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			qualifier = *fmt++;
    			if (unlikely(qualifier == *fmt)) {
    				if (qualifier == 'h') {
    					qualifier = 'H';
    					fmt++;
    				} else if (qualifier == 'l') {
    					qualifier = 'L';
    					fmt++;
    				}
    			}
    		}
    
    		if (!*fmt || !*str)
    			break;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		case 'c':
    		{
    
    			char *s = (char *)va_arg(args, char*);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			if (field_width == -1)
    				field_width = 1;
    			do {
    				*s++ = *str++;
    			} while (--field_width > 0 && *str);
    			num++;
    		}
    		continue;
    		case 's':
    		{
    
    			char *s = (char *)va_arg(args, char *);
    			if (field_width == -1)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			/* first, skip leading white space in buffer */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    			/* now copy until next white space */
    
    			while (*str && !isspace(*str) && field_width--)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				*s++ = *str++;
    			*s = '\0';
    			num++;
    		}
    		continue;
    		case 'n':
    			/* return number of characters read so far */
    		{
    
    			int *i = (int *)va_arg(args, int*);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			*i = str - buf;
    		}
    		continue;
    		case 'o':
    			base = 8;
    			break;
    		case 'x':
    		case 'X':
    			base = 16;
    			break;
    		case 'i':
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		case 'd':
    			is_sign = 1;
    		case 'u':
    			break;
    		case '%':
    			/* looking for '%' in str */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				return num;
    			continue;
    		default:
    			/* invalid format; stop here */
    			return num;
    		}
    
    		/* have some sort of integer conversion.
    		 * first, skip white space in buffer.
    		 */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    		digit = *str;
    		if (is_sign && digit == '-')
    			digit = *(str + 1);
    
    		if (!digit
    
    		    || (base == 16 && !isxdigit(digit))
    		    || (base == 10 && !isdigit(digit))
    		    || (base == 8 && (!isdigit(digit) || digit > '7'))
    		    || (base == 0 && !isdigit(digit)))
    			break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		case 'H':	/* that's 'hh' in format */
    			if (is_sign) {
    
    				signed char *s = (signed char *)va_arg(args, signed char *);
    				*s = (signed char)simple_strtol(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			} else {
    
    				unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
    				*s = (unsigned char)simple_strtoul(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    			break;
    		case 'h':
    			if (is_sign) {
    
    				short *s = (short *)va_arg(args, short *);
    				*s = (short)simple_strtol(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			} else {
    
    				unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
    				*s = (unsigned short)simple_strtoul(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    			break;
    		case 'l':
    			if (is_sign) {
    
    				long *l = (long *)va_arg(args, long *);
    				*l = simple_strtol(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			} else {
    
    				unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
    				*l = simple_strtoul(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    			break;
    		case 'L':
    			if (is_sign) {
    
    				long long *l = (long long *)va_arg(args, long long *);
    				*l = simple_strtoll(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			} else {
    
    				unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
    				*l = simple_strtoull(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    			break;
    		case 'Z':
    		case 'z':
    		{
    
    			size_t *s = (size_t *)va_arg(args, size_t *);
    			*s = (size_t)simple_strtoul(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    		break;
    		default:
    			if (is_sign) {
    
    				int *i = (int *)va_arg(args, int *);
    				*i = (int)simple_strtol(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			} else {
    
    				unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
    				*i = (unsigned int)simple_strtoul(str, &next, base);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    			break;
    		}
    		num++;
    
    		if (!next)
    			break;
    		str = next;
    	}
    
    
    	/*
    	 * Now we've come all the way through so either the input string or the
    	 * format ended. In the former case, there can be a %n at the current
    	 * position in the format that needs to be filled.
    	 */
    	if (*fmt == '%' && *(fmt + 1) == 'n') {
    		int *p = (int *)va_arg(args, int *);
    		*p = str - buf;
    	}
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return num;