Skip to content

Commit c81baaf

Browse files
committed
Migrate snprintf to musl. Move the most common sprintf-related code to libc instead of libcextra to avoid pulling libcextra in on common runs.
1 parent 955e744 commit c81baaf

File tree

5 files changed

+38
-40
lines changed

5 files changed

+38
-40
lines changed

src/library.js

-19
Original file line numberDiff line numberDiff line change
@@ -2802,25 +2802,6 @@ LibraryManager.library = {
28022802
var stdin = {{{ makeGetValue(makeGlobalUse('_stdin'), '0', 'void*') }}};
28032803
return _fscanf(stdin, format, varargs);
28042804
},
2805-
snprintf__deps: ['_formatString', 'malloc'],
2806-
snprintf: function(s, n, format, varargs) {
2807-
// int snprintf(char *restrict s, size_t n, const char *restrict format, ...);
2808-
// http://pubs.opengroup.org/onlinepubs/000095399/functions/printf.html
2809-
var result = __formatString(format, varargs);
2810-
var limit = (n === undefined) ? result.length
2811-
: Math.min(result.length, Math.max(n - 1, 0));
2812-
if (s < 0) {
2813-
s = -s;
2814-
var buf = _malloc(limit+1);
2815-
{{{ makeSetValue('s', '0', 'buf', 'i8*') }}};
2816-
s = buf;
2817-
}
2818-
for (var i = 0; i < limit; i++) {
2819-
{{{ makeSetValue('s', 'i', 'result[i]', 'i8') }}};
2820-
}
2821-
if (limit < n || (n === undefined)) {{{ makeSetValue('s', 'i', '0', 'i8') }}};
2822-
return result.length;
2823-
},
28242805
fprintf__deps: ['fwrite', '_formatString'],
28252806
fprintf: function(stream, format, varargs) {
28262807
// int fprintf(FILE *restrict stream, const char *restrict format, ...);

system/lib/libc.symbols

+11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
W bulk_free
5757
W calloc
5858
W free
59+
T frexp
60+
T frexpf
61+
T frexpl
5962
W independent_calloc
6063
W independent_comalloc
6164
T isdigit
@@ -72,14 +75,18 @@
7275
W malloc_usable_size
7376
W mallopt
7477
W memalign
78+
T MUSL_vfprintf
7579
W posix_memalign
7680
W pvalloc
7781
W realloc
7882
W realloc_in_place
7983
T scalbn
8084
T scalbnl
85+
T memchr
8186
T memcmp
8287
T memcpy
88+
T snprintf
89+
T sprintf
8390
T strtod
8491
T strtoull
8592
T strtoll
@@ -101,3 +108,7 @@
101108
T strtold_l
102109
T tolower
103110
W valloc
111+
T vsnprintf
112+
T vsprintf
113+
T wcrtomb
114+
T wctomb
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
#include <stdarg.h>
3+
4+
int snprintf(char *restrict s, size_t n, const char *restrict fmt, ...)
5+
{
6+
int ret;
7+
va_list ap;
8+
va_start(ap, fmt);
9+
ret = vsnprintf(s, n, fmt, ap);
10+
va_end(ap);
11+
return ret;
12+
}
13+

system/lib/libcextra.symbols

-10
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
T fcvt
3535
T fputwc
3636
W fputwc_unlocked
37-
T frexp
38-
T frexpf
39-
T frexpl
4037
T fwprintf
4138
T gcvt
4239
T getopt
@@ -117,7 +114,6 @@
117114
T mbstowcs
118115
T mbtowc
119116
T memccpy
120-
T memchr
121117
T memmem
122118
T mempcpy
123119
W memrchr
@@ -167,7 +163,6 @@
167163
T strverscmp
168164
T strxfrm
169165
W strxfrm_l
170-
T sprintf
171166
T swprintf
172167
T tgamma
173168
T tgammaf
@@ -187,10 +182,7 @@
187182
T verrx
188183
T MUSL_vfscanf
189184
T vsscanf
190-
T MUSL_vfprintf
191185
T vfwprintf
192-
T vsnprintf
193-
T vsprintf
194186
T vswprintf
195187
T vwarn
196188
T vwarnx
@@ -199,7 +191,6 @@
199191
T warnx
200192
T wcpcpy
201193
T wcpncpy
202-
T wcrtomb
203194
T wcscasecmp
204195
T wcscasecmp_l
205196
T wcscat
@@ -239,7 +230,6 @@
239230
T wcsxfrm
240231
T wcsxfrm_l
241232
T wctob
242-
T wctomb
243233
T wctrans
244234
T wctrans_l
245235
T wctype

tools/system_libs.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,27 @@ def create_libc():
6767
'shgetc.c',
6868
]],
6969
['math', [
70+
'frexp.c',
71+
'frexpf.c',
72+
'frexpl.c',
7073
'scalbn.c',
7174
'scalbnl.c',
7275
]],
76+
['multibyte', [
77+
'wctomb.c',
78+
'wcrtomb.c',
79+
]],
7380
['stdio', [
7481
'__overflow.c',
7582
'__toread.c',
7683
'__towrite.c',
7784
'__uflow.c',
85+
'fwrite.c',
86+
'snprintf.c',
87+
'sprintf.c',
88+
'vfprintf.c',
89+
'vsnprintf.c',
90+
'vsprintf.c',
7891
]],
7992
['stdlib', [
8093
'atof.c',
@@ -84,6 +97,7 @@ def create_libc():
8497
'strtol.c',
8598
]],
8699
['string', [
100+
'memchr.c',
87101
'memcmp.c',
88102
'strcasecmp.c',
89103
'strcmp.c',
@@ -199,9 +213,6 @@ def create_libcextra():
199213
'__cosdf.c',
200214
'__sin.c',
201215
'__sindf.c',
202-
'frexp.c',
203-
'frexpf.c',
204-
'frexpl.c',
205216
'ilogb.c',
206217
'ilogbf.c',
207218
'ilogbl.c',
@@ -237,12 +248,10 @@ def create_libcextra():
237248
'mbsrtowcs.c',
238249
'mbstowcs.c',
239250
'mbtowc.c',
240-
'wcrtomb.c',
241251
'wcsnrtombs.c',
242252
'wcsrtombs.c',
243253
'wcstombs.c',
244254
'wctob.c',
245-
'wctomb.c',
246255
]],
247256
['regex', [
248257
'fnmatch.c',
@@ -262,14 +271,9 @@ def create_libcextra():
262271
'wprintf.c',
263272
'fputwc.c',
264273
'fputws.c',
265-
'fwrite.c',
266-
'sprintf.c',
267274
'sscanf.c',
268275
'vasprintf.c',
269-
'vfprintf.c',
270276
'vfscanf.c',
271-
'vsnprintf.c',
272-
'vsprintf.c',
273277
'vsscanf.c',
274278
]],
275279
['stdlib', [
@@ -290,7 +294,6 @@ def create_libcextra():
290294
'memccpy.c',
291295
'memmem.c',
292296
'mempcpy.c',
293-
'memchr.c',
294297
'memrchr.c',
295298
'rindex.c',
296299
'stpcpy.c',

0 commit comments

Comments
 (0)