forked from KolibriOS/kolibrios
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:
parent
c09b0569fc
commit
ffca41d7f9
@ -6,6 +6,8 @@
|
||||
** dependable answers.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#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
|
@ -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 *);
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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;
|
||||
}
|
||||
|
Binary file not shown.
@ -1,43 +1,48 @@
|
||||
#include <stdio.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 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);
|
||||
|
||||
debug_printf("Read file...\n");
|
||||
f=fopen("testfile.txt","r");
|
||||
i=0;
|
||||
while((test_str2[i]=fgetc(f))!=EOF && i<READ_MAX){
|
||||
fputc(test_str2[i], stdout);
|
||||
i++;
|
||||
}
|
||||
printf("\n%s\n", test_str1);
|
||||
if(!strcmp(test_str2, test_str1)){
|
||||
puts("TEST: OK!");
|
||||
}else{
|
||||
puts("TEST: FAIL!");
|
||||
}
|
||||
fclose(f);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
#include <ctype.h>
|
||||
|
||||
int tolower(int c)
|
||||
{
|
||||
if (isupper(c)) return c | 32;
|
||||
return c;
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
#include <ctype.h>
|
||||
|
||||
int toupper(int c)
|
||||
{
|
||||
if (islower(c)) return c & 0x5f;
|
||||
return 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"
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -2,5 +2,10 @@
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -1,42 +1,53 @@
|
||||
#include "stddef.h"
|
||||
#include "sys/ksys.h"
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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) {
|
||||
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;
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user