stdio.c 926 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017/10/15 bernard implement stdio for IAR dlib.
  9. */
  10. #include <rtthread.h>
  11. #ifdef RT_USING_POSIX
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include "libc.h"
  18. #define STDIO_DEVICE_NAME_MAX 32
  19. static int std_fd = -1;
  20. int libc_stdio_set_console(const char* device_name, int mode)
  21. {
  22. int fd;
  23. char name[STDIO_DEVICE_NAME_MAX];
  24. snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
  25. name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
  26. fd = open(name, mode, 0);
  27. if (fd >= 0)
  28. {
  29. if (std_fd >= 0)
  30. {
  31. close(std_fd);
  32. }
  33. std_fd = fd;
  34. }
  35. return std_fd;
  36. }
  37. int libc_stdio_get_console(void) {
  38. return std_fd;
  39. }
  40. #endif /* RT_USING_POSIX */