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

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

Issue 1714623002: [fusl] clang-format fusl (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: headers too Created 4 years, 10 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
OLDNEW
1 #include "stdio_impl.h" 1 #include "stdio_impl.h"
2 #include <string.h> 2 #include <string.h>
3 #include <inttypes.h> 3 #include <inttypes.h>
4 #include <errno.h> 4 #include <errno.h>
5 5
6 #define MIN(a,b) ((a)<(b) ? (a) : (b)) 6 #define MIN(a, b) ((a) < (b) ? (a) : (b))
7 7
8 ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restric t f) 8 ssize_t getdelim(char** restrict s,
9 { 9 size_t* restrict n,
10 » char *tmp; 10 int delim,
11 » unsigned char *z; 11 FILE* restrict f) {
12 » size_t k; 12 char* tmp;
13 » size_t i=0; 13 unsigned char* z;
14 » int c; 14 size_t k;
15 size_t i = 0;
16 int c;
15 17
16 » FLOCK(f); 18 FLOCK(f);
17 19
18 » if (!n || !s) { 20 if (!n || !s) {
19 » » f->flags |= F_ERR; 21 f->flags |= F_ERR;
20 » » FUNLOCK(f); 22 FUNLOCK(f);
21 » » errno = EINVAL; 23 errno = EINVAL;
22 » » return -1; 24 return -1;
23 » } 25 }
24 26
25 » if (!*s) *n=0; 27 if (!*s)
28 *n = 0;
26 29
27 » for (;;) { 30 for (;;) {
28 » » z = memchr(f->rpos, delim, f->rend - f->rpos); 31 z = memchr(f->rpos, delim, f->rend - f->rpos);
29 » » k = z ? z - f->rpos + 1 : f->rend - f->rpos; 32 k = z ? z - f->rpos + 1 : f->rend - f->rpos;
30 » » if (i+k+1 >= *n) { 33 if (i + k + 1 >= *n) {
31 » » » if (k >= SIZE_MAX/2-i) goto oom; 34 if (k >= SIZE_MAX / 2 - i)
32 » » » size_t m = i+k+2; 35 goto oom;
33 » » » if (!z && m < SIZE_MAX/4) m += m/2; 36 size_t m = i + k + 2;
34 » » » tmp = realloc(*s, m); 37 if (!z && m < SIZE_MAX / 4)
35 » » » if (!tmp) { 38 m += m / 2;
36 » » » » m = i+k+2; 39 tmp = realloc(*s, m);
37 » » » » tmp = realloc(*s, m); 40 if (!tmp) {
38 » » » » if (!tmp) goto oom; 41 m = i + k + 2;
39 » » » } 42 tmp = realloc(*s, m);
40 » » » *s = tmp; 43 if (!tmp)
41 » » » *n = m; 44 goto oom;
42 » » } 45 }
43 » » memcpy(*s+i, f->rpos, k); 46 *s = tmp;
44 » » f->rpos += k; 47 *n = m;
45 » » i += k; 48 }
46 » » if (z) break; 49 memcpy(*s + i, f->rpos, k);
47 » » if ((c = getc_unlocked(f)) == EOF) { 50 f->rpos += k;
48 » » » if (!i || !feof(f)) { 51 i += k;
49 » » » » FUNLOCK(f); 52 if (z)
50 » » » » return -1; 53 break;
51 » » » } 54 if ((c = getc_unlocked(f)) == EOF) {
52 » » » break; 55 if (!i || !feof(f)) {
53 » » } 56 FUNLOCK(f);
54 » » if (((*s)[i++] = c) == delim) break; 57 return -1;
55 » } 58 }
56 » (*s)[i] = 0; 59 break;
60 }
61 if (((*s)[i++] = c) == delim)
62 break;
63 }
64 (*s)[i] = 0;
57 65
58 » FUNLOCK(f); 66 FUNLOCK(f);
59 67
60 » return i; 68 return i;
61 oom: 69 oom:
62 » f->flags |= F_ERR; 70 f->flags |= F_ERR;
63 » FUNLOCK(f); 71 FUNLOCK(f);
64 » errno = ENOMEM; 72 errno = ENOMEM;
65 » return -1; 73 return -1;
66 } 74 }
67 75
68 weak_alias(getdelim, __getdelim); 76 weak_alias(getdelim, __getdelim);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698