home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / strstr.arc / STRLIB.DOC < prev    next >
Text File  |  1987-06-11  |  5KB  |  109 lines

  1.           Introduction
  2.           ------------
  3.  
  4.           This .ARC file contains an UNOFFICIAL fix for the strstr() function
  5.           supplied as part of the Turbo C  "standard library."   Included are
  6.           the source file and .OBJ files for each of the memory models.  Also
  7.           included is a DOS BATCH file to  make  updating  Borland's standard
  8.           libraries  as  painless  as  possible  (provided you own a copy  of
  9.           Microsoft LIB or some other full-featured librarian utility).
  10.  
  11.  
  12.           The Problem
  13.           -----------
  14.  
  15.           The  strstr()  function  distributed  by  Borland  as  part of  the
  16.           standard  C  libraries  for   Turbo  C   contains  a  reported  and
  17.           acknowledged bug that results in the function  returning  erroneous
  18.           results when the 'key' string matches a _terminating_ sub-string of
  19.           the target string.  For example:
  20.  
  21.                     chptr = strstr ("efg","abcdefg");
  22.  
  23.           yields chptr  ==  NULL.    It should yield a valid pointer  to  the
  24.           character 'e' in "abcdefg".   Microsoft  C  4.0's strstr() function
  25.           exhibits this same strange behavior.    In addition,  Microsoft C's
  26.           strstr() does  _NOT_  fail when str2 (the string being searched for
  27.           an occurence of str1) is a NULL string and  str1 (the 'key' string)
  28.           is a non-NULL string!  This, in my humble opinion, is also a bug in
  29.           the Microsoft implementation of the function.  For example:
  30.  
  31.                     chptr = strstr ("efg","");
  32.  
  33.           will return a valid pointer  (to the single NULL in str2)  when one
  34.           would expect it  to return a  NULL  pointer  (i.e.,  'no match'  or
  35.           failure).  I don't know whether the original strstr() function also
  36.           exhibits this behavior.    In any event,  the fix provided herewith
  37.           returns a NULL pointer in this case.
  38.  
  39.  
  40.           The Fix
  41.           -------
  42.  
  43.           The  UNOFFICIAL  strstr() fix in this .ARChive employs an iterative
  44.           design approach for speed;  the routine employs _no_ calls to other
  45.           standard library functions,  and it  is smaller  than  the original
  46.           Borland version.
  47.  
  48.           Descriptions of the files included in this .ARChive are as follows:
  49.  
  50.                     STRSTR.C    - source
  51.                     STRSTRS.OBJ - SMALL
  52.                     STRSTRC.OBJ - COMPACT
  53.                     STRSTRM.OBJ - MEDIUM
  54.                     STRSTRL.OBJ - LARGE
  55.                     STRSTRH.OBJ - HUGE
  56.  
  57.           In addition,  as mentioned above,  a DOS BATCH file (STRLIB.BAT) is
  58.           provided to fully automate updating the Turbo C libraries (provided
  59.           you use  Microsoft's LIB  as  the  librarian -- you can use another
  60.           librarian that has delete and add -- update -- capabilities, you'll
  61.           just have to change that line in  STRLIB.BAT).   All you need to do
  62.           to update the  library  is  de-ARC  the  appropriate  memory  model
  63.           version(s) of STRSTR.OBJ and type:
  64.  
  65.                     strlib x
  66.  
  67.           at the DOS command prompt,  where x is the letter corresponding  to
  68.           the memory model, to wit:
  69.  
  70.                     s - SMALL
  71.                     c - COMPACT
  72.                     m - MEDIUM
  73.                     l - LARGE (ell)
  74.                     h - HUGE
  75.  
  76.           Both the source .OBJ file(s) and the target .LIB file(s) must be in
  77.           the same  directory and  STRLIB.BAT  must  be  launched  from  that
  78.           directory.
  79.  
  80.           Of course, you don't have to modify your Borland libraries  (and if
  81.           you do,  please modify a _copy_ of the original distribution file).
  82.           You can just put the object version in any project or make  file or
  83.           include it by name on the TCC command line.    The standard library
  84.           version  will  not  be  used  (the  first  reference  resolves  the
  85.           link and the standard library is always linked in last).
  86.  
  87.  
  88.           And, Finally ...
  89.           ----------------
  90.  
  91.           I have tested this fix under all memory models.  The  NULL  pointer
  92.           and NULL-string cases behave as you'd expect.  If a NULL pointer is
  93.           passed for either argument  to  the  function,  a  NULL  pointer is
  94.           returned.      If   either   string   contains    ONLY   the   '\0'
  95.           string-terminating  character  (i.e., is a  NULL  string),  a  NULL
  96.           pointer is returned.  Thus, a NULL string is not considered to be a
  97.           'valid'  string  for  the  purposes  of  this   function   by  this
  98.           implementation  (this was a conscious  design decision which seemed
  99.           intuitive to me and for which I could find no guidance  at  all  in
  100.           K&R).    A  NULL pointer return from this function is equivalent to
  101.           "no match" or "fail."
  102.  
  103.  
  104.  
  105.           Brad Paulsen
  106.           76703,1005
  107.           CompuServe Programmers' SIG
  108.           GO PROGSIG
  109.