From owner-freebsd-questions@FreeBSD.ORG Mon Apr 14 23:26:43 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 D5FD91065670 for ; Mon, 14 Apr 2008 23:26:43 +0000 (UTC) (envelope-from fbsd.questions@rachie.is-a-geek.net) Received: from snoogles.rachie.is-a-geek.net (rachie.is-a-geek.net [66.230.99.27]) by mx1.freebsd.org (Postfix) with ESMTP id A30178FC1C for ; Mon, 14 Apr 2008 23:26:43 +0000 (UTC) (envelope-from fbsd.questions@rachie.is-a-geek.net) Received: from localhost (localhost [127.0.0.1]) by snoogles.rachie.is-a-geek.net (Postfix) with ESMTP id AFD671CC91; Mon, 14 Apr 2008 15:26:42 -0800 (AKDT) From: Mel To: freebsd-questions@freebsd.org Date: Tue, 15 Apr 2008 01:26:40 +0200 User-Agent: KMail/1.9.7 References: <539c60b90804141543h29b3c2f4y8c38694821d67d95@mail.gmail.com> <877if0qbnz.fsf@kobe.laptop> In-Reply-To: <877if0qbnz.fsf@kobe.laptop> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200804150126.40980.fbsd.questions@rachie.is-a-geek.net> Cc: Giorgos Keramidas , stevefranks@ieee.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:26:43 -0000 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))); 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. -- Mel Problem with today's modular software: they start with the modules and never get to the software part.