From owner-freebsd-hackers Mon Aug 11 03:23:49 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id DAA25936 for hackers-outgoing; Mon, 11 Aug 1997 03:23:49 -0700 (PDT) Received: from cheops.anu.edu.au (avalon@cheops.anu.edu.au [150.203.76.24]) by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id DAA25927 for ; Mon, 11 Aug 1997 03:23:44 -0700 (PDT) Message-Id: <199708111023.DAA25927@hub.freebsd.org> Received: by cheops.anu.edu.au (1.37.109.16/16.2) id AA017925006; Mon, 11 Aug 1997 20:23:26 +1000 From: Darren Reed Subject: Re: creating man 9f To: karpen@ocean.campus.luth.se (Mikael Karpberg) Date: Mon, 11 Aug 1997 20:23:26 +1000 (EST) Cc: avalon@coombs.anu.edu.au, hackers@FreeBSD.ORG In-Reply-To: <199708101505.RAA12760@ocean.campus.luth.se> from "Mikael Karpberg" at Aug 10, 97 05:05:55 pm X-Mailer: ELM [version 2.4 PL23] Content-Type: text Sender: owner-freebsd-hackers@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk In some mail from Mikael Karpberg, sie said: > > Not really realted, but it's always annoyed me that all the strcat and strcpy > functions are so ineffective. They return a pointer to the buffer you > supply to the function (which is useless since you have it already) and > not a pointer to the trailing NUL. So if you want to add two strings you > use strcat twice, and thereby have the whole string walked through at least > too times too much. This always leads to me writing my own "xstrcpy()" or so, > which makes it possible to do: > > ptr = xstrcpy(buffer, "first "); > ptr = xstrcpy(ptr, "second"); > ptr = xstrcpy(ptr, "third"); > ..etc.. > > And having it an inline function you basically waste no CPU for meaningless > functioncalls and looping through something you've already looped through. > > My point is... shouldn't the kernel, at least, be written with efficiency > in mind? No matter if it's not that big of a deal (I guess the kernel is not > doing too much string handling, anyway). But I guess this is not the only > case of inefficient code. How about this: buffer[sizeof(buffer) - 1] = '\0'; len = strlen(strncpy(bufer, sizeof(buffer) - 1, userstring));A or strcat(strcpy(buffer, "first"), " second")); but it's not often done, as its not as easy to read as: buffer[sizeof(buffer) - 1] = '\0'; strncpy(bufer, sizeof(buffer) - 1, userstring); len = strlen(buffer); What would be cool is a varargs version of strcat: char *strvcat(char *dest, char *src1, ..., NULL) (which is sort of what you're doing above :) Darren