Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!brl-tgr!tgr!gwyn@BRL.ARPA From: gwyn@BRL.ARPA (VLD/VMB) Newsgroups: net.lang.c Subject: Re: Need strnlen(). Message-ID: <3099@brl-tgr.ARPA> Date: Mon, 11-Nov-85 00:52:30 EST Article-I.D.: brl-tgr.3099 Posted: Mon Nov 11 00:52:30 1985 Date-Received: Tue, 12-Nov-85 05:07:01 EST Sender: news@brl-tgr.ARPA Lines: 26 Re: proposed strnlen() All the str*() functions work with NUL-terminated strings. Probably the best thing that could happen if you fed them something else would be for the implementation to access unallocated memory. At least then you would be able to find the bug. Your suggestion would make more sense for the mem*() functions, which work with arbitary byte arrays. A good generalization would be "find the first occurrence of a given byte, not going beyond a certain distance". If you look up memchr(), you will see that that's what it does. It is possible that your system is not System V compatible, in which case the memchr() function may not be in your C library. But it is very easy to implement and there is no need to invent a different-shaped wheel. char *memchr( const char *s, int c, int n ); returns a pointer to the first occurrence of `c' in the first `n' bytes of memory area (whose lowest address is) `s', or NULL if not found. Calling this function with `c' set to '\0' is equivalent to your proposed strnlen(). (You would have to handle the exception case, and use whatever memchr() returns minus `s' in the regular case for the length of the string.)