ax-libc 0.5.10

ArceOS user program library for C apps
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "printf.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

// LOCK used by `puts()`
#ifdef AX_CONFIG_MULTITASK
#include <pthread.h>
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
#endif

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

FILE __stdin_FILE = {.fd = 0, .buffer_len = 0};

FILE __stdout_FILE = {.fd = 1, .buffer_len = 0};

FILE __stderr_FILE = {.fd = 2, .buffer_len = 0};

FILE *const stdin = &__stdin_FILE;
FILE *const stdout = &__stdout_FILE;
FILE *const stderr = &__stderr_FILE;

// Returns: number of chars written, negative for failure
// Warn: buffer_len[f] will not be changed
static int __write_buffer(FILE *f)
{
    int r = 0;
    if (f->buffer_len == 0)
        return 0;
    r = write(f->fd, f->buf, f->buffer_len);
    return r;
}

// Clear buffer_len[f]
static void __clear_buffer(FILE *f)
{
    f->buffer_len = 0;
}

static int __fflush(FILE *f)
{
    int r = __write_buffer(f);
    __clear_buffer(f);
    return r >= 0 ? 0 : r;
}

static int out(FILE *f, const char *s, size_t l)
{
    int ret = 0;
    for (size_t i = 0; i < l; i++) {
        char c = s[i];
        f->buf[f->buffer_len++] = c;
        if (f->buffer_len == FILE_BUF_SIZE || c == '\n') {
            int r = __write_buffer(f);
            __clear_buffer(f);
            if (r < 0)
                return r;
            if (r < f->buffer_len)
                return ret + r;
            ret += r;
        }
    }
    return ret;
}

int getchar(void)
{
    unimplemented();
    return 0;
}

int fflush(FILE *f)
{
    return __fflush(f);
}

static inline int do_putc(int c, FILE *f)
{
    char byte = c;
    return out(f, &byte, 1);
}

int fputc(int c, FILE *f)
{
    return do_putc(c, f);
}

int putc(int c, FILE *f)
{
    return do_putc(c, f);
}

int putchar(int c)
{
    return do_putc(c, stdout);
}

int puts(const char *s)
{
#ifdef AX_CONFIG_MULTITASK
    pthread_mutex_lock(&lock);
#endif

    int r = write(1, (const void *)s, strlen(s));
    char brk[1] = {'\n'};
    write(1, (const void *)brk, 1);

#ifdef AX_CONFIG_MULTITASK
    pthread_mutex_unlock(&lock);
#endif

    return r;
}

void perror(const char *msg)
{
    FILE *f = stderr;
    char *errstr = strerror(errno);

    if (msg && *msg) {
        out(f, msg, strlen(msg));
        out(f, ": ", 2);
    }
    out(f, errstr, strlen(errstr));
    out(f, "\n", 1);
}

static void __out_wrapper(char c, void *arg)
{
    out(arg, &c, 1);
}

int printf(const char *restrict fmt, ...)
{
    int ret;
    va_list ap;
    va_start(ap, fmt);
    ret = vfprintf(stdout, fmt, ap);
    va_end(ap);
    return ret;
}

int fprintf(FILE *restrict f, const char *restrict fmt, ...)
{
    int ret;
    va_list ap;
    va_start(ap, fmt);
    ret = vfprintf(f, fmt, ap);
    va_end(ap);
    return ret;
}

int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
{
    return vfctprintf(__out_wrapper, f, fmt, ap);
}

// TODO
int sscanf(const char *restrict __s, const char *restrict __format, ...)
{
    unimplemented();
    return 0;
}

#ifdef AX_CONFIG_FS

int __fmodeflags(const char *mode)
{
    int flags;
    if (strchr(mode, '+'))
        flags = O_RDWR;
    else if (*mode == 'r')
        flags = O_RDONLY;
    else
        flags = O_WRONLY;
    if (strchr(mode, 'x'))
        flags |= O_EXCL;
    if (strchr(mode, 'e'))
        flags |= O_CLOEXEC;
    if (*mode != 'r')
        flags |= O_CREAT;
    if (*mode == 'w')
        flags |= O_TRUNC;
    if (*mode == 'a')
        flags |= O_APPEND;
    return flags;
}

FILE *fopen(const char *filename, const char *mode)
{
    FILE *f;
    int flags;

    if (!strchr("rwa", *mode)) {
        errno = EINVAL;
        return 0;
    }

    f = (FILE *)malloc(sizeof(FILE));

    flags = __fmodeflags(mode);
    // TODO: currently mode is unused in ax_open
    int fd = open(filename, flags, 0666);
    if (fd < 0)
        return NULL;
    f->fd = fd;

    return f;
}

char *fgets(char *restrict s, int n, FILE *restrict f)
{
    if (n == 0)
        return NULL;
    if (n == 1) {
        *s = '\0';
        return s;
    }

    int cnt = 0;
    while (cnt < n - 1) {
        char c;
        if (read(f->fd, (void *)&c, 1) > 0) {
            if (c != '\n')
                s[cnt++] = c;
            else
                break;
        } else
            break;
    }
    s[cnt] = '\0';
    return s;
}

size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f)
{
    size_t total = size * nmemb;
    size_t read_len = 0;
    size_t len = 0;
    do {
        len = read(f->fd, destv + read_len, total - read_len);
        if (len < 0)
            break;
        read_len += len;
    } while (len > 0);
    return read_len == size * nmemb ? nmemb : read_len / size;
}

size_t fwrite(const void *restrict src, size_t size, size_t nmemb, FILE *restrict f)
{
    size_t total = size * nmemb;
    size_t write_len = 0;
    size_t len = 0;
    do {
        len = write(f->fd, src + write_len, total - write_len);
        if (len < 0)
            break;
        write_len += len;
    } while (len > 0);
    return write_len == size * nmemb ? nmemb : write_len / size;
}

int fputs(const char *restrict s, FILE *restrict f)
{
    size_t l = strlen(s);
    return (fwrite(s, 1, l, f) == l) - 1;
}

int fclose(FILE *f)
{
    return close(f->fd);
}

int fileno(FILE *f)
{
    return f->fd;
}

int feof(FILE *f)
{
    unimplemented();
    return 0;
}

// TODO
int fseek(FILE *__stream, long __off, int __whence)
{
    unimplemented();
    return 0;
}

// TODO
off_t ftello(FILE *__stream)
{
    unimplemented();
    return 0;
}

// TODO
char *tmpnam(char *buf)
{
    unimplemented();
    return 0;
}

// TODO
void clearerr(FILE *f)
{
    unimplemented();
}

// TODO
int ferror(FILE *f)
{
    unimplemented();
    return 0;
}

// TODO
FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f)
{
    unimplemented();
    return 0;
}

// TODO
int fscanf(FILE *restrict f, const char *restrict fmt, ...)
{
    unimplemented();
    return 0;
}

// TODO
long ftell(FILE *f)
{
    unimplemented();
    return 0;
}

int getc(FILE *f)
{
    unimplemented();
    return 0;
}

// TODO
int remove(const char *path)
{
    unimplemented();
    return 0;
}

// TODO
int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size)
{
    unimplemented();
    return 0;
}

// TODO
FILE *tmpfile(void)
{
    unimplemented();
    return NULL;
}

int ungetc(int c, FILE *f)
{
    unimplemented();
    return 0;
}

ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restrict f)
{
    unimplemented();
    return 0;
}

ssize_t getline(char **restrict s, size_t *restrict n, FILE *restrict f)
{
    return getdelim(s, n, '\n', f);
}

int __uflow(FILE *f)
{
    unimplemented();
    return 0;
}

int getc_unlocked(FILE *f)
{
    unimplemented();
    return 0;
}

FILE *fdopen(int fd, const char *mode)
{
    unimplemented();
    return NULL;
}

#endif // AX_CONFIG_FS