Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!umich!samsung!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: index and rindex question... Message-ID: <1990Feb1.124228.15878@virtech.uucp> Date: 1 Feb 90 12:42:28 GMT References: <11716.25C6818B@urchin.fidonet.org> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 35 In article <11716.25C6818B@urchin.fidonet.org> Bob.Stout@p6.f506.n106.z1.fidonet.org (Bob Stout) writes: >In an article of <27 Jan 90 02:04:19 GMT>, (Conor P. Cahill) writes: > > >They are equivalent to the system V functions strchr() and strrchr(), > >respectively. > > Both the strchr() and strrchr() functions made it into the ANSI spec while >index() and rindex() didn't. I believe this was because the latter two >functions on some systems return an int offset of the character rather than a >pointer to it. Based on this usage, I use: The original poster has asked about the "BSD" functions index() and rindex(). The documentation as far back as 4.1BSD shows that they are the equivalent to strchr()/strrchr() (i.e. they return pointer to char). >#define index(s,c) ((strchr((s),(c))) ? (size_t)(strchr((s),(c))-(s)) : -1) >#define rindex(s,c) ((strrchr((s),(c))) ? (size_t)(strrchr((s),(c))-(s)) : -1) This would work to replace index() only when the original software came from a system that did not use the BSD implementation. I would guess that it would be rather rare today. If I remember correctly, PWB Unix had an index() that returned int. For most of the code that you run into today (yes, I know that there are still V6 and PWB systems around, in fact I work on one every once in a while) the following defines will suffice: #define index(s,c) strchr(s,c) #define rindex(s,c) strrchr(s,c) -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+