From decwrl!elroy.jpl.nasa.gov!jarthur!uunet!allbery Sun Mar 25 18:44:13 PST 1990
Article 1460 of comp.sources.misc:
Path: decwrl!elroy.jpl.nasa.gov!jarthur!uunet!allbery
From: viktor@cabbage.princeton.edu (Viktor Dukhovni)
Newsgroups: comp.sources.misc
Subject: v11i085: Fast inet_ntoa
Message-ID: <82381@uunet.UU.NET>
Date: 26 Mar 90 00:17:46 GMT
Sender: allbery@uunet.UU.NET
Lines: 96
Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)

Posting-number: Volume 11, Issue 85
Submitted-by: viktor@cabbage.princeton.edu (Viktor Dukhovni)
Archive-name: inet-ntoa/part01

	I had to write one,  and it turned out to be ~2.5x faster than the
library version on a sun4,  so here it is for those doing a LOT of
inet_ntoa calls (the named-xfer program is the only one I can think of that
will benefit from this measurably,  a Sun-4/370 took 64s to do a million
calls through the library,  and 24s with the version below).

-- 
	Viktor Dukhovni <viktor@math.princeton.edu>	: ARPA
		<...!uunet!princeton!math!viktor>	: UUCP
	Fine Hall, Washington Rd., Princeton, NJ 08544  : US-Post
		+1-(609)-258-5792		 	: VOICE

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  in.c
# Wrapped by viktor@cabbage on Sun Mar 25 17:01:05 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'in.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'in.c'\"
else
echo shar: Extracting \"'in.c'\" \(968 characters\)
sed "s/^X//" >'in.c' <<'END_OF_FILE'
X#include <sys/types.h>
X#include <netinet/in.h>
X
X#define digit(ptr,byte,pow,incr) \
X	\
X	ptr += incr; \
X	*ptr = '0' + (q=byte/pow) ; \
X	byte -= q*pow ; \
X	incr |= q != 0
X
X#define dobyte(ptr,byte,incr) \
X	incr = 0; \
X	digit(ptr,byte,100,incr) ; \
X	digit(ptr,byte,10,incr) ; \
X	digit(ptr,byte,1,incr) ; \
X	*++ptr = '.';
X
Xchar *
Xinet_ntoa(in)
X	struct in_addr in;
X{
X	static u_char buf[16];
X	register u_char b1=in.s_net;
X	register u_char b2=in.s_host;
X	register u_char b3=in.s_lh;
X	register u_char b4=in.s_impno;
X	register u_char *p = buf;
X	register u_char q, r, i;
X	
X	dobyte(p,b1,i) ; ++p;
X	dobyte(p,b2,i) ; ++p;
X	dobyte(p,b3,i) ; ++p;
X	dobyte(p,b4,i) ; *p = '\0' ;
X
X	return((char *)buf) ;
X}
X
X#ifdef TIMEIT
Xmain(argc,argv)
X  char **argv;
X{
X	long n=(argc>1) ? strtol(argv[1],0,0) : 1000000 ;
X	struct in_addr in;
X	char *s;
X
X	in.s_addr = 0x80706050;
X	while (--n) {
X		s=inet_ntoa(in) ;
X	}
X	/* Could be fast but wrong!  Should print 128.112.96.80 */
X	printf("%s\n",s) ;
X}
X#endif
END_OF_FILE
if test 968 -ne `wc -c <'in.c'`; then
    echo shar: \"'in.c'\" unpacked with wrong size!
fi
# end of 'in.c'
fi
echo shar: End of shell archive.
exit 0
	Viktor Dukhovni <viktor@math.princeton.edu>	: ARPA
		<...!uunet!princeton!math!viktor>	: UUCP
	Fine Hall, Washington Rd., Princeton, NJ 08544  : US-Post
		+1-(609)-258-5792		 	: VOICE


