Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site wanginst.UUCP Path: utzoo!linus!wanginst!vishniac From: vishniac@wanginst.UUCP (Ephraim Vishniac) Newsgroups: net.lang.c Subject: Re: Need strnlen(). Message-ID: <1324@wanginst.UUCP> Date: Thu, 14-Nov-85 08:25:18 EST Article-I.D.: wanginst.1324 Posted: Thu Nov 14 08:25:18 1985 Date-Received: Fri, 15-Nov-85 07:28:31 EST References: <767@dcl-cs.UUCP> Organization: Wang Institute, Tyngsboro, Ma. 01879 Lines: 34 > I think that there should be a function called "strnlen" as follows: > > int strnlen (string, size) > char *string; > int size; > > where "size" is the maximum number of bytes in "string". > I agree that such a facility is needed, but I don't think it will ever be provided as "standard" C. The basic problem (or the C problem, if you prefer :-) is that C defines a representation for strings, but leaves the user to implement the operations. This allows one to cook up all sorts of invalid strings (such as the unterminated ones the original poster is worried about). To my mind, a string consists of three things: 1. The characters of the string; 2. The length of the string, either as such or encoded by marking the string; 3. The storage block where the string is located, which has its own attributes (alignment and size, to name two). C has no problem with the first (the characters are easy to access); some problems with the second (null termination is good for some purposes, rotten for others); and completely ignores the third. But: since this is C, you don't have to use the standard representation and functions. Just as I did when sufficiently burned, you can use your own representation and macros. Then the only problem is that nobody will use your modules, because they're "non-standard". -- Ephraim Vishniac [apollo, bbncca, cadmus, decvax, harvard, linus, masscomp]!wanginst!vishniac vishniac%Wang-Inst@Csnet-Relay