Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Aug 1997 20:23:26 +1000 (EST)
From:      Darren Reed <avalon@coombs.anu.edu.au>
To:        karpen@ocean.campus.luth.se (Mikael Karpberg)
Cc:        avalon@coombs.anu.edu.au, hackers@FreeBSD.ORG
Subject:   Re: creating man 9f
Message-ID:  <199708111023.DAA25927@hub.freebsd.org>
In-Reply-To: <199708101505.RAA12760@ocean.campus.luth.se> from "Mikael Karpberg" at Aug 10, 97 05:05:55 pm

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199708111023.DAA25927>