- while (*fmt != '\0') {
- if (*fmt == '%') {
- static char dummy[] = "";
- PRINTF state = Flags;
- char *pval = dummy; /* avoid const-cast */
- double fval = 0.0;
- int done = FALSE;
- int ival = 0;
- int prec = -1;
- int type = 0;
- int used = 0;
- int width = -1;
- size_t f = 0;
-
- format[f++] = *fmt;
- while (*++fmt != '\0' && len >= 0 && !done) {
- format[f++] = *fmt;
-
- if (isdigit(*fmt)) {
- int num = *fmt - '0';
- if (state == Flags && num != 0)
- state = Width;
- if (state == Width) {
- if (width < 0)
- width = 0;
- width = (width * 10) + num;
- } else if (state == Prec) {
- if (prec < 0)
- prec = 0;
- prec = (prec * 10) + num;
- }
- } else if (*fmt == '*') {
- VA_INTGR(int);
- if (state == Flags)
- state = Width;
- if (state == Width) {
- width = ival;
- } else if (state == Prec) {
- prec = ival;
- }
- sprintf(&format[--f], "%d", ival);
- f = strlen(format);
- } else if (isalpha(*fmt)) {
- done = TRUE;
- switch (*fmt) {
- case 'Z': /* FALLTHRU */
- case 'h': /* FALLTHRU */
- case 'l': /* FALLTHRU */
- done = FALSE;
- type = *fmt;
- break;
- case 'i': /* FALLTHRU */
- case 'd': /* FALLTHRU */
- case 'u': /* FALLTHRU */
- case 'x': /* FALLTHRU */
- case 'X': /* FALLTHRU */
- if (type == 'l')
- VA_INTGR(long);
- else if (type == 'Z')
- VA_INTGR(size_t);
- else
- VA_INTGR(int);
- used = 'i';
- break;
- case 'f': /* FALLTHRU */
- case 'e': /* FALLTHRU */
- case 'E': /* FALLTHRU */
- case 'g': /* FALLTHRU */
- case 'G': /* FALLTHRU */
- VA_FLOAT(double);
- used = 'f';
- break;
- case 'c':
- VA_INTGR(int);
- used = 'i';
- break;
- case 's':
- VA_POINT(char *);
- if (prec < 0)
- prec = strlen(pval);
- if (prec > (int)length) {
- length = length + prec;
- buffer = typeRealloc(char, length, buffer);
- if (buffer == 0) {
- free(format);
- return -1;
- }
- }
- used = 'p';
- break;
- case 'p':
- VA_POINT(void *);
- used = 'p';
- break;
- case 'n':
- VA_POINT(int *);
- used = 0;
- break;
- default:
- break;
- }
- } else if (*fmt == '.') {
- state = Prec;
- } else if (*fmt == '%') {
- done = TRUE;
- used = 'p';
- }
- }
- format[f] = '\0';
- switch (used) {
- case 'i':
- sprintf(buffer, format, ival);
- break;
- case 'f':
- sprintf(buffer, format, fval);
- break;
- default:
- sprintf(buffer, format, pval);
- break;
+ if (fmt == 0 || *fmt == '\0')
+ return 0;
+ fmt_len = strlen(fmt) + 1;
+ if ((format = typeMalloc(char, fmt_len)) == 0)
+ return -1;
+ if ((buffer = typeMalloc(char, length)) == 0) {
+ free(format);
+ return -1;
+ }
+
+ while (*fmt != '\0') {
+ if (*fmt == '%') {
+ static char dummy[] = "";
+ PRINTF state = Flags;
+ char *pval = dummy; /* avoid const-cast */
+ double fval = 0.0;
+ int done = FALSE;
+ int ival = 0;
+ int prec = -1;
+ int type = 0;
+ int used = 0;
+ int width = -1;
+ size_t f = 0;
+
+ format[f++] = *fmt;
+ while (*++fmt != '\0' && len >= 0 && !done) {
+ format[f++] = *fmt;
+
+ if (isdigit(UChar(*fmt))) {
+ int num = *fmt - '0';
+ if (state == Flags && num != 0)
+ state = Width;
+ if (state == Width) {
+ if (width < 0)
+ width = 0;
+ width = (width * 10) + num;
+ } else if (state == Prec) {
+ if (prec < 0)
+ prec = 0;
+ prec = (prec * 10) + num;
+ }
+ } else if (*fmt == '*') {
+ VA_INTGR(int);
+ if (state == Flags)
+ state = Width;
+ if (state == Width) {
+ width = ival;
+ } else if (state == Prec) {
+ prec = ival;
+ }
+ _nc_SPRINTF(fmt_arg,
+ _nc_SLIMIT(sizeof(fmt_arg))
+ "%d", ival);
+ fmt_len += strlen(fmt_arg);
+ if ((format = _nc_doalloc(format, fmt_len)) == 0) {
+ free(buffer);
+ return -1;
+ }
+ --f;
+ _nc_STRCPY(&format[f], fmt_arg, fmt_len - f);
+ f = strlen(format);
+ } else if (isalpha(UChar(*fmt))) {
+ done = TRUE;
+ switch (*fmt) {
+ case 'Z': /* FALLTHRU */
+ case 'h': /* FALLTHRU */
+ case 'l': /* FALLTHRU */
+ done = FALSE;
+ type = *fmt;
+ break;
+ case 'i': /* FALLTHRU */
+ case 'd': /* FALLTHRU */
+ case 'u': /* FALLTHRU */
+ case 'x': /* FALLTHRU */
+ case 'X': /* FALLTHRU */
+ if (type == 'l')
+ VA_INTGR(long);
+ else if (type == 'Z')
+ VA_INTGR(size_t);
+ else
+ VA_INTGR(int);
+ used = 'i';
+ break;
+ case 'f': /* FALLTHRU */
+ case 'e': /* FALLTHRU */
+ case 'E': /* FALLTHRU */
+ case 'g': /* FALLTHRU */
+ case 'G': /* FALLTHRU */
+ VA_FLOAT(double);
+ used = 'f';
+ break;
+ case 'c':
+ VA_INTGR(int);
+ used = 'i';
+ break;
+ case 's':
+ VA_POINT(char *);
+ if (prec < 0)
+ prec = (int) strlen(pval);
+ if (prec > (int) length) {
+ length = length + (size_t) prec;
+ buffer = typeRealloc(char, length, buffer);
+ if (buffer == 0) {
+ free(format);
+ return -1;
+ }