gets.c 917 B

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