Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: fusl/src/stdio/fgets.c

Issue 1573973002: Add a "fork" of musl as //fusl. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fusl/src/stdio/fgetpos.c ('k') | fusl/src/stdio/fgetwc.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "stdio_impl.h"
2 #include <string.h>
3
4 #define MIN(a,b) ((a)<(b) ? (a) : (b))
5
6 char *fgets(char *restrict s, int n, FILE *restrict f)
7 {
8 char *p = s;
9 unsigned char *z;
10 size_t k;
11 int c;
12
13 FLOCK(f);
14
15 if (n--<=1) {
16 f->mode |= f->mode-1;
17 FUNLOCK(f);
18 if (n) return 0;
19 *s = 0;
20 return s;
21 }
22
23 while (n) {
24 z = memchr(f->rpos, '\n', f->rend - f->rpos);
25 k = z ? z - f->rpos + 1 : f->rend - f->rpos;
26 k = MIN(k, n);
27 memcpy(p, f->rpos, k);
28 f->rpos += k;
29 p += k;
30 n -= k;
31 if (z || !n) break;
32 if ((c = getc_unlocked(f)) < 0) {
33 if (p==s || !feof(f)) s = 0;
34 break;
35 }
36 n--;
37 if ((*p++ = c) == '\n') break;
38 }
39 if (s) *p = 0;
40
41 FUNLOCK(f);
42
43 return s;
44 }
45
46 weak_alias(fgets, fgets_unlocked);
OLDNEW
« no previous file with comments | « fusl/src/stdio/fgetpos.c ('k') | fusl/src/stdio/fgetwc.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698