From owner-freebsd-hackers Tue Feb 20 9:29:41 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from news.lucky.net (news.lucky.net [193.193.193.102]) by hub.freebsd.org (Postfix) with ESMTP id 94C3537B491 for ; Tue, 20 Feb 2001 09:29:33 -0800 (PST) (envelope-from news@news.ntu-kpi.kiev.ua) Received: (from mail@localhost) by news.lucky.net (8.Who.Cares/8.Who.Cares) id TLI20173 for freebsd-hackers@freebsd.org; Tue, 20 Feb 2001 19:29:29 +0200 (envelope-from news@news.ntu-kpi.kiev.ua) From: "Andrey Simonenko" To: freebsd-hackers@freebsd.org Subject: Re: Staticaly allocated buffers in library. Is it correct? Date: Tue, 20 Feb 2001 18:14:42 +0300 Organization: NTUU "KPI" Message-ID: <96u5ao$70r$1@igloo.uran.net.ua> References: <200102192046.f1JKkl738082@earth.backplane.com> X-Trace: igloo.uran.net.ua 982685848 7195 10.18.54.109 (20 Feb 2001 16:17:28 GMT) X-Complaints-To: newsmaster@news.ntu-kpi.kiev.ua X-Newsreader: Microsoft Outlook Express 5.00.2014.211 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Let's look at implementation of getaddrinfo(3) function (there are some functions more which do the same way). We can find source for this function in /usr/src/lib/libc/net/getaddrinfo.c file. This functions in some case reads /etc/hosts file and try to find out there host name. getaddrinfo(3) calls some functions and function _gethtent() tries to read line by linefrom /etc/hosts file: static struct addrinfo * _gethtent(hostf, name, pai) FILE *hostf; const char *name; const struct addrinfo *pai; { char *p; char *cp, *tname, *cname; struct addrinfo hints, *res0, *res; int error; const char *addr; char hostbuf[8*1024]; again: if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) return (NULL); if (*p == '#') goto again; if (!(cp = strpbrk(p, "#\n"))) goto again; *cp = '\0'; if (!(cp = strpbrk(p, " \t"))) goto again; *cp++ = '\0'; We can see if line is bigger than 8k, then _gethtent() reads until the end of line. In most case this function doesn't find needed host name in such lines, but in some case it can find part of line as correct host name and tries to fetch IP address, but it also will not work, because we lose beginning of line when "goto again". This code can be simply rewriten as loop with fgets(), strlen()/strchr() and realloc(), but it causes speed lost in this function. Also I understand that 8k for line in /etc/hosts is enough and should not be problem for most of _real life_ situations. Matt Dillon wrote in message news:200102192046.f1JKkl738082@earth.backplane.com... > :> fgets() with the proper length limitation, using a statically allocated > :> buffer is not a big deal. Most configuration files couldn't have long > :> lines and still be legal anyway. > : > :Note that the classical loop > : while (fgets(buf, n, fp) != NULL) { > : tokenize(buf, args...); > : ... > : } > :may have problems if the line is too long, so one needs to detect it by > :looking for the '\n'. if none is found, then one can either abort on error > :or ignore the line. In the latter case, you need to read the remaining chars > :so that the next fgets won't get them. > : > :regards, > :mouss > > Yes, but we are talking about simple stupid config files here. Programs > which actually tokenize an input stream typically do not use fgets(). > Tokenizers either use [f]lex, [f]getc(), read() (and handle the buffering > themselves), or mmap(). > To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message