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

Side by Side Diff: fusl/src/stdio/vsnprintf.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/vscanf.c ('k') | fusl/src/stdio/vsprintf.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 <limits.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdint.h>
6
7 static size_t sn_write(FILE *f, const unsigned char *s, size_t l)
8 {
9 size_t k = f->wend - f->wpos;
10 if (k > l) k = l;
11 memcpy(f->wpos, s, k);
12 f->wpos += k;
13 /* pretend to succeed, but discard extra data */
14 return l;
15 }
16
17 int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list ap)
18 {
19 int r;
20 char b;
21 FILE f = { .lbf = EOF, .write = sn_write, .lock = -1 };
22
23 if (n-1 > INT_MAX-1) {
24 if (n) {
25 errno = EOVERFLOW;
26 return -1;
27 }
28 s = &b;
29 n = 1;
30 }
31
32 /* Ensure pointers don't wrap if "infinite" n is passed in */
33 if (n > (char *)0+SIZE_MAX-s-1) n = (char *)0+SIZE_MAX-s-1;
34 f.buf_size = n;
35 f.buf = f.wpos = (void *)s;
36 f.wbase = f.wend = (void *)(s+n);
37 r = vfprintf(&f, fmt, ap);
38
39 /* Null-terminate, overwriting last char if dest buffer is full */
40 if (n) f.wpos[-(f.wpos == f.wend)] = 0;
41 return r;
42 }
OLDNEW
« no previous file with comments | « fusl/src/stdio/vscanf.c ('k') | fusl/src/stdio/vsprintf.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698