stdio.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 armcc.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <rtthread.h>
  14. #include "libc.h"
  15. #if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS)
  16. #include <dfs_posix.h>
  17. #define STDIO_DEVICE_NAME_MAX 32
  18. static int std_fd = -1;
  19. int libc_stdio_set_console(const char* device_name, int mode)
  20. {
  21. int fd;
  22. char name[STDIO_DEVICE_NAME_MAX];
  23. snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
  24. name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
  25. fd = open(name, mode, 0);
  26. if (fd >= 0)
  27. {
  28. if (std_fd >= 0)
  29. {
  30. close(std_fd);
  31. }
  32. std_fd = fd;
  33. }
  34. return std_fd;
  35. }
  36. int libc_stdio_get_console(void)
  37. {
  38. return std_fd;
  39. }
  40. int libc_stdio_read(void *buffer, size_t size)
  41. {
  42. if (std_fd >= 0)
  43. {
  44. return read(std_fd, buffer, size);
  45. }
  46. else
  47. {
  48. rt_kprintf("Illegal stdio input!\n");
  49. return 0;
  50. }
  51. }
  52. int libc_stdio_write(const void *buffer, size_t size)
  53. {
  54. if (std_fd >= 0)
  55. {
  56. return write(std_fd, buffer, size);
  57. }
  58. else
  59. {
  60. rt_kprintf("Illegal stdio output!\n");
  61. return size;
  62. }
  63. }
  64. #endif