1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4
5 #include <reent.h>
6 #include <sys/errno.h>
7 #include <sys/unistd.h>
8 #include <sys/time.h>
9 #include <stdarg.h>
10 #include <csi_config.h>
11 #ifndef CONFIG_KERNEL_NONE
12 #include <csi_kernel.h>
13 #endif
14
15 #include <drv_usart.h>
16 #include <k_api.h>
17
18 extern usart_handle_t console_handle;
19
_execve_r(struct _reent * ptr,const char * name,char * const * argv,char * const * env)20 int _execve_r(struct _reent *ptr, const char *name, char *const *argv,
21 char *const *env)
22 {
23 ptr->_errno = ENOTSUP;
24 return -1;
25 }
26
_fcntl_r(struct _reent * ptr,int fd,int cmd,int arg)27 int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
28 {
29 ptr->_errno = ENOTSUP;
30 return -1;
31 }
32
_fork_r(struct _reent * ptr)33 int _fork_r(struct _reent *ptr)
34 {
35 ptr->_errno = ENOTSUP;
36 return -1;
37 }
38
_getpid_r(struct _reent * ptr)39 int _getpid_r(struct _reent *ptr)
40 {
41 ptr->_errno = ENOTSUP;
42 return 0;
43 }
44
_isatty_r(struct _reent * ptr,int fd)45 int _isatty_r(struct _reent *ptr, int fd)
46 {
47 if (fd >= 0 && fd < 3) {
48 return 1;
49 }
50
51 ptr->_errno = ENOTSUP;
52 return -1;
53 }
54
_kill_r(struct _reent * ptr,int pid,int sig)55 int _kill_r(struct _reent *ptr, int pid, int sig)
56 {
57 ptr->_errno = ENOTSUP;
58 return -1;
59 }
60
_link_r(struct _reent * ptr,const char * old,const char * new)61 int _link_r(struct _reent *ptr, const char *old, const char *new)
62 {
63 ptr->_errno = ENOTSUP;
64 return -1;
65 }
66
_lseek_r(struct _reent * ptr,int fd,_off_t pos,int whence)67 _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
68 {
69 ptr->_errno = ENOTSUP;
70 return -1;
71 }
72
_mkdir_r(struct _reent * ptr,const char * name,int mode)73 int _mkdir_r(struct _reent *ptr, const char *name, int mode)
74 {
75 ptr->_errno = ENOTSUP;
76 return -1;
77 }
78
_open_r(struct _reent * ptr,const char * file,int flags,int mode)79 int _open_r(struct _reent *ptr, const char *file, int flags, int mode)
80 {
81 ptr->_errno = ENOTSUP;
82 return -1;
83 }
84
_close_r(struct _reent * ptr,int fd)85 int _close_r(struct _reent *ptr, int fd)
86 {
87 ptr->_errno = ENOTSUP;
88 return -1;
89 }
90
_read_r(struct _reent * ptr,int fd,void * buf,size_t nbytes)91 _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
92 {
93 ptr->_errno = ENOTSUP;
94 return -1;
95 }
96
97 /*
98 * implement _write_r here
99 */
_write_r(struct _reent * ptr,int fd,const void * buf,size_t nbytes)100 _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
101 {
102 const char *tmp = buf;
103 int i = 0;
104
105 if (buf == NULL) {
106 return 0;
107 }
108 if ((fd == STDOUT_FILENO) || (fd == STDERR_FILENO)) {
109 for (i = 0; i < nbytes; i++) {
110 if (*tmp == '\n') {
111 csi_usart_putchar(console_handle, '\r');
112 }
113 csi_usart_putchar(console_handle, *tmp);
114 tmp++;
115 }
116
117 return nbytes;
118 } else {
119 return -1;
120 }
121 }
122
_fstat_r(struct _reent * ptr,int fd,struct stat * pstat)123 int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
124 {
125 ptr->_errno = ENOTSUP;
126 return -1;
127 }
128
_rename_r(struct _reent * ptr,const char * old,const char * new)129 int _rename_r(struct _reent *ptr, const char *old, const char *new)
130 {
131 ptr->_errno = ENOTSUP;
132 return 0;
133 }
134
_sbrk_r(struct _reent * ptr,ptrdiff_t incr)135 void *_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
136 {
137 ptr->_errno = ENOTSUP;
138 return NULL;
139 }
140
_stat_r(struct _reent * ptr,const char * file,struct stat * pstat)141 int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
142 {
143 ptr->_errno = ENOTSUP;
144 return 0;
145 }
146
_times_r(struct _reent * ptr,struct tms * ptms)147 _CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms)
148 {
149 ptr->_errno = ENOTSUP;
150 return -1;
151 }
152
_unlink_r(struct _reent * ptr,const char * file)153 int _unlink_r(struct _reent *ptr, const char *file)
154 {
155 ptr->_errno = ENOTSUP;
156 return 0;
157 }
158
_wait_r(struct _reent * ptr,int * status)159 int _wait_r(struct _reent *ptr, int *status)
160 {
161 ptr->_errno = ENOTSUP;
162 return -1;
163 }
164
_exit(int status)165 void _exit(int status)
166 {
167 while (1)
168 ;
169 }
170
_system(const char * s)171 void _system(const char *s)
172 {
173 return;
174 }
175
abort(void)176 void abort(void)
177 {
178 while (1)
179 ;
180 }
181
182