diff --git a/programs/develop/ktcc/trunk/libc.obj/include/stdio.h b/programs/develop/ktcc/trunk/libc.obj/include/stdio.h index 24383b2602..d0990e408e 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/stdio.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/stdio.h @@ -47,9 +47,10 @@ extern void _FUNC(debug_printf)(const char* format, ...); typedef size_t fpos_t; -#define _FILEMODE_R 1 << 0 // Read -#define _FILEMODE_W 1 << 1 // Write -#define _FILEMODE_A 1 << 2 // Append +#define _FILEMODE_R 1 << 0 // Read +#define _FILEMODE_W 1 << 1 // Write +#define _FILEMODE_A 1 << 2 // Append +#define _FILEMODE_PLUS 1 << 3 // Plus typedef struct FILE_s { char *name; diff --git a/programs/develop/ktcc/trunk/libc.obj/include/stdlib.h b/programs/develop/ktcc/trunk/libc.obj/include/stdlib.h index 286d0eab98..532474e39f 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/stdlib.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/stdlib.h @@ -43,6 +43,7 @@ extern int _FUNC(rand)(void); extern void _FUNC(__assert_fail)(const char *expr, const char *file, int line, const char *func); extern void _FUNC(qsort)(void *base0, size_t n, size_t size, int (*compar)(const void *, const void *)); +extern double _FUNC(strtod)(const char *s, char **sret); extern double _FUNC(atof)(const char *ascii); #endif diff --git a/programs/develop/ktcc/trunk/libc.obj/source/libc.c b/programs/develop/ktcc/trunk/libc.obj/source/libc.c index 86b2bb1c8e..16bb0c8806 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/libc.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/libc.c @@ -100,6 +100,7 @@ #include "stdlib/rand.c" #include "stdlib/qsort.c" #include "stdlib/assert.c" +#include "stdlib/strtod.c" #include "stdlib/atof.c" #include "math/acosh.c" diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdio/fclose.c b/programs/develop/ktcc/trunk/libc.obj/source/stdio/fclose.c index 8f558f4778..e4a7c7212a 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdio/fclose.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdio/fclose.c @@ -1,7 +1,9 @@ +#include "stddef.h" #include #include int fclose(FILE *stream) { free(stream); + stream = NULL; return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c b/programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c index be28b9398a..0aec53d777 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c @@ -24,7 +24,7 @@ size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict strea return nmemb; } - if(stream->mode & _FILEMODE_R){ + if(stream->mode != _FILEMODE_W && stream->mode != _FILEMODE_A){ if(!stream->__ungetc_emu_buff){ ((char*) ptr)[0]=(char)stream->__ungetc_emu_buff; //debug_printf("Ungetc: %x\n", ((char*) ptr)[0]); diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdio/freopen.c b/programs/develop/ktcc/trunk/libc.obj/source/stdio/freopen.c index e8fde3144e..a26a0eb115 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdio/freopen.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdio/freopen.c @@ -1,53 +1,62 @@ -#include "stddef.h" -#include "sys/ksys.h" +#include +#include #include #include #include #include +#include -#define CREATE_FILE() if(_ksys_file_create(_name)){ \ - errno= EIO; \ - free(out); \ - out = NULL; \ - } + +static FILE* _set_errno(FILE *out, int err){ + errno = err; + if(out){ + free(out->name); + free(out); + } + out = NULL; + return out; +} + +static void _create_file(char *name, FILE *out){ + if(_ksys_file_create(name)){ + _set_errno(out, EIO); + } +} FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) { if(!_name || !_mode || !out){ - errno = EINVAL; - return NULL; + return _set_errno(out, EINVAL); } - if (strchr(_mode, 'r')) { out->mode = _FILEMODE_R; } - if (strchr(_mode, 'a')) { out->mode = _FILEMODE_A; } - if (strchr(_mode, 'w')) { out->mode = _FILEMODE_W; } - ksys_bdfe_t info; int no_file = _ksys_file_get_info(_name, &info); + if(!no_file && info.attributes & IS_FOLDER){ + return _set_errno(out, EISDIR); + } + out->eof=0; out->error=0; out->position=0; out->name = strdup(_name); - switch (out->mode) { - case _FILEMODE_A : + if (strchr(_mode, 'r')) { out->mode = _FILEMODE_R; } + if (strchr(_mode, 'w')) { out->mode = _FILEMODE_W; } + if (strchr(_mode, 'a')) { out->mode = _FILEMODE_A; } + if (strchr(_mode, '+')) { out->mode |= _FILEMODE_PLUS; } + + if(out->mode & _FILEMODE_A){ if(no_file){ - CREATE_FILE(); + _create_file(out->name, out); } out->position = info.size; - break; - case _FILEMODE_W : - CREATE_FILE(); - break; - case _FILEMODE_R : + }else if(out->mode & _FILEMODE_W){ + _create_file(out->name, out); + }else if((out->mode & _FILEMODE_R)){ if(no_file){ - free(out); - out = NULL; + _set_errno(out, ENOENT); } - break; - default: - free(out); - out = NULL; - break; + }else{ + _set_errno(out, EINVAL); } return out; } \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/atof.c b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/atof.c index f9f7f98a91..0db314b442 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/atof.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/atof.c @@ -1,8 +1,7 @@ /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include -double -atof(const char *ascii) +double atof(const char *ascii) { return strtod(ascii, 0); } diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/strtod.c b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/strtod.c new file mode 100644 index 0000000000..f095b36902 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/strtod.c @@ -0,0 +1,96 @@ + /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ +#include + +#define unconst(__v, __t) __extension__ ({union { const __t __cp; __t __p; } __q; __q.__cp = __v; __q.__p;}) + +double strtod(const char *s, char **sret) +{ + long double r; /* result */ + int e; /* exponent */ + long double d; /* scale */ + int sign; /* +- 1.0 */ + int esign; + int i; + int flags=0; + + r = 0.0; + sign = 1.0; + e = 0; + esign = 1; + + while ((*s == ' ') || (*s == '\t')) + s++; + + if (*s == '+') + s++; + else if (*s == '-') + { + sign = -1; + s++; + } + + while ((*s >= '0') && (*s <= '9')) + { + flags |= 1; + r *= 10.0; + r += *s - '0'; + s++; + } + + if (*s == '.') + { + d = 0.1L; + s++; + + while ((*s >= '0') && (*s <= '9')) + { + flags |= 2; + r += d * (*s - '0'); + s++; + d *= 0.1L; + } + } + + if (flags == 0) + { + if (sret) + *sret = unconst(s, char *); + return 0; + } + + if ((*s == 'e') || (*s == 'E')) + { + s++; + if (*s == '+') + s++; + else if (*s == '-') + { + s++; + esign = -1; + } + + if ((*s < '0') || (*s > '9')) + { + if (sret) + *sret = unconst(s, char *); + return r; + } + + while ((*s >= '0') && (*s <= '9')) + { + e *= 10; + e += *s - '0'; + s++; + } + } + if (esign < 0) + for (i = 1; i <= e; i++) + r *= 0.1L; + else + for (i = 1; i <= e; i++) + r *= 10.0; + + if (sret) + *sret = unconst(s, char *); + return r * sign; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc.obj/source/symbols.txt b/programs/develop/ktcc/trunk/libc.obj/source/symbols.txt index 43a24dc64f..8da25ec0f6 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/symbols.txt +++ b/programs/develop/ktcc/trunk/libc.obj/source/symbols.txt @@ -63,6 +63,7 @@ strtol srand rand qsort +strtod __assert_fail !____STRING____ !memcpy