stdio.c 923 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #endif