[PATCH 2/2] getopt.c: use fputs()/fputc() instead of fprintf() to, save code space
Freddie Chopin
freddie_chopin@op.pl
Fri Nov 8 19:42:00 GMT 2013
This is another approach aiming to reduce code and RAM usage of
getopt(). Using fputs()/fputc() makes the function about 200 bytes
bigger than when using f[i]printf() (on ARMv7-M, Cortex-M3), but now
getopt() and family have zero dependency on heavy-weight formatting
functions.
Regards,
FCh
-------------- next part --------------
From 7b66269186dcd32c32cda4b9f67a336a0f626510 Mon Sep 17 00:00:00 2001
From: Freddie Chopin <freddie.chopin@gmail.com>
Date: Fri, 8 Nov 2013 20:35:00 +0100
Subject: [PATCH 2/2] getopt.c: use fputs()/fputc() instead of fprintf() to
save code space
---
newlib/libc/stdlib/getopt.c | 34 +++++++++++++++++++++-------------
1 file changed, 21 insertions(+), 13 deletions(-)
diff --git a/newlib/libc/stdlib/getopt.c b/newlib/libc/stdlib/getopt.c
index c2bd060..7cd4207 100644
--- a/newlib/libc/stdlib/getopt.c
+++ b/newlib/libc/stdlib/getopt.c
@@ -177,7 +177,9 @@ write_globals (struct getopt_data *data)
optwhere = data->optwhere;
}
-/* getopt_internal: the function that does all the dirty work */
+/* getopt_internal: the function that does all the dirty work
+ NOTE: to reduce the code and RAM footprint this function uses
+ fputs()/fputc() to do output to stderr instead of fprintf(). */
static int
getopt_internal (int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *longind, int only,
@@ -311,12 +313,14 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
{
/* we have ambiguous options */
if (data->opterr)
- fprintf (stderr, "%s: option `%s' is ambiguous "
- "(could be `--%s' or `--%s')\n",
- argv[0],
- argv[data->optind],
- longopts[longopt_match].name,
- longopts[optindex].name);
+ fputs (argv[0], stderr);
+ fputs (": option `", stderr);
+ fputs (argv[data->optind], stderr);
+ fputs ("' is ambiguous (could be `--", stderr);
+ fputs (longopts[longopt_match].name, stderr);
+ fputs ("' or `--", stderr);
+ fputs (longopts[optindex].name, stderr);
+ fputs ("')\n", stderr);
return (data->optopt = '?');
}
}
@@ -334,9 +338,10 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
{
/* couldn't find option in shortopts */
if (data->opterr)
- fprintf (stderr,
- "%s: invalid option -- `-%c'\n",
- argv[0], argv[data->optind][data->optwhere]);
+ fputs (argv[0], stderr);
+ fputs (": invalid option -- `-", stderr);
+ fputc (argv[data->optind][data->optwhere], stderr);
+ fputs ("'\n", stderr);
data->optwhere++;
if (argv[data->optind][data->optwhere] == '\0')
{
@@ -373,17 +378,20 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
{
if (data->opterr)
{
- fprintf (stderr, "%s: argument required for option `", argv[0]);
+ fputs (argv[0], stderr);
+ fputs (": argument required for option `-", stderr);
if (longopt_match >= 0)
{
- fprintf (stderr, "--%s'\n", longopts[longopt_match].name);
+ fputc ('-', stderr);
+ fputs (longopts[longopt_match].name, stderr);
data->optopt = initial_colon ? ':' : '\?';
}
else
{
- fprintf (stderr, "-%c'\n", *cp);
+ fputc (*cp, stderr);
data->optopt = *cp;
}
+ fputs ("'\n", stderr);
}
data->optind++;
return initial_colon ? ':' : '\?';
--
1.8.3.msysgit.0
More information about the Newlib
mailing list