remove or make static various unused __-prefixed symbols
[musl] / src / stdio / fgets.c
index 3135a69..d3f9819 100644 (file)
@@ -1,22 +1,25 @@
 #include "stdio_impl.h"
+#include <string.h>
 
 #define MIN(a,b) ((a)<(b) ? (a) : (b))
 
-char *fgets(char *s, int n, FILE *f)
+char *fgets(char *restrict s, int n, FILE *restrict f)
 {
        char *p = s;
        unsigned char *z;
        size_t k;
        int c;
 
+       FLOCK(f);
+
        if (n--<=1) {
+               f->mode |= f->mode-1;
+               FUNLOCK(f);
                if (n) return 0;
                *s = 0;
                return s;
        }
 
-       FLOCK(f);
-
        while (n) {
                z = memchr(f->rpos, '\n', f->rend - f->rpos);
                k = z ? z - f->rpos + 1 : f->rend - f->rpos;
@@ -33,7 +36,7 @@ char *fgets(char *s, int n, FILE *f)
                n--;
                if ((*p++ = c) == '\n') break;
        }
-       *p = 0;
+       if (s) *p = 0;
 
        FUNLOCK(f);