OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.24/] [disk/] [tools/] [fs-NetBSD/] [loader/] [auxlib.c] - Blame information for rev 333

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 hellwig
/*
2
 * auxlib.c -- auxiliary library for standalone programs
3
 */
4
 
5
 
6
#include "types.h"
7
#include "stdarg.h"
8
#include "auxlib.h"
9
#include "biolib.h"
10
 
11
 
12
/**************************************************************/
13
 
14
/* string functions */
15
 
16
 
17
int strlen(char *str) {
18
  int i;
19
 
20
  i = 0;
21
  while (*str++ != '\0') {
22
    i++;
23
  }
24
  return i;
25
}
26
 
27
 
28
int strcmp(char *str1, char *str2) {
29
  while (*str1 == *str2) {
30
    if (*str1 == '\0') {
31
      return 0;
32
    }
33
    str1++;
34
    str2++;
35
  }
36
  return (* (unsigned char *) str1) - (* (unsigned char *) str2);
37
}
38
 
39
 
40
void strcpy(char *dst, char *src) {
41
  while ((*dst++ = *src++) != '\0') ;
42
}
43
 
44
 
45
/**************************************************************/
46
 
47
/* terminal I/O */
48
 
49
 
50
/*
51
 * Input a character from the console.
52
 */
53
char getchar(void) {
54
  return getc();
55
}
56
 
57
 
58
/*
59
 * Output a character on the console.
60
 * Replace LF by CR/LF.
61
 */
62
void putchar(char c) {
63
  if (c == '\n') {
64
    putchar('\r');
65
  }
66
  putc(c);
67
}
68
 
69
 
70
/**************************************************************/
71
 
72
/* get a line from the terminal */
73
 
74
 
75
void getLine(char *prompt, char *line, int max) {
76
  int index;
77
  char c;
78
 
79
  printf("%s%s", prompt, line);
80
  index = strlen(line);
81
  while (1) {
82
    c = getchar();
83
    switch (c) {
84
      case '\r':
85
        putchar('\n');
86
        line[index] = '\0';
87
        return;
88
      case '\b':
89
      case 0x7F:
90
        if (index == 0) {
91
          break;
92
        }
93
        putchar('\b');
94
        putchar(' ');
95
        putchar('\b');
96
        index--;
97
        break;
98
      default:
99
        if (c == '\t') {
100
          c = ' ';
101
        }
102
        if (c < 0x20 || c > 0x7E) {
103
          break;
104
        }
105
        putchar(c);
106
        line[index++] = c;
107
        break;
108
    }
109
  }
110
}
111
 
112
 
113
/**************************************************************/
114
 
115
/* scaled-down version of printf */
116
 
117
 
118
/*
119
 * Count the number of characters needed to represent
120
 * a given number in base 10.
121
 */
122
static int countPrintn(long n) {
123
  long a;
124
  int res;
125
 
126
  res = 0;
127
  if (n < 0) {
128
    res++;
129
    n = -n;
130
  }
131
  a = n / 10;
132
  if (a != 0) {
133
    res += countPrintn(a);
134
  }
135
  return res + 1;
136
}
137
 
138
 
139
/*
140
 * Output a number in base 10.
141
 */
142
static void printn(long n) {
143
  long a;
144
 
145
  if (n < 0) {
146
    putchar('-');
147
    n = -n;
148
  }
149
  a = n / 10;
150
  if (a != 0) {
151
    printn(a);
152
  }
153
  putchar(n % 10 + '0');
154
}
155
 
156
 
157
/*
158
 * Count the number of characters needed to represent
159
 * a given number in a given base.
160
 */
161
static int countPrintu(unsigned long n, unsigned long b) {
162
  unsigned long a;
163
  int res;
164
 
165
  res = 0;
166
  a = n / b;
167
  if (a != 0) {
168
    res += countPrintu(a, b);
169
  }
170
  return res + 1;
171
}
172
 
173
 
174
/*
175
 * Output a number in a given base.
176
 */
177
static void printu(unsigned long n, unsigned long b, Bool upperCase) {
178
  unsigned long a;
179
 
180
  a = n / b;
181
  if (a != 0) {
182
    printu(a, b, upperCase);
183
  }
184
  if (upperCase) {
185
    putchar("0123456789ABCDEF"[n % b]);
186
  } else {
187
    putchar("0123456789abcdef"[n % b]);
188
  }
189
}
190
 
191
 
192
/*
193
 * Output a number of filler characters.
194
 */
195
static void fill(int numFillers, char filler) {
196
  while (numFillers-- > 0) {
197
    putchar(filler);
198
  }
199
}
200
 
201
 
202
/*
203
 * Formatted output with a variable argument list.
204
 */
205
void vprintf(char *fmt, va_list ap) {
206
  char c;
207
  int n;
208
  long ln;
209
  unsigned int u;
210
  unsigned long lu;
211
  char *s;
212
  Bool negFlag;
213
  char filler;
214
  int width, count;
215
 
216
  while (1) {
217
    while ((c = *fmt++) != '%') {
218
      if (c == '\0') {
219
        return;
220
      }
221
      putchar(c);
222
    }
223
    c = *fmt++;
224
    if (c == '-') {
225
      negFlag = TRUE;
226
      c = *fmt++;
227
    } else {
228
      negFlag = FALSE;
229
    }
230
    if (c == '0') {
231
      filler = '0';
232
      c = *fmt++;
233
    } else {
234
      filler = ' ';
235
    }
236
    width = 0;
237
    while (c >= '0' && c <= '9') {
238
      width *= 10;
239
      width += c - '0';
240
      c = *fmt++;
241
    }
242
    if (c == 'd') {
243
      n = va_arg(ap, int);
244
      count = countPrintn(n);
245
      if (width > 0 && !negFlag) {
246
        fill(width - count, filler);
247
      }
248
      printn(n);
249
      if (width > 0 && negFlag) {
250
        fill(width - count, filler);
251
      }
252
    } else
253
    if (c == 'u' || c == 'o' || c == 'x' || c == 'X') {
254
      u = va_arg(ap, int);
255
      count = countPrintu(u,
256
                c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10));
257
      if (width > 0 && !negFlag) {
258
        fill(width - count, filler);
259
      }
260
      printu(u,
261
             c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10),
262
             c == 'X');
263
      if (width > 0 && negFlag) {
264
        fill(width - count, filler);
265
      }
266
    } else
267
    if (c == 'l') {
268
      c = *fmt++;
269
      if (c == 'd') {
270
        ln = va_arg(ap, long);
271
        count = countPrintn(ln);
272
        if (width > 0 && !negFlag) {
273
          fill(width - count, filler);
274
        }
275
        printn(ln);
276
        if (width > 0 && negFlag) {
277
          fill(width - count, filler);
278
        }
279
      } else
280
      if (c == 'u' || c == 'o' || c == 'x' || c == 'X') {
281
        lu = va_arg(ap, long);
282
        count = countPrintu(lu,
283
                  c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10));
284
        if (width > 0 && !negFlag) {
285
          fill(width - count, filler);
286
        }
287
        printu(lu,
288
               c == 'o' ? 8 : ((c == 'x' || c == 'X') ? 16 : 10),
289
               c == 'X');
290
        if (width > 0 && negFlag) {
291
          fill(width - count, filler);
292
        }
293
      } else {
294
        putchar('l');
295
        putchar(c);
296
      }
297
    } else
298
    if (c == 's') {
299
      s = va_arg(ap, char *);
300
      count = strlen(s);
301
      if (width > 0 && !negFlag) {
302
        fill(width - count, filler);
303
      }
304
      while ((c = *s++) != '\0') {
305
        putchar(c);
306
      }
307
      if (width > 0 && negFlag) {
308
        fill(width - count, filler);
309
      }
310
    } else
311
    if (c == 'c') {
312
      c = va_arg(ap, char);
313
      putchar(c);
314
    } else {
315
      putchar(c);
316
    }
317
  }
318
}
319
 
320
 
321
/*
322
 * Formatted output.
323
 * This is a scaled-down version of the C library's
324
 * printf. Used to print diagnostic information on
325
 * the console (and optionally to a logfile).
326
 */
327
void printf(char *fmt, ...) {
328
  va_list ap;
329
 
330
  va_start(ap, fmt);
331
  vprintf(fmt, ap);
332
  va_end(ap);
333
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.