Re: question about change in inet_ntoa.c



Giorgos Keramidas wrote:
On 2008-02-23 02:37, ithilgore <ithilgore.fbsd@xxxxxxxxx> wrote:
ithilgore wrote:
I was looking at the differences between some old FreeBSD code
and the one of 7.0-RC1 and was wondering about a change
in inet_ntoa.c

/***** 7.0-RC1 **************/

sprintf(buf, "%d.%d.%d.%d",
ucp[0] & 0xff,
ucp[1] & 0xff,
ucp[2] & 0xff,
ucp[3] & 0xff);


/****** 4.11-RELEASE ***********/


static const char fmt[] = "%u.%u.%u%u";
if ((size_t)snprintf(dst, size, fmt, src[0], src[1], src[2], src[3])
>= size) {
....
....

Was there a specific purpose of changing the more easy and simple way
of %u instead of the combination of %d and and-ing with 0xff ??
It essentially gives the same result but increases overhead (i think) in the more
recent version.
I just noticed I made a mistake. The second code is libc's version of
inet_ntoa. But the question still counts. Why not use the plain
simpler version of libc ?

I don't see ucp[] in RELENG_6, RELENG_7 or CURRENT. Where did you get
the version shown as `7.0-RC1' above?


I got the source code from the ftp.freebsd.org and I just downloaded 7.0-RC3 to be certain.
Both in 7.0-RC1 and in 7.0-RC3 in src/sys/libkern/ is the following code of inet_ntoa.c :

#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/sys/libkern/inet_ntoa.c,v 1.6 2005/01/07 00:24:32 imp Exp $");

#include <sys/param.h>
#include <sys/systm.h>

#include <netinet/in.h>

char *
inet_ntoa(struct in_addr ina)
{
static char buf[4*sizeof "123"];
unsigned char *ucp = (unsigned char *)&ina;

sprintf(buf, "%d.%d.%d.%d",
ucp[0] & 0xff,
ucp[1] & 0xff,
ucp[2] & 0xff,
ucp[3] & 0xff);
return buf;
}

.....followed by the reentrant version of inet_ntoa : inet_ntoa_r


On the other hand, in version 4.11 RELEASE in /usr/src/lib/libc/net/inet_ntoa.c &
inet_ntop.c (actually it is inet_ntop.c code but with the same functionality) which is
called by inet_ntoa there is :

static const char *
inet_ntop4(src, dst, size)
const u_char *src;
char *dst;
size_t size;
{
static const char fmt[] = "%u.%u.%u.%u";

if ((size_t)snprintf(dst, size, fmt, src[0], src[1], src[2], src[3])
>= size) {
errno = ENOSPC;
return (NULL);
}
return (dst);
}






_______________________________________________
freebsd-net@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-net
To unsubscribe, send any mail to "freebsd-net-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: tower of hanoi, recursion
    ... The following program satifies a chapter quiz ... public static void hanoi(int top, char src, char mid, char dst) { ...
    (comp.lang.java.help)
  • Re: to free() or not to free() in lex/yacc
    ... Berk Birand wrote: ... char *copy_name{ ... return dst; ... I never actually thought of using strdup for this problem. ...
    (comp.lang.c)
  • About contrib/smbfs/lib/smb/nls.c
    ... There's a problem of mount_smb when mounting a share with Chinese Big5 ... nls_str_upper(char *dst, const char *src) ...
    (freebsd-questions)
  • Re: pointer questions
    ... char* (pointer to char), and dst is initialized with that value. ... Whether this makes sense depends on the type of src: ...
    (comp.lang.c)
  • Different ways
    ... replacement is larger then the whole array or the whole word!? ... So there must be ways to increase the size of the destination string. ... and return dst ... char newString; ...
    (comp.lang.c)