1
0

stdio.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * File : stdio.c
  3. * Brief : stdio/console
  4. *
  5. * This file is part of RT-Thread RTOS
  6. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * Change Logs:
  23. * Date Author Notes
  24. * 2017/10/15 bernard implement stdio for armcc.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <rtthread.h>
  30. #include "libc.h"
  31. #if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS)
  32. #include <dfs_posix.h>
  33. #define STDIO_DEVICE_NAME_MAX 32
  34. static int std_fd = -1;
  35. int libc_stdio_set_console(const char* device_name, int mode)
  36. {
  37. int fd;
  38. char name[STDIO_DEVICE_NAME_MAX];
  39. snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
  40. name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
  41. fd = open(name, mode, 0);
  42. if (fd >= 0)
  43. {
  44. if (std_fd >= 0)
  45. {
  46. close(std_fd);
  47. }
  48. std_fd = fd;
  49. }
  50. return std_fd;
  51. }
  52. int libc_stdio_get_console(void)
  53. {
  54. return std_fd;
  55. }
  56. int libc_stdio_read(void *buffer, size_t size)
  57. {
  58. return read(std_fd, buffer, size);
  59. }
  60. int libc_stdio_write(const void *buffer, size_t size)
  61. {
  62. return write(std_fd, buffer, size);
  63. }
  64. #endif