summaryrefslogtreecommitdiff
path: root/src/printf.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1997-04-06 19:30:07 +0000
committerJim Meyering <jim@meyering.net>1997-04-06 19:30:07 +0000
commit39b444e420d9684c7cebed67a694842bda69107e (patch)
tree06556c347832386a3af54a7f9747205a44b1787a /src/printf.c
parent00ea088ccd66beef38355469925293db01a2bd94 (diff)
downloadcoreutils-39b444e420d9684c7cebed67a694842bda69107e.tar.xz
Reorder functions to obviate forward decls.
Diffstat (limited to 'src/printf.c')
-rw-r--r--src/printf.c447
1 files changed, 219 insertions, 228 deletions
diff --git a/src/printf.c b/src/printf.c
index 10581a2a4..62460bbae 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -59,21 +59,12 @@ unsigned long strtoul ();
#endif
#define isodigit(c) ((c) >= '0' && (c) <= '7')
-#define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
+#define hextobin(c) ((c) >= 'a' && (c) <= 'f' ? (c) - 'a' + 10 : \
+ (c) >= 'A' && (c) <= 'F' ? (c) - 'A' + 10 : (c) - '0')
#define octtobin(c) ((c) - '0')
char *xmalloc ();
-static double xstrtod __P ((char *s));
-static int print_esc __P ((char *escstart));
-static int print_formatted __P ((char *format, int argc, char **argv));
-static long xstrtol __P ((char *s));
-static unsigned long xstrtoul __P ((char *s));
-static void print_direc __P ((char *start, size_t length, int field_width, int precision, char *argument));
-static void print_esc_char __P ((int c));
-static void print_esc_string __P ((char *str));
-static void verify __P ((char *s, char *end));
-
/* The value to return to the calling program. */
static int exit_status;
@@ -125,199 +116,58 @@ ARGUMENTs converted to proper type first. Variable widths are handled.\n\
exit (status);
}
-int
-main (int argc, char **argv)
+static void
+verify (const char *s, const char *end)
{
- char *format;
- int args_used;
-
- program_name = argv[0];
- setlocale (LC_ALL, "");
- bindtextdomain (PACKAGE, LOCALEDIR);
- textdomain (PACKAGE);
-
- exit_status = 0;
-
- /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
- if (getenv ("POSIXLY_CORRECT") == NULL)
- parse_long_options (argc, argv, "printf", GNU_PACKAGE, VERSION, usage);
-
- if (argc == 1)
+ if (errno)
{
- fprintf (stderr, _("Usage: %s format [argument...]\n"), program_name);
- exit (1);
+ error (0, errno, "%s", s);
+ exit_status = 1;
}
-
- format = argv[1];
- argc -= 2;
- argv += 2;
-
- do
+ else if (*end)
{
- args_used = print_formatted (format, argc, argv);
- argc -= args_used;
- argv += args_used;
+ if (s == end)
+ error (0, 0, _("%s: expected a numeric value"), s);
+ else
+ error (0, 0, _("%s: value not completely converted"), s);
+ exit_status = 1;
}
- while (args_used > 0 && argc > 0);
-
- if (argc > 0)
- error (0, 0, _("warning: excess arguments have been ignored"));
-
- exit (exit_status);
}
-/* Print the text in FORMAT, using ARGV (with ARGC elements) for
- arguments to any `%' directives.
- Return the number of elements of ARGV used. */
-
-static int
-print_formatted (char *format, int argc, char **argv)
+static unsigned long
+xstrtoul (const char *s)
{
- int save_argc = argc; /* Preserve original value. */
- char *f; /* Pointer into `format'. */
- char *direc_start; /* Start of % directive. */
- size_t direc_length; /* Length of % directive. */
- int field_width; /* Arg to first '*', or -1 if none. */
- int precision; /* Arg to second '*', or -1 if none. */
-
- for (f = format; *f; ++f)
- {
- switch (*f)
- {
- case '%':
- direc_start = f++;
- direc_length = 1;
- field_width = precision = -1;
- if (*f == '%')
- {
- putchar ('%');
- break;
- }
- if (*f == 'b')
- {
- if (argc > 0)
- {
- print_esc_string (*argv);
- ++argv;
- --argc;
- }
- break;
- }
- if (strchr ("-+ #", *f))
- {
- ++f;
- ++direc_length;
- }
- if (*f == '*')
- {
- ++f;
- ++direc_length;
- if (argc > 0)
- {
- field_width = xstrtoul (*argv);
- ++argv;
- --argc;
- }
- else
- field_width = 0;
- }
- else
- while (ISDIGIT (*f))
- {
- ++f;
- ++direc_length;
- }
- if (*f == '.')
- {
- ++f;
- ++direc_length;
- if (*f == '*')
- {
- ++f;
- ++direc_length;
- if (argc > 0)
- {
- precision = xstrtoul (*argv);
- ++argv;
- --argc;
- }
- else
- precision = 0;
- }
- else
- while (ISDIGIT (*f))
- {
- ++f;
- ++direc_length;
- }
- }
- if (*f == 'l' || *f == 'L' || *f == 'h')
- {
- ++f;
- ++direc_length;
- }
- if (!strchr ("diouxXfeEgGcs", *f))
- error (1, 0, _("%%%c: invalid directive"), *f);
- ++direc_length;
- if (argc > 0)
- {
- print_direc (direc_start, direc_length, field_width,
- precision, *argv);
- ++argv;
- --argc;
- }
- else
- print_direc (direc_start, direc_length, field_width,
- precision, "");
- break;
+ char *end;
+ unsigned long val;
- case '\\':
- f += print_esc (f);
- break;
+ errno = 0;
+ val = strtoul (s, &end, 0);
+ verify (s, end);
+ return val;
+}
- default:
- putchar (*f);
- }
- }
+static long
+xstrtol (const char *s)
+{
+ char *end;
+ long val;
- return save_argc - argc;
+ errno = 0;
+ val = strtol (s, &end, 0);
+ verify (s, end);
+ return val;
}
-/* Print a \ escape sequence starting at ESCSTART.
- Return the number of characters in the escape sequence
- besides the backslash. */
-
-static int
-print_esc (char *escstart)
+static double
+xstrtod (const char *s)
{
- register char *p = escstart + 1;
- int esc_value = 0; /* Value of \nnn escape. */
- int esc_length; /* Length of \nnn escape. */
+ char *end;
+ double val;
- /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
- if (*p == 'x')
- {
- for (esc_length = 0, ++p;
- esc_length < 3 && ISXDIGIT (*p);
- ++esc_length, ++p)
- esc_value = esc_value * 16 + hextobin (*p);
- if (esc_length == 0)
- error (1, 0, _("missing hexadecimal number in escape"));
- putchar (esc_value);
- }
- else if (*p == '0')
- {
- for (esc_length = 0, ++p;
- esc_length < 3 && isodigit (*p);
- ++esc_length, ++p)
- esc_value = esc_value * 8 + octtobin (*p);
- putchar (esc_value);
- }
- else if (strchr ("\"\\abcfnrtv", *p))
- print_esc_char (*p++);
- else
- error (1, 0, _("\\%c: invalid escape"), *p);
- return p - escstart - 1;
+ errno = 0;
+ val = strtod (s, &end);
+ verify (s, end);
+ return val;
}
/* Output a single-character \ escape. */
@@ -357,6 +207,43 @@ print_esc_char (int c)
}
}
+/* Print a \ escape sequence starting at ESCSTART.
+ Return the number of characters in the escape sequence
+ besides the backslash. */
+
+static int
+print_esc (char *escstart)
+{
+ register char *p = escstart + 1;
+ int esc_value = 0; /* Value of \nnn escape. */
+ int esc_length; /* Length of \nnn escape. */
+
+ /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
+ if (*p == 'x')
+ {
+ for (esc_length = 0, ++p;
+ esc_length < 3 && ISXDIGIT (*p);
+ ++esc_length, ++p)
+ esc_value = esc_value * 16 + hextobin (*p);
+ if (esc_length == 0)
+ error (1, 0, _("missing hexadecimal number in escape"));
+ putchar (esc_value);
+ }
+ else if (*p == '0')
+ {
+ for (esc_length = 0, ++p;
+ esc_length < 3 && isodigit (*p);
+ ++esc_length, ++p)
+ esc_value = esc_value * 8 + octtobin (*p);
+ putchar (esc_value);
+ }
+ else if (strchr ("\"\\abcfnrtv", *p))
+ print_esc_char (*p++);
+ else
+ error (1, 0, _("\\%c: invalid escape"), *p);
+ return p - escstart - 1;
+}
+
/* Print string STR, evaluating \ escapes. */
static void
@@ -469,56 +356,160 @@ print_direc (char *start, size_t length, int field_width, int precision, char *a
free (p);
}
-static unsigned long
-xstrtoul (char *s)
+/* Print the text in FORMAT, using ARGV (with ARGC elements) for
+ arguments to any `%' directives.
+ Return the number of elements of ARGV used. */
+
+static int
+print_formatted (char *format, int argc, char **argv)
{
- char *end;
- unsigned long val;
+ int save_argc = argc; /* Preserve original value. */
+ char *f; /* Pointer into `format'. */
+ char *direc_start; /* Start of % directive. */
+ size_t direc_length; /* Length of % directive. */
+ int field_width; /* Arg to first '*', or -1 if none. */
+ int precision; /* Arg to second '*', or -1 if none. */
- errno = 0;
- val = strtoul (s, &end, 0);
- verify (s, end);
- return val;
-}
+ for (f = format; *f; ++f)
+ {
+ switch (*f)
+ {
+ case '%':
+ direc_start = f++;
+ direc_length = 1;
+ field_width = precision = -1;
+ if (*f == '%')
+ {
+ putchar ('%');
+ break;
+ }
+ if (*f == 'b')
+ {
+ if (argc > 0)
+ {
+ print_esc_string (*argv);
+ ++argv;
+ --argc;
+ }
+ break;
+ }
+ if (strchr ("-+ #", *f))
+ {
+ ++f;
+ ++direc_length;
+ }
+ if (*f == '*')
+ {
+ ++f;
+ ++direc_length;
+ if (argc > 0)
+ {
+ field_width = xstrtoul (*argv);
+ ++argv;
+ --argc;
+ }
+ else
+ field_width = 0;
+ }
+ else
+ while (ISDIGIT (*f))
+ {
+ ++f;
+ ++direc_length;
+ }
+ if (*f == '.')
+ {
+ ++f;
+ ++direc_length;
+ if (*f == '*')
+ {
+ ++f;
+ ++direc_length;
+ if (argc > 0)
+ {
+ precision = xstrtoul (*argv);
+ ++argv;
+ --argc;
+ }
+ else
+ precision = 0;
+ }
+ else
+ while (ISDIGIT (*f))
+ {
+ ++f;
+ ++direc_length;
+ }
+ }
+ if (*f == 'l' || *f == 'L' || *f == 'h')
+ {
+ ++f;
+ ++direc_length;
+ }
+ if (!strchr ("diouxXfeEgGcs", *f))
+ error (1, 0, _("%%%c: invalid directive"), *f);
+ ++direc_length;
+ if (argc > 0)
+ {
+ print_direc (direc_start, direc_length, field_width,
+ precision, *argv);
+ ++argv;
+ --argc;
+ }
+ else
+ print_direc (direc_start, direc_length, field_width,
+ precision, "");
+ break;
-static long
-xstrtol (char *s)
-{
- char *end;
- long val;
+ case '\\':
+ f += print_esc (f);
+ break;
- errno = 0;
- val = strtol (s, &end, 0);
- verify (s, end);
- return val;
+ default:
+ putchar (*f);
+ }
+ }
+
+ return save_argc - argc;
}
-static double
-xstrtod (char *s)
+int
+main (int argc, char **argv)
{
- char *end;
- double val;
+ char *format;
+ int args_used;
- errno = 0;
- val = strtod (s, &end);
- verify (s, end);
- return val;
-}
+ program_name = argv[0];
+ setlocale (LC_ALL, "");
+ bindtextdomain (PACKAGE, LOCALEDIR);
+ textdomain (PACKAGE);
-static void
-verify (char *s, char *end)
-{
- if (errno)
+ exit_status = 0;
+
+ /* Don't recognize --help or --version if POSIXLY_CORRECT is set. */
+ if (getenv ("POSIXLY_CORRECT") == NULL)
+ parse_long_options (argc, argv, "printf", GNU_PACKAGE, VERSION, usage);
+
+ if (argc == 1)
{
- error (0, errno, "%s", s);
- exit_status = 1;
+ fprintf (stderr, _("Usage: %s format [argument...]\n"), program_name);
+ exit (1);
}
- else if (*end)
+
+ format = argv[1];
+ argc -= 2;
+ argv += 2;
+
+ do
{
- if (s == end)
- error (0, 0, _("%s: expected a numeric value"), s);
- else
- error (0, 0, _("%s: value not completely converted"), s);
- exit_status = 1;
+ args_used = print_formatted (format, argc, argv);
+ argc -= args_used;
+ argv += args_used;
}
+ while (args_used > 0 && argc > 0);
+
+ if (argc > 0)
+ error (0, 0, _("warning: excess arguments have been ignored"));
+
+ exit (exit_status);
}