File: printf.c

package info (click to toggle)
brickos 0.9.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,700 kB
  • ctags: 1,727
  • sloc: ansic: 9,139; cpp: 860; makefile: 717; asm: 693; sh: 123; perl: 61
file content (151 lines) | stat: -rw-r--r-- 3,666 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*! \file   printf.c
    \brief  printf function
    \author Matthew Ahrens <mahrens@acm.org>
*/
    
/*
 *  The contents of this file are subject to the Mozilla Public License
 *  Version 1.0 (the "License"); you may not use this file except in
 *  compliance with the License. You may obtain a copy of the License at
 *  http://www.mozilla.org/MPL/
 *
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 *  License for the specific language governing rights and limitations
 *  under the License.
 *
 *  The Original Code is from the Brown Computer Science Department,
 *  Weenix support code.
 *
 *  The Initial Developer of the Original Code is David Powell.
 *
 *  Contributor(s): Matthew Ahrens <mahrens@acm.org>
 */

#include <stdarg.h>

static const char hexdigits[] = "0123456789ABCDEF";
static const char lhexdigits[] = "0123456789abcdef";
static const char empty_string[] = "(null string)";

/* This function takes an unsigned int and puts its ascii
 * representation at a particular location in a buffer of given
 * length.  The radix used is must also be provided, as well as a
 * string containing the characters used in the ascii representation.
 *
 * This function makes no attempt to handle two-byte characters. 
 */
static int itoa(unsigned int value, char *dst, int pos, int len,
		int radix, const char *digits)
{
	char	sbuf[10];
	int	spos;

	spos = 0;
	while(value) {
		sbuf[spos] = value % radix;
		spos++;
		value /= radix;
	}
	if(!spos)
		*sbuf = 0, spos = 1;

	for(spos--; pos<len && spos>=0; pos++,spos--) {
		dst[pos] = digits[(int)sbuf[spos]];
	}

	return pos-1;
}


/* This function takes a buffer, its length, a format specification,
 * and a va_list, formats the provided data according the the format
 * spec, and places the result in the buffer.  It returns the length of
 * the output.  The only format options supported by this function
 * are:
 *   %d - decimal unsigned int
 *   %i -    "        "     "
 *   %x - hexidecimal unsigned int (lowercase letters)
 *   %X -      "          "     "   (uppercase letters)
 *   %c - character
 *   %s - string
 *   %% - '%' character
 */
int vsnprintf(char *dst, int len, const char *fmt, va_list arg)
{
	int		pos;
	int		cc;
	const char	*temp;
	unsigned int	scratch;

	for(pos=0, cc=0; (pos<len) && (cc=*fmt); pos++, fmt++) {
		if(cc!='%')
			dst[pos] = cc;
		else {
			cc = *(++fmt);
			switch(cc) {
			case 'd':
			case 'i':
				/* integer */
				scratch = va_arg(arg, unsigned);
				pos = itoa(scratch, dst, pos, len, 10,
					   hexdigits);
				break;
			case 'X':
				/* hexadecimal (uppercase) */
				scratch = va_arg(arg, unsigned);
				pos = itoa(scratch, dst, pos, len, 16,
					   hexdigits);
				break;
			case 'x':
				/* hexadecimal (lowercase) */
				scratch = va_arg(arg, unsigned);
				pos = itoa(scratch, dst, pos, len, 16,
					   lhexdigits);
				break;
			case 's':
				/* string */
				temp = va_arg(arg, const char*);
				if(!temp)
					temp = empty_string;
				for(; pos<len && *temp; pos++, temp++) {
					dst[pos] = *temp;
				}
				pos--;
				break;
			case 'c':
				/* character */
				dst[pos] = va_arg(arg, int);
				break;
			case '\0':
			case '%':
				dst[pos] = cc;
				break;
			}

			if(!cc) {
				break;
			}
		}
	}

	dst[pos++] = '\0';

	return pos;
}


/* This function prints the specified format into the provided buffer
 * taking care not to access beyond the end of the string. 
 */
char *snprintf(char *dst, int len, const char *fmt, ...)
{
	va_list	arg;

	va_start(arg, fmt);
	(void) vsnprintf(dst, len, fmt, arg);
	va_end(arg);

	return dst;
}