123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- #define WANT_FLOAT_ERROR 0
- #define WANT_GNU_ERRNO 0
- #define _GNU_SOURCE
- #define _STDIO_UTILITY
- #include <stdio.h>
- #include <stdarg.h>
- #include <limits.h>
- #include <string.h>
- #include <errno.h>
- #include <ctype.h>
- #define __PRINTF_INFO_NO_BITFIELD
- #include <printf.h>
- #ifdef __STDIO_THREADSAFE
- #include <pthread.h>
- #endif
- #if defined(__UCLIBC_HAS_FLOATS__)
- extern size_t _dtostr(FILE * fp, long double x, struct printf_info *info);
- #endif
- enum {
- FLAG_PLUS = 0,
- FLAG_MINUS_LJUSTIFY,
- FLAG_HASH,
- FLAG_0_PAD,
- FLAG_SPACE,
- };
- static const char spec[] = "+-#0 ";
- static const char qual[] = "hlLq";
- #if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR
- static const char dbl_err[] = "<DOUBLE>";
- #endif
- #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
- static const char u_spec[] = "%nbopxXudicsfgGeEaA";
- #else
- static const char u_spec[] = "%nbopxXudics";
- #endif
- static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
- int vfprintf(FILE * __restrict op, register const char * __restrict fmt,
- va_list ap)
- {
- int i, cnt, lval, len;
- register char *p;
- const char *fmt0;
- int preci, width;
- #define upcase i
- int radix, dpoint ;
- char tmp[65];
- char flag[sizeof(spec)];
- __STDIO_THREADLOCK(op);
- cnt = 0;
- while (*fmt) {
- if (*fmt == '%') {
- fmt0 = fmt;
- ++fmt;
- width = -1;
- preci = -5;
- radix = 10;
- dpoint = 0;
- #if INT_MAX != LONG_MAX
- lval = 0;
- #else
- lval = 1;
- #endif
-
- for (p =(char *) spec ; *p ; p++) {
- flag[p-spec] = '\0';
- }
- flag[FLAG_0_PAD] = ' ';
-
- for (p = (char *)spec ; *p ; ) {
- if (*fmt == *p) {
- flag[p-spec] = *fmt++;
- p = (char *)spec;
- } else {
- p++;
- }
- }
-
- if (!flag[FLAG_PLUS]) {
- flag[FLAG_PLUS] = flag[FLAG_SPACE];
- }
-
- do {
- if (*fmt == '.') {
- ++fmt;
- dpoint = 1;
- }
- if (*fmt == '*') {
- ++fmt;
- i = va_arg(ap, int);
- } else {
- for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
- i = (i * 10) + (*fmt - '0');
- }
- }
- if (dpoint) {
- preci = i;
- if (i<0) {
- preci = -5;
- }
- } else {
- width = i;
- if (i<0) {
- width = -i;
- flag[FLAG_MINUS_LJUSTIFY] = 1;
- }
- }
- } while ((*fmt == '.') && !dpoint );
-
- for (p = (char *) qual ; *p ; p++) {
- if (*p == *fmt) {
- lval = p - qual;
- ++fmt;
- if ((*p == 'l') && (*fmt == *p)) {
- ++lval;
- ++fmt;
- }
- }
- }
- #if WANT_GNU_ERRNO
- if (*fmt == 'm') {
- flag[FLAG_PLUS] = '\0';
- flag[FLAG_0_PAD] = ' ';
- p = _glibc_strerror_r(errno, tmp, sizeof(tmp));
- goto print;
- }
- #endif
-
- for (p = (char *) u_spec ; *p ; p++) {
- if (*fmt != *p) continue;
- if (p-u_spec < 1) {
- goto charout;
- }
- if (p-u_spec < 2) {
- *(va_arg(ap, int *)) = cnt;
- goto nextfmt;
- }
- if (p-u_spec < 8) {
- radix = u_radix[p-u_spec-2];
- upcase = ((*p == 'x') ? __UIM_LOWER : __UIM_UPPER);
- if (*p == 'p') {
- lval = (sizeof(char *) == sizeof(long));
- upcase = __UIM_LOWER;
- flag[FLAG_HASH] = 'p';
- }
- p = _uintmaxtostr((tmp + sizeof(tmp) - 1),
- ((lval>1)
- ? va_arg(ap, uintmax_t)
- : (uintmax_t)
- va_arg(ap, unsigned long)),
- radix, upcase);
- flag[FLAG_PLUS] = '\0';
- if (*p != '0') {
- if (flag[FLAG_HASH]) {
- if (radix == 8) {
- *--p = '0';
- } else if (radix != 10) {
- flag[FLAG_PLUS] = '0';
- *--p = 'b';
- if (radix == 16) {
- *p = 'x';
- if (*fmt == 'X') {
- *p = 'X';
- }
- }
- }
- }
- } else if (flag[FLAG_HASH] == 'p') {
- p = "(nil)";
- }
- } else if (p-u_spec < 10) {
- p = _uintmaxtostr((tmp + sizeof(tmp) - 1),
- ((lval>1)
- ? va_arg(ap, uintmax_t)
- : (uintmax_t) ((intmax_t)
- va_arg(ap, long))),
- -radix, upcase);
- } else if (p-u_spec < 12) {
- flag[FLAG_PLUS] = '\0';
- flag[FLAG_0_PAD] = ' ';
- if (*p == 'c') {
- p = tmp;
- *p = va_arg(ap, int);
-
- len = 1;
- goto print_len_set;
- } else {
- p = va_arg(ap, char *);
- if (!p) {
- p = "(null)";
- preci = 6;
- } else {
- if (preci < 0) {
- preci = INT_MAX;
- }
- }
- len = strnlen(p, preci);
- goto print_len_set;
- }
- #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
- } else if (p-u_spec < 27) {
- #endif
- #if defined(__UCLIBC_HAS_FLOATS__)
- struct printf_info info;
- if (preci < 0) {
- preci = 6;
- }
- info.width = width;
- info.prec = preci;
- info.spec = *fmt;
- info.pad = flag[FLAG_0_PAD];
- info._flags = 0;
- if (flag[FLAG_PLUS] == '+') {
- PRINT_INFO_SET_FLAG(&info,showsign);
- } else if (flag[FLAG_PLUS] == ' ') {
- PRINT_INFO_SET_FLAG(&info,space);
- }
- if (flag[FLAG_HASH]) {
- PRINT_INFO_SET_FLAG(&info,alt);
- }
- if (flag[FLAG_MINUS_LJUSTIFY]) {
- PRINT_INFO_SET_FLAG(&info,left);
- }
- cnt += _dtostr(op,
- ((lval > 1)
- ? va_arg(ap, long double)
- : (long double) va_arg(ap, double)),
- &info);
- goto nextfmt;
- #elif WANT_FLOAT_ERROR
- (void) ((lval > 1) ? va_arg(ap, long double)
- : va_arg(ap, double));
- p = (char *) dbl_err;
- #endif
- }
- #if WANT_GNU_ERRNO
- print:
- #endif
- {
-
- len = strnlen(p, SIZE_MAX);
- print_len_set:
- if ((*p == '-')
- #if WANT_GNU_ERRNO
- && (*fmt != 'm')
- #endif
- && (*fmt != 's')) {
- flag[FLAG_PLUS] = *p++;
- --len;
- }
- if (flag[FLAG_PLUS]) {
- ++len;
- ++preci;
- if (flag[FLAG_PLUS] == '0') {
- ++preci;
- }
- }
- if (preci >= 0) {
- if ((*fmt == 's')
- #if WANT_GNU_ERRNO
- || (*fmt == 'm')
- #endif
- ) {
- if (len > preci) {
- len = preci;
- } else {
- preci = len;
- }
- }
- preci -= len;
- if (preci < 0) {
- preci = 0;
- }
- width -= preci;
- }
- width -= len;
- if (width < 0) {
- width = 0;
- }
- if (preci < 0) {
- preci = 0;
- if (!flag[FLAG_MINUS_LJUSTIFY]
-
- && (flag[FLAG_0_PAD] == '0')) {
- preci = width;
- width = 0;
- }
- }
- while (width + len + preci) {
- unsigned char ch;
-
- if ((!len && !preci)
- || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
- ch = ' ';
- --width;
- } else if (flag[FLAG_PLUS]) {
- ch = flag[FLAG_PLUS];
- if (flag[FLAG_PLUS]=='0') {
- flag[FLAG_PLUS] = *p++;
- } else {
- flag[FLAG_PLUS] = '\0';
- }
- --len;
- } else if (preci) {
- ch = '0';
- --preci;
- } else {
- ch = *p++;
- --len;
- }
- ++cnt;
- putc(ch, op);
- }
- }
- goto nextfmt;
- }
- fmt = fmt0;
- }
- charout:
- ++cnt;
- putc(*fmt, op);
- nextfmt:
- ++fmt;
- }
- i = (__FERROR(op)) ? -1 : cnt;
- __STDIO_THREADLOCK(op);
- return i;
- }
|