2 * Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
30 #include <sys/queue.h>
37 #include <ucg_gloss_chardev.h>
39 /* provide a dummy environment */
40 char *__env[1] = { 0 };
41 char **environ = __env;
43 /* provide a definition of errno if not already provided */
46 /* defined by the linker, used by _sbrk */
49 /* open a device, return its file descriptor */
51 _open_r(struct _reent *reent, const char *file, int flags, int mode)
53 const struct ucg_chardev *dev;
55 dev = ucg_chardev_lookup_from_name(file);
57 reent->_errno = ENODEV;
62 dev->open_r(reent, file, flags, mode);
69 _close_r(struct _reent *reent, int fd)
71 const struct ucg_chardev *dev;
73 dev = ucg_chardev_lookup_from_fd(fd);
75 reent->_errno = EBADF;
79 /* call the close() handler if defined */
81 return dev->close_r(reent, fd);
88 _read_r(struct _reent *reent, int fd, void *ptr, size_t len)
90 const struct ucg_chardev *dev;
92 dev = ucg_chardev_lookup_from_fd(fd);
94 reent->_errno = EBADF;
99 return dev->read_r(reent, fd, ptr, len);
101 reent->_errno = ENODEV;
105 /* write to a device */
107 _write_r(struct _reent *reent, int fd, const void *ptr, size_t len)
109 const struct ucg_chardev *dev;
111 dev = ucg_chardev_lookup_from_fd(fd);
113 reent->_errno = EBADF;
118 return dev->write_r(reent, fd, ptr, len);
120 reent->_errno = ENODEV;
124 /* exit: print error code and loop forever */
128 printf("exit %d\n", rc);
132 /* set owner: not implemented */
134 _chown_r(struct _reent *reent,
135 __attribute__((unused)) const char *path,
136 __attribute__((unused)) uid_t owner,
137 __attribute__((unused)) gid_t group)
139 reent->_errno = ENOSYS;
143 /* execve: not implemented */
145 _execve_r(struct _reent *reent,
146 __attribute__((unused)) const char *name,
147 __attribute__((unused)) char *const *argv,
148 __attribute__((unused)) char *const *env)
150 reent->_errno = ENOSYS;
154 /* fork: not implemented */
156 _fork_r(struct _reent *reent)
158 reent->_errno = ENOSYS;
162 /* fstat: not implemented, all devices are character devices */
164 _fstat_r(__attribute__((unused)) struct _reent *reent,
165 __attribute__((unused)) int fildes,
168 st->st_mode = S_IFCHR;
172 /* getpid: not implemented */
174 _getpid_r(struct _reent *reent)
176 reent->_errno = ENOSYS;
182 /* gettimeofday: not implemented */
184 _gettimeofday_r(struct _reent *reent,
185 __attribute__((unused)) struct timeval *ptimeval,
186 __attribute__((unused)) void *ptimezone)
188 reent->_errno = ENOSYS;
192 /* isatty; not implemented */
194 _isatty_r(struct _reent *reent, __attribute__((unused)) int file)
196 reent->_errno = ENOTTY;
200 /* kill a process: not implemented */
202 _kill_r(struct _reent *reent,
203 __attribute__((unused)) int pid,
204 __attribute__((unused)) int sig)
206 reent->_errno = ENOSYS;
210 /* link: not implemented */
212 _link_r(struct _reent *reent,
213 __attribute__((unused)) const char *existing,
214 __attribute__((unused)) const char *new)
216 reent->_errno = EMLINK;
220 /* lseek: not implemented */
222 _lseek_r(__attribute__((unused)) struct _reent *reent,
223 __attribute__((unused)) int file,
224 __attribute__((unused)) _off_t ptr,
225 __attribute__((unused)) int dir)
230 /* readlink: not implemented */
232 _readlink_r(struct _reent *reent,
233 __attribute__((unused)) const char *path,
234 __attribute__((unused)) char *buf,
235 __attribute__((unused)) size_t bufsize)
237 reent->_errno = ENOSYS;
243 _sbrk_r(__attribute__((unused)) struct _reent *reent, ptrdiff_t incr)
245 static char *heap_end = NULL;
248 if (heap_end == NULL)
251 prev_heap_end = heap_end;
253 return (caddr_t)prev_heap_end;
256 /* stat: not implemented, all devices are character devices */
258 _stat_r(__attribute__((unused)) struct _reent *reent,
259 __attribute__((unused)) const char *file,
262 st->st_mode = S_IFCHR;
266 /* symlink: not implemented */
268 _symlink_r(struct _reent *reent,
269 __attribute__((unused)) const char *path1,
270 __attribute__((unused)) const char *path2)
272 reent->_errno = ENOSYS;
276 /* times: not timplemented */
278 _times_r(struct _reent *reent,
279 __attribute__((unused)) struct tms *buf)
281 reent->_errno = ENOSYS;
285 /* unlink: not implemented */
287 _unlink_r(struct _reent *reent,
288 __attribute__((unused)) const char *name)
290 reent->_errno = EMLINK;
294 /* wait: not implemented */
296 _wait_r(struct _reent *reent, __attribute__((unused)) int *status)
298 reent->_errno = ENOSYS;