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
This commit is contained in:
turbocat 2021-05-23 15:28:07 +00:00
parent c09b0569fc
commit ffca41d7f9
18 changed files with 130 additions and 82 deletions

View File

@ -6,6 +6,8 @@
** dependable answers. ** dependable answers.
*/ */
#include <stddef.h>
#define __ALNUM 1 #define __ALNUM 1
#define __ALPHA 2 #define __ALPHA 2
#define __CNTRL 4 #define __CNTRL 4
@ -39,7 +41,7 @@ extern unsigned short *__is;
#define isascii(c) (!((c)&(~0x7f))) #define isascii(c) (!((c)&(~0x7f)))
#define toascii(c) ((c)&0x7f) #define toascii(c) ((c)&0x7f)
extern unsigned char tolower(unsigned char c); extern int _FUNC(tolower)(int c);
extern unsigned char toupper(unsigned char c); extern int _FUNC(toupper)(int c);
#endif #endif

View File

@ -47,23 +47,17 @@ extern void _FUNC(debug_printf)(const char* format, ...);
typedef size_t fpos_t; typedef size_t fpos_t;
#define _STDIO_F_R 1 << 0 // Read #define _FILEMODE_R 1 << 0 // Read
#define _STDIO_F_W 1 << 1 // Write #define _FILEMODE_W 1 << 1 // Write
#define _STDIO_F_A 1 << 2 // Append #define _FILEMODE_A 1 << 2 // Append
#define _STDIO_F_X 1 << 3 // eXclusive
#define _STDIO_F_B 1 << 4 // Binary
typedef struct FILE_s { typedef struct FILE_s {
char *name; char *name;
fpos_t position; fpos_t position;
int error; int error;
int eof; int eof;
int kind; // 0 - undiefned, 1 - text, 2 - binary int mode; // flags _FILEMODE_*
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 __ungetc_emu_buff; // Uses __ungetc_emu (temporary solution!) int __ungetc_emu_buff; // Uses __ungetc_emu (temporary solution!)
int start_size;
} FILE; } FILE;
#define _IOFBF 0 #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 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 size_t _FUNC(fwrite)(const void *restrict, size_t size, size_t count, FILE *restrict);
extern int _FUNC(getc)(FILE *); extern int _FUNC(getc)(FILE *);
#define getc _FUNC(fgetc) #define getc() _FUNC(fgetc)(stdin)
extern int _FUNC(getchar)(void); extern int _FUNC(getchar)(void);
extern int _FUNC(printf)(const char *restrict, ...); extern int _FUNC(printf)(const char *restrict, ...);
extern int _FUNC(putc)(int, FILE *); extern int _FUNC(putc)(int, FILE *);

View File

@ -12,7 +12,7 @@
typedef unsigned ino_t; typedef unsigned ino_t;
struct dirent{ struct dirent{
ino_t d_ino; //File serial number. ino_t d_ino; //File serial number.
char d_name[PATH_MAX]; // Name of entry. char d_name[PATH_MAX]; // Name of entry.
unsigned d_type; unsigned d_type;
}; };

View File

@ -884,7 +884,12 @@ int _ksys_file_read_file(const char *name, unsigned long long offset, unsigned s
k.p21 = name; k.p21 = name;
int status; int status;
unsigned bytes_read_v; 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) { if (!status) {
*bytes_read = bytes_read_v; *bytes_read = bytes_read_v;
} }

View File

@ -1,43 +1,48 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#define READ_MAX 255
static char test_str1[] = "123454567890abcdefghijklmnopqrstufvwxyz";
static char test_str2[READ_MAX];
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i=0;
int i;
char c;
FILE *f; FILE *f;
FILE *fin;
FILE *fout;
//write to file //write to file
debug_printf("Write file...\n");
f=fopen("testfile.txt","w"); f=fopen("testfile.txt","w");
for(i=0;i<50;i++) while(test_str1[i]!='a'){
{ fputc(test_str1[i],f);
fputc('1',f); i++;
} }
fclose(f); fclose(f);
//append to file //append to file
debug_printf("Apend file...\n");
f=fopen("testfile.txt","a"); f=fopen("testfile.txt","a");
fputs(test_str1+i,f);
for(i=0;i<50;i++) char null_term = '\0';
{ fwrite(&null_term, sizeof(char), 1, f);
fputc('2',f);
}
fclose(f); fclose(f);
//copy from testfile.txt to copyfile.txt //copy from testfile.txt to copyfile.txt
debug_printf("Read file...\n");
fin=fopen("testfile.txt","r"); f=fopen("testfile.txt","r");
fout=fopen("copyfile.txt","w"); i=0;
/* while((test_str2[i]=fgetc(f))!=EOF && i<READ_MAX){
while((c=fgetc(fin))!=EOF) fputc(test_str2[i], stdout);
{ i++;
fputc(c,fout); }
}*/ printf("\n%s\n", test_str1);
fclose(fin); if(!strcmp(test_str2, test_str1)){
fclose(fout); puts("TEST: OK!");
}else{
puts("TEST: FAIL!");
}
fclose(f);
} }

View File

@ -0,0 +1,7 @@
#include <ctype.h>
int tolower(int c)
{
if (isupper(c)) return c | 32;
return c;
}

View File

@ -0,0 +1,7 @@
#include <ctype.h>
int toupper(int c)
{
if (islower(c)) return c & 0x5f;
return c;
}

View File

@ -1,4 +1,6 @@
#include "ctype/is.c" #include "ctype/is.c"
#include "ctype/tolower.c"
#include "ctype/toupper.c"
#include "sys/rewinddir.c" #include "sys/rewinddir.c"
#include "sys/readdir.c" #include "sys/readdir.c"
@ -49,6 +51,7 @@
#include "stdio/rewind.c" #include "stdio/rewind.c"
#include "stdio/vfprintf.c" #include "stdio/vfprintf.c"
#include "stdio/fprintf.c" #include "stdio/fprintf.c"
#include "stdio/ungetc.c"
#include "string/strerror.c" #include "string/strerror.c"
#include "string/strxfrm.c" #include "string/strxfrm.c"

View File

@ -2,8 +2,8 @@
int fgetc(FILE* stream) int fgetc(FILE* stream)
{ {
int c=EOF; int c=0;
if(fwrite(&c, sizeof(int), 1, stream)==1){ if(fread(&c, sizeof(char), 1, stream)==1){
return c; return c;
}else{ }else{
return EOF; return EOF;

View File

@ -4,7 +4,7 @@
int fputc(int c, FILE *stream) int fputc(int c, FILE *stream)
{ {
if(fwrite(&c, sizeof(int), 1, stream)==1){ if(fwrite(&c, sizeof(char), 1, stream)==1){
return c; return c;
}else{ }else{
return EOF; return EOF;

View File

@ -2,5 +2,10 @@
#include <string.h> #include <string.h>
int fputs(const char *str, FILE *stream){ 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;
}
} }

View File

@ -8,20 +8,24 @@ size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict strea
unsigned bytes_count = size * nmemb; unsigned bytes_count = size * nmemb;
if(!stream){ if(!stream){
errno = EINVAL; errno = EBADF;
return 0; return 0;
} }
if(stream==stdin){ if(stream==stdin){
__con_init(); __con_init();
__con_gets((char*)ptr, bytes_count); __con_gets((char*)ptr, bytes_count+1);
return nmemb; return nmemb;
} }
else{ 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); 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; errno = EIO;
stream->error = errno; stream->error = errno;
return 0; return 0;

View File

@ -1,42 +1,53 @@
#include "stddef.h"
#include "sys/ksys.h" #include "sys/ksys.h"
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#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) { FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) {
static ksys_bdfe_t info; if(!_name || !_mode || !out){
info.size=0; errno = EINVAL;
if (!out) {
errno = ENOMEM;
return NULL; 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->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; } switch (out->mode) {
if (strchr(_mode, 'x')) { out->mode |= _STDIO_F_X; } case _FILEMODE_A :
if (strchr(_mode, 'a')) { out->mode |= _STDIO_F_A; } if(no_file){
if (strchr(_mode, 'r')) { out->mode |= _STDIO_F_R; } CREATE_FILE();
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;
} }
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; return out;
} }

View File

@ -9,7 +9,7 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restric
unsigned bytes_count = size * nmemb; unsigned bytes_count = size * nmemb;
if(!stream){ if(!stream){
errno = EINVAL; errno = EBADF;
return 0; return 0;
} }
@ -25,7 +25,7 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restric
} }
} }
else{ 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); unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written);
if (status != KSYS_FS_ERR_SUCCESS) { if (status != KSYS_FS_ERR_SUCCESS) {
errno = EIO; errno = EIO;

View File

@ -2,7 +2,9 @@
#include "conio.h" #include "conio.h"
int getchar(void) { int getchar(void) {
int c = __con_getch(); __con_init();
char c = 0;
__con_gets(&c, 2);
if (c == 0) { if (c == 0) {
c = EOF; c = EOF;
} }

View File

@ -11,12 +11,12 @@ int ungetc(int c, FILE* file)
return EOF; return EOF;
} }
if (file->mode != _STDIO_F_R){ if (file->mode != _FILEMODE_R){
errno = EACCES; errno = EACCES;
return EOF; 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; errno = EOF;
return EOF; return EOF;

View File

@ -40,6 +40,7 @@ vprintf
vfscanf vfscanf
vsnprintf vsnprintf
vsscanf vsscanf
ungetc
!____STDLIB____ !____STDLIB____
abs abs
atoi atoi
@ -125,5 +126,7 @@ longjmp
setjmp setjmp
!____CTYPE____ !____CTYPE____
__is __is
tolower
toupper
!___CONIO___ !___CONIO___
con_set_title con_set_title