gets.c 949 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. link_warning(gets, "the 'gets' function is dangerous and should not be used.")
  9. /* UNSAFE FUNCTION -- do not bother optimizing */
  10. libc_hidden_proto(getchar_unlocked)
  11. libc_hidden_proto(__fgetc_unlocked)
  12. #ifdef __STDIO_GETC_MACRO
  13. libc_hidden_proto(__stdin)
  14. #else
  15. #define __stdin stdin
  16. #endif
  17. char *gets(char *s)
  18. {
  19. register char *p = s;
  20. int c;
  21. __STDIO_AUTO_THREADLOCK_VAR;
  22. __STDIO_AUTO_THREADLOCK(stdin);
  23. /* Note: don't worry about performance here... this shouldn't be used!
  24. * Therefore, force actual function call. */
  25. while (((c = getchar_unlocked()) != EOF) && ((*p = c) != '\n')) {
  26. ++p;
  27. }
  28. if ((c == EOF) || (s == p)) {
  29. s = NULL;
  30. } else {
  31. *p = 0;
  32. }
  33. __STDIO_AUTO_THREADUNLOCK(stdin);
  34. return s;
  35. }