-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlib.c
226 lines (189 loc) · 5.96 KB
/
lib.c
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
215
216
217
218
219
220
221
222
223
224
225
226
// lib.c
// Copyright (c) 2018 J. M. Spivey
#include "lib.h"
#include "hardware.h"
#define NMAX 16 // Max digits in a printed number
/* utoa -- convert unsigned to decimal or hex */
static char *utoa(unsigned x, unsigned base, char *nbuf) {
char *p = &nbuf[NMAX];
const char *hex = "0123456789abcdef";
*--p = '\0';
do {
*--p = hex[x % base];
x = x / base;
} while (x != 0);
return p;
}
/* itoa -- convert signed integer to decimal */
static char *itoa(int v, char *nbuf) {
if (v >= 0)
return utoa(v, 10, nbuf);
else {
char *p = utoa(-v, 10, nbuf);
*--p = '-';
return p;
}
}
/* atoi -- convert decimal string to integer */
int atoi(const char *p) {
unsigned x = 0;
int minus = 0;
if (*p == '-') {
minus = 1; p++;
}
while (*p >= '0' && *p <= '9')
x = 10 * x + (*p++ - '0');
if (minus)
return -x;
else
return x;
}
/* xtou -- convert hex string to unsigned */
unsigned xtou(char *p) {
unsigned x = 0;
for (;;) {
if (*p >= '0' && *p <= '9')
x = (x << 4) + (*p++ - '0');
else if (*p >= 'a' && *p <= 'f')
x = (x << 4) + (*p++ - 'a' + 10);
else if (*p >= 'A' && *p <= 'F')
x = (x << 4) + (*p++ - 'A' + 10);
else
break;
}
return x;
}
/* FORMATTED OUTPUT */
/* The functions defined here provide a version of printf that calls a
client-supplied function print_buf to output characters, a thread-safe
version of sprintf, and a skeleton do_print that calls a supplied
function on each output character. The print_buf interface is
potentially the most efficient, because printf buffers characters
internally and passes them to print_buf 16 at a time, reducing context
switches if print_buf sends messages to a device driver process.
The three public functions share a common skeleton _do_print that
takes as parameters a "putc-function" and a pointer q that is passed
to the function with each character. Different putc-functions and
different interpretations of the pointer q are needed for different
applications. */
/* do_string -- output or buffer each character of a string */
static void do_string(void (*putc)(void *, char), void *q, char *str) {
for (char *p = str; *p != '\0'; p++)
putc(q, *p);
}
/* _do_print -- the guts of printf */
void _do_print(void (*putc)(void *, char), void *q,
const char *fmt, va_list va) {
int x;
char nbuf[NMAX];
for (const char *p = fmt; *p != 0; p++) {
if (*p == '%' && *(p+1) != '\0') {
switch (*++p) {
case 'c':
putc(q, va_arg(va, int));
break;
case 'd':
do_string(putc, q, itoa(va_arg(va, int), nbuf));
break;
case 's':
do_string(putc, q, va_arg(va, char *));
break;
case 'u':
do_string(putc, q, utoa(va_arg(va, unsigned), 10, nbuf));
break;
case 'x':
x = va_arg(va, unsigned);
if (x == 0)
putc(q, '0');
else {
putc(q, '0'); putc(q, 'x');
do_string(putc, q, utoa(x, 16, nbuf));
}
break;
default:
putc(q, *p);
break;
}
} else {
putc(q, *p);
}
}
}
/* f_printc -- putc-function that calls q as a function */
static void f_printc(void *q, char c) {
void (*f)(char) = q;
f(c);
}
/* do_print -- public skeleton for printf */
void do_print(void (*putc)(char), const char *fmt, va_list va) {
_do_print(f_printc, (void *) putc, fmt, va);
}
/* f_storec -- putc-function that uses q as a character pointer */
void f_storec(void *q, char c) {
char **p = q;
*(*p)++ = c;
}
/* sprintf -- print to a character array */
int sprintf(char *buf, const char *fmt, ...) {
// Note the usual problem with buffer overflow
char *p = buf;
va_list va;
va_start(va, fmt);
_do_print(f_storec, (void *) &p, fmt, va);
va_end(va);
*p++ = '\0';
return (p - buf);
}
/* printf's internal buffer complicates the behaviour of the 'chaos'
example: don't be tempted to make the buffer bigger, or all chaos will
be removed! Clients not running under micro:bian see no benefit from
the buffering because there print_buf is usually implemented by
repeated calls to serial_putc. */
/* print_buf -- must be provided by client */
extern void print_buf(char *buf, int n);
#define NBUF 16
/* struct buffer -- buffer for use by printf */
struct buffer {
char buf[NBUF]; /* Characters in the buffer */
int nbuf; /* Number of characters */
};
/* flush -- flush a buffer by calling print_buf */
static void flush(struct buffer *b) {
print_buf(b->buf, b->nbuf);
b->nbuf = 0;
}
/* f_bufferc -- putc-function that stores characters in a buffer */
static void f_bufferc(void *q, char c) {
struct buffer *b = q;
if (b->nbuf == NBUF) flush(b);
b->buf[b->nbuf++] = c;
}
/* printf -- print using client-supplied print_buf */
void printf(const char *fmt, ...) {
va_list va;
struct buffer b;
b.nbuf = 0;
va_start(va, fmt);
_do_print(f_bufferc, &b, fmt, va);
va_end(va);
if (b.nbuf > 0) flush(&b);
}
/* prandom -- pseudorandom numbers in the range [1 .. 2^31-1) */
unsigned prandom(void) {
static unsigned seed = 31415926;
#define M 0x7fffffff
#define A 48271
#define Q (M / A) // = 44488
#define R (M % A) // = 3399
// Make the function thread-safe by temporarily disabling interrupts
unsigned prev = get_primask();
intr_disable();
// Lehmer congruential method: see Knuth vol. 2, p. 185.
int val = seed;
int quo = val / Q; // One division per call
val = A * (val - Q * quo) - R * quo;
if (val < 0) val += M;
seed = val;
set_primask(prev);
return val;
}