1 /* Argument-processing fragment for vfprintf.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* This file is included twice from vfprintf-internal.c, for standard
20 and GNU-style positional (%N$) arguments. Before that,
21 process_arg_int etc. macros have to be defined to extract one
22 argument of the appropriate type, in addition to the file-specific
23 macros in vfprintf-internal.c. */
26 /* Start real work. We know about all flags and modifiers and
27 now process the wanted format specifier. */
29 /* Write a literal "%". */
30 Xprintf_buffer_putc (buf, L_('%'));
34 /* Signed decimal integer. */
39 long long int signed_number = process_arg_long_long_int ();
40 is_negative = signed_number < 0;
41 number.longlong = is_negative ? (- signed_number) : signed_number;
43 goto LABEL (longlong_number);
47 long int signed_number;
49 signed_number = process_arg_long_int ();
51 signed_number = (signed char) process_arg_unsigned_int ();
53 signed_number = process_arg_int ();
55 signed_number = (short int) process_arg_unsigned_int ();
57 is_negative = signed_number < 0;
58 number.word = is_negative ? (- signed_number) : signed_number;
64 LABEL (form_unsigned):
65 /* Unsigned decimal integer. */
67 goto LABEL (unsigned_number);
71 /* Unsigned octal integer. */
73 goto LABEL (unsigned_number);
77 /* Unsigned hexadecimal integer. */
79 goto LABEL (unsigned_number);
83 /* Unsigned binary integer. */
85 goto LABEL (unsigned_number);
88 LABEL (unsigned_number): /* Unsigned number of base BASE. */
90 /* ISO specifies the `+' and ` ' flags only for signed
98 number.longlong = process_arg_unsigned_long_long_int ();
100 LABEL (longlong_number):
102 /* Supply a default precision if none was given. */
105 /* We have to take care for the '0' flag. If a precision
106 is given it must be ignored. */
109 /* If the precision is 0 and the number is 0 nothing has to
110 be written for the number, except for the 'o' format in
112 if (prec == 0 && number.longlong == 0)
115 if (base == 8 && alt)
119 /* Put the number in WORK. */
120 string = _itoa (number.longlong, workend, base, spec == L_('X'));
121 /* Simplify further test for num != 0. */
122 number.word = number.longlong != 0;
127 number.word = process_arg_unsigned_long_int ();
129 number.word = (unsigned char) process_arg_unsigned_int ();
131 number.word = process_arg_unsigned_int ();
133 number.word = (unsigned short int) process_arg_unsigned_int ();
137 /* Supply a default precision if none was given. */
140 /* We have to take care for the '0' flag. If a precision
141 is given it must be ignored. */
144 /* If the precision is 0 and the number is 0 nothing has to
145 be written for the number, except for the 'o' format in
147 if (prec == 0 && number.word == 0)
150 if (base == 8 && alt)
154 /* Put the number in WORK. */
155 string = _itoa_word (number.word, workend, base,
159 /* Grouping is also used for outdigits translation. */
160 struct grouping_iterator iter;
161 bool number_slow_path = group || (use_outdigits && base == 10);
163 __grouping_iterator_init (&iter, LC_NUMERIC, _NL_CURRENT_LOCALE,
165 else if (use_outdigits && base == 10)
166 __grouping_iterator_init_none (&iter, workend - string);
169 #ifndef COMPILE_WPRINTF
170 if (use_outdigits && base == 10)
171 number_length = __translated_number_width (_NL_CURRENT_LOCALE,
174 number_length = workend - string;
176 number_length += iter.separators * strlen (thousands_sep);
178 number_length = workend - string;
179 /* All wide separators have length 1. */
180 if (group && thousands_sep != L'\0')
181 number_length += iter.separators;
184 /* The marker comes right before the number, but is not subject
186 bool octal_marker = (prec <= number_length && number.word != 0
187 && alt && base == 8);
189 prec = MAX (0, prec - (workend - string));
193 width -= number_length + prec;
195 if (number.word != 0 && alt && (base == 16 || base == 2))
196 /* Account for 0X, 0x, 0B or 0b hex or binary marker. */
202 if (is_negative || showsign || space)
207 Xprintf_buffer_pad (buf, L_(' '), width);
212 Xprintf_buffer_putc (buf, L_('-'));
214 Xprintf_buffer_putc (buf, L_('+'));
216 Xprintf_buffer_putc (buf, L_(' '));
218 if (number.word != 0 && alt && (base == 16 || base == 2))
220 Xprintf_buffer_putc (buf, L_('0'));
221 Xprintf_buffer_putc (buf, spec);
225 Xprintf_buffer_pad (buf, L_('0'), width);
228 Xprintf_buffer_putc (buf, L_('0'));
230 if (number_slow_path)
231 group_number (buf, &iter, string, workend, thousands_sep,
232 use_outdigits && base == 10);
234 Xprintf_buffer_write (buf, string, workend - string);
242 Xprintf_buffer_putc (buf, L_('-'));
247 Xprintf_buffer_putc (buf, L_('+'));
252 Xprintf_buffer_putc (buf, L_(' '));
256 if (number.word != 0 && alt && (base == 16 || base == 2))
258 Xprintf_buffer_putc (buf, L_('0'));
259 Xprintf_buffer_putc (buf, spec);
266 width -= workend - string + prec;
268 Xprintf_buffer_pad (buf, L_('0'), prec);
271 Xprintf_buffer_putc (buf, L_('0'));
273 if (number_slow_path)
274 group_number (buf, &iter, string, workend, thousands_sep,
275 use_outdigits && base == 10);
277 Xprintf_buffer_write (buf, string, workend - string);
279 Xprintf_buffer_pad (buf, L_(' '), width);
283 LABEL (form_pointer):
284 /* Generic pointer. */
286 const void *ptr = process_arg_pointer ();
289 /* If the pointer is not NULL, write it as a %#x spec. */
291 number.word = (unsigned long int) ptr;
300 /* Write "(nil)" for a nil pointer. */
301 string = (CHAR_T *) L_("(nil)");
302 /* Make sure the full string "(nil)" is printed. */
305 /* This is a wide string iff compiling wprintf. */
306 is_long = sizeof (CHAR_T) > 1;
307 goto LABEL (print_string);
313 if ((mode_flags & PRINTF_FORTIFY) != 0)
315 if (! readonly_format)
317 extern int __readonly_area (const void *, size_t)
320 = __readonly_area (format, ((STR_LEN (format) + 1)
323 if (readonly_format < 0)
324 __libc_fatal ("*** %n in writable segment detected ***\n");
326 /* Answer the count of characters written. */
327 void *ptrptr = process_arg_pointer ();
328 unsigned int written = Xprintf_buffer_done (buf);
330 *(long long int *) ptrptr = written;
331 else if (is_long_num)
332 *(long int *) ptrptr = written;
334 *(char *) ptrptr = written;
336 *(int *) ptrptr = written;
338 *(short int *) ptrptr = written;
341 LABEL (form_strerror):
342 /* Print description of error ERRNO. */
344 string = (CHAR_T *) __get_errname (save_errno);
346 string = (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer,
347 WORK_BUFFER_SIZE * sizeof (CHAR_T));
350 /* Print as a decimal number. */
352 is_negative = save_errno < 0;
353 number.word = save_errno;
355 number.word = -number.word;
360 is_long = 0; /* This is no wide-char string. */
361 goto LABEL (print_string);
364 LABEL (form_character):
367 goto LABEL (form_wcharacter);
368 --width; /* Account for the character itself. */
370 Xprintf_buffer_pad (buf, L_(' '), width);
371 #ifdef COMPILE_WPRINTF
372 __wprintf_buffer_putc (buf, __btowc ((unsigned char) /* Promoted. */
373 process_arg_int ()));
375 __printf_buffer_putc (buf, (unsigned char) /* Promoted. */
379 Xprintf_buffer_pad (buf, L_(' '), width);
386 /* The string argument could in fact be `char *' or `wchar_t *'.
387 But this should not make a difference here. */
388 #ifdef COMPILE_WPRINTF
389 string = (CHAR_T *) process_arg_wstring ();
391 string = (CHAR_T *) process_arg_string ();
393 /* Entry point for printing other strings. */
394 LABEL (print_string):
398 /* Write "(null)" if there's space. */
399 if (prec == -1 || prec >= (int) array_length (null) - 1)
401 string = (CHAR_T *) null;
402 len = array_length (null) - 1;
406 string = (CHAR_T *) L"";
410 else if (!is_long && spec != L_('S'))
412 #ifdef COMPILE_WPRINTF
413 outstring_converted_wide_string (buf, (const char *) string,
415 /* The padding has already been written. */
419 /* Search for the end of the string, but don't search past
420 the length (in bytes) specified by the precision. */
421 len = __strnlen (string, prec);
423 len = strlen (string);
428 #ifdef COMPILE_WPRINTF
430 /* Search for the end of the string, but don't search past
431 the length specified by the precision. */
432 len = __wcsnlen (string, prec);
434 len = __wcslen (string);
436 outstring_converted_wide_string (buf, (const wchar_t *) string,
438 /* The padding has already been written. */
443 if ((width -= len) < 0)
445 Xprintf_buffer_write (buf, string, len);
450 Xprintf_buffer_pad (buf, L_(' '), width);
451 Xprintf_buffer_write (buf, string, len);
453 Xprintf_buffer_pad (buf, L_(' '), width);
457 #ifdef COMPILE_WPRINTF
458 LABEL (form_wcharacter):
460 /* Wide character. */
463 Xprintf_buffer_pad (buf, L_(' '), width);
464 Xprintf_buffer_putc (buf, process_arg_wchar_t ());
466 Xprintf_buffer_pad (buf, L_(' '), width);
470 #else /* !COMPILE_WPRINTF */
471 LABEL (form_wcharacter):
473 /* Wide character. */
474 char wcbuf[MB_LEN_MAX];
478 memset (&mbstate, '\0', sizeof (mbstate_t));
479 len = __wcrtomb (wcbuf, process_arg_wchar_t (), &mbstate);
480 if (len == (size_t) -1)
482 /* Something went wrong during the conversion. Bail out. */
483 __printf_buffer_mark_failed (buf);
488 Xprintf_buffer_pad (buf, L_(' '), width);
489 Xprintf_buffer_write (buf, wcbuf, len);
491 Xprintf_buffer_pad (buf, L_(' '), width);
494 #endif /* !COMPILE_WPRINTF */