From ffca41d7f9ef06650735edd84f5809ca5e7676f6 Mon Sep 17 00:00:00 2001 From: turbocat Date: Sun, 23 May 2021 15:28:07 +0000 Subject: [PATCH] kolibri-libc: - Added ungetc, tollower and toupper to export - Fixed ungetc emu - Fixed file io functions - Update file_io test git-svn-id: svn://kolibrios.org@8730 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../libraries/kolibri-libc/include/ctype.h | 8 ++- .../libraries/kolibri-libc/include/stdio.h | 16 ++--- .../kolibri-libc/include/sys/dirent.h | 4 +- .../libraries/kolibri-libc/include/sys/ksys.h | 7 +- .../libraries/kolibri-libc/lib/libc.obj.a | Bin 62030 -> 63614 bytes .../libraries/kolibri-libc/samples/file_io.c | 55 ++++++++------- .../kolibri-libc/source/ctype/tolower.c | 7 ++ .../kolibri-libc/source/ctype/toupper.c | 7 ++ .../libraries/kolibri-libc/source/libc.c | 3 + .../kolibri-libc/source/stdio/fgetc.c | 4 +- .../kolibri-libc/source/stdio/fputc.c | 2 +- .../kolibri-libc/source/stdio/fputs.c | 7 +- .../kolibri-libc/source/stdio/fread.c | 12 ++-- .../kolibri-libc/source/stdio/freopen.c | 65 ++++++++++-------- .../kolibri-libc/source/stdio/fwrite.c | 4 +- .../kolibri-libc/source/stdio/getchar.c | 4 +- .../kolibri-libc/source/stdio/ungetc.c | 4 +- .../libraries/kolibri-libc/source/symbols.txt | 3 + 18 files changed, 130 insertions(+), 82 deletions(-) create mode 100644 programs/develop/libraries/kolibri-libc/source/ctype/tolower.c create mode 100644 programs/develop/libraries/kolibri-libc/source/ctype/toupper.c diff --git a/programs/develop/libraries/kolibri-libc/include/ctype.h b/programs/develop/libraries/kolibri-libc/include/ctype.h index 5f87ebe292..5bec93e3f0 100644 --- a/programs/develop/libraries/kolibri-libc/include/ctype.h +++ b/programs/develop/libraries/kolibri-libc/include/ctype.h @@ -6,6 +6,8 @@ ** dependable answers. */ +#include + #define __ALNUM 1 #define __ALPHA 2 #define __CNTRL 4 @@ -39,7 +41,7 @@ extern unsigned short *__is; #define isascii(c) (!((c)&(~0x7f))) #define toascii(c) ((c)&0x7f) -extern unsigned char tolower(unsigned char c); -extern unsigned char toupper(unsigned char c); +extern int _FUNC(tolower)(int c); +extern int _FUNC(toupper)(int c); -#endif \ No newline at end of file +#endif diff --git a/programs/develop/libraries/kolibri-libc/include/stdio.h b/programs/develop/libraries/kolibri-libc/include/stdio.h index 097f42e21b..63e860522d 100644 --- a/programs/develop/libraries/kolibri-libc/include/stdio.h +++ b/programs/develop/libraries/kolibri-libc/include/stdio.h @@ -47,23 +47,17 @@ extern void _FUNC(debug_printf)(const char* format, ...); typedef size_t fpos_t; -#define _STDIO_F_R 1 << 0 // Read -#define _STDIO_F_W 1 << 1 // Write -#define _STDIO_F_A 1 << 2 // Append -#define _STDIO_F_X 1 << 3 // eXclusive -#define _STDIO_F_B 1 << 4 // Binary +#define _FILEMODE_R 1 << 0 // Read +#define _FILEMODE_W 1 << 1 // Write +#define _FILEMODE_A 1 << 2 // Append typedef struct FILE_s { char *name; fpos_t position; int error; int eof; - int kind; // 0 - undiefned, 1 - text, 2 - binary - int orientation; // 0 - undiefned, 1 - byte, 2 - wide - int mode; // flags _STDIO_F_* - int append_offset; // do not seek before this point ("a" mode) + int mode; // flags _FILEMODE_* int __ungetc_emu_buff; // Uses __ungetc_emu (temporary solution!) - int start_size; } FILE; #define _IOFBF 0 @@ -99,7 +93,7 @@ extern size_t _FUNC(fread)(void *restrict, size_t size, size_t count, FILE *rest extern int _FUNC(fscanf)(FILE *restrict, const char *restrict, ...); extern size_t _FUNC(fwrite)(const void *restrict, size_t size, size_t count, FILE *restrict); extern int _FUNC(getc)(FILE *); -#define getc _FUNC(fgetc) +#define getc() _FUNC(fgetc)(stdin) extern int _FUNC(getchar)(void); extern int _FUNC(printf)(const char *restrict, ...); extern int _FUNC(putc)(int, FILE *); diff --git a/programs/develop/libraries/kolibri-libc/include/sys/dirent.h b/programs/develop/libraries/kolibri-libc/include/sys/dirent.h index 584adbea1c..04ea95033a 100644 --- a/programs/develop/libraries/kolibri-libc/include/sys/dirent.h +++ b/programs/develop/libraries/kolibri-libc/include/sys/dirent.h @@ -12,7 +12,7 @@ typedef unsigned ino_t; struct dirent{ - ino_t d_ino; //File serial number. + ino_t d_ino; //File serial number. char d_name[PATH_MAX]; // Name of entry. unsigned d_type; }; @@ -31,4 +31,4 @@ void _FUNC(rewinddir)(DIR *dir); void _FUNC(seekdir)(DIR *dir, unsigned pos); unsigned _FUNC(telldir)(DIR *dir); -#endif // _DIRENT_H_ \ No newline at end of file +#endif // _DIRENT_H_ diff --git a/programs/develop/libraries/kolibri-libc/include/sys/ksys.h b/programs/develop/libraries/kolibri-libc/include/sys/ksys.h index c88df52c20..e7f90beb80 100644 --- a/programs/develop/libraries/kolibri-libc/include/sys/ksys.h +++ b/programs/develop/libraries/kolibri-libc/include/sys/ksys.h @@ -884,7 +884,12 @@ int _ksys_file_read_file(const char *name, unsigned long long offset, unsigned s k.p21 = name; int status; unsigned bytes_read_v; - _ksys_work_files(&k); + asm_inline( + "int $0x40" + :"=a"(status), "=b"(bytes_read_v) + :"a"(70), "b"(&k) + :"memory" + ); if (!status) { *bytes_read = bytes_read_v; } diff --git a/programs/develop/libraries/kolibri-libc/lib/libc.obj.a b/programs/develop/libraries/kolibri-libc/lib/libc.obj.a index eef019ae8764ee38363de2b2d62b30da0f5dae1a..633e3738b20e4bca2a2c06a6c88d0d0759b85599 100644 GIT binary patch delta 672 zcmX}ne`w5c9LMoz-*txxjW+q!u+R}R!`d*75JCtcKklyExaoUmcV{`-Ws*!|d^<8Z zjSw<%Hd(sX#nRHHrAwEt>$--eYb{RgkITEe<6DnDuWz6C=lgkmLy5ffa9&%br@Uv6sRzz0Sd)bG3^x05VPD+Eao&rNej&L!Gd-$Jb^_o zVR0vviZXdG7b_%Jh)UU8)$M^bJ+L+aH9=V42OGque%Ks>tpl(v3_BvQQ|ykyo;d6q zg#ANsFbRi7;7AILqhJ=6G}MXXqF$&9*u_aVIGVuK42@UhUt8deN5a|b;JE?k+Tpwp zF5H8Q58;xuz1#_{;;OX0)(u(@c*RZ0w`9F7?gZd&5IUszeJTDR438r4I0{eW@Jz}+ zm$EOC;1^vfcs&Yl(sJ)x1p<2L&4=JbcsCjPir~X^=${K8=R;^Ad|Cnn%i!}$2v@Fa~HH#SpUV|00qcnn%akbdK32bX`FA z&?AFhOX(f@hW;xU7zSlBBn-=9L=GeK86E$OImx(lOekbhm>l;hVQTEDAuZl!78Nk7 zggK?my-xaV=7)uMS@eL#k6BXAvPzbRl~t^&X7yXvd|=&Y*4MD1mh3tA=@yYP<2RCv|=JK7jhc{1{HdD1MHmF*Hr2Is8hYWg5R{&^nv8 gjI!|wiN)zj!xOSM72h1Wv!X8PcvD4ATfa{K0MR3ex&QzG diff --git a/programs/develop/libraries/kolibri-libc/samples/file_io.c b/programs/develop/libraries/kolibri-libc/samples/file_io.c index 09597041f1..f1b97c1e99 100644 --- a/programs/develop/libraries/kolibri-libc/samples/file_io.c +++ b/programs/develop/libraries/kolibri-libc/samples/file_io.c @@ -1,43 +1,48 @@ #include #include +#include + +#define READ_MAX 255 + +static char test_str1[] = "123454567890abcdefghijklmnopqrstufvwxyz"; +static char test_str2[READ_MAX]; int main(int argc, char **argv) { - - int i; - char c; + int i=0; FILE *f; - FILE *fin; - FILE *fout; //write to file + debug_printf("Write file...\n"); f=fopen("testfile.txt","w"); - for(i=0;i<50;i++) - { - fputc('1',f); + while(test_str1[i]!='a'){ + fputc(test_str1[i],f); + i++; } fclose(f); //append to file + debug_printf("Apend file...\n"); f=fopen("testfile.txt","a"); - - for(i=0;i<50;i++) - { - fputc('2',f); - } + fputs(test_str1+i,f); + char null_term = '\0'; + fwrite(&null_term, sizeof(char), 1, f); fclose(f); //copy from testfile.txt to copyfile.txt - - fin=fopen("testfile.txt","r"); - fout=fopen("copyfile.txt","w"); -/* - while((c=fgetc(fin))!=EOF) - { - fputc(c,fout); - }*/ - fclose(fin); - fclose(fout); - -} \ No newline at end of file + debug_printf("Read file...\n"); + f=fopen("testfile.txt","r"); + i=0; + while((test_str2[i]=fgetc(f))!=EOF && i + +int tolower(int c) +{ + if (isupper(c)) return c | 32; + return c; +} diff --git a/programs/develop/libraries/kolibri-libc/source/ctype/toupper.c b/programs/develop/libraries/kolibri-libc/source/ctype/toupper.c new file mode 100644 index 0000000000..1799f03077 --- /dev/null +++ b/programs/develop/libraries/kolibri-libc/source/ctype/toupper.c @@ -0,0 +1,7 @@ +#include + +int toupper(int c) +{ + if (islower(c)) return c & 0x5f; + return c; +} diff --git a/programs/develop/libraries/kolibri-libc/source/libc.c b/programs/develop/libraries/kolibri-libc/source/libc.c index d8d9326653..3e8d4ad953 100644 --- a/programs/develop/libraries/kolibri-libc/source/libc.c +++ b/programs/develop/libraries/kolibri-libc/source/libc.c @@ -1,4 +1,6 @@ #include "ctype/is.c" +#include "ctype/tolower.c" +#include "ctype/toupper.c" #include "sys/rewinddir.c" #include "sys/readdir.c" @@ -49,6 +51,7 @@ #include "stdio/rewind.c" #include "stdio/vfprintf.c" #include "stdio/fprintf.c" +#include "stdio/ungetc.c" #include "string/strerror.c" #include "string/strxfrm.c" diff --git a/programs/develop/libraries/kolibri-libc/source/stdio/fgetc.c b/programs/develop/libraries/kolibri-libc/source/stdio/fgetc.c index 326539027b..7405ef4c11 100644 --- a/programs/develop/libraries/kolibri-libc/source/stdio/fgetc.c +++ b/programs/develop/libraries/kolibri-libc/source/stdio/fgetc.c @@ -2,8 +2,8 @@ int fgetc(FILE* stream) { - int c=EOF; - if(fwrite(&c, sizeof(int), 1, stream)==1){ + int c=0; + if(fread(&c, sizeof(char), 1, stream)==1){ return c; }else{ return EOF; diff --git a/programs/develop/libraries/kolibri-libc/source/stdio/fputc.c b/programs/develop/libraries/kolibri-libc/source/stdio/fputc.c index 0fe29f0c88..ddca544553 100644 --- a/programs/develop/libraries/kolibri-libc/source/stdio/fputc.c +++ b/programs/develop/libraries/kolibri-libc/source/stdio/fputc.c @@ -4,7 +4,7 @@ int fputc(int c, FILE *stream) { - if(fwrite(&c, sizeof(int), 1, stream)==1){ + if(fwrite(&c, sizeof(char), 1, stream)==1){ return c; }else{ return EOF; diff --git a/programs/develop/libraries/kolibri-libc/source/stdio/fputs.c b/programs/develop/libraries/kolibri-libc/source/stdio/fputs.c index f6488c8bbf..cc0926cb92 100644 --- a/programs/develop/libraries/kolibri-libc/source/stdio/fputs.c +++ b/programs/develop/libraries/kolibri-libc/source/stdio/fputs.c @@ -2,5 +2,10 @@ #include int fputs(const char *str, FILE *stream){ - return fwrite(str, sizeof(char), strlen(str), stream); + size_t str_len = strlen(str); + if(str_len == fwrite(str, sizeof(char), str_len , stream)){ + return str[str_len-1]; + }else{ + return EOF; + } } \ No newline at end of file diff --git a/programs/develop/libraries/kolibri-libc/source/stdio/fread.c b/programs/develop/libraries/kolibri-libc/source/stdio/fread.c index 594b3ba561..ac0f75a1e5 100644 --- a/programs/develop/libraries/kolibri-libc/source/stdio/fread.c +++ b/programs/develop/libraries/kolibri-libc/source/stdio/fread.c @@ -8,20 +8,24 @@ size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict strea unsigned bytes_count = size * nmemb; if(!stream){ - errno = EINVAL; + errno = EBADF; return 0; } if(stream==stdin){ __con_init(); - __con_gets((char*)ptr, bytes_count); + __con_gets((char*)ptr, bytes_count+1); return nmemb; } else{ - if(stream->mode != _STDIO_F_W){ + if(stream->mode & _FILEMODE_R){ + if(!stream->__ungetc_emu_buff){ + ((char*) ptr)[0]=(char)stream->__ungetc_emu_buff; + //debug_printf("Ungetc: %x\n", ((char*) ptr)[0]); + } unsigned status = _ksys_file_read_file(stream->name, stream->position, bytes_count, ptr , &bytes_read); - if (status != KSYS_FS_ERR_SUCCESS) { + if (status) { errno = EIO; stream->error = errno; return 0; diff --git a/programs/develop/libraries/kolibri-libc/source/stdio/freopen.c b/programs/develop/libraries/kolibri-libc/source/stdio/freopen.c index f5d25e623d..e8fde3144e 100644 --- a/programs/develop/libraries/kolibri-libc/source/stdio/freopen.c +++ b/programs/develop/libraries/kolibri-libc/source/stdio/freopen.c @@ -1,42 +1,53 @@ +#include "stddef.h" #include "sys/ksys.h" #include #include #include #include +#define CREATE_FILE() if(_ksys_file_create(_name)){ \ + errno= EIO; \ + free(out); \ + out = NULL; \ + } + FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) { - static ksys_bdfe_t info; - info.size=0; - if (!out) { - errno = ENOMEM; + if(!_name || !_mode || !out){ + errno = EINVAL; return NULL; } - _ksys_file_get_info(_name, &info); + 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); + out->eof=0; + out->error=0; + out->position=0; out->name = strdup(_name); - out->position = 0; - out->error = 0; - out->eof = 0; - out->kind = 0; - out->orientation = 0; - out->mode = 0; - out->start_size = info.size; - - if (strchr(_mode, 'b')) { out->mode |= _STDIO_F_B; } - if (strchr(_mode, 'x')) { out->mode |= _STDIO_F_X; } - if (strchr(_mode, 'a')) { out->mode |= _STDIO_F_A; } - if (strchr(_mode, 'r')) { out->mode |= _STDIO_F_R; } - if (strchr(_mode, 'w')) { out->mode |= _STDIO_F_W; } - if (strchr(_mode, '+')) { out->mode |= _STDIO_F_R | _STDIO_F_W; } - - if (out->mode & _STDIO_F_A) { - out->position = info.size; - out->append_offset = info.size; - } else if(out->mode & _STDIO_F_W){ - if(_ksys_file_create(_name)){ - return NULL; + + switch (out->mode) { + case _FILEMODE_A : + if(no_file){ + CREATE_FILE(); } + out->position = info.size; + break; + case _FILEMODE_W : + CREATE_FILE(); + break; + case _FILEMODE_R : + if(no_file){ + free(out); + out = NULL; + } + break; + default: + free(out); + out = NULL; + break; } return out; -} +} \ No newline at end of file diff --git a/programs/develop/libraries/kolibri-libc/source/stdio/fwrite.c b/programs/develop/libraries/kolibri-libc/source/stdio/fwrite.c index 716e455d51..dbfb6c3066 100644 --- a/programs/develop/libraries/kolibri-libc/source/stdio/fwrite.c +++ b/programs/develop/libraries/kolibri-libc/source/stdio/fwrite.c @@ -9,7 +9,7 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restric unsigned bytes_count = size * nmemb; if(!stream){ - errno = EINVAL; + errno = EBADF; return 0; } @@ -25,7 +25,7 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restric } } else{ - if(stream->mode != _STDIO_F_R){ + if(stream->mode != _FILEMODE_R){ unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written); if (status != KSYS_FS_ERR_SUCCESS) { errno = EIO; diff --git a/programs/develop/libraries/kolibri-libc/source/stdio/getchar.c b/programs/develop/libraries/kolibri-libc/source/stdio/getchar.c index 829ee18807..35c986a8f2 100644 --- a/programs/develop/libraries/kolibri-libc/source/stdio/getchar.c +++ b/programs/develop/libraries/kolibri-libc/source/stdio/getchar.c @@ -2,7 +2,9 @@ #include "conio.h" int getchar(void) { - int c = __con_getch(); + __con_init(); + char c = 0; + __con_gets(&c, 2); if (c == 0) { c = EOF; } diff --git a/programs/develop/libraries/kolibri-libc/source/stdio/ungetc.c b/programs/develop/libraries/kolibri-libc/source/stdio/ungetc.c index 40028b80bc..37ecacb0ed 100644 --- a/programs/develop/libraries/kolibri-libc/source/stdio/ungetc.c +++ b/programs/develop/libraries/kolibri-libc/source/stdio/ungetc.c @@ -11,12 +11,12 @@ int ungetc(int c, FILE* file) return EOF; } - if (file->mode != _STDIO_F_R){ + if (file->mode != _FILEMODE_R){ errno = EACCES; return EOF; } - if (file->position > file->start_size || file->position == 0 || c == EOF || file->__ungetc_emu_buff != EOF) + if (file->position == 0 || c == EOF) { errno = EOF; return EOF; diff --git a/programs/develop/libraries/kolibri-libc/source/symbols.txt b/programs/develop/libraries/kolibri-libc/source/symbols.txt index 662baf0a2f..6d7625b6d1 100644 --- a/programs/develop/libraries/kolibri-libc/source/symbols.txt +++ b/programs/develop/libraries/kolibri-libc/source/symbols.txt @@ -40,6 +40,7 @@ vprintf vfscanf vsnprintf vsscanf +ungetc !____STDLIB____ abs atoi @@ -125,5 +126,7 @@ longjmp setjmp !____CTYPE____ __is +tolower +toupper !___CONIO___ con_set_title