[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
getaddrinfo.c: don't require snprintf; new function: shorttostr
From: |
Jim Meyering |
Subject: |
getaddrinfo.c: don't require snprintf; new function: shorttostr |
Date: |
Sat, 21 Oct 2006 10:39:24 +0200 |
Paul, Simon,
Any objection to the changes below?
I noticed that getaddrinfo uses snprintf to perform a simple
short-to-string conversion. For such a simple job, snprintf
and its dependent, vasnprintf, are overkill.
Instead, how about using a new shorttostr function instead?
Why a new function?
If there were an inttostr *function*, I would have been happy
to use that, but there isn't. Substituting any of the other *tostr
functions would require using an 8-byte type on some systems,
which makes them too wasteful for me.
2006-10-21 Jim Meyering <address@hidden>
* lib/getaddrinfo.c (getnameinfo): Use new lightweight shorttostr,
in place of snprintf.
* modules/inttostr (Files): Add lib/shorttostr.c.
* lib/shorttostr.c (inttostr): New file/function.
* lib/inttostr.h (shorttostr): Declare.
* m4/inttostr.m4: Add AC_LIBOBJ([shorttostr]).
Index: lib/getaddrinfo.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/getaddrinfo.c,v
retrieving revision 1.16
diff -u -r1.16 getaddrinfo.c
--- lib/getaddrinfo.c 18 Sep 2006 18:07:05 -0000 1.16
+++ lib/getaddrinfo.c 21 Oct 2006 08:37:42 -0000
@@ -39,7 +39,7 @@
#define N_(String) String
#include "inet_ntop.h"
-#include "snprintf.h"
+#include "intprops.h"
#include "strdup.h"
/* BeOS has AF_INET, but not PF_INET. */
@@ -405,10 +405,14 @@
#if HAVE_IPV6
case AF_INET6:
#endif
- if (snprintf (service, servicelen, "%d",
- ntohs (((const struct sockaddr_in *) sa)->sin_port))
- + 1 > servicelen)
- return EAI_OVERFLOW;
+ {
+ short int port = ntohs (((const struct sockaddr_in *) sa)->sin_port);
+ char buf[INT_BUFSIZE_BOUND (port)];
+ char const *s = shorttostr (port, buf);
+ if (strlen (s) + 1 > servicelen)
+ return EAI_OVERFLOW;
+ memcpy (service, s, strlen (s) + 1);
+ }
break;
}
Index: lib/shorttostr.c
===================================================================
RCS file: lib/shorttostr.c
diff -N lib/shorttostr.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/shorttostr.c 21 Oct 2006 08:37:42 -0000
@@ -0,0 +1,3 @@
+#define inttostr shorttostr
+#define inttype short int
+#include "inttostr.c"
Index: lib/inttostr.h
===================================================================
RCS file: /sources/gnulib/gnulib/lib/inttostr.h,v
retrieving revision 1.7
diff -u -r1.7 inttostr.h
--- lib/inttostr.h 21 Aug 2006 06:11:26 -0000 1.7
+++ lib/inttostr.h 21 Oct 2006 08:37:42 -0000
@@ -27,3 +27,4 @@
char *offtostr (off_t, char *);
char *imaxtostr (intmax_t, char *);
char *umaxtostr (uintmax_t, char *);
+char *shorttostr (short int, char *);
Index: modules/inttostr
===================================================================
RCS file: /sources/gnulib/gnulib/modules/inttostr,v
retrieving revision 1.11
diff -u -r1.11 inttostr
--- modules/inttostr 13 Oct 2006 12:40:23 -0000 1.11
+++ modules/inttostr 21 Oct 2006 08:37:42 -0000
@@ -7,6 +7,7 @@
lib/inttostr.h
lib/offtostr.c
lib/umaxtostr.c
+lib/shorttostr.c
m4/inttostr.m4
Depends-on:
Index: m4/inttostr.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/inttostr.m4,v
retrieving revision 1.5
diff -u -r1.5 inttostr.m4
--- m4/inttostr.m4 21 Aug 2006 06:11:26 -0000 1.5
+++ m4/inttostr.m4 21 Oct 2006 08:37:42 -0000
@@ -1,4 +1,4 @@
-#serial 6
+#serial 7
dnl Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -9,6 +9,7 @@
AC_LIBOBJ([imaxtostr])
AC_LIBOBJ([offtostr])
AC_LIBOBJ([umaxtostr])
+ AC_LIBOBJ([shorttostr])
gl_PREREQ_INTTOSTR
gl_PREREQ_IMAXTOSTR
@@ -30,3 +31,6 @@
# Prerequisites of lib/umaxtostr.c.
AC_DEFUN([gl_PREREQ_UMAXTOSTR], [:])
+
+# Prerequisites of lib/shorttostr.c.
+AC_DEFUN([gl_PREREQ_SHORTTOSTR], [:])
Index: MODULES.html.sh
===================================================================
RCS file: /sources/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.152
diff -u -r1.152 MODULES.html.sh
--- MODULES.html.sh 16 Oct 2006 11:55:35 -0000 1.152
+++ MODULES.html.sh 21 Oct 2006 08:37:42 -0000
@@ -1802,6 +1802,7 @@
func_begin_table
func_module intprops
func_module inttostr
+ func_module shorttostr
func_module xstrtoimax
func_module xstrtoumax
func_end_table