Mon May 6 09:51:05 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / stdio-common / vfprintf.c
blob9b245745584bd719417cb43ece0f1663fea4297f
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <ctype.h>
20 #include <limits.h>
21 #include <printf.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <wchar.h>
26 #include "_itoa.h"
27 #include "../locale/localeinfo.h"
29 /* This code is shared between the standard stdio implementation found
30 in GNU C library and the libio implementation originally found in
31 GNU libg++.
33 Beside this it is also shared between the normal and wide character
34 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
36 #ifndef COMPILE_WPRINTF
37 # define CHAR_T char
38 # define UCHAR_T unsigned char
39 # define INT_T int
40 # define L_(Str) Str
41 # define ISDIGIT(Ch) isdigit (Ch)
43 # ifdef USE_IN_LIBIO
44 # define PUT(F, S, N) _IO_sputn (F, S, N)
45 # define PAD(Padchar) \
46 if (width > 0) \
47 done += _IO_padn (s, Padchar, width)
48 # else
49 # define PUTC(C, F) putc (C, F)
50 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
51 # define PAD(Padchar) \
52 if (width > 0) \
53 { if (__printf_pad (s, Padchar, width) == -1) \
54 return -1; else done += width; }
55 # endif
56 #else
57 # define vfprintf vfwprintf
58 # define CHAR_T wchar_t
59 # define UCHAR_T uwchar_t
60 # define INT_T wint_t
61 # define L_(Str) L##Str
62 # define ISDIGIT(Ch) iswdigit (Ch)
64 # ifdef USE_IN_LIBIO
65 # define PUT(F, S, N) _IO_sputn (F, S, N)
66 # define PAD(Padchar) \
67 if (width > 0) \
68 done += _IO_wpadn (s, Padchar, width)
69 # else
70 # define PUTC(C, F) wputc (C, F)
71 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
72 # define PAD(Padchar) \
73 if (width > 0) \
74 { if (__wprintf_pad (s, Padchar, width) == -1) \
75 return -1; else done += width; }
76 # endif
77 #endif
79 /* Include the shared code for parsing the format string. */
80 #include "printf-parse.h"
83 #ifdef USE_IN_LIBIO
84 /* This code is for use in libio. */
85 # include <libioP.h>
86 # define PUTC(C, F) _IO_putc (C, F)
87 # define vfprintf _IO_vfprintf
88 # define size_t _IO_size_t
89 # define FILE _IO_FILE
90 # define va_list _IO_va_list
91 # undef BUFSIZ
92 # define BUFSIZ _IO_BUFSIZ
93 # define ARGCHECK(S, Format) \
94 do \
95 { \
96 /* Check file argument for consistence. */ \
97 CHECK_FILE (S, -1); \
98 if (S->_flags & _IO_NO_WRITES || Format == NULL) \
99 { \
100 MAYBE_SET_EINVAL; \
101 return -1; \
103 } while (0)
104 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
105 #else /* ! USE_IN_LIBIO */
106 /* This code is for use in the GNU C library. */
107 # include <stdio.h>
108 # define PUT(F, S, N) fwrite (S, 1, N, F)
109 # define ARGCHECK(S, Format) \
110 do \
112 /* Check file argument for consistence. */ \
113 if (!__validfp(S) || !S->__mode.__write || Format == NULL) \
115 errno = EINVAL; \
116 return -1; \
118 if (!S->__seen) \
120 if (__flshfp (S, EOF) == EOF) \
121 return -1; \
124 while (0)
125 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
126 #endif /* USE_IN_LIBIO */
129 #define outchar(Ch) \
130 do \
132 register const int outc = (Ch); \
133 if (PUTC (outc, s) == EOF) \
134 return -1; \
135 else \
136 ++done; \
138 while (0)
140 #define outstring(String, Len) \
141 do \
143 if (PUT (s, String, Len) != Len) \
144 return -1; \
145 done += Len; \
147 while (0)
149 /* For handling long_double and longlong we use the same flag. */
150 #ifndef is_longlong
151 # define is_longlong is_long_double
152 #endif
155 /* Global variables. */
156 static const char null[] = "(null)";
159 /* Helper function to provide temporary buffering for unbuffered streams. */
160 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list));
162 /* Handle unknown format specifier. */
163 static int printf_unknown __P ((FILE *, const struct printf_info *,
164 const void *const *));
166 /* Group digits of number string. */
167 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t));
170 /* The function itself. */
172 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
174 /* The character used as thousands separator. */
175 wchar_t thousands_sep;
177 /* The string describing the size of groups of digits. */
178 const char *grouping;
180 /* Place to accumulate the result. */
181 int done;
183 /* Current character in format string. */
184 const UCHAR_T *f;
186 /* End of leading constant string. */
187 const UCHAR_T *lead_str_end;
189 /* Points to next format specifier. */
190 const UCHAR_T *end_of_spec;
192 /* Buffer intermediate results. */
193 char work_buffer[1000];
194 #define workend (&work_buffer[sizeof (work_buffer) - 1])
196 /* State for restartable multibyte character handling functions. */
197 mbstate_t mbstate;
199 /* We have to save the original argument pointer. */
200 va_list ap_save;
202 /* Count number of specifiers we already processed. */
203 int nspecs_done;
206 /* This table maps a character into a number representing a
207 class. In each step there is a destination label for each
208 class. */
209 static const int jump_table[] =
211 /* ' ' */ 1, 0, 0, /* '#' */ 4,
212 0, /* '%' */ 14, 0, /* '\''*/ 6,
213 0, 0, /* '*' */ 7, /* '+' */ 2,
214 0, /* '-' */ 3, /* '.' */ 9, 0,
215 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
216 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
217 /* '8' */ 8, /* '9' */ 8, 0, 0,
218 0, 0, 0, 0,
219 0, 0, 0, 0,
220 0, /* 'E' */ 19, 0, /* 'G' */ 19,
221 0, 0, 0, 0,
222 /* 'L' */ 12, 0, 0, 0,
223 0, 0, 0, 0,
224 0, 0, 0, 0,
225 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
226 0, 0, 0, 0,
227 0, 0, 0, /* 'c' */ 20,
228 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
229 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
230 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
231 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
232 0, /* 'u' */ 16, 0, 0,
233 /* 'x' */ 18
236 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
237 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
238 #define JUMP(ChExpr, table) \
239 do \
241 const void *ptr; \
242 spec = (ChExpr); \
243 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
244 : table[CHAR_CLASS (spec)]; \
245 goto *ptr; \
247 while (0)
249 #define STEP0_3_TABLE \
250 /* Step 0: at the beginning. */ \
251 static const void *step0_jumps[25] = \
253 REF (form_unknown), \
254 REF (flag_space), /* for ' ' */ \
255 REF (flag_plus), /* for '+' */ \
256 REF (flag_minus), /* for '-' */ \
257 REF (flag_hash), /* for '<hash>' */ \
258 REF (flag_zero), /* for '0' */ \
259 REF (flag_quote), /* for '\'' */ \
260 REF (width_asterics), /* for '*' */ \
261 REF (width), /* for '1'...'9' */ \
262 REF (precision), /* for '.' */ \
263 REF (mod_half), /* for 'h' */ \
264 REF (mod_long), /* for 'l' */ \
265 REF (mod_longlong), /* for 'L', 'q' */ \
266 REF (mod_size_t), /* for 'Z' */ \
267 REF (form_percent), /* for '%' */ \
268 REF (form_integer), /* for 'd', 'i' */ \
269 REF (form_unsigned), /* for 'u' */ \
270 REF (form_octal), /* for 'o' */ \
271 REF (form_hexa), /* for 'X', 'x' */ \
272 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
273 REF (form_character), /* for 'c' */ \
274 REF (form_string), /* for 's' */ \
275 REF (form_pointer), /* for 'p' */ \
276 REF (form_number), /* for 'n' */ \
277 REF (form_strerror) /* for 'm' */ \
278 }; \
279 /* Step 1: after processing width. */ \
280 static const void *step1_jumps[25] = \
282 REF (form_unknown), \
283 REF (form_unknown), /* for ' ' */ \
284 REF (form_unknown), /* for '+' */ \
285 REF (form_unknown), /* for '-' */ \
286 REF (form_unknown), /* for '<hash>' */ \
287 REF (form_unknown), /* for '0' */ \
288 REF (form_unknown), /* for '\'' */ \
289 REF (form_unknown), /* for '*' */ \
290 REF (form_unknown), /* for '1'...'9' */ \
291 REF (precision), /* for '.' */ \
292 REF (mod_half), /* for 'h' */ \
293 REF (mod_long), /* for 'l' */ \
294 REF (mod_longlong), /* for 'L', 'q' */ \
295 REF (mod_size_t), /* for 'Z' */ \
296 REF (form_percent), /* for '%' */ \
297 REF (form_integer), /* for 'd', 'i' */ \
298 REF (form_unsigned), /* for 'u' */ \
299 REF (form_octal), /* for 'o' */ \
300 REF (form_hexa), /* for 'X', 'x' */ \
301 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
302 REF (form_character), /* for 'c' */ \
303 REF (form_string), /* for 's' */ \
304 REF (form_pointer), /* for 'p' */ \
305 REF (form_number), /* for 'n' */ \
306 REF (form_strerror) /* for 'm' */ \
307 }; \
308 /* Step 2: after processing precision. */ \
309 static const void *step2_jumps[25] = \
311 REF (form_unknown), \
312 REF (form_unknown), /* for ' ' */ \
313 REF (form_unknown), /* for '+' */ \
314 REF (form_unknown), /* for '-' */ \
315 REF (form_unknown), /* for '<hash>' */ \
316 REF (form_unknown), /* for '0' */ \
317 REF (form_unknown), /* for '\'' */ \
318 REF (form_unknown), /* for '*' */ \
319 REF (form_unknown), /* for '1'...'9' */ \
320 REF (form_unknown), /* for '.' */ \
321 REF (mod_half), /* for 'h' */ \
322 REF (mod_long), /* for 'l' */ \
323 REF (mod_longlong), /* for 'L', 'q' */ \
324 REF (mod_size_t), /* for 'Z' */ \
325 REF (form_percent), /* for '%' */ \
326 REF (form_integer), /* for 'd', 'i' */ \
327 REF (form_unsigned), /* for 'u' */ \
328 REF (form_octal), /* for 'o' */ \
329 REF (form_hexa), /* for 'X', 'x' */ \
330 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
331 REF (form_character), /* for 'c' */ \
332 REF (form_string), /* for 's' */ \
333 REF (form_pointer), /* for 'p' */ \
334 REF (form_number), /* for 'n' */ \
335 REF (form_strerror) /* for 'm' */ \
336 }; \
337 /* Step 3: after processing first 'l' modifier. */ \
338 static const void *step3_jumps[25] = \
340 REF (form_unknown), \
341 REF (form_unknown), /* for ' ' */ \
342 REF (form_unknown), /* for '+' */ \
343 REF (form_unknown), /* for '-' */ \
344 REF (form_unknown), /* for '<hash>' */ \
345 REF (form_unknown), /* for '0' */ \
346 REF (form_unknown), /* for '\'' */ \
347 REF (form_unknown), /* for '*' */ \
348 REF (form_unknown), /* for '1'...'9' */ \
349 REF (form_unknown), /* for '.' */ \
350 REF (form_unknown), /* for 'h' */ \
351 REF (mod_longlong), /* for 'l' */ \
352 REF (form_unknown), /* for 'L', 'q' */ \
353 REF (form_unknown), /* for 'Z' */ \
354 REF (form_percent), /* for '%' */ \
355 REF (form_integer), /* for 'd', 'i' */ \
356 REF (form_unsigned), /* for 'u' */ \
357 REF (form_octal), /* for 'o' */ \
358 REF (form_hexa), /* for 'X', 'x' */ \
359 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
360 REF (form_character), /* for 'c' */ \
361 REF (form_string), /* for 's' */ \
362 REF (form_pointer), /* for 'p' */ \
363 REF (form_number), /* for 'n' */ \
364 REF (form_strerror) /* for 'm' */ \
367 #define STEP4_TABLE \
368 /* Step 4: processing format specifier. */ \
369 static const void *step4_jumps[25] = \
371 REF (form_unknown), \
372 REF (form_unknown), /* for ' ' */ \
373 REF (form_unknown), /* for '+' */ \
374 REF (form_unknown), /* for '-' */ \
375 REF (form_unknown), /* for '<hash>' */ \
376 REF (form_unknown), /* for '0' */ \
377 REF (form_unknown), /* for '\'' */ \
378 REF (form_unknown), /* for '*' */ \
379 REF (form_unknown), /* for '1'...'9' */ \
380 REF (form_unknown), /* for '.' */ \
381 REF (form_unknown), /* for 'h' */ \
382 REF (form_unknown), /* for 'l' */ \
383 REF (form_unknown), /* for 'L', 'q' */ \
384 REF (form_unknown), /* for 'Z' */ \
385 REF (form_percent), /* for '%' */ \
386 REF (form_integer), /* for 'd', 'i' */ \
387 REF (form_unsigned), /* for 'u' */ \
388 REF (form_octal), /* for 'o' */ \
389 REF (form_hexa), /* for 'X', 'x' */ \
390 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
391 REF (form_character), /* for 'c' */ \
392 REF (form_string), /* for 's' */ \
393 REF (form_pointer), /* for 'p' */ \
394 REF (form_number), /* for 'n' */ \
395 REF (form_strerror) /* for 'm' */ \
399 #define process_arg(fspec) \
400 /* Start real work. We know about all flag and modifiers and \
401 now process the wanted format specifier. */ \
402 LABEL (form_percent): \
403 /* Write a literal "%". */ \
404 outchar ('%'); \
405 break; \
407 LABEL (form_integer): \
408 /* Signed decimal integer. */ \
409 base = 10; \
411 if (is_longlong) \
413 long long int signed_number; \
415 signed_number = va_arg (ap, long long int); \
417 is_negative = signed_number < 0; \
418 number.longlong = is_negative ? (- signed_number) : signed_number; \
420 goto LABEL (longlong_number); \
422 else \
424 long int signed_number; \
426 if (is_long) \
427 signed_number = va_arg (ap, long int); \
428 else /* `short int' will be promoted to `int'. */ \
429 signed_number = va_arg (ap, int); \
431 is_negative = signed_number < 0; \
432 number.word = is_negative ? (- signed_number) : signed_number; \
434 goto LABEL (number); \
436 /* NOTREACHED */ \
438 LABEL (form_unsigned): \
439 /* Unsigned decimal integer. */ \
440 base = 10; \
441 goto LABEL (unsigned_number); \
442 /* NOTREACHED */ \
444 LABEL (form_octal): \
445 /* Unsigned octal integer. */ \
446 base = 8; \
447 goto LABEL (unsigned_number); \
448 /* NOTREACHED */ \
450 LABEL (form_hexa): \
451 /* Unsigned hexadecimal integer. */ \
452 base = 16; \
454 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
456 /* ANSI specifies the `+' and ` ' flags only for signed \
457 conversions. */ \
458 is_negative = 0; \
459 showsign = 0; \
460 space = 0; \
462 if (is_longlong) \
464 number.longlong = va_arg (ap, unsigned long long int); \
466 LABEL (longlong_number): \
467 if (prec < 0) \
468 /* Supply a default precision if none was given. */ \
469 prec = 1; \
470 else \
471 /* We have to take care for the '0' flag. If a precision \
472 is given it must be ignored. */ \
473 pad = ' '; \
475 /* If the precision is 0 and the number is 0 nothing has to \
476 be written for the number. */ \
477 if (prec == 0 && number.longlong == 0) \
478 string = workend; \
479 else \
481 /* Put the number in WORK. */ \
482 string = _itoa (number.longlong, workend + 1, base, \
483 spec == 'X'); \
484 string -= 1; \
485 if (group && grouping) \
486 string = group_number (string, workend, grouping, \
487 thousands_sep); \
489 /* Simply further test for num != 0. */ \
490 number.word = number.longlong != 0; \
492 else \
494 if (is_long) \
495 number.word = va_arg (ap, unsigned long int); \
496 else \
497 number.word = va_arg (ap, unsigned int); /* Promoted. */ \
499 LABEL (number): \
500 if (prec < 0) \
501 /* Supply a default precision if none was given. */ \
502 prec = 1; \
503 else \
504 /* We have to take care for the '0' flag. If a precision \
505 is given it must be ignored. */ \
506 pad = ' '; \
508 /* If the precision is 0 and the number is 0 nothing has to \
509 be written for the number. */ \
510 if (prec == 0 && number.word == 0) \
511 string = workend; \
512 else \
514 /* Put the number in WORK. */ \
515 string = _itoa_word (number.word, workend + 1, base, \
516 spec == 'X'); \
517 string -= 1; \
518 if (group && grouping) \
519 string = group_number (string, workend, grouping, \
520 thousands_sep); \
524 prec -= workend - string; \
526 if (prec > 0) \
527 /* Add zeros to the precision. */ \
528 while (prec-- > 0) \
529 *string-- = '0'; \
530 else if (number.word != 0 && alt && base == 8) \
531 /* Add octal marker. */ \
532 *string-- = '0'; \
534 if (!left) \
536 width -= workend - string; \
538 if (number.word != 0 && alt && base == 16) \
539 /* Account for 0X hex marker. */ \
540 width -= 2; \
542 if (is_negative || showsign || space) \
543 --width; \
545 if (pad == '0') \
547 while (width-- > 0) \
548 *string-- = '0'; \
550 if (number.word != 0 && alt && base == 16) \
552 *string-- = spec; \
553 *string-- = '0'; \
556 if (is_negative) \
557 *string-- = '-'; \
558 else if (showsign) \
559 *string-- = '+'; \
560 else if (space) \
561 *string-- = ' '; \
563 else \
565 if (number.word != 0 && alt && base == 16) \
567 *string-- = spec; \
568 *string-- = '0'; \
571 if (is_negative) \
572 *string-- = '-'; \
573 else if (showsign) \
574 *string-- = '+'; \
575 else if (space) \
576 *string-- = ' '; \
578 while (width-- > 0) \
579 *string-- = ' '; \
582 outstring (string + 1, workend - string); \
584 break; \
586 else \
588 if (number.word != 0 && alt && base == 16) \
590 *string-- = spec; \
591 *string-- = '0'; \
594 if (is_negative) \
595 *string-- = '-'; \
596 else if (showsign) \
597 *string-- = '+'; \
598 else if (space) \
599 *string-- = ' '; \
601 width -= workend - string; \
602 outstring (string + 1, workend - string); \
604 PAD (' '); \
605 break; \
608 LABEL (form_float): \
610 /* Floating-point number. This is handled by printf_fp.c. */ \
611 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
612 const void **const)); \
613 const void *ptr; \
614 int function_done; \
616 if (is_long_double) \
617 the_arg.pa_long_double = va_arg (ap, long double); \
618 else \
619 the_arg.pa_double = va_arg (ap, double); \
621 ptr = (const void *) &the_arg; \
623 if (fspec == NULL) \
625 struct printf_info info = { prec: prec, \
626 width: width, \
627 spec: spec, \
628 is_long_double: is_long_double, \
629 is_short: is_short, \
630 is_long: is_long, \
631 alt: alt, \
632 space: space, \
633 left: left, \
634 showsign: showsign, \
635 group: group, \
636 pad: pad }; \
638 function_done = __printf_fp (s, &info, &ptr); \
640 else \
641 function_done = __printf_fp (s, &fspec->info, &ptr); \
643 if (function_done < 0) \
644 /* Error in print handler. */ \
645 return -1; \
647 done += function_done; \
649 break; \
651 LABEL (form_character): \
652 /* Character. */ \
653 --width; /* Account for the character itself. */ \
654 if (!left) \
655 PAD (' '); \
656 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
657 if (left) \
658 PAD (' '); \
659 break; \
661 LABEL (form_string): \
663 size_t len; \
665 /* The string argument could in fact be `char *' or `wchar_t *'. \
666 But this should not make a difference here. */ \
667 string = (char *) va_arg (ap, const char *); \
669 /* Entry point for printing other strings. */ \
670 LABEL (print_string): \
672 if (string == NULL) \
674 /* Write "(null)" if there's space. */ \
675 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
677 string = (char *) null; \
678 len = sizeof (null) - 1; \
680 else \
682 string = (char *) ""; \
683 len = 0; \
686 else if (!is_long) \
688 if (prec != -1) \
690 /* Search for the end of the string, but don't search past \
691 the length specified by the precision. */ \
692 const char *end = memchr (string, '\0', prec); \
693 if (end) \
694 len = end - string; \
695 else \
696 len = prec; \
698 else \
699 len = strlen (string); \
701 else \
703 const wchar_t *s2 = (const wchar_t *) string; \
704 mbstate_t mbstate = 0; \
706 len = wcsrtombs (NULL, &s2, prec != -1 ? prec : UINT_MAX, \
707 &mbstate); \
708 if (len == (size_t) -1) \
709 /* Illegal wide-character string. */ \
710 return -1; \
712 s2 = (const wchar_t *) string; \
713 mbstate = 0; \
714 string = alloca (len + 1); \
715 (void) wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
716 &mbstate); \
719 if ((width -= len) < 0) \
721 outstring (string, len); \
722 break; \
725 if (!left) \
726 PAD (' '); \
727 outstring (string, len); \
728 if (left) \
729 PAD (' '); \
731 break; \
733 LABEL (form_pointer): \
734 /* Generic pointer. */ \
736 const void *ptr; \
737 ptr = va_arg (ap, void *); \
738 if (ptr != NULL) \
740 /* If the pointer is not NULL, write it as a %#x spec. */ \
741 base = 16; \
742 number.word = (unsigned long int) ptr; \
743 is_negative = 0; \
744 alt = 1; \
745 group = 0; \
746 spec = 'x'; \
747 goto LABEL (number); \
749 else \
751 /* Write "(nil)" for a nil pointer. */ \
752 string = (char *) "(nil)"; \
753 /* Make sure the full string "(nil)" is printed. */ \
754 if (prec < 5) \
755 prec = 5; \
756 is_long = 0; /* This is no wide-char string. */ \
757 goto LABEL (print_string); \
760 /* NOTREACHED */ \
762 LABEL (form_number): \
763 /* Answer the count of characters written. */ \
764 if (is_longlong) \
765 *(long long int *) va_arg (ap, void *) = done; \
766 else if (is_long) \
767 *(long int *) va_arg (ap, void *) = done; \
768 else if (!is_short) \
769 *(int *) va_arg (ap, void *) = done; \
770 else \
771 *(short int *) va_arg (ap, void *) = done; \
772 break; \
774 LABEL (form_strerror): \
775 /* Print description of error ERRNO. */ \
777 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
779 string = (char *) \
780 _strerror_internal (errno, work_buffer, sizeof work_buffer); \
782 is_long = 0; /* This is no wide-char string. */ \
783 goto LABEL (print_string)
786 /* Sanity check of arguments. */
787 ARGCHECK (s, format);
789 if (UNBUFFERED_P (s))
790 /* Use a helper function which will allocate a local temporary buffer
791 for the stream and then call us again. */
792 return buffered_vfprintf (s, format, ap);
794 /* Initialize local variables. */
795 done = 0;
796 grouping = (const char *) -1;
797 mbstate = 0;
798 ap_save = ap;
799 nspecs_done = 0;
801 /* Find the first format specifier. */
802 f = lead_str_end = find_spec (format, &mbstate);
804 /* Write the literal text before the first format. */
805 outstring ((const UCHAR_T *) format,
806 lead_str_end - (const UCHAR_T *) format);
808 /* If we only have to print a simple string, return now. */
809 if (*f == L_('\0'))
810 return done;
812 /* Process whole format string. */
815 #define REF(Name) &&do_##Name
816 #define LABEL(Name) do_##Name
817 STEP0_3_TABLE;
818 STEP4_TABLE;
820 int is_negative; /* Flag for negative number. */
821 union
823 unsigned long long int longlong;
824 unsigned long int word;
825 } number;
826 int base;
827 union printf_arg the_arg;
828 char *string; /* Pointer to argument string. */
829 int alt = 0; /* Alternate format. */
830 int space = 0; /* Use space prefix if no sign is needed. */
831 int left = 0; /* Left-justify output. */
832 int showsign = 0; /* Always begin with plus or minus sign. */
833 int group = 0; /* Print numbers according grouping rules. */
834 int is_long_double = 0; /* Argument is long double/ long long int. */
835 int is_short = 0; /* Argument is long int. */
836 int is_long = 0; /* Argument is short int. */
837 int width = 0; /* Width of output; 0 means none specified. */
838 int prec = -1; /* Precision of output; -1 means none specified. */
839 char pad = ' '; /* Padding character. */
840 CHAR_T spec;
842 /* Get current character in format string. */
843 JUMP (*++f, step0_jumps);
845 /* ' ' flag. */
846 LABEL (flag_space):
847 space = 1;
848 JUMP (*++f, step0_jumps);
850 /* '+' flag. */
851 LABEL (flag_plus):
852 showsign = 1;
853 JUMP (*++f, step0_jumps);
855 /* The '-' flag. */
856 LABEL (flag_minus):
857 left = 1;
858 pad = L_(' ');
859 JUMP (*++f, step0_jumps);
861 /* The '#' flag. */
862 LABEL (flag_hash):
863 alt = 1;
864 JUMP (*++f, step0_jumps);
866 /* The '0' flag. */
867 LABEL (flag_zero):
868 if (!left)
869 pad = L_('0');
870 JUMP (*++f, step0_jumps);
872 /* The '\'' flag. */
873 LABEL (flag_quote):
874 group = 1;
876 /* XXX Completely wrong. Use wctob. */
877 if (grouping == (const char *) -1)
879 /* Figure out the thousands separator character. */
880 if (mbtowc (&thousands_sep,
881 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
882 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
883 thousands_sep = (wchar_t)
884 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
885 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
886 if (*grouping == '\0' || *grouping == CHAR_MAX
887 || thousands_sep == L'\0')
888 grouping = NULL;
890 JUMP (*++f, step0_jumps);
892 /* Get width from argument. */
893 LABEL (width_asterics):
895 const UCHAR_T *tmp; /* Temporary value. */
897 tmp = ++f;
898 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
899 /* The width comes from a positional parameter. */
900 goto do_positional;
902 width = va_arg (ap, int);
904 /* Negative width means left justified. */
905 if (width < 0)
907 width = -width;
908 pad = L_(' ');
909 left = 1;
912 JUMP (*f, step1_jumps);
914 /* Given width in format string. */
915 LABEL (width):
916 width = read_int (&f);
917 if (*f == L_('$'))
918 /* Oh, oh. The argument comes from a positional parameter. */
919 goto do_positional;
920 JUMP (*f, step1_jumps);
922 LABEL (precision):
923 ++f;
924 if (*f == L_('*'))
926 const UCHAR_T *tmp; /* Temporary value. */
928 tmp = ++f;
929 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
930 /* The precision comes from a positional parameter. */
931 goto do_positional;
933 prec = va_arg (ap, int);
935 /* If the precision is negative the precision is omitted. */
936 if (prec < 0)
937 prec = -1;
939 else if (ISDIGIT (*f))
940 prec = read_int (&f);
941 else
942 prec = 0;
943 JUMP (*f, step2_jumps);
945 /* Process 'h' modifier. No other modifier is allowed to
946 follow. */
947 LABEL (mod_half):
948 is_short = 1;
949 JUMP (*++f, step4_jumps);
951 /* Process 'l' modifier. There might another 'l' follow. */
952 LABEL (mod_long):
953 is_long = 1;
954 JUMP (*++f, step3_jumps);
956 /* Process 'L', 'q', or 'll' modifier. No other modifier is
957 allowed to follow. */
958 LABEL (mod_longlong):
959 is_long_double = 1;
960 JUMP (*++f, step4_jumps);
962 LABEL (mod_size_t):
963 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
964 is_long = sizeof (size_t) > sizeof (unsigned int);
965 JUMP (*++f, step4_jumps);
968 /* Process current format. */
969 while (1)
971 process_arg (((struct printf_spec *) NULL));
973 LABEL (form_unknown):
974 if (spec == L_('\0'))
975 /* The format string ended before the specifier is complete. */
976 return -1;
978 /* If we are in the fast loop force entering the complicated
979 one. */
980 goto do_positional;
983 /* Look for next format specifier. */
984 f = find_spec ((end_of_spec = ++f), &mbstate);
986 /* Write the following constant string. */
987 outstring (end_of_spec, f - end_of_spec);
989 while (*f != L_('\0'));
991 /* We processed the whole format without any positional parameters. */
992 return done;
994 /* Here starts the more complex loop to handle positional parameters. */
995 do_positional:
997 /* Array with information about the needed arguments. This has to
998 be dynamically extendable. */
999 size_t nspecs = 0;
1000 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1001 struct printf_spec *specs
1002 = alloca (nspecs_max * sizeof (struct printf_spec));
1004 /* The number of arguments the format string requests. This will
1005 determine the size of the array needed to store the argument
1006 attributes. */
1007 size_t nargs = 0;
1008 int *args_type;
1009 union printf_arg *args_value;
1011 /* Positional parameters refer to arguments directly. This could
1012 also determine the maximum number of arguments. Track the
1013 maximum number. */
1014 size_t max_ref_arg = 0;
1016 /* Just a counter. */
1017 int cnt;
1020 if (grouping == (const char *) -1)
1022 /* XXX Use wctob. But this is incompatible for now. */
1023 /* Figure out the thousands separator character. */
1024 if (mbtowc (&thousands_sep,
1025 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1026 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1027 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1028 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1029 if (*grouping == '\0' || *grouping == CHAR_MAX
1030 || thousands_sep == L'\0')
1031 grouping = NULL;
1034 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1036 if (nspecs >= nspecs_max)
1038 /* Extend the array of format specifiers. */
1039 struct printf_spec *old = specs;
1041 nspecs_max *= 2;
1042 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1044 if (specs == &old[nspecs])
1045 /* Stack grows up, OLD was the last thing allocated;
1046 extend it. */
1047 nspecs_max += nspecs_max / 2;
1048 else
1050 /* Copy the old array's elements to the new space. */
1051 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1052 if (old == &specs[nspecs])
1053 /* Stack grows down, OLD was just below the new
1054 SPECS. We can use that space when the new space
1055 runs out. */
1056 nspecs_max += nspecs_max / 2;
1060 /* Parse the format specifier. */
1061 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg, NULL);
1064 /* Determine the number of arguments the format string consumes. */
1065 nargs = MAX (nargs, max_ref_arg);
1067 /* Allocate memory for the argument descriptions. */
1068 args_type = alloca (nargs * sizeof (int));
1069 memset (args_type, 0, nargs * sizeof (int));
1070 args_value = alloca (nargs * sizeof (union printf_arg));
1072 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1073 still zero after this loop, format is invalid. For now we
1074 simply use 0 as the value. */
1076 /* Fill in the types of all the arguments. */
1077 for (cnt = 0; cnt < nspecs; ++cnt)
1079 /* If the width is determined by an argument this is an int. */
1080 if (specs[cnt].width_arg != -1)
1081 args_type[specs[cnt].width_arg] = PA_INT;
1083 /* If the precision is determined by an argument this is an int. */
1084 if (specs[cnt].prec_arg != -1)
1085 args_type[specs[cnt].prec_arg] = PA_INT;
1087 switch (specs[cnt].ndata_args)
1089 case 0: /* No arguments. */
1090 break;
1091 case 1: /* One argument; we already have the type. */
1092 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1093 break;
1094 default:
1095 /* We have more than one argument for this format spec.
1096 We must call the arginfo function again to determine
1097 all the types. */
1098 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1099 (&specs[cnt].info,
1100 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1101 break;
1105 /* Now we know all the types and the order. Fill in the argument
1106 values. */
1107 for (cnt = 0, ap = ap_save; cnt < nargs; ++cnt)
1108 switch (args_type[cnt])
1110 #define T(tag, mem, type) \
1111 case tag: \
1112 args_value[cnt].mem = va_arg (ap, type); \
1113 break
1115 T (PA_CHAR, pa_char, int); /* Promoted. */
1116 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1117 T (PA_INT, pa_int, int);
1118 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1119 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1120 T (PA_FLOAT, pa_float, double); /* Promoted. */
1121 T (PA_DOUBLE, pa_double, double);
1122 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1123 T (PA_STRING, pa_string, const char *);
1124 T (PA_POINTER, pa_pointer, void *);
1125 #undef T
1126 default:
1127 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1128 args_value[cnt].pa_pointer = va_arg (ap, void *);
1129 else
1130 args_value[cnt].pa_long_double = 0.0;
1131 break;
1134 /* Now walk through all format specifiers and process them. */
1135 for (; nspecs_done < nspecs; ++nspecs_done)
1137 #undef REF
1138 #define REF(Name) &&do2_##Name
1139 #undef LABEL
1140 #define LABEL(Name) do2_##Name
1141 STEP4_TABLE;
1143 int is_negative;
1144 union
1146 unsigned long long int longlong;
1147 unsigned long int word;
1148 } number;
1149 int base;
1150 union printf_arg the_arg;
1151 char *string; /* Pointer to argument string. */
1153 /* Fill variables from values in struct. */
1154 int alt = specs[nspecs_done].info.alt;
1155 int space = specs[nspecs_done].info.space;
1156 int left = specs[nspecs_done].info.left;
1157 int showsign = specs[nspecs_done].info.showsign;
1158 int group = specs[nspecs_done].info.group;
1159 int is_long_double = specs[nspecs_done].info.is_long_double;
1160 int is_short = specs[nspecs_done].info.is_short;
1161 int is_long = specs[nspecs_done].info.is_long;
1162 int width = specs[nspecs_done].info.width;
1163 int prec = specs[nspecs_done].info.prec;
1164 char pad = specs[nspecs_done].info.pad;
1165 CHAR_T spec = specs[nspecs_done].info.spec;
1167 /* Fill in last information. */
1168 if (specs[nspecs_done].width_arg != -1)
1170 /* Extract the field width from an argument. */
1171 specs[nspecs_done].info.width =
1172 args_value[specs[nspecs_done].width_arg].pa_int;
1174 if (specs[nspecs_done].info.width < 0)
1175 /* If the width value is negative left justification is
1176 selected and the value is taken as being positive. */
1178 specs[nspecs_done].info.width *= -1;
1179 left = specs[nspecs_done].info.left = 1;
1181 width = specs[nspecs_done].info.width;
1184 if (specs[nspecs_done].prec_arg != -1)
1186 /* Extract the precision from an argument. */
1187 specs[nspecs_done].info.prec =
1188 args_value[specs[nspecs_done].prec_arg].pa_int;
1190 if (specs[nspecs_done].info.prec < 0)
1191 /* If the precision is negative the precision is
1192 omitted. */
1193 specs[nspecs_done].info.prec = -1;
1195 prec = specs[nspecs_done].info.prec;
1198 /* Process format specifiers. */
1199 while (1)
1201 JUMP (spec, step4_jumps);
1203 process_arg ((&specs[nspecs_done]));
1205 LABEL (form_unknown):
1207 extern printf_function **__printf_function_table;
1208 int function_done;
1209 printf_function *function;
1210 unsigned int i;
1211 const void **ptr;
1213 function =
1214 (__printf_function_table == NULL ? NULL :
1215 __printf_function_table[specs[nspecs_done].info.spec]);
1217 if (function == NULL)
1218 function = &printf_unknown;
1220 ptr = alloca (specs[nspecs_done].ndata_args
1221 * sizeof (const void *));
1223 /* Fill in an array of pointers to the argument values. */
1224 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1225 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1227 /* Call the function. */
1228 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1230 /* If an error occured we don't have information about #
1231 of chars. */
1232 if (function_done < 0)
1233 return -1;
1235 done += function_done;
1237 break;
1240 /* Write the following constant string. */
1241 outstring (specs[nspecs_done].end_of_fmt,
1242 specs[nspecs_done].next_fmt
1243 - specs[nspecs_done].end_of_fmt);
1247 return done;
1250 #ifdef USE_IN_LIBIO
1251 # undef vfprintf
1252 # ifdef strong_alias
1253 /* This is for glibc. */
1254 strong_alias (_IO_vfprintf, vfprintf);
1255 # else
1256 # if defined __ELF__ || defined __GNU_LIBRARY__
1257 # include <gnu-stabs.h>
1258 # ifdef weak_alias
1259 weak_alias (_IO_vfprintf, vfprintf);
1260 # endif
1261 # endif
1262 # endif
1263 #endif
1265 /* Handle an unknown format specifier. This prints out a canonicalized
1266 representation of the format spec itself. */
1267 static int
1268 printf_unknown (FILE *s, const struct printf_info *info,
1269 const void *const *args)
1272 int done = 0;
1273 char work_buffer[BUFSIZ];
1274 register char *w;
1276 outchar ('%');
1278 if (info->alt)
1279 outchar ('#');
1280 if (info->group)
1281 outchar ('\'');
1282 if (info->showsign)
1283 outchar ('+');
1284 else if (info->space)
1285 outchar (' ');
1286 if (info->left)
1287 outchar ('-');
1288 if (info->pad == '0')
1289 outchar ('0');
1291 if (info->width != 0)
1293 w = _itoa_word (info->width, workend + 1, 10, 0);
1294 while (++w <= workend)
1295 outchar (*w);
1298 if (info->prec != -1)
1300 outchar ('.');
1301 w = _itoa_word (info->prec, workend + 1, 10, 0);
1302 while (++w <= workend)
1303 outchar (*w);
1306 if (info->spec != '\0')
1307 outchar (info->spec);
1309 return done;
1312 /* Group the digits according to the grouping rules of the current locale.
1313 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1314 static char *
1315 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1316 wchar_t thousands_sep)
1318 int len;
1319 char *src, *s;
1321 /* We treat all negative values like CHAR_MAX. */
1323 if (*grouping == CHAR_MAX || *grouping < 0)
1324 /* No grouping should be done. */
1325 return w;
1327 len = *grouping;
1329 /* Copy existing string so that nothing gets overwritten. */
1330 src = (char *) alloca (rear_ptr - w);
1331 memcpy (src, w + 1, rear_ptr - w);
1332 s = &src[rear_ptr - w - 1];
1333 w = rear_ptr;
1335 /* Process all characters in the string. */
1336 while (s >= src)
1338 *w-- = *s--;
1340 if (--len == 0 && s >= src)
1342 /* A new group begins. */
1343 *w-- = thousands_sep;
1345 len = *grouping++;
1346 if (*grouping == '\0')
1347 /* The previous grouping repeats ad infinitum. */
1348 --grouping;
1349 else if (*grouping == CHAR_MAX || *grouping < 0)
1351 /* No further grouping to be done.
1352 Copy the rest of the number. */
1354 *w-- = *s--;
1355 while (s >= src);
1356 break;
1360 return w;
1363 #ifdef USE_IN_LIBIO
1364 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1365 struct helper_file
1367 struct _IO_FILE_plus _f;
1368 _IO_FILE *_put_stream;
1371 static int
1372 _IO_helper_overflow (_IO_FILE *s, int c)
1374 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1375 int used = s->_IO_write_ptr - s->_IO_write_base;
1376 if (used)
1378 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1379 s->_IO_write_ptr -= written;
1381 return _IO_putc (c, s);
1384 static const struct _IO_jump_t _IO_helper_jumps =
1386 JUMP_INIT_DUMMY,
1387 JUMP_INIT (finish, _IO_default_finish),
1388 JUMP_INIT (overflow, _IO_helper_overflow),
1389 JUMP_INIT (underflow, _IO_default_underflow),
1390 JUMP_INIT (uflow, _IO_default_uflow),
1391 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1392 JUMP_INIT (xsputn, _IO_default_xsputn),
1393 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1394 JUMP_INIT (seekoff, _IO_default_seekoff),
1395 JUMP_INIT (seekpos, _IO_default_seekpos),
1396 JUMP_INIT (setbuf, _IO_default_setbuf),
1397 JUMP_INIT (sync, _IO_default_sync),
1398 JUMP_INIT (doallocate, _IO_default_doallocate),
1399 JUMP_INIT (read, _IO_default_read),
1400 JUMP_INIT (write, _IO_default_write),
1401 JUMP_INIT (seek, _IO_default_seek),
1402 JUMP_INIT (close, _IO_default_close),
1403 JUMP_INIT (stat, _IO_default_stat)
1406 static int
1407 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1408 _IO_va_list args)
1410 char buf[_IO_BUFSIZ];
1411 struct helper_file helper;
1412 register _IO_FILE *hp = (_IO_FILE *) &helper;
1413 int result, to_flush;
1415 /* Initialize helper. */
1416 helper._put_stream = s;
1417 hp->_IO_write_base = buf;
1418 hp->_IO_write_ptr = buf;
1419 hp->_IO_write_end = buf + sizeof buf;
1420 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1421 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1423 /* Now print to helper instead. */
1424 result = _IO_vfprintf (hp, format, args);
1426 /* Now flush anything from the helper to the S. */
1427 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1429 if (_IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1430 return -1;
1433 return result;
1436 #else /* !USE_IN_LIBIO */
1438 static int
1439 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1441 char buf[BUFSIZ];
1442 int result;
1444 s->__bufp = s->__buffer = buf;
1445 s->__bufsize = sizeof buf;
1446 s->__put_limit = s->__buffer + s->__bufsize;
1447 s->__get_limit = s->__buffer;
1449 /* Now use buffer to print. */
1450 result = vfprintf (s, format, args);
1452 if (fflush (s) == EOF)
1453 result = -1;
1454 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1455 s->__bufsize = 0;
1457 return result;
1460 /* Pads string with given number of a specified character.
1461 This code is taken from iopadn.c of the GNU I/O library. */
1462 #define PADSIZE 16
1463 static const CHAR_T blanks[PADSIZE] =
1464 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1465 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1466 static const CHAR_T zeroes[PADSIZE] =
1467 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1468 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1470 ssize_t
1471 #ifndef COMPILE_WPRINTF
1472 __printf_pad (FILE *s, char pad, size_t count)
1473 #else
1474 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1475 #endif
1477 const CHAR_T *padptr;
1478 register size_t i;
1480 padptr = pad == L_(' ') ? blanks : zeroes;
1482 for (i = count; i >= PADSIZE; i -= PADSIZE)
1483 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1484 return -1;
1485 if (i > 0)
1486 if (PUT (s, padptr, i) != i)
1487 return -1;
1489 return count;
1491 #undef PADSIZE
1492 #endif /* USE_IN_LIBIO */