summaryrefslogtreecommitdiff
path: root/libc/include/stdio.h
blob: ee6226f4bac2efdd1d53c336f43188ec68015f56 (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
#ifndef __STDIO_H__
#define __STDIO_H__

#include <reent.h>
#include <stdarg.h>
#include <stddef.h>
#include <unistd.h>
#include <malloc.h>

static const int EOF = -1;

struct _FILE {
    int fd;
};

typedef struct _FILE FILE;

int printf(const char * format, ...);
int fprintf(FILE *stream, const char * format, ...);
int sprintf(char * str, const char * format, ...);
int snprintf(char * str, size_t size, const char * format, ...);
int asprintf(char ** strp, const char * format, ...);

int vprintf(const char * format, va_list ap);
int vfprintf(FILE *stream, const char * format, va_list ap);
int vsprintf(char * str, const char * format, va_list ap);
int vsnprintf(char * str, size_t size, const char * format, va_list ap);
int vasprintf(char ** strp, const char * format, va_list ap);

void __sinit(struct _reent *);

// We don't even buffer, so...
static inline int fflush(FILE *stream) { return 0; }

// hopefully, since all of the mode crap is static, gcc will optimize most of it away.
static inline FILE * fopen(const char * fname, const char * mode) {
    FILE * r = NULL;
    int flags = 0, plus = 0, append = 0, fd;
    if (!mode || !mode[0]) {
        _impure_ptr->_errno = EINVAL;
        return NULL;
    }
    
    if (mode[1] == 'b') {
        plus = mode[2] == '+';
    } else if (mode[1]) {
        if (mode[1] != '+') {
            _impure_ptr->_errno = EINVAL;
            return NULL;
        }
        plus = 1;
    }
    
    switch (mode[0]) {
    case 'r':
        if (plus) {
            flags = O_RDWR;
        } else {
            flags = O_RDONLY;
        }
        break;
    case 'w':
        if (plus) {
            flags = O_RDWR | O_CREAT | O_TRUNC;
        } else {
            flags = O_WRONLY | O_CREAT | O_TRUNC;
        }
        break;
    case 'a':
        append = 1;
        if (plus) { // won't be properly supported
            flags = O_RDWR | O_CREAT;
        } else {
            flags = O_WRONLY | O_CREAT;
        }
        break;
    default:
        _impure_ptr->_errno = EINVAL;
        return NULL;
    }
    
    fd = open(fname, flags);
    
    if (fd >= 0) {
        r = (FILE *) malloc(sizeof(FILE));
        r->fd = fd;
    }
    
    return r;
}

static inline int fclose(FILE * stream) {
    int fd;
    
    if (!stream) {
        _impure_ptr->_errno = EINVAL;
        return -1;
    }
    
    fd = stream->fd;
    free(stream);
    return close(fd);
}

// Again, the compiler should do the right thing, and optimize depending on the values of size and nmemb.
// Luckily, we always will get into the short cases.
static inline size_t fread(void * _ptr, size_t size, size_t nmemb, FILE * stream) {
    int i;
    uint8_t * ptr = (uint8_t *) _ptr;
    
    if (!stream) {
        _impure_ptr->_errno = EINVAL;
        return -1;
    }
    
    if (size == 1)
        return read(stream->fd, ptr, nmemb);
    
    if (nmemb == 1)
        return read(stream->fd, ptr, size) == size ? 1 : 0;
    
    for (i = 0; i < nmemb; i++) {
        if (read(stream->fd, ptr + size * i, size) != size)
            return i;
    }
    
    return nmemb;
}

static inline size_t fwrite(const void * _ptr, size_t size, size_t nmemb, FILE * stream) {
    int i;
    const uint8_t * ptr = (const uint8_t *) _ptr;
    
    if (!stream) {
        _impure_ptr->_errno = EINVAL;
        return -1;
    }
    
    if (size == 1)
        return write(stream->fd, ptr, nmemb);
    
    if (nmemb == 1)
        return write(stream->fd, ptr, size) == size ? 1 : 0;
    
    for (i = 0; i < nmemb; i++) {
        if (write(stream->fd, ptr + size * i, size) != size)
            return i;
    }
    
    return nmemb;
}

extern FILE * stdin, * stdout, * stderr;

#endif