aboutsummaryrefslogtreecommitdiff
blob: d3a27df28c6ecfb82d7b908c212057e7de4ff2e3 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* sb_printf.c
 * Minimal printf implementation that is designed to work without calling back
 * into the C library or libsandbox.  Goes straight to kernel so that this
 * should be usable from anywhere.  All state is on stack, so also avoids
 * signal, threaded, and other fun async behavior.
 *
 * The following conversion specifiers are supported:
 *	c - character
 *	s - string
 *	d - integer
 *	i - integer
 *	u - unsigned integer
 *	X - HEX number
 *	x - hex number
 *	p - pointer
 *
 * The following modifiers are supported:
 *	z - size_t
 *	* - width is an argument
 *
 * Copyright 1999-2008 Gentoo Foundation
 * Licensed under the GPL-2
 */

#include "headers.h"
#include "sbutil.h"

#define MOD_STAR   (1 << 0)
#define MOD_SIZE_T (1 << 1)
#define MOD_LONG_T (1 << 2)

void sb_vfdprintf(int fd, const char *format, va_list args)
{
	const char *fmt = format;
	const char *format_end = format + strlen(format);
	const char *conv = fmt;

	while (fmt < format_end) {
		conv = memchr(fmt, '%', format_end - fmt);

		if (conv != fmt) {
			size_t out_bytes = (conv ? conv : format_end) - fmt;
			sb_write(fd, fmt, out_bytes);
			if (!conv)
				return;
		}

		char buf[50];
		size_t idx;
		unsigned int u, x;
		char hex_base;
		size_t padding = 0;

		unsigned int modifiers = 0;
 eat_more:
		switch (conv[1]) {
			default:
				sb_fdprintf(fd, "{invalid conversion specifier in string: %s}", format);
				break;

			case '%':
				if (modifiers) {
 inv_modifier:
					sb_fdprintf(fd, "{invalid modifier in string: %s}", format);
				}
				sb_write(fd, conv, 1);
				break;
			case 'l':
				++conv;
				if (modifiers & MOD_LONG_T)
					goto inv_modifier;
				modifiers |= MOD_LONG_T;
				goto eat_more;
			case 'z':
				++conv;
				if (modifiers & MOD_SIZE_T)
					goto inv_modifier;
				modifiers |= MOD_SIZE_T;
				goto eat_more;
			case '*':
				++conv;
				if (modifiers & MOD_STAR)
					goto inv_modifier;
				modifiers |= MOD_STAR;
				padding = va_arg(args, int);
				goto eat_more;

			case 'c': {
				char c = va_arg(args, int);
				sb_write(fd, &c, 1);
				break;
			}
			case 's': {
				char *s = va_arg(args, char *);
				if (!s)
					s = "(null)";
				size_t len = strlen(s);
				while (len < padding--)
					sb_write(fd, " ", 1);
				sb_write(fd, s, len);
				break;
			}

			case 'd':
			case 'i': {
				long i;
				if (modifiers & MOD_SIZE_T)
					i = va_arg(args, ssize_t);
				else if (modifiers & MOD_LONG_T)
					i = va_arg(args, long);
				else
					i = va_arg(args, int);
				u = i;
				if (i < 0) {
					sb_write(fd, "-", 1);
					u = i * -1;
				}
 out_uint:
				idx = 0;
				do {
					buf[idx++] = '0' + (u % 10);
				} while (u /= 10);
				while (idx--)
					sb_write(fd, buf+idx, 1);
				break;
			}
			case 'u': {
				u = va_arg(args, unsigned int);
				goto out_uint;
				break;
			}

			case 'X': {
				hex_base = 'A';
				goto out_hex;
			}
			case 'x': {
				hex_base = 'a';
 out_hex:
				if (modifiers & MOD_SIZE_T)
					x = va_arg(args, size_t);
				else
					x = va_arg(args, unsigned int);
 out_ptr:
				idx = 0;
				do {
					u = x % 16;
					buf[idx++] = (u < 10 ? '0' + u : hex_base + u - 10);
				} while (x /= 16);
				while (idx < padding)
					buf[idx++] = '0';
				buf[idx++] = 'x';
				buf[idx++] = '0';
				while (idx--)
					sb_write(fd, buf+idx, 1);
				break;
			}
			case 'p': {
				void *p = va_arg(args, void *);
				hex_base = 'a';
				padding = sizeof(p) * 2;
				x = (unsigned long)p;
				goto out_ptr;
			}
		}

		fmt = conv + 2;
	}
}

void sb_fdprintf(int fd, const char *format, ...)
{
	va_list args;
	va_start(args, format);
	sb_vfdprintf(fd, format, args);
	va_end(args);
}

/* All output goes to stderr as any unexpected interjection into stdout
 * could easily break shell scripts and such that are piping or evaluating
 * or some such thing.
 */
void sb_printf(const char *format, ...)
{
	va_list args;
	va_start(args, format);
	sb_vfdprintf(STDERR_FILENO, format, args);
	va_end(args);
}

static bool nocolor, init_color = false;
void sb_efunc(const char *color, const char *hilight, const char *format, ...)
{
	save_errno();

	int fd = STDERR_FILENO;

	if (!init_color) {
		nocolor = is_env_on(ENV_NOCOLOR);
		init_color = true;
	}

	if (!nocolor)
		sb_fdprintf(fd, "%s%s%s", color, hilight, COLOR_NORMAL);
	else
		sb_fdprintf(fd, "%s", hilight);

	va_list args;
	va_start(args, format);
	sb_vfdprintf(fd, format, args);
	va_end(args);

	restore_errno();
}