1 /* Argument-processing fragment for vfprintf.
2 Copyright (C) 1991-2022 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 "%". */
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
)
120 /* Put the number in WORK. */
121 string
= _itoa (number
.longlong
, workend
, base
,
123 if (group
&& grouping
)
124 string
= group_number (work_buffer
, string
, workend
,
125 grouping
, thousands_sep
);
126 if (use_outdigits
&& base
== 10)
127 string
= _i18n_number_rewrite (string
, workend
, workend
);
129 /* Simplify further test for num != 0. */
130 number
.word
= number
.longlong
!= 0;
135 number
.word
= process_arg_unsigned_long_int ();
137 number
.word
= (unsigned char) process_arg_unsigned_int ();
139 number
.word
= process_arg_unsigned_int ();
141 number
.word
= (unsigned short int) process_arg_unsigned_int ();
145 /* Supply a default precision if none was given. */
148 /* We have to take care for the '0' flag. If a precision
149 is given it must be ignored. */
152 /* If the precision is 0 and the number is 0 nothing has to
153 be written for the number, except for the 'o' format in
155 if (prec
== 0 && number
.word
== 0)
158 if (base
== 8 && alt
)
163 /* Put the number in WORK. */
164 string
= _itoa_word (number
.word
, workend
, base
,
166 if (group
&& grouping
)
167 string
= group_number (work_buffer
, string
, workend
,
168 grouping
, thousands_sep
);
169 if (use_outdigits
&& base
== 10)
170 string
= _i18n_number_rewrite (string
, workend
, workend
);
174 if (prec
<= workend
- string
&& number
.word
!= 0 && alt
&& base
== 8)
175 /* Add octal marker. */
178 prec
= MAX (0, prec
- (workend
- string
));
182 width
-= workend
- string
+ prec
;
184 if (number
.word
!= 0 && alt
&& (base
== 16 || base
== 2))
185 /* Account for 0X, 0x, 0B or 0b hex or binary marker. */
188 if (is_negative
|| showsign
|| space
)
204 if (number
.word
!= 0 && alt
&& (base
== 16 || base
== 2))
213 outstring (string
, workend
- string
);
235 if (number
.word
!= 0 && alt
&& (base
== 16 || base
== 2))
242 width
-= workend
- string
+ prec
;
252 outstring (string
, workend
- string
);
258 LABEL (form_pointer
):
259 /* Generic pointer. */
261 const void *ptr
= process_arg_pointer ();
264 /* If the pointer is not NULL, write it as a %#x spec. */
266 number
.word
= (unsigned long int) ptr
;
275 /* Write "(nil)" for a nil pointer. */
276 string
= (CHAR_T
*) L_("(nil)");
277 /* Make sure the full string "(nil)" is printed. */
280 /* This is a wide string iff compiling wprintf. */
281 is_long
= sizeof (CHAR_T
) > 1;
282 goto LABEL (print_string
);
288 if ((mode_flags
& PRINTF_FORTIFY
) != 0)
290 if (! readonly_format
)
292 extern int __readonly_area (const void *, size_t)
295 = __readonly_area (format
, ((STR_LEN (format
) + 1)
298 if (readonly_format
< 0)
299 __libc_fatal ("*** %n in writable segment detected ***\n");
301 /* Answer the count of characters written. */
302 void *ptrptr
= process_arg_pointer ();
304 *(long long int *) ptrptr
= done
;
305 else if (is_long_num
)
306 *(long int *) ptrptr
= done
;
308 *(char *) ptrptr
= done
;
310 *(int *) ptrptr
= done
;
312 *(short int *) ptrptr
= done
;
315 LABEL (form_strerror
):
316 /* Print description of error ERRNO. */
318 string
= (CHAR_T
*) __get_errname (save_errno
);
320 string
= (CHAR_T
*) __strerror_r (save_errno
, (char *) work_buffer
,
321 WORK_BUFFER_SIZE
* sizeof (CHAR_T
));
324 /* Print as a decimal number. */
326 is_negative
= save_errno
< 0;
327 number
.word
= save_errno
;
329 number
.word
= -number
.word
;
334 is_long
= 0; /* This is no wide-char string. */
335 goto LABEL (print_string
);
338 LABEL (form_character
):
341 goto LABEL (form_wcharacter
);
342 --width
; /* Account for the character itself. */
345 #ifdef COMPILE_WPRINTF
346 outchar (__btowc ((unsigned char) process_arg_int ())); /* Promoted. */
348 outchar ((unsigned char) process_arg_int ()); /* Promoted. */
358 /* The string argument could in fact be `char *' or `wchar_t *'.
359 But this should not make a difference here. */
360 #ifdef COMPILE_WPRINTF
361 string
= (CHAR_T
*) process_arg_wstring ();
363 string
= (CHAR_T
*) process_arg_string ();
365 /* Entry point for printing other strings. */
366 LABEL (print_string
):
370 /* Write "(null)" if there's space. */
371 if (prec
== -1 || prec
>= (int) array_length (null
) - 1)
373 string
= (CHAR_T
*) null
;
374 len
= array_length (null
) - 1;
378 string
= (CHAR_T
*) L
"";
382 else if (!is_long
&& spec
!= L_('S'))
384 #ifdef COMPILE_WPRINTF
385 done
= outstring_converted_wide_string
386 (s
, (const char *) string
, prec
, width
, left
, done
);
389 /* The padding has already been written. */
393 /* Search for the end of the string, but don't search past
394 the length (in bytes) specified by the precision. */
395 len
= __strnlen (string
, prec
);
397 len
= strlen (string
);
402 #ifdef COMPILE_WPRINTF
404 /* Search for the end of the string, but don't search past
405 the length specified by the precision. */
406 len
= __wcsnlen (string
, prec
);
408 len
= __wcslen (string
);
410 done
= outstring_converted_wide_string
411 (s
, (const wchar_t *) string
, prec
, width
, left
, done
);
414 /* The padding has already been written. */
419 if ((width
-= len
) < 0)
421 outstring (string
, len
);
427 outstring (string
, len
);
433 #ifdef COMPILE_WPRINTF
434 LABEL (form_wcharacter
):
436 /* Wide character. */
440 outchar (process_arg_wchar_t ());
446 #else /* !COMPILE_WPRINTF */
447 LABEL (form_wcharacter
):
449 /* Wide character. */
450 char buf
[MB_LEN_MAX
];
454 memset (&mbstate
, '\0', sizeof (mbstate_t));
455 len
= __wcrtomb (buf
, process_arg_wchar_t (), &mbstate
);
456 if (len
== (size_t) -1)
458 /* Something went wrong during the conversion. Bail out. */
465 outstring (buf
, len
);
470 #endif /* !COMPILE_WPRINTF */