James Carlson <carls...@workingcode.com> wrote: > On 5/1/10 11:31 AM, Joerg Schilling wrote: > > OK, let's see.... here is the code: > [...] > > #include <schily/standard.h> > > #include <schily/unistd.h> > > #include <schily/schily.h> > > > > #ifndef HAVE_STRNLEN > > > > EXPORT size_t > > strnlen(s, len) > > register const char *s; > > register size_t len; > > The above will need to be rewritten to conform with the style used in > the current OpenSolaris libc. That means: > > - none of this "schily" stuff > > - remove "register" > > - remove the unneeded ifndef > > - remove "EXPORT" > > - use ANSI C not K&R function declaration > > - prefer to use good variable names if possible; "s" isn't.
You Most of the code I see from Sun does not follow these rules and the variable names are of course OK in a piece of code that is easy to read (all string sources in ON use tose 's' variables..... Note that K&K function _implemetations_ are needed for portability and that there is no benefit from using ANSI C here as there are ANCI C function _declarations_. I hope you know the difference between a function "declaration" and a function "implementation". > - prefer to use only needed headers; probably <sys/types.h> > and <string.h>. Well, in contrary to what's in Solaris, I write portable code. For a programmer, it should be easy to convert to other include files. > - code is incomplete; need updates to header files, Makefiles, > packaging, and library control files (mapfiles). See above. > > if (++len == 0) { /* unsigned overflow */ > > while (len-- > 0 && *rs++ != '\0') > > ; > > if (len == 0) > > return (rs - s); > > return (--rs - s); > > That case doesn't appear to work. If len is (size_t)~0, then ++len will > set len to 0 (unsigned overflow), making the first "if" statement true. > The "while" statement's first clause will check len>0 using len's value > before the post-decrement, and then post-decrement len back to ~0. It > will immediately terminate because 0>0 is false. It of course works correctly, but there was a copy/paste bug... Thank you for this hint. The code should be: if (++len == 0) { /* unsigned overflow */ len--; while (len-- > 0 && *rs++ != '\0') ; if (len == 0) return (rs - s); return (--rs - s); > I suspect that what may have been wanted was "--len > 0" in the while > statement, but then that makes the first "if" statement unnecessary. > > It may be easier just to make "len == 0" a single special case on entry: > > if (len == 0) > return (0); > while (*rs++ != '\0' && --len > 0) > ; > if (len == 0) > return (rs - s); > else > return ((rs - s) - 1); Your code is incorrect an not acceptable to me. Your code tries to access more than "len" chars from the string, which is forbidden by the standard. Note that there is similar incorrect code in libc from Solaris that either accesses more data than permitted or does not work correctly with len == ~0. You may like to check other strn*() functions in libc for this reason. BTW: Other missing code in libc: wcsncat() wcsncpy() wcsdup() wcsnlen() It have it.... Jörg -- EMail:jo...@schily.isdn.cs.tu-berlin.de (home) Jörg Schilling D-13353 Berlin j...@cs.tu-berlin.de (uni) joerg.schill...@fokus.fraunhofer.de (work) Blog: http://schily.blogspot.com/ URL: http://cdrecord.berlios.de/private/ ftp://ftp.berlios.de/pub/schily _______________________________________________ opensolaris-code mailing list opensolaris-code@opensolaris.org http://mail.opensolaris.org/mailman/listinfo/opensolaris-code