int main(int argc, char *argv[]) { size_t len; int end, rval; char *format, *fmt, *start; #ifndef SHELL setlocale(LC_ALL, ""); #endif /* * We may not use getopt(3) because calling * "printf -f%s oo" may not result in an invalid * option error. * However common usage and other implementations seem * to indicate that we need to allow -- as a discardable * option separator. */ if (argc > 1 && strcmp(argv[1], "--") == 0) { argc--; argv++; } if (argc < 2) { usage(); return (1); } argv++; #ifdef SHELL INTOFF; #endif /* * Basic algorithm is to scan the format string for conversion * specifications -- once one is found, find out if the field * width or precision is a '*'; if it is, gather up value. Note, * format strings are reused as necessary to use up the provided * arguments, arguments of zero/null string are provided to use * up the format string. */ fmt = format = *argv; escape(fmt, 1, &len); /* backslash interpretation */ rval = end = 0; gargv = ++argv; for (;;) { maxargv = gargv; myargv = gargv; for (myargc = 0; gargv[myargc]; myargc++) /* nop */; start = fmt; while (fmt < format + len) { if (fmt[0] == '%') { fwrite(start, 1, fmt - start, stdout); if (fmt[1] == '%') { /* %% prints a % */ putchar('%'); fmt += 2; } else { fmt = printf_doformat(fmt, &rval); if (fmt == NULL || fmt == end_fmt) { #ifdef SHELL INTON; #endif return (fmt == NULL ? 1 : rval); } end = 0; } start = fmt; } else fmt++; if (gargv > maxargv) maxargv = gargv; } gargv = maxargv; if (end == 1) { warnx("missing format character"); #ifdef SHELL INTON; #endif return (1); } fwrite(start, 1, fmt - start, stdout); if (!*gargv) { #ifdef SHELL INTON; #endif return (rval); } /* Restart at the beginning of the format string. */ fmt = format; end = 1; } /* NOTREACHED */ }
int32_t printfcmd(int32_t argc, cstring_t argv[]) { size_t len; int32_t chopped, end, rval; cstring_t format; cstring_t fmt; cstring_t start; nextopt(""); argc -= (int32_t)(intptr_t)(argptr - argv); argv = argptr; if (argc < 1) { usage(); return (1); } INTOFF; /* * Basic algorithm is to scan the format string for conversion * specifications -- once one is found, find out if the field * width or precision is a '*'; if it is, gather up value. Note, * format strings are reused as necessary to use up the provided * arguments, arguments of zero/null string are provided to use * up the format string. */ fmt = format = *argv; chopped = escape(fmt, 1, &len); /* backslash interpretation */ rval = end = 0; gargv = ++argv; for (;;) { start = fmt; while (fmt < format + len) { if (fmt[0] == '%') { fwrite(start, 1, fmt - start, stdout); if (fmt[1] == '%') { /* %% prints a % */ putchar('%'); fmt += 2; } else { fmt = printf_doformat(fmt, &rval); if (fmt == NULL) { INTON; return (1); } end = 0; } start = fmt; } else fmt++; } if (end == 1) { warnx("missing format character"); INTON; return (1); } fwrite(start, 1, fmt - start, stdout); if (chopped || !*gargv) { INTON; return (rval); } /* Restart at the beginning of the format string. */ fmt = format; end = 1; } /* NOTREACHED */ }
int main(int argc, char *argv[]) { size_t len; int end, rval; char *format, *fmt, *start; #ifndef SHELL int ch; (void) setlocale(LC_ALL, ""); #endif #ifdef SHELL nextopt(""); argc -= argptr - argv; argv = argptr; #else while ((ch = getopt(argc, argv, "")) != -1) switch (ch) { case '?': default: usage(); return (1); } argc -= optind; argv += optind; #endif if (argc < 1) { usage(); return (1); } #ifdef SHELL INTOFF; #endif /* * Basic algorithm is to scan the format string for conversion * specifications -- once one is found, find out if the field * width or precision is a '*'; if it is, gather up value. Note, * format strings are reused as necessary to use up the provided * arguments, arguments of zero/null string are provided to use * up the format string. */ fmt = format = *argv; escape(fmt, 1, &len); /* backslash interpretation */ rval = end = 0; gargv = ++argv; for (;;) { maxargv = gargv; myargv = gargv; for (myargc = 0; gargv[myargc]; myargc++) /* nop */; start = fmt; while (fmt < format + len) { if (fmt[0] == '%') { fwrite(start, 1, fmt - start, stdout); if (fmt[1] == '%') { /* %% prints a % */ putchar('%'); fmt += 2; } else { fmt = printf_doformat(fmt, &rval); if (fmt == NULL || fmt == end_fmt) { #ifdef SHELL INTON; #endif return (fmt == NULL ? 1 : rval); } end = 0; } start = fmt; } else fmt++; if (gargv > maxargv) maxargv = gargv; } gargv = maxargv; if (end == 1) { warnx("missing format character"); #ifdef SHELL INTON; #endif return (1); } fwrite(start, 1, fmt - start, stdout); if (!*gargv) { #ifdef SHELL INTON; #endif return (rval); } /* Restart at the beginning of the format string. */ fmt = format; end = 1; } /* NOTREACHED */ }