author | Rich Felker <dalias@aerifal.cx> | 2019年02月13日 18:48:04 -0500 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2019年02月13日 18:48:04 -0500 |
commit | b2020571f07beaa9873ef0e5ade456b57b589042 (patch) | |
tree | c490136176fecac7cc65c7bde05d02d77b7cb060 /src/stdio/gets.c | |
parent | 099b89d3840c30d7dd962e18668c2e6d39f0c626 (diff) | |
download | musl-b2020571f07beaa9873ef0e5ade456b57b589042.tar.gz |
-rw-r--r-- | src/stdio/gets.c | 11 |
diff --git a/src/stdio/gets.c b/src/stdio/gets.c index 6c4645e5..17963b93 100644 --- a/src/stdio/gets.c +++ b/src/stdio/gets.c @@ -4,7 +4,12 @@ char *gets(char *s) { - char *ret = fgets(s, INT_MAX, stdin); - if (ret && s[strlen(s)-1] == '\n') s[strlen(s)-1] = 0; - return ret; + size_t i=0; + int c; + FLOCK(stdin); + while ((c=getc_unlocked(stdin)) != EOF && c != '\n') s[i++] = c; + s[i] = 0; + if (c != '\n' && (!feof(stdin) || !i)) s = 0; + FUNLOCK(stdin); + return s; } |