home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / STRCHR.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  3.0 KB  |  98 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strchr.cas
  3.  *
  4.  * function(s)
  5.  *        strchr - scans a string for the first occurrence of a
  6.  *        given character
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*[]------------------------------------------------------------[]*/
  10. /*|                                                              |*/
  11. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  12. /*|                                                              |*/
  13. /*|                                                              |*/
  14. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  15. /*|     All Rights Reserved.                                     |*/
  16. /*|                                                              |*/
  17. /*[]------------------------------------------------------------[]*/
  18.  
  19. #pragma  inline
  20. #include <asmrules.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. /*---------------------------------------------------------------------*
  25.  
  26. Name            strchr - scans a string for the first occurrence of a
  27.                          given character
  28.  
  29. Usage           char *strchr(const char *str, int c);
  30.  
  31. Prototype in    string.h
  32.  
  33. Description     strchr scans a string in the forward direction, looking for a
  34.                 specific character. strchr finds the first occurrence of the
  35.                 character ch in the string str.
  36.  
  37.                 The null-terminator is considered
  38.                 to be part of the string, so that, for example
  39.  
  40.                         strchr(strs, 0)
  41.  
  42.                 returns a pointer to the terminating null character of the
  43.                 string "strs".
  44.  
  45. Return value    strchr  returns a pointer to the first occurrence of the
  46.                 character ch in str; if ch does not occur in str, strchr
  47.                 returns NULL.
  48.  
  49. *---------------------------------------------------------------------*/
  50. #undef strchr            /* not an intrinsic */
  51. char *strchr(const char *s, int c)
  52. {
  53. asm     cld
  54. #if  defined(__LARGE__) || defined(__COMPACT__)
  55. asm    push    ds
  56. #endif
  57. asm    LDS_    si, s
  58. asm    mov    bl,c
  59. asm    test    si, 1            /* SI even? */
  60. asm    jz    cmp_loop
  61. asm    lodsb
  62. asm    cmp    al, bl            /* process first character */
  63. asm    je    success2
  64. asm    and    al, al
  65. asm    jz    failure
  66. cmp_loop:
  67. asm    lodsw                /* get 2 chars at a time */
  68. asm    cmp    al, bl
  69. asm    je    success1        /* if first  char matches */
  70. asm    and    al, al
  71. asm    jz    failure            /* give up if end of string */
  72. asm    cmp    ah, bl
  73. asm    je    success2        /* if second char matches */
  74. asm    and    ah, ah
  75. asm    jnz    cmp_loop        /* continue if more characters */
  76. failure:
  77. #if  defined(__LARGE__) || defined(__COMPACT__)
  78. asm    pop    ds
  79. #endif
  80.         return( NULL );
  81. success2:
  82. asm    inc    si
  83. success1:
  84. asm    lea    ax, [si-2]        /* point AX at matching char */
  85. #if    LDATA
  86. asm    mov    dx,ds
  87. #endif
  88. #if  defined(__LARGE__) || defined(__COMPACT__)
  89. asm    pop    ds
  90. #endif
  91.  
  92. #if LDATA
  93.         return( (char *)(MK_LONG) );
  94. #else
  95.         return( (char *)_AX );
  96. #endif
  97. }
  98.