- register char *s;
- register char *t;
- register char *f;
- bool dolong;
-#ifdef HAS_QUAD
- bool doquad;
-#endif /* HAS_QUAD */
- char ch;
- register char *send;
- register SV *arg;
- char *xs;
- I32 xlen;
- I32 pre;
- I32 post;
- double value;
- STRLEN arglen;
-
- sv_setpv(sv,"");
- len--; /* don't count pattern string */
- t = s = SvPV(*sarg, arglen); /* XXX Don't know t is writeable */
- send = s + arglen;
- sarg++;
- for ( ; ; len--) {
-
- /*SUPPRESS 560*/
- if (len <= 0 || !(arg = *sarg++))
- arg = &sv_no;
-
- /*SUPPRESS 530*/
- for ( ; t < send && *t != '%'; t++) ;
- if (t >= send)
- break; /* end of run_format string, ignore extra args */
- f = t;
- *buf = '\0';
- xs = buf;
-#ifdef HAS_QUAD
- doquad =
-#endif /* HAS_QUAD */
- dolong = FALSE;
- pre = post = 0;
- for (t++; t < send; t++) {
- switch (*t) {
- default:
- ch = *(++t);
- *t = '\0';
- (void)sprintf(xs,f);
- len++, sarg--;
- xlen = strlen(xs);
- break;
- case 'n': case '*':
- croak("Use of %c in printf format not supported", *t);
-
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- case '.': case '#': case '-': case '+': case ' ':
- continue;
- case 'l':
-#ifdef HAS_QUAD
- if (dolong) {
- dolong = FALSE;
- doquad = TRUE;
- } else
-#endif
- dolong = TRUE;
- continue;
- case 'c':
- ch = *(++t);
- *t = '\0';
- xlen = SvIV(arg);
- if (strEQ(f,"%c")) { /* some printfs fail on null chars */
- *xs = xlen;
- xs[1] = '\0';
- xlen = 1;
- }
- else {
- (void)sprintf(xs,f,xlen);
- xlen = strlen(xs);
- }
- break;
- case 'D':
- dolong = TRUE;
- /* FALL THROUGH */
- case 'd':
- ch = *(++t);
- *t = '\0';
-#ifdef HAS_QUAD
- if (doquad)
- (void)sprintf(buf,s,(Quad_t)SvNV(arg));
- else
-#endif
- if (dolong)
- (void)sprintf(xs,f,(long)SvNV(arg));
- else
- (void)sprintf(xs,f,SvIV(arg));
- xlen = strlen(xs);
- break;
- case 'X': case 'O':
- dolong = TRUE;
- /* FALL THROUGH */
- case 'x': case 'o': case 'u':
- ch = *(++t);
- *t = '\0';
- value = SvNV(arg);
-#ifdef HAS_QUAD
- if (doquad)
- (void)sprintf(buf,s,(unsigned Quad_t)value);
- else
-#endif
- if (dolong)
- (void)sprintf(xs,f,U_L(value));
- else
- (void)sprintf(xs,f,U_I(value));
- xlen = strlen(xs);
- break;
- case 'E': case 'e': case 'f': case 'G': case 'g':
- ch = *(++t);
- *t = '\0';
- (void)sprintf(xs,f,SvNV(arg));
- xlen = strlen(xs);
- break;
- case 's':
- ch = *(++t);
- *t = '\0';
- xs = SvPV(arg, arglen);
- xlen = (I32)arglen;
- if (strEQ(f,"%s")) { /* some printfs fail on >128 chars */
- break; /* so handle simple cases */
- }
- else if (f[1] == '-') {
- char *mp = strchr(f, '.');
- I32 min = atoi(f+2);
-
- if (mp) {
- I32 max = atoi(mp+1);
-
- if (xlen > max)
- xlen = max;
- }
- if (xlen < min)
- post = min - xlen;
- break;
- }
- else if (isDIGIT(f[1])) {
- char *mp = strchr(f, '.');
- I32 min = atoi(f+1);
-
- if (mp) {
- I32 max = atoi(mp+1);
-
- if (xlen > max)
- xlen = max;
- }
- if (xlen < min)
- pre = min - xlen;
- break;
- }
- strcpy(tokenbuf+64,f); /* sprintf($s,...$s...) */
- *t = ch;
- (void)sprintf(buf,tokenbuf+64,xs);
- xs = buf;
- xlen = strlen(xs);
- break;
- }
- /* end of switch, copy results */
- *t = ch;
- if (xs == buf && xlen >= sizeof(buf)) { /* Ooops! */
- PerlIO_puts(PerlIO_stderr(),"panic: sprintf overflow - memory corrupted!\n");
- my_exit(1);
- }
- SvGROW(sv, SvCUR(sv) + (f - s) + xlen + 1 + pre + post);
- sv_catpvn(sv, s, f - s);
- if (pre) {
- repeatcpy(SvPVX(sv) + SvCUR(sv), " ", 1, pre);
- SvCUR(sv) += pre;
- }
- sv_catpvn(sv, xs, xlen);
- if (post) {
- repeatcpy(SvPVX(sv) + SvCUR(sv), " ", 1, post);
- SvCUR(sv) += post;
- }
- s = t;
- break; /* break from for loop */
- }
- }
- sv_catpvn(sv, s, t - s);