home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume27 / dmake / part06 < prev    next >
Encoding:
Text File  |  1992-01-29  |  40.1 KB  |  1,613 lines

  1. Newsgroups: comp.sources.misc
  2. From: dvadura@plg.waterloo.edu (Dennis Vadura)
  3. Subject:  v27i107:  dmake - dmake Version 3.8, Part06/41
  4. Message-ID: <1992Jan28.031325.7080@sparky.imd.sterling.com>
  5. X-Md4-Signature: 5905e441a96d0f300b3d5765afceea14
  6. Date: Tue, 28 Jan 1992 03:13:25 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: dvadura@plg.waterloo.edu (Dennis Vadura)
  10. Posting-number: Volume 27, Issue 107
  11. Archive-name: dmake/part06
  12. Environment: Atari-ST, Coherent, Mac, MSDOS, OS/2, UNIX
  13. Supersedes: dmake: Volume 19, Issue 22-58
  14.  
  15. ---- Cut Here and feed the following to sh ----
  16. # this is dmake.shar.06 (part 6 of a multipart archive)
  17. # do not concatenate these parts, unpack them in order with /bin/sh
  18. # file dmake/dbug/malloc/mlc_chn.c continued
  19. #
  20. if test ! -r _shar_seq_.tmp; then
  21.     echo 'Please unpack part 1 first!'
  22.     exit 1
  23. fi
  24. (read Scheck
  25.  if test "$Scheck" != 6; then
  26.     echo Please unpack part "$Scheck" next!
  27.     exit 1
  28.  else
  29.     exit 0
  30.  fi
  31. ) < _shar_seq_.tmp || exit 1
  32. if test -f _shar_wnt_.tmp; then
  33. sed 's/^X//' << 'SHAR_EOF' >> 'dmake/dbug/malloc/mlc_chn.c' &&
  34. X            rtn++;
  35. X            continue;
  36. X        }
  37. X
  38. X        /* 
  39. X         * verify segments are correctly linked together
  40. X         */
  41. X        
  42. X        if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  43. X            (ptr->next && (ptr->next->prev != ptr) ) ||
  44. X            ((ptr->next == NULL) && (ptr->prev == NULL)) )
  45. X        {
  46. X            malloc_errno = M_CODE_BAD_CONNECT;
  47. X            if( todo )
  48. X            {
  49. X                malloc_warning(func);
  50. X            }
  51. X            rtn++;
  52. X            continue;
  53. X        }
  54. X
  55. X        /*
  56. X         * If this segment is allocated
  57. X         */
  58. X
  59. X        if( (ptr->flag & M_INUSE) != 0 )
  60. X        {
  61. X            /*
  62. X             * verify no overflow of data area
  63. X             */
  64. X
  65. X            for(i=ptr->r_size; i < ptr->s.size; i++)
  66. X            {
  67. X                if( ptr->data[i] != M_FILL )
  68. X                {
  69. X                    malloc_errno = M_CODE_OVERRUN;
  70. X                    if( todo )
  71. X                    {
  72. X                        malloc_warning(func);
  73. X                    }
  74. X                    rtn++;
  75. X                    break;
  76. X                }
  77. X            }
  78. X        }
  79. X        else /* it's not allocated so */
  80. X        {
  81. X            /*
  82. X             * verify no reuse of freed data blocks
  83. X             */
  84. X
  85. X            for(i=0; i < ptr->s.size; i++)
  86. X            {
  87. X                if( ptr->data[i] != M_FREE_FILL )
  88. X                {
  89. X                    malloc_errno = M_CODE_REUSE;
  90. X                    if( todo )
  91. X                    {
  92. X                        malloc_warning(func);
  93. X                    }
  94. X                    rtn++;
  95. X                    break;
  96. X                }
  97. X            }
  98. X        }
  99. X
  100. X    } /* for(... */
  101. X
  102. X    return(rtn);
  103. X
  104. } /* malloc_chain_check(... */
  105. SHAR_EOF
  106. chmod 0640 dmake/dbug/malloc/mlc_chn.c ||
  107. echo 'restore of dmake/dbug/malloc/mlc_chn.c failed'
  108. Wc_c="`wc -c < 'dmake/dbug/malloc/mlc_chn.c'`"
  109. test 3448 -eq "$Wc_c" ||
  110.     echo 'dmake/dbug/malloc/mlc_chn.c: original size 3448, current size' "$Wc_c"
  111. rm -f _shar_wnt_.tmp
  112. fi
  113. # ============= dmake/dbug/malloc/patchlevel ==============
  114. if test -f 'dmake/dbug/malloc/patchlevel' -a X"$1" != X"-c"; then
  115.     echo 'x - skipping dmake/dbug/malloc/patchlevel (File already exists)'
  116.     rm -f _shar_wnt_.tmp
  117. else
  118. > _shar_wnt_.tmp
  119. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/patchlevel' &&
  120. 3
  121. SHAR_EOF
  122. chmod 0640 dmake/dbug/malloc/patchlevel ||
  123. echo 'restore of dmake/dbug/malloc/patchlevel failed'
  124. Wc_c="`wc -c < 'dmake/dbug/malloc/patchlevel'`"
  125. test 2 -eq "$Wc_c" ||
  126.     echo 'dmake/dbug/malloc/patchlevel: original size 2, current size' "$Wc_c"
  127. rm -f _shar_wnt_.tmp
  128. fi
  129. # ============= dmake/dbug/malloc/realloc.c ==============
  130. if test -f 'dmake/dbug/malloc/realloc.c' -a X"$1" != X"-c"; then
  131.     echo 'x - skipping dmake/dbug/malloc/realloc.c (File already exists)'
  132.     rm -f _shar_wnt_.tmp
  133. else
  134. > _shar_wnt_.tmp
  135. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/realloc.c' &&
  136. /*
  137. X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  138. X * You may copy, distribute, and use this software as long as this
  139. X * copyright statement is not removed.
  140. X */
  141. #include <stdio.h>
  142. #include "malloc.h"
  143. X
  144. /*
  145. X * Function:    realloc()
  146. X *
  147. X * Purpose:    to re-allocate a data area.
  148. X *
  149. X * Arguments:    cptr    - pointer to area to reallocate
  150. X *        size    - size to change area to
  151. X *
  152. X * Returns:    pointer to new area (may be same area)
  153. X *
  154. X * Narrative:    verify pointer is within malloc region
  155. X *        obtain mlist pointer from cptr
  156. X *        verify magic number is correct
  157. X *        verify inuse flag is set
  158. X *        verify connection to adjoining segments is correct
  159. X *        save requested size
  160. X *        round-up size to appropriate boundry
  161. X *        IF size is bigger than what is in this segment
  162. X *            try to join next segment to this segment
  163. X *        IF size is less than what is is this segment
  164. X *            determine leftover amount of space
  165. X *        ELSE
  166. X *            allocate new segment of size bites
  167. X *            IF allocation failed
  168. X *                return NULL
  169. X *            copy previous data to new segment
  170. X *            free previous segment
  171. X *            return new pointer
  172. X *        split of extra space in this segment (if any)
  173. X *        clear bytes beyound what they had before
  174. X *        return pointer to data 
  175. X */
  176. #ifndef lint
  177. static
  178. char rcs_hdr[] = "$Id: realloc.c,v 1.1 1992/01/24 03:29:11 dvadura Exp $";
  179. #endif
  180. X
  181. char *
  182. realloc(cptr,size)
  183. X    char            * cptr;
  184. X    unsigned int          size;
  185. {
  186. X    void              free();
  187. X    char            * func = "realloc";
  188. X    int              i;
  189. X    char            * malloc();
  190. X    extern int          malloc_checking;
  191. X    extern struct mlist     * malloc_end;
  192. X    extern int          malloc_errno;
  193. X    extern char        * malloc_data_end;
  194. X    extern char        * malloc_data_start;
  195. X    void              malloc_join();
  196. X    void              malloc_memset();
  197. X    void              malloc_split();
  198. X    char            * memcpy();
  199. X    char            * new_cptr;
  200. X    struct mlist        * ptr;
  201. X    int              r_size;
  202. X
  203. X    /*
  204. X     * IF malloc chain checking is on, go do it.
  205. X     */
  206. X    if( malloc_checking )
  207. X    {
  208. X        (void) malloc_chain_check(1);
  209. X    }
  210. X
  211. X    /*
  212. X     * verify that cptr is within the malloc region...
  213. X     */
  214. X    if( cptr < malloc_data_start || cptr > malloc_data_end )
  215. X    {
  216. X        malloc_errno = M_CODE_BAD_PTR;
  217. X        malloc_warning(func);
  218. X        return (NULL);
  219. X    }
  220. X
  221. X    /* 
  222. X     * convert pointer to mlist struct pointer.  To do this we must 
  223. X     * move the pointer backwards the correct number of bytes...
  224. X     */
  225. X    
  226. X    ptr = (struct mlist *) (cptr - M_SIZE);
  227. X    
  228. X    if( (ptr->flag&M_MAGIC) != M_MAGIC )
  229. X    {
  230. X        malloc_errno = M_CODE_BAD_MAGIC;
  231. X        malloc_warning(func);
  232. X        return(NULL);
  233. X    }
  234. X
  235. X    if( ! (ptr->flag & M_INUSE) )
  236. X    {
  237. X        malloc_errno = M_CODE_NOT_INUSE ;
  238. X        malloc_warning(func);
  239. X        return(NULL);
  240. X    }
  241. X
  242. X     if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  243. X        (ptr->next && (ptr->next->prev != ptr) ) ||
  244. X        ((ptr->next == NULL) && (ptr->prev == NULL)) )
  245. X    {
  246. X        malloc_errno = M_CODE_BAD_CONNECT;
  247. X        malloc_warning(func);
  248. X        return(NULL);
  249. X    }
  250. X
  251. X    r_size = ++size;
  252. X
  253. X    M_ROUNDUP(size);
  254. X
  255. X    if( size > ptr->s.size )
  256. X    {
  257. X        malloc_join(ptr,ptr->next,1,1);
  258. X    }
  259. X
  260. X    if( size > ptr->s.size )
  261. X    {
  262. X        /*
  263. X         * else we can't combine it, so lets allocate a new chunk,
  264. X         * copy the data and free the old chunk...
  265. X         */
  266. X        new_cptr = malloc(size);
  267. X
  268. X        if( new_cptr == (char *) 0)
  269. X        {
  270. X            return(new_cptr);
  271. X        }
  272. X
  273. X        if( r_size < ptr->r_size )
  274. X        {
  275. X            i = r_size;
  276. X        }
  277. X        else
  278. X        {
  279. X            i = ptr->r_size;
  280. X        }
  281. X        (void)memcpy(new_cptr,ptr->data,i);
  282. X        free(cptr);
  283. X        return(new_cptr);
  284. X
  285. X    } /* else... */
  286. X
  287. X    /*
  288. X     * save amount of real data in new segment (this will be used in the
  289. X     * memset later) and then save requested size of this segment.
  290. X     */
  291. X
  292. X    if( ptr->r_size < r_size )
  293. X    {
  294. X        i = ptr->r_size;
  295. X    }
  296. X    else
  297. X    {
  298. X        i = r_size;
  299. X    }
  300. X
  301. X    ptr->r_size = r_size;
  302. X
  303. X    /*
  304. X     * split off extra free space at end of this segment, if possible...
  305. X     */
  306. X
  307. X    malloc_split(ptr);
  308. X
  309. X    malloc_memset( ptr->data+i, M_FILL, (int) (ptr->s.size - i));
  310. X        
  311. X    return(ptr->data);
  312. X
  313. } /* realloc(... */
  314. X
  315. X
  316. /*
  317. X * $Log: realloc.c,v $
  318. X * Revision 1.1  1992/01/24  03:29:11  dvadura
  319. X * dmake Version 3.8, Initial revision
  320. X *
  321. X * Revision 1.8  90/08/29  21:22:52  cpcahil
  322. X * miscellaneous lint fixes
  323. X * 
  324. X * Revision 1.7  90/05/11  00:13:10  cpcahil
  325. X * added copyright statment
  326. X * 
  327. X * Revision 1.6  90/02/25  11:01:20  cpcahil
  328. X * added support for malloc chain checking.
  329. X * 
  330. X * Revision 1.5  90/02/24  21:50:31  cpcahil
  331. X * lots of lint fixes
  332. X * 
  333. X * Revision 1.4  90/02/24  17:29:39  cpcahil
  334. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  335. X * id string
  336. X * 
  337. X * Revision 1.3  90/02/24  17:20:00  cpcahil
  338. X * attempt to get rid of full path in rcs header.
  339. X * 
  340. X * Revision 1.2  90/02/24  15:14:20  cpcahil
  341. X * 1. added function header 
  342. X * 2. changed calls to malloc_warning to conform to new usage
  343. X * 3. added setting of malloc_errno
  344. X *  4. broke up bad pointer determination so that errno's would be more
  345. X *    descriptive
  346. X * 
  347. X * Revision 1.1  90/02/22  23:17:43  cpcahil
  348. X * Initial revision
  349. X * 
  350. X */
  351. SHAR_EOF
  352. chmod 0640 dmake/dbug/malloc/realloc.c ||
  353. echo 'restore of dmake/dbug/malloc/realloc.c failed'
  354. Wc_c="`wc -c < 'dmake/dbug/malloc/realloc.c'`"
  355. test 4750 -eq "$Wc_c" ||
  356.     echo 'dmake/dbug/malloc/realloc.c: original size 4750, current size' "$Wc_c"
  357. rm -f _shar_wnt_.tmp
  358. fi
  359. # ============= dmake/dbug/malloc/string.c ==============
  360. if test -f 'dmake/dbug/malloc/string.c' -a X"$1" != X"-c"; then
  361.     echo 'x - skipping dmake/dbug/malloc/string.c (File already exists)'
  362.     rm -f _shar_wnt_.tmp
  363. else
  364. > _shar_wnt_.tmp
  365. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/string.c' &&
  366. /*
  367. X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  368. X * You may copy, distribute, and use this software as long as this
  369. X * copyright statement is not removed.
  370. X */
  371. X
  372. #include <stdio.h>
  373. #include <string.h>
  374. #include <sys/types.h>
  375. #include "malloc.h"
  376. X
  377. #ifndef lint
  378. static
  379. char rcs_hdr[] = "$Id: string.c,v 1.1 1992/01/24 03:29:13 dvadura Exp $";
  380. #endif
  381. X
  382. int    malloc_checking = 0;
  383. X
  384. char *
  385. strcat(str1,str2)
  386. X    register char    * str1;
  387. X    register char    * str2;
  388. {
  389. X    char        * rtn;
  390. X    int      len;
  391. X
  392. X    /* 
  393. X     * check pointers agains malloc region.  The malloc* functions
  394. X     * will properly handle the case where a pointer does not
  395. X     * point into malloc space.
  396. X     */
  397. X    malloc_checking = 1;
  398. X
  399. X    len = strlen(str2);
  400. X    malloc_check_str("strcat", str2);
  401. X
  402. X    len += strlen(str1) + 1;
  403. X    malloc_checking = 0;
  404. X
  405. X    malloc_check_data("strcat", str1, len);
  406. X
  407. X    rtn = str1;
  408. X
  409. X    while( *str1 )
  410. X    {
  411. X        str1++;
  412. X    }
  413. X    
  414. X    while( (*str1 = *str2) != '\0' )
  415. X    {
  416. X        str1++;
  417. X        str2++;
  418. X    }
  419. X    
  420. X    return(rtn);
  421. }
  422. X
  423. char *
  424. strdup(str1)
  425. X    register char    * str1;
  426. {
  427. X    char        * malloc();
  428. X    char        * rtn;
  429. X    register char    * str2;
  430. X
  431. X    malloc_check_str("strdup", str1);
  432. X
  433. X    rtn = str2 = malloc((unsigned)strlen(str1)+1);
  434. X
  435. X    if( rtn != (char *) 0)
  436. X    {
  437. X        while( (*str2 = *str1) != '\0' )
  438. X        {
  439. X            str1++;
  440. X            str2++;
  441. X        }
  442. X    }
  443. X
  444. X    return(rtn);
  445. }
  446. X
  447. char *
  448. strncat(str1,str2,len)
  449. X    register char    * str1;
  450. X    register char    * str2;
  451. X    register int      len;
  452. {
  453. X    int           len1;
  454. X    int           len2;
  455. X    char        * rtn;
  456. X
  457. X    malloc_check_strn("strncat", str2, len);
  458. X
  459. X    malloc_checking = 1;
  460. X
  461. X    len2 = strlen(str2) + 1;
  462. X    len1 = strlen(str1);
  463. X
  464. X    malloc_checking = 0;
  465. X
  466. X
  467. X    if( (len+1) < len2 )
  468. X    {
  469. X        len1 += len + 1;
  470. X    }
  471. X    else
  472. X    {
  473. X        len1 += len2;
  474. X    }
  475. X    malloc_check_data("strncat", str1, len1);
  476. X
  477. X    rtn = str1;
  478. X
  479. X    while( *str1 )
  480. X    {
  481. X        str1++;
  482. X    }
  483. X
  484. X    while( len-- && ((*str1++ = *str2++) != '\0') )
  485. X    {
  486. X    }
  487. X    
  488. X    if( ! len )
  489. X    {
  490. X        *str1 = '\0';
  491. X    }
  492. X
  493. X    return(rtn);
  494. }
  495. X
  496. int
  497. strcmp(str1,str2)
  498. X    register char    * str1;
  499. X    register char    * str2;
  500. {
  501. X    malloc_check_str("strcmp", str1);
  502. X    malloc_check_str("strcmp", str2);
  503. X
  504. X    while( *str1 && (*str1 == *str2) )
  505. X    {
  506. X        str1++;
  507. X        str2++;
  508. X    }
  509. X
  510. X
  511. X    /*
  512. X     * in order to deal with the case of a negative last char of either
  513. X     * string when the other string has a null
  514. X     */
  515. X    if( (*str2 == '\0') && (*str1 == '\0') )
  516. X    {
  517. X        return(0);
  518. X    }
  519. X    else if( *str2 == '\0' )
  520. X    {
  521. X        return(1);
  522. X    }
  523. X    else if( *str1 == '\0' )
  524. X    {
  525. X        return(-1);
  526. X    }
  527. X    
  528. X    return( *str1 - *str2 );
  529. }
  530. X
  531. int
  532. strncmp(str1,str2,len)
  533. X    register char    * str1;
  534. X    register char    * str2;
  535. X    register int      len;
  536. {
  537. X    malloc_check_strn("strncmp", str1, len);
  538. X    malloc_check_strn("strncmp", str2, len);
  539. X
  540. X    while( --len >= 0 && *str1 && (*str1 == *str2) )
  541. X    {
  542. X        str1++;
  543. X        str2++;
  544. X    }
  545. X
  546. X    if( len < 0 )
  547. X    {
  548. X        return(0);
  549. X    }
  550. X    /*
  551. X     * in order to deal with the case of a negative last char of either
  552. X     * string when the other string has a null
  553. X     */
  554. X    if( (*str2 == '\0') && (*str1 == '\0') )
  555. X    {
  556. X        return(0);
  557. X    }
  558. X    else if( *str2 == '\0' )
  559. X    {
  560. X        return(1);
  561. X    }
  562. X    else if( *str1 == '\0' )
  563. X    {
  564. X        return(-1);
  565. X    }
  566. X    
  567. X    return( *str1 - *str2 );
  568. }
  569. X
  570. char *
  571. strcpy(str1,str2)
  572. X    register char    * str1;
  573. X    register char    * str2;
  574. {
  575. X    char        * rtn;
  576. X    int          len;
  577. X
  578. X    malloc_checking = 1;
  579. X    len = strlen(str2) + 1;
  580. X    malloc_checking = 0;
  581. X
  582. X    malloc_check_data("strcpy", str1, len);
  583. X    malloc_check_data("strcpy", str2, len);
  584. X
  585. X    rtn = str1;
  586. X
  587. X    while( (*str1++ = *str2++) != '\0')
  588. X    {
  589. X    }
  590. X
  591. X    return(rtn);
  592. }
  593. X
  594. char *
  595. strncpy(str1,str2,len)
  596. X    register char    * str1;
  597. X    register char    * str2;
  598. X    register int      len;
  599. {
  600. X    extern int      malloc_checking;
  601. X    char        * rtn;
  602. X
  603. X    malloc_check_data("strncpy", str1, len);
  604. X    malloc_check_strn("strncpy", str2, len);
  605. X
  606. X    rtn = str1;
  607. X
  608. X    while((len-- > 0) && (*str1++ = *str2++) != '\0')
  609. X    {
  610. X    }
  611. X    while( (len-- > 0) )
  612. X    {
  613. X        *str1++ = '\0';
  614. X    }
  615. X
  616. X    return(rtn);
  617. }
  618. X
  619. int
  620. strlen(str1)
  621. X    register char    * str1;
  622. {
  623. X    register char    * s;
  624. X
  625. X    if(! malloc_checking )
  626. X    {
  627. X        malloc_check_str("strlen", str1);
  628. X    }
  629. X
  630. X    for( s = str1; *s; s++)
  631. X    {
  632. X    }
  633. X
  634. X    return( s - str1 );
  635. }
  636. X
  637. char *
  638. strchr(str1,c)
  639. X    register char    * str1;
  640. X    register int      c;
  641. {
  642. X    malloc_check_str("strchr", str1);
  643. X
  644. X    while( *str1 && (*str1 != (char) c) )
  645. X    {
  646. X        str1++;
  647. X    }
  648. X
  649. X    if(*str1 != (char) c)
  650. X    {
  651. X        str1 = (char *) 0;
  652. X    }
  653. X
  654. X    return(str1);
  655. }
  656. X
  657. char *
  658. strrchr(str1,c)
  659. X    register char    * str1;
  660. X    register int      c;
  661. {
  662. X    register char    * rtn = (char *) 0;
  663. X
  664. X    malloc_check_str("strrchr", str1);
  665. X
  666. X    while( *str1 )
  667. X    {
  668. X        if(*str1 == (char) c )
  669. X        {
  670. X            rtn = str1;
  671. X        }
  672. X        str1++;
  673. X    }
  674. X
  675. X    if( *str1 == (char) c)
  676. X    {
  677. X        rtn = str1;
  678. X    }
  679. X
  680. X    return(rtn);
  681. }
  682. X
  683. char *
  684. index(str1,c)
  685. X    char        * str1;
  686. X    char          c;
  687. {
  688. X    return( strchr(str1,c) );
  689. }
  690. X
  691. char *
  692. rindex(str1,c)
  693. X    char        * str1;
  694. X    char          c;
  695. {
  696. X    return( strrchr(str1,c) );
  697. }
  698. X
  699. char *
  700. strpbrk(str1,str2)
  701. X    register char    * str1;
  702. X    register char    * str2;
  703. {
  704. X    register char    * tmp;
  705. X
  706. X    malloc_check_str("strpbrk", str1);
  707. X    malloc_check_str("strpbrk", str2);
  708. X
  709. X    while(*str1)
  710. X    {
  711. X        for( tmp=str2; *tmp && *tmp != *str1; tmp++)
  712. X        {
  713. X        }
  714. X        if( *tmp )
  715. X        {
  716. X            break;
  717. X        }
  718. X        str1++;
  719. X    }
  720. X
  721. X    if( ! *str1 )
  722. X    {
  723. X        str1 = (char *) 0;
  724. X    }
  725. X
  726. X    return(str1);
  727. }
  728. X
  729. int
  730. strspn(str1,str2)
  731. X    register char    * str1;
  732. X    register char    * str2;
  733. {
  734. X    register char    * tmp;
  735. X    char        * orig = str1;
  736. X
  737. X    malloc_check_str("strspn", str1);
  738. X    malloc_check_str("strspn", str2);
  739. X
  740. X    while(*str1)
  741. X    {
  742. X        for( tmp=str2; *tmp && *tmp != *str1; tmp++)
  743. X        {
  744. X        }
  745. X        if(! *tmp )
  746. X        {
  747. X            break;
  748. X        }
  749. X        str1++;
  750. X    }
  751. X
  752. X    return( (int) (str1 - orig) );
  753. }
  754. X
  755. int
  756. strcspn(str1,str2)
  757. X    register char    * str1;
  758. X    register char    * str2;
  759. {
  760. X    register char    * tmp;
  761. X    char        * orig = str1;
  762. X
  763. X    malloc_check_str("strcspn", str1);
  764. X    malloc_check_str("strcspn", str2);
  765. X
  766. X    while(*str1)
  767. X    {
  768. X        for( tmp=str2; *tmp && *tmp != *str1; tmp++)
  769. X        {
  770. X        }
  771. X        if( *tmp )
  772. X        {
  773. X            break;
  774. X        }
  775. X        str1++;
  776. X    }
  777. X
  778. X    return( (int) (str1 - orig) );
  779. }
  780. X
  781. /*
  782. X * strtok() source taken from that posted to comp.lang.c by Chris Torek
  783. X * in Jan 1990.
  784. X */
  785. X
  786. /*
  787. X * Copyright (c) 1989 The Regents of the University of California.
  788. X * All rights reserved.
  789. X *
  790. X * Redistribution and use in source and binary forms are permitted
  791. X * provided that the above copyright notice and this paragraph are
  792. X * duplicated in all such forms and that any documentation,
  793. X * advertising materials, and other materials related to such
  794. X * distribution and use acknowledge that the software was developed
  795. X * by the University of California, Berkeley.  The name of the
  796. X * University may not be used to endorse or promote products derived
  797. X * from this software without specific prior written permission.
  798. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  799. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  800. X * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  801. X */
  802. X
  803. /*
  804. X * Get next token from string s (NULL on 2nd, 3rd, etc. calls),
  805. X * where tokens are nonempty strings separated by runs of
  806. X * chars from delim.  Writes NULs into s to end tokens.  delim need not
  807. X * remain constant from call to call.
  808. X *
  809. X * Modified by cpc:     changed variable names to conform with naming
  810. X *            conventions used in rest of code.  Added malloc pointer
  811. X *            check calls.
  812. X */
  813. char *
  814. strtok(str1, str2)
  815. X    char     * str1;
  816. X    char    * str2;
  817. {
  818. X    static char     * last;
  819. X    char        * strtoken();
  820. X
  821. X    if( str1 )
  822. X    {
  823. X        malloc_check_str("strtok", str1);
  824. X        last = str1;
  825. X    }
  826. X    malloc_check_str("strtok", str2);
  827. X
  828. X    return (strtoken(&last, str2, 1));
  829. }
  830. X
  831. X
  832. /*
  833. X * Get next token from string *stringp, where tokens are (possibly empty)
  834. X * strings separated by characters from delim.  Tokens are separated
  835. X * by exactly one delimiter iff the skip parameter is false; otherwise
  836. X * they are separated by runs of characters from delim, because we
  837. X * skip over any initial `delim' characters.
  838. X *
  839. X * Writes NULs into the string at *stringp to end tokens.
  840. X * delim will usually, but need not, remain constant from call to call.
  841. X * On return, *stringp points past the last NUL written (if there might
  842. X * be further tokens), or is NULL (if there are definitely no more tokens).
  843. X *
  844. X * If *stringp is NULL, strtoken returns NULL.
  845. X */
  846. char *
  847. strtoken(stringp, delim, skip)
  848. X    register char **stringp;
  849. X    register char *delim;
  850. X    int skip;
  851. {
  852. X    register char *s;
  853. X    register char *spanp;
  854. X    register int c, sc;
  855. X    char *tok;
  856. X
  857. X    if ((s = *stringp) == NULL)
  858. X        return (NULL);
  859. X
  860. X    if (skip) {
  861. X        /*
  862. X         * Skip (span) leading delimiters (s += strspn(s, delim)).
  863. X         */
  864. X    cont:
  865. X        c = *s;
  866. X        for (spanp = delim; (sc = *spanp++) != 0;) {
  867. X            if (c == sc) {
  868. X                s++;
  869. X                goto cont;
  870. X            }
  871. X        }
  872. X        if (c == 0) {        /* no token found */
  873. X            *stringp = NULL;
  874. X            return (NULL);
  875. X        }
  876. X    }
  877. X
  878. X    /*
  879. X     * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  880. X     * Note that delim must have one NUL; we stop if we see that, too.
  881. X     */
  882. X    for (tok = s;;) {
  883. X        c = *s++;
  884. X        spanp = delim;
  885. X        do {
  886. X            if ((sc = *spanp++) == c) {
  887. X                if (c == 0)
  888. X                    s = NULL;
  889. X                else
  890. X                    s[-1] = 0;
  891. X                *stringp = s;
  892. X                return (tok);
  893. X            }
  894. X        } while (sc != 0);
  895. X    }
  896. X    /* NOTREACHED */
  897. }
  898. X
  899. /*
  900. X * $Log: string.c,v $
  901. X * Revision 1.1  1992/01/24  03:29:13  dvadura
  902. X * dmake Version 3.8, Initial revision
  903. X *
  904. X * Revision 1.7  90/08/29  22:24:19  cpcahil
  905. X * added new function to check on strings up to a specified length 
  906. X * and used it within several strn* functions.
  907. X * 
  908. X * Revision 1.6  90/07/16  20:06:56  cpcahil
  909. X * fixed several minor bugs found with Henry Spencer's string/mem function
  910. X * tester program.
  911. X * 
  912. X * Revision 1.5  90/06/10  14:59:49  cpcahil
  913. X * Fixed a couple of bugs in strncpy & strdup
  914. X * 
  915. X * Revision 1.4  90/05/11  00:13:10  cpcahil
  916. X * added copyright statment
  917. X * 
  918. X * Revision 1.3  90/02/24  21:50:32  cpcahil
  919. X * lots of lint fixes
  920. X * 
  921. X * Revision 1.2  90/02/24  17:29:40  cpcahil
  922. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  923. X * id string
  924. X * 
  925. X * Revision 1.1  90/02/22  23:17:44  cpcahil
  926. X * Initial revision
  927. X * 
  928. X */
  929. SHAR_EOF
  930. chmod 0640 dmake/dbug/malloc/string.c ||
  931. echo 'restore of dmake/dbug/malloc/string.c failed'
  932. Wc_c="`wc -c < 'dmake/dbug/malloc/string.c'`"
  933. test 9473 -eq "$Wc_c" ||
  934.     echo 'dmake/dbug/malloc/string.c: original size 9473, current size' "$Wc_c"
  935. rm -f _shar_wnt_.tmp
  936. fi
  937. # ============= dmake/dbug/malloc/testmem.c ==============
  938. if test -f 'dmake/dbug/malloc/testmem.c' -a X"$1" != X"-c"; then
  939.     echo 'x - skipping dmake/dbug/malloc/testmem.c (File already exists)'
  940.     rm -f _shar_wnt_.tmp
  941. else
  942. > _shar_wnt_.tmp
  943. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/testmem.c' &&
  944. /* 
  945. X * This stuff is all stolen (with permission, since it was in the public
  946. X * domain) from Henry Spencer's string and memory library.  Thanks Henry.
  947. X */
  948. X
  949. /*
  950. X * Test program for string(3) routines.
  951. X * 
  952. X * Note that at least one Bell Labs implementation of the string
  953. X * routines flunks a couple of these tests -- the ones which test
  954. X * behavior on "negative" characters.
  955. X */
  956. X
  957. #include <stdio.h>
  958. #include <string.h>
  959. X
  960. char * index();
  961. char * rindex();
  962. X
  963. #define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  964. X
  965. char *it = "<UNSET>";        /* Routine name for message routines. */
  966. int waserror = 0;        /* For exit status. */
  967. X
  968. char uctest[] = "\004\203";    /* For testing signedness of chars. */
  969. int charsigned;            /* Result. */
  970. X
  971. /*
  972. X - check - complain if condition is not true
  973. X */
  974. void
  975. check(thing, number)
  976. int thing;
  977. int number;            /* Test number for error message. */
  978. {
  979. X    if (!thing) {
  980. X        printf("%s flunked test %d\n", it, number);
  981. X        waserror = 1;
  982. X    }
  983. }
  984. X
  985. /*
  986. X - equal - complain if first two args don't strcmp as equal
  987. X */
  988. void
  989. equal(a, b, number)
  990. char *a;
  991. char *b;
  992. int number;            /* Test number for error message. */
  993. {
  994. X    check(a != NULL && b != NULL && STREQ(a, b), number);
  995. }
  996. X
  997. char one[50];
  998. char two[50];
  999. X
  1000. #ifdef UNIXERR
  1001. #define ERR 1
  1002. #endif
  1003. #ifdef BERKERR
  1004. #define ERR 1
  1005. #endif
  1006. #ifdef ERR
  1007. int f;
  1008. extern char *sys_errlist[];
  1009. extern int sys_nerr;
  1010. extern int errno;
  1011. #endif
  1012. X
  1013. /* ARGSUSED */
  1014. main(argc, argv)
  1015. int argc;
  1016. char *argv[];
  1017. {
  1018. X    /*
  1019. X     * First, establish whether chars are signed.
  1020. X     */
  1021. X    if (uctest[0] < uctest[1])
  1022. X        charsigned = 0;
  1023. X    else
  1024. X        charsigned = 1;
  1025. X
  1026. X    /*
  1027. X     * Then, do the rest of the work.  Split into two functions because
  1028. X     * some compilers get unhappy about a single immense function.
  1029. X     */
  1030. X    first();
  1031. X    second();
  1032. X
  1033. X    exit((waserror) ? 1 : 0);
  1034. }
  1035. X
  1036. first()
  1037. {
  1038. X    /*
  1039. X     * Test strcmp first because we use it to test other things.
  1040. X     */
  1041. X    it = "strcmp";
  1042. X    check(strcmp("", "") == 0, 1);        /* Trivial case. */
  1043. X    check(strcmp("a", "a") == 0, 2);    /* Identity. */
  1044. X    check(strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  1045. X    check(strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  1046. X    check(strcmp("abcd", "abc") > 0, 5);
  1047. X    check(strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  1048. X    check(strcmp("abce", "abcd") > 0, 7);
  1049. X    check(strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  1050. X    if (charsigned)                /* Sign-bit comparison. */
  1051. X        check(strcmp("a\203", "a\003") < 0, 9);
  1052. X    else
  1053. X        check(strcmp("a\203", "a\003") > 0, 9);
  1054. X    check(strcmp("a", "a\203") < 0, 10);    /* Tricky if char signed. */
  1055. X
  1056. X    /*
  1057. X     * Test strcpy next because we need it to set up other tests.
  1058. X     */
  1059. X    it = "strcpy";
  1060. X    check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  1061. X    equal(one, "abcd", 2);            /* Basic test. */
  1062. X
  1063. X    (void) strcpy(one, "x");
  1064. X    equal(one, "x", 3);            /* Writeover. */
  1065. X    equal(one+2, "cd", 4);            /* Wrote too much? */
  1066. X
  1067. X    (void) strcpy(two, "hi there");
  1068. X    (void) strcpy(one, two);
  1069. X    equal(one, "hi there", 5);        /* Basic test encore. */
  1070. X    equal(two, "hi there", 6);        /* Stomped on source? */
  1071. X
  1072. X    (void) strcpy(one, "");
  1073. X    equal(one, "", 7);            /* Boundary condition. */
  1074. X
  1075. X    /*
  1076. X     * strcat
  1077. X     */
  1078. X    it = "strcat";
  1079. X    (void) strcpy(one, "ijk");
  1080. X    check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  1081. X    equal(one, "ijklmn", 2);        /* Basic test. */
  1082. X
  1083. X    (void) strcpy(one, "x");
  1084. X    (void) strcat(one, "yz");
  1085. X    equal(one, "xyz", 3);            /* Writeover. */
  1086. X    equal(one+4, "mn", 4);            /* Wrote too much? */
  1087. X
  1088. X    (void) strcpy(one, "gh");
  1089. X    (void) strcpy(two, "ef");
  1090. X    (void) strcat(one, two);
  1091. X    equal(one, "ghef", 5);            /* Basic test encore. */
  1092. X    equal(two, "ef", 6);            /* Stomped on source? */
  1093. X
  1094. X    (void) strcpy(one, "");
  1095. X    (void) strcat(one, "");
  1096. X    equal(one, "", 7);            /* Boundary conditions. */
  1097. X    (void) strcpy(one, "ab");
  1098. X    (void) strcat(one, "");
  1099. X    equal(one, "ab", 8);
  1100. X    (void) strcpy(one, "");
  1101. X    (void) strcat(one, "cd");
  1102. X    equal(one, "cd", 9);
  1103. X
  1104. X    /*
  1105. X     * strncat - first test it as strcat, with big counts, then
  1106. X     * test the count mechanism.
  1107. X     */
  1108. X    it = "strncat";
  1109. X    (void) strcpy(one, "ijk");
  1110. X    check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  1111. X    equal(one, "ijklmn", 2);        /* Basic test. */
  1112. X
  1113. X    (void) strcpy(one, "x");
  1114. X    (void) strncat(one, "yz", 99);
  1115. X    equal(one, "xyz", 3);            /* Writeover. */
  1116. X    equal(one+4, "mn", 4);            /* Wrote too much? */
  1117. X
  1118. X    (void) strcpy(one, "gh");
  1119. X    (void) strcpy(two, "ef");
  1120. X    (void) strncat(one, two, 99);
  1121. X    equal(one, "ghef", 5);            /* Basic test encore. */
  1122. X    equal(two, "ef", 6);            /* Stomped on source? */
  1123. X
  1124. X    (void) strcpy(one, "");
  1125. X    (void) strncat(one, "", 99);
  1126. X    equal(one, "", 7);            /* Boundary conditions. */
  1127. X    (void) strcpy(one, "ab");
  1128. X    (void) strncat(one, "", 99);
  1129. X    equal(one, "ab", 8);
  1130. X    (void) strcpy(one, "");
  1131. X    (void) strncat(one, "cd", 99);
  1132. X    equal(one, "cd", 9);
  1133. X
  1134. X    (void) strcpy(one, "ab");
  1135. X    (void) strncat(one, "cdef", 2);
  1136. X    equal(one, "abcd", 10);            /* Count-limited. */
  1137. X
  1138. X    (void) strncat(one, "gh", 0);
  1139. X    equal(one, "abcd", 11);            /* Zero count. */
  1140. X
  1141. X    (void) strncat(one, "gh", 2);
  1142. X    equal(one, "abcdgh", 12);        /* Count and length equal. */
  1143. X
  1144. X    /*
  1145. X     * strncmp - first test as strcmp with big counts, then test
  1146. X     * count code.
  1147. X     */
  1148. X    it = "strncmp";
  1149. X    check(strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  1150. X    check(strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  1151. X    check(strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  1152. X    check(strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  1153. X    check(strncmp("abcd", "abc", 99) > 0, 5);
  1154. X    check(strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  1155. X    check(strncmp("abce", "abcd", 99) > 0, 7);
  1156. X    check(strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  1157. X    if (charsigned)                /* Sign-bit comparison. */
  1158. X        check(strncmp("a\203", "a\003", 2) < 0, 9);
  1159. X    else
  1160. X        check(strncmp("a\203", "a\003", 2) > 0, 9);
  1161. X    check(strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  1162. X    check(strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  1163. X    check(strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  1164. X    check(strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  1165. X
  1166. X    /*
  1167. X     * strncpy - testing is a bit different because of odd semantics
  1168. X     */
  1169. X    it = "strncpy";
  1170. X    check(strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  1171. X    equal(one, "abc", 2);            /* Did the copy go right? */
  1172. X
  1173. X    (void) strcpy(one, "abcdefgh");
  1174. X    (void) strncpy(one, "xyz", 2);
  1175. X    equal(one, "xycdefgh", 3);        /* Copy cut by count. */
  1176. X
  1177. X    (void) strcpy(one, "abcdefgh");
  1178. X    (void) strncpy(one, "xyz", 3);        /* Copy cut just before NUL. */
  1179. X    equal(one, "xyzdefgh", 4);
  1180. X
  1181. X    (void) strcpy(one, "abcdefgh");
  1182. X    (void) strncpy(one, "xyz", 4);        /* Copy just includes NUL. */
  1183. X    equal(one, "xyz", 5);
  1184. X    equal(one+4, "efgh", 6);        /* Wrote too much? */
  1185. X
  1186. X    (void) strcpy(one, "abcdefgh");
  1187. X    (void) strncpy(one, "xyz", 5);        /* Copy includes padding. */
  1188. X    equal(one, "xyz", 7);
  1189. X    equal(one+4, "", 8);
  1190. X    equal(one+5, "fgh", 9);
  1191. X
  1192. X    (void) strcpy(one, "abc");
  1193. X    (void) strncpy(one, "xyz", 0);        /* Zero-length copy. */
  1194. X    equal(one, "abc", 10);    
  1195. X
  1196. X    (void) strncpy(one, "", 2);        /* Zero-length source. */
  1197. X    equal(one, "", 11);
  1198. X    equal(one+1, "", 12);    
  1199. X    equal(one+2, "c", 13);
  1200. X
  1201. X    (void) strcpy(one, "hi there");
  1202. X    (void) strncpy(two, one, 9);
  1203. X    equal(two, "hi there", 14);        /* Just paranoia. */
  1204. X    equal(one, "hi there", 15);        /* Stomped on source? */
  1205. X
  1206. X    /*
  1207. X     * strlen
  1208. X     */
  1209. X    it = "strlen";
  1210. X    check(strlen("") == 0, 1);        /* Empty. */
  1211. X    check(strlen("a") == 1, 2);        /* Single char. */
  1212. X    check(strlen("abcd") == 4, 3);        /* Multiple chars. */
  1213. X
  1214. X    /*
  1215. X     * strchr
  1216. X     */
  1217. X    it = "strchr";
  1218. X    check(strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  1219. X    (void) strcpy(one, "abcd");
  1220. X    check(strchr(one, 'c') == one+2, 2);    /* Basic test. */
  1221. X    check(strchr(one, 'd') == one+3, 3);    /* End of string. */
  1222. X    check(strchr(one, 'a') == one, 4);    /* Beginning. */
  1223. X    check(strchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  1224. X    (void) strcpy(one, "ababa");
  1225. X    check(strchr(one, 'b') == one+1, 6);    /* Finding first. */
  1226. X    (void) strcpy(one, "");
  1227. X    check(strchr(one, 'b') == NULL, 7);    /* Empty string. */
  1228. X    check(strchr(one, '\0') == one, 8);    /* NUL in empty string. */
  1229. X
  1230. X    /*
  1231. X     * index - just like strchr
  1232. X     */
  1233. X    it = "index";
  1234. X    check(index("abcd", 'z') == NULL, 1);    /* Not found. */
  1235. X    (void) strcpy(one, "abcd");
  1236. X    check(index(one, 'c') == one+2, 2);    /* Basic test. */
  1237. X    check(index(one, 'd') == one+3, 3);    /* End of string. */
  1238. X    check(index(one, 'a') == one, 4);    /* Beginning. */
  1239. X    check(index(one, '\0') == one+4, 5);    /* Finding NUL. */
  1240. X    (void) strcpy(one, "ababa");
  1241. X    check(index(one, 'b') == one+1, 6);    /* Finding first. */
  1242. X    (void) strcpy(one, "");
  1243. X    check(index(one, 'b') == NULL, 7);    /* Empty string. */
  1244. X    check(index(one, '\0') == one, 8);    /* NUL in empty string. */
  1245. X
  1246. X    /*
  1247. X     * strrchr
  1248. X     */
  1249. X    it = "strrchr";
  1250. X    check(strrchr("abcd", 'z') == NULL, 1);    /* Not found. */
  1251. X    (void) strcpy(one, "abcd");
  1252. X    check(strrchr(one, 'c') == one+2, 2);    /* Basic test. */
  1253. X    check(strrchr(one, 'd') == one+3, 3);    /* End of string. */
  1254. X    check(strrchr(one, 'a') == one, 4);    /* Beginning. */
  1255. X    check(strrchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  1256. X    (void) strcpy(one, "ababa");
  1257. X    check(strrchr(one, 'b') == one+3, 6);    /* Finding last. */
  1258. X    (void) strcpy(one, "");
  1259. X    check(strrchr(one, 'b') == NULL, 7);    /* Empty string. */
  1260. X    check(strrchr(one, '\0') == one, 8);    /* NUL in empty string. */
  1261. X
  1262. X    /*
  1263. X     * rindex - just like strrchr
  1264. X     */
  1265. X    it = "rindex";
  1266. X    check(rindex("abcd", 'z') == NULL, 1);    /* Not found. */
  1267. X    (void) strcpy(one, "abcd");
  1268. X    check(rindex(one, 'c') == one+2, 2);    /* Basic test. */
  1269. X    check(rindex(one, 'd') == one+3, 3);    /* End of string. */
  1270. X    check(rindex(one, 'a') == one, 4);    /* Beginning. */
  1271. X    check(rindex(one, '\0') == one+4, 5);    /* Finding NUL. */
  1272. X    (void) strcpy(one, "ababa");
  1273. X    check(rindex(one, 'b') == one+3, 6);    /* Finding last. */
  1274. X    (void) strcpy(one, "");
  1275. X    check(rindex(one, 'b') == NULL, 7);    /* Empty string. */
  1276. X    check(rindex(one, '\0') == one, 8);    /* NUL in empty string. */
  1277. }
  1278. X
  1279. second()
  1280. {
  1281. X    /*
  1282. X     * strpbrk - somewhat like strchr
  1283. X     */
  1284. X    it = "strpbrk";
  1285. X    check(strpbrk("abcd", "z") == NULL, 1);    /* Not found. */
  1286. X    (void) strcpy(one, "abcd");
  1287. X    check(strpbrk(one, "c") == one+2, 2);    /* Basic test. */
  1288. X    check(strpbrk(one, "d") == one+3, 3);    /* End of string. */
  1289. X    check(strpbrk(one, "a") == one, 4);    /* Beginning. */
  1290. X    check(strpbrk(one, "") == NULL, 5);    /* Empty search list. */
  1291. X    check(strpbrk(one, "cb") == one+1, 6);    /* Multiple search. */
  1292. X    (void) strcpy(one, "abcabdea");
  1293. X    check(strpbrk(one, "b") == one+1, 7);    /* Finding first. */
  1294. X    check(strpbrk(one, "cb") == one+1, 8);    /* With multiple search. */
  1295. X    check(strpbrk(one, "db") == one+1, 9);    /* Another variant. */
  1296. X    (void) strcpy(one, "");
  1297. X    check(strpbrk(one, "bc") == NULL, 10);    /* Empty string. */
  1298. X    check(strpbrk(one, "") == NULL, 11);    /* Both strings empty. */
  1299. X
  1300. #if 0
  1301. X    /*
  1302. X     * strstr - somewhat like strchr
  1303. X     */
  1304. X    it = "strstr";
  1305. X    check(strstr("abcd", "z") == NULL, 1);    /* Not found. */
  1306. X    check(strstr("abcd", "abx") == NULL, 2);    /* Dead end. */
  1307. X    (void) strcpy(one, "abcd");
  1308. X    check(strstr(one, "c") == one+2, 3);    /* Basic test. */
  1309. X    check(strstr(one, "bc") == one+1, 4);    /* Multichar. */
  1310. X    check(strstr(one, "d") == one+3, 5);    /* End of string. */
  1311. X    check(strstr(one, "cd") == one+2, 6);    /* Tail of string. */
  1312. X    check(strstr(one, "abc") == one, 7);    /* Beginning. */
  1313. X    check(strstr(one, "abcd") == one, 8);    /* Exact match. */
  1314. X    check(strstr(one, "abcde") == NULL, 9);    /* Too long. */
  1315. X    check(strstr(one, "de") == NULL, 10);    /* Past end. */
  1316. X    check(strstr(one, "") == one+4, 11);    /* Finding empty. */
  1317. X    (void) strcpy(one, "ababa");
  1318. X    check(strstr(one, "ba") == one+1, 12);    /* Finding first. */
  1319. X    (void) strcpy(one, "");
  1320. X    check(strstr(one, "b") == NULL, 13);    /* Empty string. */
  1321. X    check(strstr(one, "") == one, 14);    /* Empty in empty string. */
  1322. X    (void) strcpy(one, "bcbca");
  1323. X    check(strstr(one, "bca") == one+2, 15);    /* False start. */
  1324. X    (void) strcpy(one, "bbbcabbca");
  1325. X    check(strstr(one, "bbca") == one+1, 16);    /* With overlap. */
  1326. #endif
  1327. X
  1328. X    /*
  1329. X     * strspn
  1330. X     */
  1331. X    it = "strspn";
  1332. X    check(strspn("abcba", "abc") == 5, 1);    /* Whole string. */
  1333. X    check(strspn("abcba", "ab") == 2, 2);    /* Partial. */
  1334. X    check(strspn("abc", "qx") == 0, 3);    /* None. */
  1335. X    check(strspn("", "ab") == 0, 4);    /* Null string. */
  1336. X    check(strspn("abc", "") == 0, 5);    /* Null search list. */
  1337. X
  1338. X    /*
  1339. X     * strcspn
  1340. X     */
  1341. X    it = "strcspn";
  1342. X    check(strcspn("abcba", "qx") == 5, 1);    /* Whole string. */
  1343. X    check(strcspn("abcba", "cx") == 2, 2);    /* Partial. */
  1344. X    check(strcspn("abc", "abc") == 0, 3);    /* None. */
  1345. X    check(strcspn("", "ab") == 0, 4);    /* Null string. */
  1346. X    check(strcspn("abc", "") == 3, 5);    /* Null search list. */
  1347. X
  1348. X    /*
  1349. X     * strtok - the hard one
  1350. X     */
  1351. X    it = "strtok";
  1352. X    (void) strcpy(one, "first, second, third");
  1353. X    equal(strtok(one, ", "), "first", 1);    /* Basic test. */
  1354. X    equal(one, "first", 2);
  1355. X    equal(strtok((char *)NULL, ", "), "second", 3);
  1356. X    equal(strtok((char *)NULL, ", "), "third", 4);
  1357. X    check(strtok((char *)NULL, ", ") == NULL, 5);
  1358. X    (void) strcpy(one, ", first, ");
  1359. X    equal(strtok(one, ", "), "first", 6);    /* Extra delims, 1 tok. */
  1360. X    check(strtok((char *)NULL, ", ") == NULL, 7);
  1361. X    (void) strcpy(one, "1a, 1b; 2a, 2b");
  1362. X    equal(strtok(one, ", "), "1a", 8);    /* Changing delim lists. */
  1363. X    equal(strtok((char *)NULL, "; "), "1b", 9);
  1364. X    equal(strtok((char *)NULL, ", "), "2a", 10);
  1365. X    (void) strcpy(two, "x-y");
  1366. X    equal(strtok(two, "-"), "x", 11);    /* New string before done. */
  1367. X    equal(strtok((char *)NULL, "-"), "y", 12);
  1368. X    check(strtok((char *)NULL, "-") == NULL, 13);
  1369. X    (void) strcpy(one, "a,b, c,, ,d");
  1370. X    equal(strtok(one, ", "), "a", 14);    /* Different separators. */
  1371. X    equal(strtok((char *)NULL, ", "), "b", 15);
  1372. X    equal(strtok((char *)NULL, " ,"), "c", 16);    /* Permute list too. */
  1373. X    equal(strtok((char *)NULL, " ,"), "d", 17);
  1374. X    check(strtok((char *)NULL, ", ") == NULL, 18);
  1375. X    check(strtok((char *)NULL, ", ") == NULL, 19);    /* Persistence. */
  1376. X    (void) strcpy(one, ", ");
  1377. X    check(strtok(one, ", ") == NULL, 20);    /* No tokens. */
  1378. X    (void) strcpy(one, "");
  1379. X    check(strtok(one, ", ") == NULL, 21);    /* Empty string. */
  1380. X    (void) strcpy(one, "abc");
  1381. X    equal(strtok(one, ", "), "abc", 22);    /* No delimiters. */
  1382. X    check(strtok((char *)NULL, ", ") == NULL, 23);
  1383. X    (void) strcpy(one, "abc");
  1384. X    equal(strtok(one, ""), "abc", 24);    /* Empty delimiter list. */
  1385. X    check(strtok((char *)NULL, "") == NULL, 25);
  1386. X    (void) strcpy(one, "abcdefgh");
  1387. X    (void) strcpy(one, "a,b,c");
  1388. X    equal(strtok(one, ","), "a", 26);    /* Basics again... */
  1389. X    equal(strtok((char *)NULL, ","), "b", 27);
  1390. X    equal(strtok((char *)NULL, ","), "c", 28);
  1391. X    check(strtok((char *)NULL, ",") == NULL, 29);
  1392. X    equal(one+6, "gh", 30);            /* Stomped past end? */
  1393. X    equal(one, "a", 31);            /* Stomped old tokens? */
  1394. X    equal(one+2, "b", 32);
  1395. X    equal(one+4, "c", 33);
  1396. X
  1397. X    /*
  1398. X     * memcmp
  1399. X     */
  1400. X    it = "memcmp";
  1401. X    check(memcmp("a", "a", 1) == 0, 1);    /* Identity. */
  1402. X    check(memcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  1403. X    check(memcmp("abcd", "abce", 4) < 0, 3);    /* Honestly unequal. */
  1404. X    check(memcmp("abce", "abcd", 4) > 0, 4);
  1405. X    check(memcmp("alph", "beta", 4) < 0, 5);
  1406. X    if (charsigned)                /* Sign-bit comparison. */
  1407. X        check(memcmp("a\203", "a\003", 2) < 0, 6);
  1408. X    else
  1409. X        check(memcmp("a\203", "a\003", 2) > 0, 6);
  1410. X    check(memcmp("abce", "abcd", 3) == 0, 7);    /* Count limited. */
  1411. X    check(memcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  1412. X
  1413. X    /*
  1414. X     * memchr
  1415. X     */
  1416. X    it = "memchr";
  1417. X    check(memchr("abcd", 'z', 4) == NULL, 1);    /* Not found. */
  1418. X    (void) strcpy(one, "abcd");
  1419. X    check(memchr(one, 'c', 4) == one+2, 2);    /* Basic test. */
  1420. X    check(memchr(one, 'd', 4) == one+3, 3);    /* End of string. */
  1421. X    check(memchr(one, 'a', 4) == one, 4);    /* Beginning. */
  1422. X    check(memchr(one, '\0', 5) == one+4, 5);    /* Finding NUL. */
  1423. X    (void) strcpy(one, "ababa");
  1424. X    check(memchr(one, 'b', 5) == one+1, 6);    /* Finding first. */
  1425. X    check(memchr(one, 'b', 0) == NULL, 7);    /* Zero count. */
  1426. X    check(memchr(one, 'a', 1) == one, 8);    /* Singleton case. */
  1427. X    (void) strcpy(one, "a\203b");
  1428. X    check(memchr(one, 0203, 3) == one+1, 9);    /* Unsignedness. */
  1429. X
  1430. X    /*
  1431. X     * memcpy
  1432. X     *
  1433. X     * Note that X3J11 says memcpy must work regardless of overlap.
  1434. X     * The SVID says it might fail.
  1435. X     */
  1436. X    it = "memcpy";
  1437. X    check(memcpy(one, "abc", 4) == one, 1);    /* Returned value. */
  1438. X    equal(one, "abc", 2);            /* Did the copy go right? */
  1439. X
  1440. X    (void) strcpy(one, "abcdefgh");
  1441. X    (void) memcpy(one+1, "xyz", 2);
  1442. X    equal(one, "axydefgh", 3);        /* Basic test. */
  1443. X
  1444. X    (void) strcpy(one, "abc");
  1445. X    (void) memcpy(one, "xyz", 0);
  1446. X    equal(one, "abc", 4);            /* Zero-length copy. */
  1447. X
  1448. X    (void) strcpy(one, "hi there");
  1449. X    (void) strcpy(two, "foo");
  1450. X    (void) memcpy(two, one, 9);
  1451. X    equal(two, "hi there", 5);        /* Just paranoia. */
  1452. X    equal(one, "hi there", 6);        /* Stomped on source? */
  1453. X
  1454. X    (void) strcpy(one, "abcdefgh");
  1455. X    (void) memcpy(one+1, one, 9);
  1456. X    equal(one, "aabcdefgh", 7);        /* Overlap, right-to-left. */
  1457. X
  1458. X    (void) strcpy(one, "abcdefgh");
  1459. X    (void) memcpy(one+1, one+2, 7);
  1460. X    equal(one, "acdefgh", 8);        /* Overlap, left-to-right. */
  1461. X
  1462. X    (void) strcpy(one, "abcdefgh");
  1463. X    (void) memcpy(one, one, 9);
  1464. X    equal(one, "abcdefgh", 9);        /* 100% overlap. */
  1465. X
  1466. X    /*
  1467. X     * memccpy - first test like memcpy, then the search part
  1468. X     *
  1469. X     * The SVID, the only place where memccpy is mentioned, says
  1470. X     * overlap might fail, so we don't try it.  Besides, it's hard
  1471. X     * to see the rationale for a non-left-to-right memccpy.
  1472. X     */
  1473. X    it = "memccpy";
  1474. X    check(memccpy(one, "abc", 'q', 4) == NULL, 1);    /* Returned value. */
  1475. X    equal(one, "abc", 2);            /* Did the copy go right? */
  1476. X
  1477. X    (void) strcpy(one, "abcdefgh");
  1478. X    (void) memccpy(one+1, "xyz", 'q', 2);
  1479. X    equal(one, "axydefgh", 3);        /* Basic test. */
  1480. X
  1481. X    (void) strcpy(one, "abc");
  1482. X    (void) memccpy(one, "xyz", 'q', 0);
  1483. X    equal(one, "abc", 4);            /* Zero-length copy. */
  1484. X
  1485. X    (void) strcpy(one, "hi there");
  1486. X    (void) strcpy(two, "foo");
  1487. X    (void) memccpy(two, one, 'q', 9);
  1488. X    equal(two, "hi there", 5);        /* Just paranoia. */
  1489. X    equal(one, "hi there", 6);        /* Stomped on source? */
  1490. X
  1491. X    (void) strcpy(one, "abcdefgh");
  1492. X    (void) strcpy(two, "horsefeathers");
  1493. X    check(memccpy(two, one, 'f', 9) == two+6, 7);    /* Returned value. */
  1494. X    equal(one, "abcdefgh", 8);        /* Source intact? */
  1495. X    equal(two, "abcdefeathers", 9);        /* Copy correct? */
  1496. X
  1497. X    (void) strcpy(one, "abcd");
  1498. X    (void) strcpy(two, "bumblebee");
  1499. X    check(memccpy(two, one, 'a', 4) == two+1, 10);    /* First char. */
  1500. X    equal(two, "aumblebee", 11);
  1501. X    check(memccpy(two, one, 'd', 4) == two+4, 12);    /* Last char. */
  1502. X    equal(two, "abcdlebee", 13);
  1503. X    (void) strcpy(one, "xyz");
  1504. X    check(memccpy(two, one, 'x', 1) == two+1, 14);    /* Singleton. */
  1505. X    equal(two, "xbcdlebee", 15);
  1506. X
  1507. X    /*
  1508. X     * memset
  1509. X     */
  1510. X    it = "memset";
  1511. X    (void) strcpy(one, "abcdefgh");
  1512. X    check(memset(one+1, 'x', 3) == one+1, 1);    /* Return value. */
  1513. X    equal(one, "axxxefgh", 2);        /* Basic test. */
  1514. X
  1515. X    (void) memset(one+2, 'y', 0);
  1516. X    equal(one, "axxxefgh", 3);        /* Zero-length set. */
  1517. X
  1518. X    (void) memset(one+5, 0, 1);
  1519. X    equal(one, "axxxe", 4);            /* Zero fill. */
  1520. X    equal(one+6, "gh", 5);            /* And the leftover. */
  1521. X
  1522. X    (void) memset(one+2, 010045, 1);
  1523. X    equal(one, "ax\045xe", 6);        /* Unsigned char convert. */
  1524. X
  1525. X    /*
  1526. X     * bcopy - much like memcpy
  1527. X     *
  1528. X     * Berklix manual is silent about overlap, so don't test it.
  1529. X     */
  1530. X    it = "bcopy";
  1531. X    (void) bcopy("abc", one, 4);
  1532. X    equal(one, "abc", 1);            /* Simple copy. */
  1533. X
  1534. X    (void) strcpy(one, "abcdefgh");
  1535. X    (void) bcopy("xyz", one+1, 2);
  1536. X    equal(one, "axydefgh", 2);        /* Basic test. */
  1537. X
  1538. X    (void) strcpy(one, "abc");
  1539. X    (void) bcopy("xyz", one, 0);
  1540. X    equal(one, "abc", 3);            /* Zero-length copy. */
  1541. X
  1542. X    (void) strcpy(one, "hi there");
  1543. X    (void) strcpy(two, "foo");
  1544. X    (void) bcopy(one, two, 9);
  1545. X    equal(two, "hi there", 4);        /* Just paranoia. */
  1546. X    equal(one, "hi there", 5);        /* Stomped on source? */
  1547. X
  1548. X    /*
  1549. X     * bzero
  1550. X     */
  1551. X    it = "bzero";
  1552. X    (void) strcpy(one, "abcdef");
  1553. X    bzero(one+2, 2);
  1554. X    equal(one, "ab", 1);            /* Basic test. */
  1555. X    equal(one+3, "", 2);
  1556. X    equal(one+4, "ef", 3);
  1557. X
  1558. X    (void) strcpy(one, "abcdef");
  1559. X    bzero(one+2, 0);
  1560. X    equal(one, "abcdef", 4);        /* Zero-length copy. */
  1561. X
  1562. X    /*
  1563. X     * bcmp - somewhat like memcmp
  1564. X     */
  1565. X    it = "bcmp";
  1566. X    check(bcmp("a", "a", 1) == 0, 1);    /* Identity. */
  1567. X    check(bcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  1568. X    check(bcmp("abcd", "abce", 4) != 0, 3);    /* Honestly unequal. */
  1569. X    check(bcmp("abce", "abcd", 4) != 0, 4);
  1570. X    check(bcmp("alph", "beta", 4) != 0, 5);
  1571. X    check(bcmp("abce", "abcd", 3) == 0, 6);    /* Count limited. */
  1572. X    check(bcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  1573. X
  1574. #ifdef ERR
  1575. X    /*
  1576. X     * strerror - VERY system-dependent
  1577. X     */
  1578. X    it = "strerror";
  1579. X    f = open("/", 1);    /* Should always fail. */
  1580. X    check(f < 0 && errno > 0 && errno < sys_nerr, 1);
  1581. X    equal(strerror(errno), sys_errlist[errno], 2);
  1582. #ifdef UNIXERR
  1583. X    equal(strerror(errno), "Is a directory", 3);
  1584. #endif
  1585. #ifdef BERKERR
  1586. X    equal(strerror(errno), "Permission denied", 3);
  1587. #endif
  1588. #endif
  1589. }
  1590. SHAR_EOF
  1591. chmod 0640 dmake/dbug/malloc/testmem.c ||
  1592. echo 'restore of dmake/dbug/malloc/testmem.c failed'
  1593. Wc_c="`wc -c < 'dmake/dbug/malloc/testmem.c'`"
  1594. test 20192 -eq "$Wc_c" ||
  1595.     echo 'dmake/dbug/malloc/testmem.c: original size 20192, current size' "$Wc_c"
  1596. rm -f _shar_wnt_.tmp
  1597. fi
  1598. # ============= dmake/dbug/malloc/testmlc.c ==============
  1599. if test -f 'dmake/dbug/malloc/testmlc.c' -a X"$1" != X"-c"; then
  1600.     echo 'x - skipping dmake/dbug/malloc/testmlc.c (File already exists)'
  1601.     rm -f _shar_wnt_.tmp
  1602. else
  1603. > _shar_wnt_.tmp
  1604. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/testmlc.c' &&
  1605. /* NOT copyright by SoftQuad Inc. -- msb, 1988 */
  1606. SHAR_EOF
  1607. true || echo 'restore of dmake/dbug/malloc/testmlc.c failed'
  1608. fi
  1609. echo 'End of part 6, continue with part 7'
  1610. echo 7 > _shar_seq_.tmp
  1611. exit 0
  1612. exit 0 # Just in case...
  1613.