Skip to content

Commit 8384d11

Browse files
Merge branch 'master' into fontlib_loadfont
2 parents 51b0bde + d093e1b commit 8384d11

14 files changed

Lines changed: 81 additions & 74 deletions

File tree

src/libc/clearerr.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "__fileioc_stdio.h"
22

3+
#include <stdint.h>
4+
35
void __attribute__((weak)) clearerr(FILE *stream)
46
{
57
if (stream == NULL ||
@@ -9,7 +11,7 @@ void __attribute__((weak)) clearerr(FILE *stream)
911
{
1012
return;
1113
}
12-
13-
_file_streams[stream->slot].eof = 0;
14-
_file_streams[stream->slot].err = 0;
14+
uint8_t index = stream->slot - 1;
15+
_file_streams[index].eof = 0;
16+
_file_streams[index].err = 0;
1517
}

src/libc/fclose.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "__fileioc_stdio.h"
22

3+
#include <stdint.h>
4+
35
int __attribute__((weak)) fclose(FILE *stream)
46
{
57
ti_var_t slot;
@@ -14,8 +16,13 @@ int __attribute__((weak)) fclose(FILE *stream)
1416

1517
slot = stream->slot;
1618

17-
_file_streams[slot - 1].slot = 0;
18-
1919
int status = ti_Close(slot);
20-
return (status == 0) ? EOF : 0;
20+
if (status == 0)
21+
{
22+
// failed to close file
23+
return EOF;
24+
}
25+
uint8_t index = slot - 1;
26+
_file_streams[index].slot = 0;
27+
return 0;
2128
}

src/libc/feof.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
int __attribute__((weak)) feof(FILE *stream)
44
{
5+
if (stream == NULL || stream == stdin || stream == stdout || stream == stderr)
6+
{
7+
return 0;
8+
}
59
return stream->eof;
610
}

src/libc/ferror.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
int __attribute__((weak)) ferror(FILE *stream)
44
{
5+
if (stream == NULL || stream == stdin || stream == stdout || stream == stderr)
6+
{
7+
return 0;
8+
}
59
return stream->err;
610
}

src/libc/fgetc.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@
22

33
int __attribute__((weak)) fgetc(FILE *stream)
44
{
5-
int c;
6-
7-
if (stream == NULL)
5+
if (stream == NULL || stream == stdout || stream == stderr)
86
{
97
return EOF;
108
}
11-
12-
if (stream == stdout || stream == stderr)
13-
{
14-
c = EOF;
15-
}
16-
else if (stream == stdin)
9+
if (stream == stdin)
1710
{
18-
c = getchar();
19-
}
20-
else
21-
{
22-
c = ti_GetC(stream->slot);
11+
return getchar();
2312
}
2413

14+
int c = ti_GetC(stream->slot);
15+
2516
if (c == EOF)
2617
{
2718
stream->eof = 1;

src/libc/fopen.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
FILE* __attribute__((weak)) fopen(const char *__restrict filename, const char *__restrict mode)
66
{
77
ti_var_t slot;
8-
uint8_t index;
98

109
slot = ti_Open(filename, mode);
1110
if (slot == 0)
1211
{
1312
return NULL;
1413
}
1514

16-
index = slot - 1;
15+
uint8_t index = slot - 1;
1716
_file_streams[index].slot = slot;
1817
_file_streams[index].eof = 0;
1918
_file_streams[index].err = 0;

src/libc/fputc.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,21 @@
22

33
int __attribute__((weak)) fputc(int c, FILE *stream)
44
{
5-
int ret;
6-
7-
if (stream == NULL)
5+
if (stream == NULL || stream == stdin)
86
{
97
return EOF;
108
}
11-
12-
if (stream == stdin)
9+
if (stream == stdout || stream == stderr)
1310
{
14-
ret = EOF;
15-
}
16-
else if (stream == stdout || stream == stderr)
17-
{
18-
ret = putchar(c);
19-
}
20-
else
21-
{
22-
ret = ti_PutC((char)c, stream->slot);
11+
return putchar(c);
2312
}
2413

14+
int ret = ti_PutC((char)c, stream->slot);
15+
/*
16+
* `ti_PutC` returns `(unsigned char)c` or `EOF` so we can skip testing for
17+
* the case of `ret != (unsigned char)c`
18+
*/
2519
if (ret == EOF)
26-
{
27-
stream->eof = 1;
28-
}
29-
30-
if (ret != c)
3120
{
3221
stream->err = 1;
3322
}

src/libc/fread.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
size_t __attribute__((weak)) fread(void *ptr, size_t size, size_t count, FILE *__restrict stream)
44
{
5-
size_t ncount;
6-
7-
if (stream == NULL ||
8-
stream == stdout ||
9-
stream == stderr)
5+
if (stream == NULL || stream == stdout || stream == stderr)
6+
{
7+
return 0;
8+
}
9+
// If size or count is zero, fread returns zero and performs no other action.
10+
if (size == 0 || count == 0)
1011
{
1112
return 0;
1213
}
@@ -16,6 +17,7 @@ size_t __attribute__((weak)) fread(void *ptr, size_t size, size_t count, FILE *_
1617
int c;
1718
char *p = ptr;
1819
size_t len = size * count;
20+
size_t bytes_read = 0;
1921

2022
for (; len > 0; len--)
2123
{
@@ -24,12 +26,13 @@ size_t __attribute__((weak)) fread(void *ptr, size_t size, size_t count, FILE *_
2426
break;
2527
}
2628
*p++ = (char)c;
29+
bytes_read++;
2730
}
2831

29-
return count;
32+
return bytes_read / size;
3033
}
3134

32-
ncount = ti_Read(ptr, size, count, stream->slot);
35+
size_t ncount = ti_Read(ptr, size, count, stream->slot);
3336
if (ncount != count)
3437
{
3538
stream->err = 1;

src/libc/fseek.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "__fileioc_stdio.h"
22

3-
int __attribute__((weak)) fseek(FILE *stream, long int offset, int origin)
3+
int __attribute__((weak)) fseek(FILE *stream, long offset, int origin)
44
{
55
if (stream == NULL ||
66
stream == stdin ||
@@ -9,6 +9,5 @@ int __attribute__((weak)) fseek(FILE *stream, long int offset, int origin)
99
{
1010
return -1;
1111
}
12-
13-
return ti_Seek((int)offset, origin, stream->slot);
12+
return (ti_Seek((int)offset, origin, stream->slot) == EOF) ? -1 : 0;
1413
}

src/libc/ftell.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "__fileioc_stdio.h"
22
#include <stdint.h>
33

4-
long int __attribute__((weak)) ftell(FILE *stream)
4+
long __attribute__((weak)) ftell(FILE *stream)
55
{
66
if (stream == NULL ||
77
stream == stdin ||
@@ -14,5 +14,5 @@ long int __attribute__((weak)) ftell(FILE *stream)
1414
// ti_Tell shouldn't return a value greater than OS_VAR_MAX_SIZE (65512) unless an error occurs
1515
uint16_t ret = ti_Tell(stream->slot);
1616
// Convert a result of UINT16_MAX to -1L, leaving other results the same
17-
return ((uint16_t)ret + 1) - 1L;
17+
return (uint16_t)(ret + 1) - 1L;
1818
}

0 commit comments

Comments
 (0)