[PATCH] getopt.c: use integer-only function fiprintf() to save code space

Freddie Chopin freddie_chopin@op.pl
Fri Nov 8 16:57:00 GMT 2013


Hello!

If you use getopt() (or any variant of this functionality) it pulls in 
fprintf() to do output to stderr in case of error. As getopt() does not 
deal with anything but strings or characters, it's much better to use 
fiprintf(), which is MUCH smaller than fprintf() - about 2.8kB for 
_vfiprintf_r() vs. 6.5kB for _vfiprintf_r(), not counting any support 
data/functions (like 4.5kB _dtoa_r()), ARMv7-M. If your program does not 
use fprintf() just using getopt increases the size of the executable by 
20kB (size of getopt() alone is ~2kB). Let's not forget RAM use, which 
is smaller in fiprintf().

Actually I think that the output could be done with fputs() and fputc() 
only, but to do that I'd first like your opinion - there's no actual 
formatting (like changing the width or whatever), just displaying. This 
would be a bit clumsy (2-7 calls to fputs()/fputc() in place of single 
f[i]printf()), but still much MUCH smaller.

Regards,
FCh
-------------- next part --------------
From a7caffb51e5b4ca48025f0975f0c98ca76a23313 Mon Sep 17 00:00:00 2001
From: Freddie Chopin <freddie.chopin@gmail.com>
Date: Fri, 8 Nov 2013 17:34:55 +0100
Subject: [PATCH] getopt.c: use integer-only function fiprintf() to save code
 space

---
 newlib/libc/stdlib/getopt.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/newlib/libc/stdlib/getopt.c b/newlib/libc/stdlib/getopt.c
index 2bea694..b358da0 100644
--- a/newlib/libc/stdlib/getopt.c
+++ b/newlib/libc/stdlib/getopt.c
@@ -311,7 +311,7 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
 		    {
 		      /* we have ambiguous options */
 		      if (data->opterr)
-			fprintf (stderr, "%s: option `%s' is ambiguous "
+			fiprintf (stderr, "%s: option `%s' is ambiguous "
 				 "(could be `--%s' or `--%s')\n",
 				 argv[0],
 				 argv[data->optind],
@@ -334,7 +334,7 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
 	{
 	  /* couldn't find option in shortopts */
 	  if (data->opterr)
-	    fprintf (stderr,
+	    fiprintf (stderr,
 		     "%s: invalid option -- `-%c'\n",
 		     argv[0], argv[data->optind][data->optwhere]);
 	  data->optwhere++;
@@ -373,15 +373,15 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
 	{
 	  if (data->opterr)
 	    {
-	      fprintf (stderr, "%s: argument required for option `", argv[0]);
+	      fiprintf (stderr, "%s: argument required for option `", argv[0]);
 	      if (longopt_match >= 0)
 		{
-		  fprintf (stderr, "--%s'\n", longopts[longopt_match].name);
+		  fiprintf (stderr, "--%s'\n", longopts[longopt_match].name);
 		  data->optopt = initial_colon ? ':' : '\?';
 		}
 	      else
 		{
-		  fprintf (stderr, "-%c'\n", *cp);
+		  fiprintf (stderr, "-%c'\n", *cp);
 		  data->optopt = *cp;
 		}
 	    }
-- 
1.8.3.msysgit.0



More information about the Newlib mailing list