-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnewlib.c
108 lines (92 loc) · 1.95 KB
/
newlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "newlib.h"
#include "usart.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#undef errno
extern int errno;
void _exit(int return_code)
{
printf("Program exited with code %d.\n", return_code);
while (1);
}
FILEHANDLE _open (const char *name, int openmode) {
return -1;
}
int _close (FILEHANDLE fh)
{
if (fh <= STDERR)
return (0);
//return (__fclose (fh));
return -1;
}
int _write (FILEHANDLE fh, const uint8_t *buf, uint32_t len, int mode)
{
if ((fh == STDOUT) || (fh == STDERR))
{
USART1_write((const char *) buf, len);
return (len);
};
if (fh <= STDERR) return (-1);
//return (__write (fh, buf, len));
return -1;
}
int _read (FILEHANDLE fh, uint8_t *buf, uint32_t len, int mode)
{
if (fh == STDIN) {
/*
for ( ; len; len--) *buf++ = getkey ();
return (0);
*/
};
if (fh <= STDERR) return (-1);
//return (__read (fh, buf, len));
return -1;
}
int _isatty (FILEHANDLE fh)
{
if (fh <= STDERR)
return (1); // Terminal I/O
return (0); // Storage I/O
}
register char *stack_ptr asm ("sp");
caddr_t _sbrk(int incr) {
extern char _end; /* Defined by the linker */
static char *heap_end;
char *prev_heap_end;
if (heap_end == 0) {
heap_end = &_end;
}
prev_heap_end = heap_end;
if (heap_end + incr > stack_ptr) {
USART1_write ("Heap and stack collision\n", 25);
abort ();
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
int _kill(int pid, int sig) {
if (pid == 1) {
switch (sig) {
case SIGABRT:
USART1_directprint("Aborted\n");
_exit(128+sig);
return 0;
}
}
errno = EINVAL;
return -1;
}
int _getpid() {
return 1;
}
int _fstat(int file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
int _lseek(int file, int ptr, int dir) {
return 0;
}