char *strcat(), *strcpy(), *fgets();

Dave Decot decot at hpisod2.HP.COM
Sat Jun 25 13:20:19 AEST 1988


> Oh, I don't know about that.  Here is a real example from source code
> I happened to have open in another layer on my terminal:
> 
> 	(void)strcat(strcat(strcat(strcat(strcat(strcpy(fn,
> 							TargetDir
> 						       ),
>        						 target
> 						),
> 					  Slash
>   					 ),
>  				   approx
>    				  ),
>   			    Slash
>     			   ),
>    		     CCMAP
>      		    );

...and because the number of characters added to the string is not returned,
this statement has to examine the characters in TargetDir (which may be long)
five extra times; those in target four extra times; those in Slash three
extra times; those in approx two extra times; those in Slash one extra time.

This is much more efficiently (and readably!) rendered as:

     (void) sprintf(fn, "%s%s/%s/%s", TargetDir, target, approx, CCMAP);

...assuming that Slash is somebody's misguided idea of saving space for
strings consisting of "/".

While it's clear that we can't change the return value of strcat() because
of applications (such as the one above) that use it, there's nothing to
prevent adding more useful functions:

    char *strecpy(x, y)	  /* strcpy(x, y); returns pointer after end of y */
    char *x, *y;

    char *strecat(x, y)	  /* strcat(x, y); returns pointer after end of y */
    char *x, *y;

    int strlcpy(x, y)	  /* strcpy(x, y); returns strlen(y) */
    char *x, *y;

    int strlcat(x, y)	  /* strcat(x, y); returns strlen(y) */
    char *x, *y;

Dave Decot
hpda!decot



More information about the Comp.std.c mailing list