/contrib/kolibri-libc/source/stdio/fgetc.c |
---|
1,38 → 1,11 |
#include <stdio.h> |
#include <errno.h> |
#include <ksys.h> |
int fgetc(FILE* stream) |
{ |
unsigned bytes_read; |
char c; |
unsigned status = _ksys_file_read_file(stream->name, stream->position, 1, &c, &bytes_read); |
if (status != _KOS_FS_ERR_SUCCESS) { |
switch (status) { |
case _KOS_FS_ERR_EOF: |
stream->eof = 1; |
break; |
case _KOS_FS_ERR_1: |
case _KOS_FS_ERR_2: |
case _KOS_FS_ERR_3: |
case _KOS_FS_ERR_4: |
case _KOS_FS_ERR_5: |
case _KOS_FS_ERR_7: |
case _KOS_FS_ERR_8: |
case _KOS_FS_ERR_9: |
case _KOS_FS_ERR_10: |
case _KOS_FS_ERR_11: |
default: |
// Just some IO error, who knows what exactly happened |
errno = EIO; |
stream->error = errno; |
break; |
} |
int c, rc; |
rc = fread(&c, sizeof(char), 1, stream); |
if(rc<1){ |
return EOF; |
} |
stream->position++; |
return c; |
} |
/contrib/kolibri-libc/source/stdio/fputc.c |
---|
1,35 → 1,9 |
#include <stdio.h> |
#include <errno.h> |
#include <ksys.h> |
int fputc(int c, FILE *stream) |
int fputc(int sym, FILE *stream) |
{ |
unsigned bytes_written; |
unsigned status = _ksys_file_write_file(stream->name, stream->position, 1, &c, &bytes_written); |
if (status != _KOS_FS_ERR_SUCCESS) { |
switch (status) { |
case _KOS_FS_ERR_1: |
case _KOS_FS_ERR_2: |
case _KOS_FS_ERR_3: |
case _KOS_FS_ERR_4: |
case _KOS_FS_ERR_5: |
case _KOS_FS_ERR_EOF: |
case _KOS_FS_ERR_7: |
case _KOS_FS_ERR_8: |
case _KOS_FS_ERR_9: |
case _KOS_FS_ERR_10: |
case _KOS_FS_ERR_11: |
default: |
// Just some IO error, who knows what exactly happened |
errno = EIO; |
stream->error = errno; |
break; |
} |
if(!fwrite(&sym, sizeof(char), 1, stream)){ |
return EOF; |
} |
stream->position++; |
return c; |
return sym; |
} |
/contrib/kolibri-libc/source/stdio/fputs.c |
---|
2,10 → 2,9 |
#include <string.h> |
int fputs(const char *str, FILE *stream){ |
for(size_t i = 0; i < strlen(str); i++){ |
if (fputc(str[i], stream) == EOF) { |
return EOF; |
int s_code; |
for(int i=0; i<strlen(str) && s_code!=EOF; i++){ |
s_code = fputc(str[i], stream); |
} |
} |
return 0; |
return s_code; |
} |
/contrib/kolibri-libc/source/stdio/fread.c |
---|
3,18 → 3,14 |
size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream) { |
unsigned bytes_read = 0; |
unsigned bytes_count = size * nmemb; |
for (size_t i = 0; i < bytes_count; i++) { |
char c = fgetc(stream); |
if (c == EOF) { |
break; |
_ksys_file_read_file(stream->name, stream->position, bytes_count, ptr, &bytes_read); |
stream->position += bytes_read; |
ksys_bdfe_t info; |
// TODO: Handle _ksys_file_get_info error somehow |
if (!_ksys_file_get_info(stream->name, &info)) { |
if (stream->position >= info.size) { |
stream->eof = 1; |
} |
ptr[i] = c; |
bytes_read++; |
} |
return bytes_read / size; |
} |
/contrib/kolibri-libc/source/stdio/fwrite.c |
---|
3,16 → 3,14 |
size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream) { |
unsigned bytes_written = 0; |
unsigned bytes_count = size * nmemb; |
for (size_t i = 0; i < bytes_count; i++) { |
char c = ptr[i]; |
if (fputc(c, stream) != c) { |
break; |
_ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written); |
stream->position += bytes_written; |
ksys_bdfe_t info; |
// TODO: Handle _ksys_file_get_info error somehow |
if (!_ksys_file_get_info(stream->name, &info)) { |
if (stream->position >= info.size) { |
stream->eof = 1; |
} |
bytes_written++; |
} |
return bytes_written / size; |
} |