aboutsummaryrefslogtreecommitdiffstats
path: root/c/libc.c
blob: 3b9883b8d803bb17e4c301df55b663a8da871170 (plain) (blame)
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
/* gbos libc C helpers (compiled once, linked into every program). */
#include "gbos.h"

/* print an unsigned int in decimal */
void putu(unsigned int n) {
    char buf[5];
    unsigned char i = 0;
    if (n == 0) { putc('0'); return; }
    while (n) { buf[i++] = '0' + (n % 10); n = n / 10; }
    while (i) putc(buf[--i]);
}

/* parse a decimal number (stops at the first non-digit) */
unsigned char atou(const char *s) {
    unsigned char n = 0;
    while (*s >= '0' && *s <= '9') { n = n * 10 + (*s - '0'); s++; }
    return n;
}

/* tokenize the argument string in place into argv[]; returns argc.
   (replaces the spaces in the arg string with NULs) */
unsigned char argv_parse(char **argv, unsigned char maxv) {
    char *p = getargs();
    unsigned char argc = 0;
    for (;;) {
        while (*p == ' ') p++;
        if (*p == 0) break;
        if (argc < maxv) argv[argc] = p;
        argc++;
        while (*p != 0 && *p != ' ') p++;
        if (*p == ' ') *p++ = 0;
    }
    return argc;
}