forked from KolibriOS/kolibrios
kolibri-libc: Reimplement fgets, fputc, fputs, fread and fwrite
Now it follows the POSIX specification better. git-svn-id: svn://kolibrios.org@8624 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
23ada42423
commit
162d919194
@ -1,11 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <ksys.h>
|
||||
|
||||
int fgetc(FILE* stream)
|
||||
{
|
||||
int c, rc;
|
||||
rc = fread(&c, sizeof(char), 1, stream);
|
||||
if(rc<1){
|
||||
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;
|
||||
}
|
||||
return EOF;
|
||||
}
|
||||
|
||||
stream->position++;
|
||||
return c;
|
||||
}
|
||||
|
@ -1,9 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <ksys.h>
|
||||
|
||||
int fputc(int sym, FILE *stream)
|
||||
int fputc(int c, FILE *stream)
|
||||
{
|
||||
if(!fwrite(&sym, sizeof(char), 1, 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;
|
||||
}
|
||||
return EOF;
|
||||
}
|
||||
return sym;
|
||||
|
||||
stream->position++;
|
||||
return c;
|
||||
}
|
@ -2,9 +2,10 @@
|
||||
#include <string.h>
|
||||
|
||||
int fputs(const char *str, FILE *stream){
|
||||
int s_code;
|
||||
for(int i=0; i<strlen(str) && s_code!=EOF; i++){
|
||||
s_code = fputc(str[i], stream);
|
||||
for(size_t i = 0; i < strlen(str); i++){
|
||||
if (fputc(str[i], stream) == EOF) {
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
return s_code;
|
||||
return 0;
|
||||
}
|
@ -3,14 +3,18 @@
|
||||
size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream) {
|
||||
unsigned bytes_read = 0;
|
||||
unsigned bytes_count = size * nmemb;
|
||||
_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;
|
||||
|
||||
for (size_t i = 0; i < bytes_count; i++) {
|
||||
char c = fgetc(stream);
|
||||
|
||||
if (c == EOF) {
|
||||
break;
|
||||
}
|
||||
|
||||
ptr[i] = c;
|
||||
|
||||
bytes_read++;
|
||||
}
|
||||
|
||||
return bytes_read / size;
|
||||
}
|
||||
|
@ -1,16 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
size_t fwrite(const void * restrict ptr,size_t size, size_t nmemb,FILE * restrict stream) {
|
||||
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;
|
||||
_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;
|
||||
|
||||
for (size_t i = 0; i < bytes_count; i++) {
|
||||
char c = ptr[i];
|
||||
|
||||
if (fputc(c, stream) != c) {
|
||||
break;
|
||||
}
|
||||
|
||||
bytes_written++;
|
||||
}
|
||||
|
||||
return bytes_written / size;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user