Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site hadron.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c Subject: Re: Need strnlen(). Message-ID: <74@hadron.UUCP> Date: Sat, 16-Nov-85 15:44:48 EST Article-I.D.: hadron.74 Posted: Sat Nov 16 15:44:48 1985 Date-Received: Mon, 18-Nov-85 07:56:34 EST References: <207@a.sei.cmu.edu> <6691@boring.UUCP> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 24 In article <6691@boring.UUCP> jack@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@seismo.{CSS.GOV,ARPA,UUCP}