From owner-freebsd-questions@FreeBSD.ORG Mon Apr 14 23:55:16 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9E5FB1065672 for ; Mon, 14 Apr 2008 23:55:16 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id 0DAEB8FC0A for ; Mon, 14 Apr 2008 23:55:15 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (adsl118-36.kln.forthnet.gr [77.49.237.36]) (authenticated bits=128) by igloo.linux.gr (8.14.2/8.14.2/Debian-3) with ESMTP id m3ENsqDB025246 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 15 Apr 2008 02:54:58 +0300 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.2/8.14.2) with ESMTP id m3ENsqCH013468; Tue, 15 Apr 2008 02:54:52 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.2/8.14.2/Submit) id m3ENsogt013467; Tue, 15 Apr 2008 02:54:50 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: Mel References: <539c60b90804141543h29b3c2f4y8c38694821d67d95@mail.gmail.com> <877if0qbnz.fsf@kobe.laptop> <200804150126.40980.fbsd.questions@rachie.is-a-geek.net> Date: Tue, 15 Apr 2008 02:54:50 +0300 In-Reply-To: <200804150126.40980.fbsd.questions@rachie.is-a-geek.net> (Mel's message of "Tue, 15 Apr 2008 01:26:40 +0200") Message-ID: <87tzi4ouut.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-MailScanner-ID: m3ENsqDB025246 X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-3.903, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.50, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: stevefranks@ieee.org, freebsd-questions@freebsd.org Subject: Re: [?OT?] strndup exists in FreeBSD? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Apr 2008 23:55:16 -0000 On Tue, 15 Apr 2008 01:26:40 +0200, Mel wrote: > On Tuesday 15 April 2008 01:06:24 Giorgos Keramidas wrote: >> On Mon, 14 Apr 2008 15:43:24 -0700, "Steve Franks" > wrote: >> > I'm getting an undefined reference to strndup, so clearly there's a >> > header somewhere with it - doesn't seem to be in my default libc, >> > however on 7.0-amd64? >> >> I don't see an strndup() function in our libc. >> >> keramida@kobe:/usr/src/lib/libc/string$ grep ^strdup *.c >> strdup.c:strdup(str) >> keramida@kobe:/usr/src/lib/libc/string$ grep ^strndup *.c >> keramida@kobe:/usr/src/lib/libc/string$ >> >> While it seems like a cool function name, what's the point of having it? >> If you know how much you want to copy, it's trivial to allocate a buffer >> large enough and strlcpy() into it. If you don't know how much you want >> to copy, then strdup() is ok anyway :) > > It can be convenient for trickery: > char file[MAXPATHLEN]; > char *dir; > ... > dir = strndup(file, (strrchr(file, '/')-file))); I know, but I don't feel safe when I see code like this. All sorts of amusing things can happen when it's used this way and strrchr() returns NULL though, so it is safer to explicitly allocate buffers and check what's going on. > Or space optimization: > char *p, *path; > path = malloc(MAXPATHLEN); > (void)strlcpy(path, argv[i], MAXPATHLEN); > p = strndup(path, strlen(path)); > free(path); > > But, personally I prefer taking the long route or wasting a few bytes > over dual allocations. I can definitely agree with this part :)