fgets.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. #ifdef __DO_UNLOCKED
  9. char *fgets_unlocked(char *__restrict s, int n,
  10. register FILE * __restrict stream)
  11. {
  12. register char *p;
  13. int c;
  14. __STDIO_STREAM_VALIDATE(stream);
  15. /* Should we assert here? Or set errno? Or just fail... */
  16. if (n <= 0) {
  17. /* __set_errno(EINVAL); */
  18. goto ERROR;
  19. }
  20. p = s;
  21. while (--n) {
  22. if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)) {
  23. if ((*p++ = __STDIO_STREAM_BUFFER_GET(stream)) == '\n') {
  24. break;
  25. }
  26. } else {
  27. if ((c = __fgetc_unlocked(stream)) == EOF) {
  28. if (__FERROR_UNLOCKED(stream)) {
  29. goto ERROR;
  30. }
  31. break;
  32. }
  33. if ((*p++ = c) == '\n') {
  34. break;
  35. }
  36. }
  37. }
  38. if (p > s) {
  39. *p = 0;
  40. return s;
  41. }
  42. ERROR:
  43. return NULL;
  44. }
  45. libc_hidden_def(fgets_unlocked)
  46. #ifndef __UCLIBC_HAS_THREADS__
  47. strong_alias(fgets_unlocked,fgets)
  48. libc_hidden_def(fgets)
  49. #endif
  50. #elif defined __UCLIBC_HAS_THREADS__
  51. char *fgets(char *__restrict s, int n,
  52. register FILE * __restrict stream)
  53. {
  54. char *retval;
  55. __STDIO_AUTO_THREADLOCK_VAR;
  56. __STDIO_AUTO_THREADLOCK(stream);
  57. retval = fgets_unlocked(s, n, stream);
  58. __STDIO_AUTO_THREADUNLOCK(stream);
  59. return retval;
  60. }
  61. libc_hidden_def(fgets)
  62. #endif