Need strnlen().
Joseph S. D. Yao
jsdy at hadron.UUCP
Sun Nov 17 06:44:48 AEST 1985
In article <6691 at boring.UUCP> jack at boring.UUCP (Jack Jansen) writes:
>I *know* these should be avoided, but I would still prefer to see
>code like
> len = strnlen(buffer,BUFSIZ);
>above
> if( (len=strlen(buffer))>BUFSIZ) len=BUFSIZ;
>which will fail at some unknown point in the future, long
>after everyone who knew the code has passed away.......
That code should have failed immediately! If it ever got to a code
review, it should have been shot dead immediately. You NEVER
assume that there's something beyond where you put it, unless ...
you put it there. ;-)
#define NUL '\0' /* Now, class, tell me why this is not NULL. */
char buffer[BUFLEN+1];
...
fread(buffer, 1, BUFLEN, infile);
buffer[BUFLEN] = NUL;
len = strlen(buffer); /* if you must */
--
Joe Yao hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}
More information about the Comp.lang.c
mailing list