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
| #include "minicrt.h"
int CrtInitIo() { return 0; }
static int open(const char *pathname, int flags, int mode) { int ret;
__asm__ volatile( "int $0x80" :"=a"(ret) :"0"(5),"b"(pathname),"c"(flags),"d"(mode) ); if (ret >= 0) return ret; return -1; }
static int write(int fd, const void *buf, int count) { int ret;
__asm__ volatile( "int $0x80" :"=a"(ret) :"0"(4),"b"(fd),"c"(buf),"d"(count) ); if (ret >= 0) return ret; return -1; }
static int read(int fd, void *buf, int count) { int ret;
__asm__ volatile( "int $0x80" :"=a"(ret) :"0"(3),"b"(fd),"c"(buf),"d"(count) ); if (ret >= 0) return ret; return -1; } static int close(int fd) { int ret;
__asm__ volatile( "int $0x80" :"=a"(ret) :"0"(6),"b"(fd) ); if (ret >= 0) return ret; return -1; }
int lseek(int fd, int offset, int whence) { int ret;
__asm__ volatile( "int $0x80" :"=a"(ret) :"0"(19),"b"(fd),"c"(offset),"d"(whence) ); if (ret >= 0) return ret; return -1; }
int fread(void *ptr, int size, int nmemb, FILE *stream) { return read((int)stream, ptr, size * nmemb); }
int fwrite(const void *ptr, int size, int nmemb, FILE *stream) { return write((int)stream, ptr, size * nmemb); }
FILE *fopen(const char *path, const char *mode) { int fd = -1; int flags = 0; int access = 00700;
if (strcmp(mode, "w") == 0) flags |= O_WRONLY | O_CREAT | O_TRUNC; if (strcmp(mode, "w+") == 0) flags |= O_RDWR | O_CREAT | O_TRUNC; if (strcmp(mode, "r") == 0) flags |= O_RDONLY; if (strcmp(mode, "r+") == 0) flags |= O_RDWR | O_CREAT; fd = open(path, flags, access); return (FILE *)fd; }
int fclose(FILE *fp) { return close((int)fp); }
int fseek(FILE *stream, long offset, int whence) { return lseek((int)stream, offset, whence); }
int fputc(int c, FILE *stream) { if (fwrite(&c, 1, 1, stream) != 1) return EOF; else return c; } int fputs(const char *s, FILE *stream) { int len;
len = strlen(s); if (fwrite(s, 1, len, stream) != len) return EOF; else return len; } int vfprintf(FILE *stream, const char *format, va_list ap) { int n = 0, flag = 0, ret; char str[20];
while (*format) { switch (*format) { case '%': if (flag == 1) { fputc('%', stream); flag = 0; n ++; } else flag = 1; break; case 'd': if (flag == 1) { itoa((int)va_arg(ap, int), str, 10); ret = fputs(str, stream); n += ret; } else { fputc('d', stream); n ++; } flag = 0; break; case 's': if (flag == 1) { ret = fputs((char *)va_arg(ap, char *), stream); n += ret; } else { fputc('s', stream); n ++; } flag = 0; break; case '\n': fputc(0x0d, stream); n ++; fputc(0x0a, stream); n ++; break; default: fputc(*format, stream); n ++; } format ++; } return n; } int fprintf(FILE *stream, const char *format, ...) { int n; va_list ap;
va_start(ap, format); n = vfprintf(stream, format, ap); va_end(ap); return n; } int printf(const char *format, ...) { int n; va_list ap;
va_start(ap, format); n = vfprintf(stdout, format, ap); va_end(ap); return n; }
|