ucrtbase: Implement _register_onexit_function().
[wine.git] / dlls / msvcrt / printf.h
blobce7a70e7571f8f7132e1500929b32b6bdf8245dc
1 /*
2 * Copyright 2011 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifdef PRINTF_WIDE
20 #define APICHAR MSVCRT_wchar_t
21 #define CONVCHAR char
22 #define FUNC_NAME(func) func ## _w
23 #else
24 #define APICHAR char
25 #define CONVCHAR MSVCRT_wchar_t
26 #define FUNC_NAME(func) func ## _a
27 #endif
29 #ifndef signbit
30 #define signbit(x) ((x) < 0)
31 #endif
33 typedef struct FUNC_NAME(pf_flags_t)
35 APICHAR Sign, LeftAlign, Alternate, PadZero;
36 int FieldLength, Precision;
37 APICHAR IntegerLength, IntegerDouble, IntegerNative;
38 APICHAR WideString;
39 APICHAR Format;
40 } FUNC_NAME(pf_flags);
42 struct FUNC_NAME(_str_ctx) {
43 MSVCRT_size_t len;
44 APICHAR *buf;
47 static int FUNC_NAME(puts_clbk_str)(void *ctx, int len, const APICHAR *str)
49 struct FUNC_NAME(_str_ctx) *out = ctx;
51 if(!out->buf)
52 return len;
54 if(out->len < len) {
55 memcpy(out->buf, str, out->len*sizeof(APICHAR));
56 out->buf += out->len;
57 out->len = 0;
58 return -1;
61 memcpy(out->buf, str, len*sizeof(APICHAR));
62 out->buf += len;
63 out->len -= len;
64 return len;
67 static inline const APICHAR* FUNC_NAME(pf_parse_int)(const APICHAR *fmt, int *val)
69 *val = 0;
71 while(isdigit(*fmt)) {
72 *val *= 10;
73 *val += *fmt++ - '0';
76 return fmt;
79 /* pf_fill: takes care of signs, alignment, zero and field padding */
80 static inline int FUNC_NAME(pf_fill)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
81 int len, FUNC_NAME(pf_flags) *flags, BOOL left)
83 int i, r = 0, written;
85 if(flags->Sign && !strchr("diaeEfFgG", flags->Format))
86 flags->Sign = 0;
88 if(left && flags->Sign) {
89 flags->FieldLength--;
90 if(flags->PadZero)
91 r = pf_puts(puts_ctx, 1, &flags->Sign);
93 written = r;
95 if((!left && flags->LeftAlign) || (left && !flags->LeftAlign)) {
96 APICHAR ch;
98 if(left && flags->PadZero)
99 ch = '0';
100 else
101 ch = ' ';
103 for(i=0; i<flags->FieldLength-len && r>=0; i++) {
104 r = pf_puts(puts_ctx, 1, &ch);
105 written += r;
110 if(r>=0 && left && flags->Sign && !flags->PadZero) {
111 r = pf_puts(puts_ctx, 1, &flags->Sign);
112 written += r;
115 return r>=0 ? written : r;
118 static inline int FUNC_NAME(pf_output_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
119 const MSVCRT_wchar_t *str, int len, MSVCRT_pthreadlocinfo locinfo)
121 #ifdef PRINTF_WIDE
122 return pf_puts(puts_ctx, len, str);
123 #else
124 LPSTR out;
125 int len_a = WideCharToMultiByte(locinfo->lc_codepage, 0, str, len, NULL, 0, NULL, NULL);
127 out = HeapAlloc(GetProcessHeap(), 0, len_a);
128 if(!out)
129 return -1;
131 WideCharToMultiByte(locinfo->lc_codepage, 0, str, len, out, len_a, NULL, NULL);
132 len = pf_puts(puts_ctx, len_a, out);
133 HeapFree(GetProcessHeap(), 0, out);
134 return len;
135 #endif
138 static inline int FUNC_NAME(pf_output_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
139 const char *str, int len, MSVCRT_pthreadlocinfo locinfo)
141 #ifdef PRINTF_WIDE
142 LPWSTR out;
143 int len_w = MultiByteToWideChar(locinfo->lc_codepage, 0, str, len, NULL, 0);
145 out = HeapAlloc(GetProcessHeap(), 0, len_w*sizeof(WCHAR));
146 if(!out)
147 return -1;
149 MultiByteToWideChar(locinfo->lc_codepage, 0, str, len, out, len_w);
150 len = pf_puts(puts_ctx, len_w, out);
151 HeapFree(GetProcessHeap(), 0, out);
152 return len;
153 #else
154 return pf_puts(puts_ctx, len, str);
155 #endif
158 static inline int FUNC_NAME(pf_output_format_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
159 const MSVCRT_wchar_t *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo)
161 int r, ret;
163 if(len < 0)
164 len = strlenW(str);
166 if(flags->Precision>=0 && flags->Precision<len)
167 len = flags->Precision;
169 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
170 ret = r;
171 if(r >= 0) {
172 r = FUNC_NAME(pf_output_wstr)(pf_puts, puts_ctx, str, len, locinfo);
173 ret += r;
175 if(r >= 0) {
176 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
177 ret += r;
180 return r>=0 ? ret : r;
183 static inline int FUNC_NAME(pf_output_format_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
184 const char *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo)
186 int r, ret;
188 if(len < 0)
189 len = strlen(str);
191 if(flags->Precision>=0 && flags->Precision<len)
192 len = flags->Precision;
194 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
195 ret = r;
196 if(r >= 0) {
197 r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, str, len, locinfo);
198 ret += r;
200 if(r >= 0) {
201 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
202 ret += r;
205 return r>=0 ? ret : r;
208 static inline int FUNC_NAME(pf_handle_string)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
209 const void *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo, BOOL legacy_wide)
211 BOOL complement_is_narrow = legacy_wide ? (sizeof(APICHAR)==sizeof(MSVCRT_wchar_t)) : FALSE;
212 #ifdef PRINTF_WIDE
213 static const MSVCRT_wchar_t nullW[] = {'(','n','u','l','l',')',0};
215 if(!str)
216 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, nullW, 6, flags, locinfo);
217 #else
218 if(!str)
219 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, "(null)", 6, flags, locinfo);
220 #endif
222 if(flags->WideString || flags->IntegerLength=='l')
223 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locinfo);
224 if(flags->IntegerLength == 'h')
225 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locinfo);
227 if((flags->Format=='S' || flags->Format=='C') == complement_is_narrow)
228 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locinfo);
229 else
230 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locinfo);
233 static inline void FUNC_NAME(pf_rebuild_format_string)(char *p, FUNC_NAME(pf_flags) *flags)
235 *p++ = '%';
236 if(flags->Alternate)
237 *p++ = flags->Alternate;
238 if(flags->Precision >= 0) {
239 sprintf(p, ".%d", flags->Precision);
240 p += strlen(p);
242 *p++ = flags->Format;
243 *p++ = 0;
246 /* pf_integer_conv: prints x to buf, including alternate formats and
247 additional precision digits, but not field characters or the sign */
248 static inline void FUNC_NAME(pf_integer_conv)(APICHAR *buf, int buf_len,
249 FUNC_NAME(pf_flags) *flags, LONGLONG x)
251 unsigned int base;
252 const char *digits;
253 int i, j, k;
255 if(flags->Format == 'o')
256 base = 8;
257 else if(flags->Format=='x' || flags->Format=='X')
258 base = 16;
259 else
260 base = 10;
262 if(flags->Format == 'X')
263 digits = "0123456789ABCDEFX";
264 else
265 digits = "0123456789abcdefx";
267 if(x<0 && (flags->Format=='d' || flags->Format=='i')) {
268 x = -x;
269 flags->Sign = '-';
272 i = 0;
273 if(x == 0) {
274 flags->Alternate = 0;
275 if(flags->Precision)
276 buf[i++] = '0';
277 } else {
278 while(x != 0) {
279 j = (ULONGLONG)x%base;
280 x = (ULONGLONG)x/base;
281 buf[i++] = digits[j];
284 k = flags->Precision-i;
285 while(k-- > 0)
286 buf[i++] = '0';
287 if(flags->Alternate) {
288 if(base == 16) {
289 buf[i++] = digits[16];
290 buf[i++] = '0';
291 } else if(base==8 && buf[i-1]!='0')
292 buf[i++] = '0';
295 /* Adjust precision so pf_fill won't truncate the number later */
296 flags->Precision = i;
298 buf[i] = '\0';
299 j = 0;
300 while(--i > j) {
301 APICHAR tmp = buf[j];
302 buf[j] = buf[i];
303 buf[i] = tmp;
304 j++;
308 static inline void FUNC_NAME(pf_fixup_exponent)(char *buf, BOOL three_digit_exp)
310 char* tmp = buf;
312 while(tmp[0] && toupper(tmp[0])!='E')
313 tmp++;
315 if(tmp[0] && (tmp[1]=='+' || tmp[1]=='-') &&
316 isdigit(tmp[2]) && isdigit(tmp[3])) {
317 #if _MSVCR_VER >= 140
318 BOOL two_digit_exp = !three_digit_exp;
319 #else
320 BOOL two_digit_exp = (MSVCRT__get_output_format() == MSVCRT__TWO_DIGIT_EXPONENT);
321 #endif
323 tmp += 2;
324 if(isdigit(tmp[2])) {
325 if(two_digit_exp && tmp[0]=='0') {
326 tmp[0] = tmp[1];
327 tmp[1] = tmp[2];
328 tmp[2] = tmp[3];
331 return; /* Exponent already 3 digits */
332 }else if(two_digit_exp) {
333 return;
336 tmp[3] = tmp[2];
337 tmp[2] = tmp[1];
338 tmp[1] = tmp[0];
339 tmp[0] = '0';
343 int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const APICHAR *fmt,
344 MSVCRT__locale_t locale, DWORD options,
345 args_clbk pf_args, void *args_ctx, __ms_va_list *valist)
347 MSVCRT_pthreadlocinfo locinfo;
348 const APICHAR *q, *p = fmt;
349 APICHAR buf[32];
350 int written = 0, pos, i;
351 FUNC_NAME(pf_flags) flags;
352 BOOL positional_params = options & MSVCRT_PRINTF_POSITIONAL_PARAMS;
353 BOOL invoke_invalid_param_handler = options & MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER;
354 #if _MSVCR_VER >= 140
355 BOOL legacy_wide = options & UCRTBASE_PRINTF_LEGACY_WIDE_SPECIFIERS;
356 BOOL legacy_msvcrt_compat = options & UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY;
357 BOOL three_digit_exp = options & UCRTBASE_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS;
358 #else
359 BOOL legacy_wide = TRUE, legacy_msvcrt_compat = TRUE, three_digit_exp = TRUE;
360 #endif
362 TRACE("Format is: %s\n", FUNC_NAME(debugstr)(fmt));
364 if(!locale)
365 locinfo = get_locinfo();
366 else
367 locinfo = locale->locinfo;
369 while(*p) {
370 /* output characters before '%' */
371 for(q=p; *q && *q!='%'; q++);
372 if(p != q) {
373 i = pf_puts(puts_ctx, q-p, p);
374 if(i < 0)
375 return i;
377 written += i;
378 p = q;
379 continue;
382 /* *p == '%' here */
383 p++;
385 /* output a single '%' character */
386 if(*p == '%') {
387 i = pf_puts(puts_ctx, 1, p++);
388 if(i < 0)
389 return i;
391 written += i;
392 continue;
395 /* check parameter position */
396 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &pos)) && *q=='$')
397 p = q+1;
398 else
399 pos = -1;
401 /* parse the flags */
402 memset(&flags, 0, sizeof(flags));
403 while(*p) {
404 if(*p=='+' || *p==' ') {
405 if(flags.Sign != '+')
406 flags.Sign = *p;
407 } else if(*p == '-')
408 flags.LeftAlign = *p;
409 else if(*p == '0')
410 flags.PadZero = *p;
411 else if(*p == '#')
412 flags.Alternate = *p;
413 else
414 break;
416 p++;
419 /* parse the width */
420 if(*p == '*') {
421 p++;
422 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
423 p = q+1;
424 else
425 i = -1;
427 flags.FieldLength = pf_args(args_ctx, i, VT_INT, valist).get_int;
428 if(flags.FieldLength < 0) {
429 flags.LeftAlign = '-';
430 flags.FieldLength = -flags.FieldLength;
432 } else while(isdigit(*p)) {
433 flags.FieldLength *= 10;
434 flags.FieldLength += *p++ - '0';
437 /* parse the precision */
438 flags.Precision = -1;
439 if(*p == '.') {
440 flags.Precision = 0;
441 p++;
442 if(*p == '*') {
443 p++;
444 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
445 p = q+1;
446 else
447 i = -1;
449 flags.Precision = pf_args(args_ctx, i, VT_INT, valist).get_int;
450 } else while(isdigit(*p)) {
451 flags.Precision *= 10;
452 flags.Precision += *p++ - '0';
456 /* parse argument size modifier */
457 while(*p) {
458 if(*p=='l' && *(p+1)=='l') {
459 flags.IntegerDouble++;
460 p += 2;
461 } else if(*p=='h' || *p=='l' || *p=='L') {
462 flags.IntegerLength = *p;
463 p++;
464 } else if(*p == 'I') {
465 if(*(p+1)=='6' && *(p+2)=='4') {
466 flags.IntegerDouble++;
467 p += 3;
468 } else if(*(p+1)=='3' && *(p+2)=='2')
469 p += 3;
470 else if(isdigit(*(p+1)) || !*(p+1))
471 break;
472 else
473 flags.IntegerNative = *p++;
474 } else if(*p == 'w')
475 flags.WideString = *p++;
476 #if _MSVCR_VER >= 140
477 else if(*p == 'z')
478 flags.IntegerNative = *p++;
479 #endif
480 else if((*p == 'F' || *p == 'N') && legacy_msvcrt_compat)
481 p++; /* ignore */
482 else
483 break;
486 flags.Format = *p;
488 if(flags.Format == 's' || flags.Format == 'S') {
489 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx,
490 pf_args(args_ctx, pos, VT_PTR, valist).get_ptr,
491 -1, &flags, locinfo, legacy_wide);
492 } else if(flags.Format == 'c' || flags.Format == 'C') {
493 int ch = pf_args(args_ctx, pos, VT_INT, valist).get_int;
495 if((ch&0xff) != ch)
496 FIXME("multibyte characters printing not supported\n");
498 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx, &ch, 1, &flags, locinfo, legacy_wide);
499 } else if(flags.Format == 'p') {
500 flags.Format = 'X';
501 flags.PadZero = '0';
502 i = flags.Precision;
503 flags.Precision = 2*sizeof(void*);
504 FUNC_NAME(pf_integer_conv)(buf, sizeof(buf)/sizeof(APICHAR), &flags,
505 (ULONG_PTR)pf_args(args_ctx, pos, VT_PTR, valist).get_ptr);
506 flags.PadZero = 0;
507 flags.Precision = i;
509 #ifdef PRINTF_WIDE
510 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, buf, -1, &flags, locinfo);
511 #else
512 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, buf, -1, &flags, locinfo);
513 #endif
514 } else if(flags.Format == 'n') {
515 int *used;
517 if(!n_format_enabled) {
518 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", MSVCRT_EINVAL);
519 return -1;
522 used = pf_args(args_ctx, pos, VT_PTR, valist).get_ptr;
523 *used = written;
524 i = 0;
525 } else if(flags.Format && strchr("diouxX", flags.Format)) {
526 APICHAR *tmp = buf;
527 int max_len;
529 /* 0 padding is added after '0x' if Alternate flag is in use */
530 if((flags.Format=='x' || flags.Format=='X') && flags.PadZero && flags.Alternate
531 && !flags.LeftAlign && flags.Precision<flags.FieldLength-2)
532 flags.Precision = flags.FieldLength - 2;
534 max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
535 if(max_len > sizeof(buf)/sizeof(APICHAR))
536 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
537 if(!tmp)
538 return -1;
540 if(flags.IntegerDouble || (flags.IntegerNative && sizeof(void*) == 8))
541 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, pf_args(args_ctx, pos,
542 VT_I8, valist).get_longlong);
543 else if(flags.Format=='d' || flags.Format=='i')
544 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, flags.IntegerLength!='h' ?
545 pf_args(args_ctx, pos, VT_INT, valist).get_int :
546 (short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
547 else
548 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, flags.IntegerLength!='h' ?
549 (unsigned)pf_args(args_ctx, pos, VT_INT, valist).get_int :
550 (unsigned short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
552 #ifdef PRINTF_WIDE
553 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, tmp, -1, &flags, locinfo);
554 #else
555 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, tmp, -1, &flags, locinfo);
556 #endif
557 if(tmp != buf)
558 HeapFree(GetProcessHeap(), 0, tmp);
559 } else if(flags.Format && strchr("aeEfFgG", flags.Format)) {
560 char float_fmt[20], buf_a[32], *tmp = buf_a, *decimal_point;
561 int len = flags.Precision + 10;
562 double val = pf_args(args_ctx, pos, VT_R8, valist).get_double;
563 int r;
564 BOOL inf = FALSE, nan = FALSE, ind = FALSE;
566 if(isinf(val))
567 inf = TRUE;
568 else if(isnan(val)) {
569 if(!signbit(val))
570 nan = TRUE;
571 else
572 ind = TRUE;
575 if(inf || nan || ind) {
576 if(ind || val<0)
577 flags.Sign = '-';
579 if(flags.Format=='g' || flags.Format=='G')
580 val = (nan ? 1.12345 : 1.1234); /* fraction will be overwritten with #INF, #IND or #QNAN string */
581 else
582 val = 1;
584 if(flags.Format=='a') {
585 if(flags.Precision==-1)
586 flags.Precision = 6; /* strlen("#INF00") */
590 if(flags.Format=='f' || flags.Format=='F') {
591 if(val>-10.0 && val<10.0)
592 i = 1;
593 else
594 i = 1 + log10(val<0 ? -val : val);
595 /* Default precision is 6, additional space for sign, separator and nullbyte is required */
596 i += (flags.Precision==-1 ? 6 : flags.Precision) + 3;
598 if(i > len)
599 len = i;
602 if(len > sizeof(buf_a))
603 tmp = HeapAlloc(GetProcessHeap(), 0, len);
604 if(!tmp)
605 return -1;
607 FUNC_NAME(pf_rebuild_format_string)(float_fmt, &flags);
608 if(val < 0) {
609 flags.Sign = '-';
610 val = -val;
613 if((inf || nan || ind) && !legacy_msvcrt_compat) {
614 static const char inf_str[] = "inf";
615 static const char ind_str[] = "nan(ind)";
616 static const char nan_str[] = "nan";
617 if(inf)
618 sprintf(tmp, inf_str);
619 else if(ind)
620 sprintf(tmp, ind_str);
621 else
622 sprintf(tmp, nan_str);
623 if (strchr("EFG", flags.Format))
624 for(i=0; tmp[i]; i++)
625 tmp[i] = toupper(tmp[i]);
626 } else {
627 sprintf(tmp, float_fmt, val);
628 if(toupper(flags.Format)=='E' || toupper(flags.Format)=='G')
629 FUNC_NAME(pf_fixup_exponent)(tmp, three_digit_exp);
632 decimal_point = strchr(tmp, '.');
633 if(decimal_point) {
634 *decimal_point = *locinfo->lconv->decimal_point;
636 if(inf || nan || ind) {
637 static const char inf_str[] = "#INF";
638 static const char ind_str[] = "#IND";
639 static const char nan_str[] = "#QNAN";
641 const char *str;
642 int size;
644 if(inf) {
645 str = inf_str;
646 size = sizeof(inf_str);
647 }else if(ind) {
648 str = ind_str;
649 size = sizeof(ind_str);
650 }else {
651 str = nan_str;
652 size = sizeof(nan_str);
655 for(i=1; i<size; i++) {
656 if(decimal_point[i]<'0' || decimal_point[i]>'9')
657 break;
659 decimal_point[i] = str[i-1];
662 if(i!=size && i!=1)
663 decimal_point[i-1]++;
667 len = strlen(tmp);
668 i = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, &flags, TRUE);
669 if(i < 0)
670 return i;
671 r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, tmp, len, locinfo);
672 if(r < 0)
673 return r;
674 i += r;
675 if(tmp != buf_a)
676 HeapFree(GetProcessHeap(), 0, tmp);
677 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, &flags, FALSE);
678 if(r < 0)
679 return r;
680 i += r;
681 } else {
682 if(invoke_invalid_param_handler) {
683 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
684 *MSVCRT__errno() = MSVCRT_EINVAL;
685 return -1;
688 continue;
691 if(i < 0)
692 return i;
693 written += i;
694 p++;
697 return written;
700 #ifndef PRINTF_WIDE
701 enum types_clbk_flags {
702 TYPE_CLBK_VA_LIST = 1,
703 TYPE_CLBK_POSITIONAL = 2,
704 TYPE_CLBK_ERROR_POS = 4,
705 TYPE_CLBK_ERROR_TYPE = 8
708 /* This functions stores types of arguments. It uses args[0] internally */
709 static printf_arg arg_clbk_type(void *ctx, int pos, int type, __ms_va_list *valist)
711 static const printf_arg ret;
712 printf_arg *args = ctx;
714 if(pos == -1) {
715 args[0].get_int |= TYPE_CLBK_VA_LIST;
716 return ret;
717 } else
718 args[0].get_int |= TYPE_CLBK_POSITIONAL;
720 if(pos<1 || pos>MSVCRT__ARGMAX)
721 args[0].get_int |= TYPE_CLBK_ERROR_POS;
722 else if(args[pos].get_int && args[pos].get_int!=type)
723 args[0].get_int |= TYPE_CLBK_ERROR_TYPE;
724 else
725 args[pos].get_int = type;
727 return ret;
729 #endif
731 static int FUNC_NAME(create_positional_ctx)(void *args_ctx, const APICHAR *format, __ms_va_list valist)
733 struct FUNC_NAME(_str_ctx) puts_ctx = {INT_MAX, NULL};
734 printf_arg *args = args_ctx;
735 int i, j;
737 i = FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk_str), &puts_ctx, format, NULL,
738 MSVCRT_PRINTF_POSITIONAL_PARAMS, arg_clbk_type, args_ctx, NULL);
739 if(i < 0)
740 return i;
742 if(args[0].get_int==0 || args[0].get_int==TYPE_CLBK_VA_LIST)
743 return 0;
744 if(args[0].get_int != TYPE_CLBK_POSITIONAL)
745 return -1;
747 for(i=MSVCRT__ARGMAX; i>0; i--)
748 if(args[i].get_int)
749 break;
751 for(j=1; j<=i; j++) {
752 switch(args[j].get_int) {
753 case VT_I8:
754 args[j].get_longlong = va_arg(valist, LONGLONG);
755 break;
756 case VT_INT:
757 args[j].get_int = va_arg(valist, int);
758 break;
759 case VT_R8:
760 args[j].get_double = va_arg(valist, double);
761 break;
762 case VT_PTR:
763 args[j].get_ptr = va_arg(valist, void*);
764 break;
765 default:
766 return -1;
770 return j;
773 #undef APICHAR
774 #undef CONVCHAR
775 #undef FUNC_NAME