fgets.c 831 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "uart/uart.h"
  2. #include "rtdef.h"
  3. #include "rtthread.h"
  4. #include <stdio.h>
  5. __attribute__((used))
  6. int fgetc(FILE *stream)
  7. {
  8. int c;
  9. while (!drv_uart_is_kbd_hit()) {
  10. rt_thread_delay(1);
  11. }
  12. c = drv_uart_get_char();
  13. if (c == '\r')
  14. c = '\n';
  15. fputc(c, stream);
  16. return c;
  17. }
  18. __attribute__((used))
  19. char *fgets(char *s, int size, FILE *stream)
  20. {
  21. char *p = s;
  22. int i = 0;
  23. for (i = 0; i < size - 1; i++){
  24. int c = fgetc(stream);
  25. if(c == '\n'){
  26. *p++ = '\n';
  27. break;
  28. }
  29. else if(c == '\0'){
  30. break;
  31. }
  32. else if(c < 0){
  33. return (void*)0;
  34. }
  35. else{
  36. *p++ = c;
  37. }
  38. }
  39. *p = '\0';
  40. return s;
  41. }
  42. __attribute__((used))
  43. int getc(FILE *stream)
  44. {
  45. return fgetc(stream);
  46. }
  47. __attribute__((used))
  48. int getchar(void)
  49. {
  50. return fgetc((void*)0);
  51. }
  52. __attribute__((used))
  53. int ungetc(int c, FILE *stream);