gets.c 897 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. /* disable macro, force actual function call */
  11. #undef getchar_unlocked
  12. libc_hidden_proto(getchar_unlocked)
  13. char *gets(char *s)
  14. {
  15. register char *p = s;
  16. int c;
  17. __STDIO_AUTO_THREADLOCK_VAR;
  18. __STDIO_AUTO_THREADLOCK(stdin);
  19. /* Note: don't worry about performance here... this shouldn't be used!
  20. * Therefore, force actual function call. */
  21. while (((c = getchar_unlocked()) != EOF) && ((*p = c) != '\n')) {
  22. ++p;
  23. }
  24. if ((c == EOF) || (s == p)) {
  25. s = NULL;
  26. } else {
  27. *p = 0;
  28. }
  29. __STDIO_AUTO_THREADUNLOCK(stdin);
  30. return s;
  31. }