home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume19 / dmake / part06 < prev    next >
Encoding:
Text File  |  1991-05-11  |  40.2 KB  |  1,921 lines

  1. Newsgroups: comp.sources.misc
  2. From: Dennis Vadura <dvadura@watdragon.waterloo.edu>
  3. Subject:  v19i027:  dmake - dmake version 3.7, Part06/37
  4. Message-ID: <1991May10.185441.21075@sparky.IMD.Sterling.COM>
  5. Date: Fri, 10 May 1991 18:54:41 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7.  
  8. Submitted-by: Dennis Vadura <dvadura@watdragon.waterloo.edu>
  9. Posting-number: Volume 19, Issue 27
  10. Archive-name: dmake/part06
  11. Supersedes: dmake-3.6: Volume 15, Issue 52-77
  12.  
  13. ---- Cut Here and feed the following to sh ----
  14. #!/bin/sh
  15. # this is dmake.shar.06 (part 6 of a multipart archive)
  16. # do not concatenate these parts, unpack them in order with /bin/sh
  17. # file dmake/dbug/malloc/mallopt.c continued
  18. #
  19. if test ! -r _shar_seq_.tmp; then
  20.     echo 'Please unpack part 1 first!'
  21.     exit 1
  22. fi
  23. (read Scheck
  24.  if test "$Scheck" != 6; then
  25.     echo Please unpack part "$Scheck" next!
  26.     exit 1
  27.  else
  28.     exit 0
  29.  fi
  30. ) < _shar_seq_.tmp || exit 1
  31. if test -f _shar_wnt_.tmp; then
  32. sed 's/^X//' << 'SHAR_EOF' >> 'dmake/dbug/malloc/mallopt.c' &&
  33. X * You may copy, distribute, and use this software as long as this
  34. X * copyright statement is not removed.
  35. X */
  36. #include <stdio.h>
  37. #include <fcntl.h>
  38. #include "malloc.h"
  39. X
  40. /*
  41. X * Function:    mallopt()
  42. X *
  43. X * Purpose:    to set options for the malloc debugging library
  44. X *
  45. X * Arguments:    none
  46. X *
  47. X * Returns:    nothing of any value
  48. X *
  49. X * Narrative:    
  50. X *
  51. X */
  52. X
  53. #ifndef lint
  54. static
  55. char rcs_hdr[] = "$Id: mallopt.c,v 1.6 90/08/29 22:23:36 cpcahil Exp $";
  56. #endif
  57. X
  58. int
  59. mallopt(cmd,value)
  60. X    int              cmd;
  61. X    union malloptarg      value;
  62. {
  63. X    int              i;
  64. X    extern int          malloc_checking;
  65. X    extern char        * malloc_data_start;
  66. X    extern int          malloc_errfd;
  67. X    extern int          malloc_fatal_level;
  68. X    void              malloc_init();
  69. X    extern int          malloc_warn_level;
  70. X    register char        * s;
  71. X
  72. X    /*
  73. X      * If not initialized...
  74. X     */
  75. X    if( malloc_data_start == (char *) 0)
  76. X    {
  77. X        malloc_init();
  78. X    }
  79. X
  80. X
  81. X    switch(cmd)
  82. X    {
  83. X        case MALLOC_WARN:
  84. X            malloc_warn_level = value.i;
  85. X            break;
  86. X
  87. X        case MALLOC_FATAL:
  88. X            malloc_fatal_level = value.i;
  89. X            break;
  90. X
  91. X        case MALLOC_CKCHAIN:
  92. X            malloc_checking = value.i;
  93. X            break;
  94. X
  95. X        case MALLOC_ERRFILE:
  96. X            
  97. X            i = open(value.str,O_CREAT|O_APPEND|O_WRONLY,0666);
  98. X            if( i == -1 )
  99. X            {
  100. X                (void) write(2,
  101. X                      "Unable to open malloc error file: ",
  102. X                      (unsigned) 34);
  103. X                for(s=value.str; *s; s++)
  104. X                {
  105. X                    /* do nothing */;
  106. X                }
  107. X                (void) write(2,value.str,
  108. X                         (unsigned)(s-value.str));
  109. X                (void) write(2,"\n",(unsigned)1);
  110. X            }
  111. X            else
  112. X            {
  113. X                if( malloc_errfd != 2 )
  114. X                {
  115. X                    (void) close(malloc_errfd);
  116. X                }
  117. X                malloc_errfd = i;
  118. X            }
  119. X            
  120. X            break;
  121. X
  122. X        default:
  123. X            return(1);
  124. X    }
  125. X
  126. X    return(0);
  127. }
  128. X
  129. /*
  130. X * $Log:    mallopt.c,v $
  131. X * Revision 1.6  90/08/29  22:23:36  cpcahil
  132. X * fixed mallopt to use a union as an argument.
  133. X * 
  134. X * Revision 1.5  90/08/29  21:22:51  cpcahil
  135. X * miscellaneous lint fixes
  136. X * 
  137. X * Revision 1.4  90/05/11  00:13:10  cpcahil
  138. X * added copyright statment
  139. X * 
  140. X * Revision 1.3  90/02/25  11:03:26  cpcahil
  141. X * changed to return int so that it agrees with l libmalloc.a's mallopt()
  142. X * 
  143. X * Revision 1.2  90/02/25  11:01:21  cpcahil
  144. X * added support for malloc chain checking.
  145. X * 
  146. X * Revision 1.1  90/02/24  21:50:24  cpcahil
  147. X * Initial revision
  148. X * 
  149. X * Revision 1.1  90/02/24  17:10:53  cpcahil
  150. X * Initial revision
  151. X * 
  152. X */
  153. SHAR_EOF
  154. chmod 0640 dmake/dbug/malloc/mallopt.c ||
  155. echo 'restore of dmake/dbug/malloc/mallopt.c failed'
  156. Wc_c="`wc -c < 'dmake/dbug/malloc/mallopt.c'`"
  157. test 2281 -eq "$Wc_c" ||
  158.     echo 'dmake/dbug/malloc/mallopt.c: original size 2281, current size' "$Wc_c"
  159. rm -f _shar_wnt_.tmp
  160. fi
  161. # ============= dmake/dbug/malloc/memory.c ==============
  162. if test -f 'dmake/dbug/malloc/memory.c' -a X"$1" != X"-c"; then
  163.     echo 'x - skipping dmake/dbug/malloc/memory.c (File already exists)'
  164.     rm -f _shar_wnt_.tmp
  165. else
  166. > _shar_wnt_.tmp
  167. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/memory.c' &&
  168. /*
  169. X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  170. X * You may copy, distribute, and use this software as long as this
  171. X * copyright statement is not removed.
  172. X */
  173. X
  174. #ifndef lint
  175. static
  176. char rcs_hdr[] = "$Id: memory.c,v 1.7 90/08/29 21:27:58 cpcahil Exp $";
  177. #endif
  178. X
  179. void malloc_check_data();
  180. X
  181. char *
  182. memccpy(ptr1, ptr2, ch, len)
  183. X    register char    * ptr1;
  184. X    register char    * ptr2;
  185. X    int          len;
  186. X    int          ch;
  187. {
  188. X    int          check;
  189. X    register int      i;
  190. X    char        * rtn;
  191. X
  192. X    /*
  193. X     * I know that the assignment could be done in the following, but
  194. X     * I wanted to perform a check before any assignment, so first I 
  195. X     * determine the length, check the pointers and then do the assignment.
  196. X     */
  197. X    for( i=0; (i < len) && (ptr2[i] != ch); i++)
  198. X    {
  199. X    }
  200. X    if( ptr2[i] == ch )
  201. X    {
  202. X        check = i+1;
  203. X    }
  204. X    else
  205. X    {
  206. X        check = len;
  207. X    }
  208. X
  209. X    malloc_check_data("memccpy", ptr1, check);
  210. X    malloc_check_data("memccpy", ptr2, check);
  211. X
  212. X    /*
  213. X     * if we found the character...
  214. X      */
  215. X
  216. X    if( i < len )
  217. X    {
  218. X        rtn = ptr1+i+1;
  219. X        i++;
  220. X    }
  221. X    else
  222. X    {
  223. X        rtn = (char *) 0;
  224. X    }
  225. X
  226. X     while( i-- )
  227. X    {
  228. X        *(ptr1++) = *(ptr2++);
  229. X    }
  230. X    
  231. X    return(rtn);
  232. }
  233. X
  234. char *
  235. memchr(ptr1,ch,len)
  236. X    register char    * ptr1;
  237. X    register int      ch;
  238. X    int          len;
  239. {
  240. X    int          i;
  241. X
  242. X    for( i=0; (i < len) && (ptr1[i] != (char) ch); i++)
  243. X    {
  244. X    }
  245. X
  246. X    malloc_check_data("memchr", ptr1, i);
  247. X
  248. X    if( i < len )
  249. X    {
  250. X        return( ptr1+i );
  251. X    }
  252. X    else
  253. X    {
  254. X        return( (char *) 0);    
  255. X    }
  256. }
  257. X
  258. char *
  259. memcpy(ptr1, ptr2, len)
  260. X    register char    * ptr1;
  261. X    register char    * ptr2;
  262. X    register int      len;
  263. {
  264. X    char        * rtn = ptr1;
  265. X
  266. X    malloc_check_data("memcpy", ptr1, len);
  267. X    malloc_check_data("memcpy", ptr2, len);
  268. X
  269. X    /*
  270. X     * while the normal memcpy does not guarrantee that it will 
  271. X     * handle overlapping memory correctly, we will try...
  272. X     */
  273. X    if( ptr1 > ptr2  && ptr1 < (ptr2+len))
  274. X    {
  275. X        ptr1 += (len-1);
  276. X        ptr2 += (len-1);
  277. X        while( len-- > 0 )
  278. X        {
  279. X            *(ptr1--) = *(ptr2--);
  280. X        }
  281. X    }
  282. X    else
  283. X    {
  284. X        while( len-- > 0 )
  285. X        {
  286. X            *(ptr1++) = *(ptr2++);
  287. X        }
  288. X    }
  289. X    
  290. X    return(rtn);
  291. }
  292. X
  293. int
  294. memcmp(ptr1, ptr2, len)
  295. X    register char    * ptr1;
  296. X    register char    * ptr2;
  297. X    register int      len;
  298. {
  299. X    malloc_check_data("memcpy", ptr1, len);
  300. X    malloc_check_data("memcpy", ptr2, len);
  301. X
  302. X    while( --len >= 0  && (*ptr1 == *ptr2) )
  303. X    {
  304. X        ptr1++;
  305. X        ptr2++;
  306. X    }
  307. X
  308. X    /* 
  309. X     * If stopped by len, return zero
  310. X     */
  311. X    if( len < 0 )
  312. X    {
  313. X        return(0);
  314. X    }
  315. X
  316. X    return( *ptr1 - *ptr2 );
  317. }
  318. X
  319. char * 
  320. memset(ptr1, ch, len)
  321. X    register char    * ptr1;
  322. X    register int      ch;
  323. X    register int      len;
  324. {
  325. X    char        * rtn = ptr1;
  326. X
  327. X    malloc_check_data("memcpy", ptr1, len);
  328. X
  329. X    while( len-- )
  330. X    {
  331. X        *(ptr1++) = ch;
  332. X    }
  333. X
  334. X    return(rtn);
  335. }
  336. X
  337. char *
  338. bcopy(ptr2,ptr1,len)
  339. X    char        * ptr2;
  340. X    char        * ptr1;
  341. X    int          len;
  342. {
  343. X    return(memcpy(ptr1,ptr2,len));
  344. }
  345. X
  346. char *
  347. bzero(ptr1,len)
  348. X    char        * ptr1;
  349. X    int          len;
  350. {
  351. X    return(memset(ptr1,'\0',len));
  352. }
  353. X
  354. int
  355. bcmp(ptr2, ptr1, len)
  356. X    char        * ptr1;
  357. X    char        * ptr2;
  358. X    int          len;
  359. {
  360. X    return( memcmp(ptr1,ptr2,len) );
  361. }
  362. X
  363. /*
  364. X * $Log:    memory.c,v $
  365. X * Revision 1.7  90/08/29  21:27:58  cpcahil
  366. X * fixed value of check in memccpy when character was not found.
  367. X * 
  368. X * Revision 1.6  90/07/16  20:06:26  cpcahil
  369. X * fixed several minor bugs found with Henry Spencer's string/mem tester 
  370. X * program.
  371. X * 
  372. X * 
  373. X * Revision 1.5  90/05/11  15:39:36  cpcahil
  374. X * fixed bug in memccpy().
  375. X * 
  376. X * Revision 1.4  90/05/11  00:13:10  cpcahil
  377. X * added copyright statment
  378. X * 
  379. X * Revision 1.3  90/02/24  21:50:29  cpcahil
  380. X * lots of lint fixes
  381. X * 
  382. X * Revision 1.2  90/02/24  17:29:41  cpcahil
  383. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  384. X * id string
  385. X * 
  386. X * Revision 1.1  90/02/22  23:17:43  cpcahil
  387. X * Initial revision
  388. X * 
  389. X */
  390. SHAR_EOF
  391. chmod 0640 dmake/dbug/malloc/memory.c ||
  392. echo 'restore of dmake/dbug/malloc/memory.c failed'
  393. Wc_c="`wc -c < 'dmake/dbug/malloc/memory.c'`"
  394. test 3505 -eq "$Wc_c" ||
  395.     echo 'dmake/dbug/malloc/memory.c: original size 3505, current size' "$Wc_c"
  396. rm -f _shar_wnt_.tmp
  397. fi
  398. # ============= dmake/dbug/malloc/mlc_chk.c ==============
  399. if test -f 'dmake/dbug/malloc/mlc_chk.c' -a X"$1" != X"-c"; then
  400.     echo 'x - skipping dmake/dbug/malloc/mlc_chk.c (File already exists)'
  401.     rm -f _shar_wnt_.tmp
  402. else
  403. > _shar_wnt_.tmp
  404. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/mlc_chk.c' &&
  405. /*
  406. X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  407. X * You may copy, distribute, and use this software as long as this
  408. X * copyright statement is not removed.
  409. X */
  410. X
  411. #include <stdio.h>
  412. #include "malloc.h"
  413. #include "debug.h"
  414. X
  415. #ifndef lint
  416. static
  417. char rcs_hdr[] = "$Id: malloc_chk.c,v 1.5 90/08/29 22:23:48 cpcahil Exp $";
  418. #endif
  419. X
  420. extern struct mlist      malloc_start;
  421. extern struct mlist    * malloc_end;
  422. extern char        * malloc_data_start;
  423. extern char        * malloc_data_end;
  424. X
  425. /*
  426. X * Function:    malloc_in_arena()
  427. X *
  428. X * Purpose:    to verify address is within malloc arena.
  429. X *
  430. X * Arguments:    ptr    - pointer to verify
  431. X *
  432. X * Returns:    TRUE    - if pointer is within malloc area
  433. X *        FALSE    - otherwise
  434. X *
  435. X * Narrative:
  436. X *   IF pointer is >= malloc area start AND <= malloc area end
  437. X *      return TRUE
  438. X *   ELSE
  439. X *      return FALSE
  440. X *
  441. X * Mod History:    
  442. X *   90/01/24    cpcahil        Initial revision.
  443. X */
  444. int
  445. malloc_in_arena(ptr)
  446. X    char    * ptr;
  447. {
  448. X    extern char    * malloc_data_start;
  449. X    extern char    * malloc_data_end;
  450. X    int          rtn = 0;
  451. X
  452. X    if( ptr >= malloc_data_start && ptr <= malloc_data_end )
  453. X    {
  454. X        rtn = 1;
  455. X    }
  456. X    
  457. X    return(rtn);
  458. }
  459. X
  460. /*
  461. X * Function:    malloc_check_str()
  462. X *
  463. X * Arguments:    func    - name of function calling this routine
  464. X *        str    - pointer to area to check
  465. X *
  466. X * Purpose:    to verify that if str is within the malloc arena, the data 
  467. X *        it points to does not extend beyond the applicable region.
  468. X *
  469. X * Returns:    Nothing of any use (function is void).
  470. X *
  471. X * Narrative:
  472. X *   IF pointer is within malloc arena
  473. X *      determin length of string
  474. X *      call malloc_verify() to verify data is withing applicable region
  475. X *   return 
  476. X *
  477. X * Mod History:    
  478. X *   90/01/24    cpcahil        Initial revision.
  479. X *   90/01/29    cpcahil        Added code to ignore recursive calls.
  480. X */
  481. void
  482. malloc_check_str(func,str)
  483. X    char        * func;
  484. X    char        * str;
  485. {
  486. X    static int      layers;
  487. X    register char    * s;
  488. X
  489. X    if( (layers++ == 0) &&  malloc_in_arena(str) )
  490. X    {
  491. X        for( s=str; *s; s++)
  492. X        {
  493. X        }
  494. X        
  495. X        malloc_verify(func,str,s-str+1);
  496. X    }
  497. X
  498. X    layers--;
  499. }
  500. X
  501. /*
  502. X * Function:    malloc_check_strn()
  503. X *
  504. X * Arguments:    func    - name of function calling this routine
  505. X *        str    - pointer to area to check
  506. X *         len     - max length of string
  507. X *
  508. X * Purpose:    to verify that if str is within the malloc arena, the data 
  509. X *        it points to does not extend beyond the applicable region.
  510. X *
  511. X * Returns:    Nothing of any use (function is void).
  512. X *
  513. X * Narrative:
  514. X *   IF pointer is within malloc arena
  515. X *      determin length of string
  516. X *      call malloc_verify() to verify data is withing applicable region
  517. X *   return 
  518. X *
  519. X * Mod History:    
  520. X *   90/01/24    cpcahil        Initial revision.
  521. X *   90/01/29    cpcahil        Added code to ignore recursive calls.
  522. X *   90/08/29    cpcahil        added length (for strn* functions)
  523. X */
  524. void
  525. malloc_check_strn(func,str,len)
  526. X    char        * func;
  527. X    char        * str;
  528. X    int          len;
  529. {
  530. X    register int      i;
  531. X    static int      layers;
  532. X    register char    * s;
  533. X
  534. X    if( (layers++ == 0) &&  malloc_in_arena(str) )
  535. X    {
  536. X        for( s=str,i=0; (i < len) && *s; s++)
  537. X        {
  538. X        }
  539. X        
  540. X        malloc_verify(func,str,s-str+1);
  541. X    }
  542. X
  543. X    layers--;
  544. }
  545. X
  546. /*
  547. X * Function:    malloc_check_data()
  548. X *
  549. X * Arguments:    func    - name of function calling this routine
  550. X *        ptr    - pointer to area to check
  551. X *        len     - length to verify
  552. X *
  553. X * Purpose:    to verify that if ptr is within the malloc arena, the data 
  554. X *        it points to does not extend beyond the applicable region.
  555. X *
  556. X * Returns:    Nothing of any use (function is void).
  557. X *
  558. X * Narrative:
  559. X *   IF pointer is within malloc arena
  560. X *      call malloc_verify() to verify data is withing applicable region
  561. X *   return 
  562. X *
  563. X * Mod History:    
  564. X *   90/01/24    cpcahil        Initial revision.
  565. X *   90/01/29    cpcahil        Added code to ignore recursive calls.
  566. X */
  567. void
  568. malloc_check_data(func,ptr,len)
  569. X    char        * func;
  570. X    char        * ptr;
  571. X    int          len;
  572. {
  573. X    static int      layers;
  574. X
  575. X    if( layers++ == 0 )
  576. X    {
  577. X        DEBUG3(40,"malloc_check_data(%s,0x%x,%d) called...",
  578. X            func,ptr,len);
  579. X        if( malloc_in_arena(ptr) )
  580. X        {
  581. X            DEBUG0(10,"pointer in malloc arena, verifying...");
  582. X            malloc_verify(func,ptr,len);
  583. X        }
  584. X    }
  585. X
  586. X    layers--;
  587. }
  588. X
  589. /*
  590. X * Function:    malloc_verify()
  591. X *
  592. X * Arguments:    func    - name of function calling the malloc check routines
  593. X *        ptr    - pointer to area to check
  594. X *        len     - length to verify
  595. X *
  596. X * Purpose:    to verify that the data ptr points to does not extend beyond
  597. X *        the applicable malloc region.  This function is only called 
  598. X *        if it has been determined that ptr points into the malloc arena.
  599. X *
  600. X * Returns:    Nothing of any use (function is void).
  601. X *
  602. X * Narrative:
  603. X *
  604. X * Mod History:    
  605. X *   90/01/24    cpcahil        Initial revision.
  606. X */
  607. void
  608. malloc_verify(func,ptr,len)
  609. X    char        * func;
  610. X    char        * ptr;
  611. X    int          len;
  612. {
  613. X    extern struct mlist    * malloc_end;
  614. X    extern int          malloc_errno;
  615. X    extern struct mlist       malloc_start;
  616. X    struct mlist        * mptr;
  617. X    
  618. X    DEBUG3(40,"malloc_verify(%s,0x%x,%d) called...", func,ptr,len);
  619. X    /*
  620. X     * Find the malloc block that includes this pointer
  621. X     */
  622. X    mptr = &malloc_start;
  623. X    while( mptr && 
  624. X        ! (((char *)mptr < ptr) && ((mptr->data+mptr->s.size) > ptr) ) )
  625. X    {
  626. X        mptr = mptr->next;
  627. X    }
  628. X
  629. X    /*
  630. X     * if ptr was not in a malloc block, it must be part of
  631. X     *    some direct sbrk() stuff, so just return.
  632. X     */
  633. X    if( ! mptr )
  634. X    {
  635. X        DEBUG1(10,"ptr (0x%x) not found in malloc search", ptr);
  636. X        return;
  637. X    }
  638. X    
  639. X    /*
  640. X      * Now we have a valid malloc block that contains the indicated
  641. X     * pointer.  We must verify that it is withing the requested block
  642. X     * size (as opposed to the real block size which is rounded up to
  643. X     * allow for correct alignment).
  644. X     */
  645. X
  646. X    DEBUG4(60,"Checking  0x%x-0x%x, 0x%x-0x%x",
  647. X            ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);
  648. X    
  649. X    if( (ptr < mptr->data) || ((ptr+len) > (mptr->data+mptr->r_size)) )
  650. X    {
  651. X        DEBUG4(0,"pointer not within region 0x%x-0x%x, 0x%x-0x%x",
  652. X            ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);
  653. X
  654. X        malloc_errno = M_CODE_OUTOF_BOUNDS;
  655. X        malloc_warning(func);
  656. X    }
  657. X
  658. X    return;
  659. }
  660. X
  661. /*
  662. X * $Log:    malloc_chk.c,v $
  663. X * Revision 1.5  90/08/29  22:23:48  cpcahil
  664. X * added new function to check on strings up to a specified length 
  665. X * and used it within several strn* functions.
  666. X * 
  667. X * Revision 1.4  90/05/11  00:13:09  cpcahil
  668. X * added copyright statment
  669. X * 
  670. X * Revision 1.3  90/02/24  21:50:22  cpcahil
  671. X * lots of lint fixes
  672. X * 
  673. X * Revision 1.2  90/02/24  17:29:38  cpcahil
  674. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  675. X * id string
  676. X * 
  677. X * Revision 1.1  90/02/24  14:57:03  cpcahil
  678. X * Initial revision
  679. X * 
  680. X */
  681. SHAR_EOF
  682. chmod 0640 dmake/dbug/malloc/mlc_chk.c ||
  683. echo 'restore of dmake/dbug/malloc/mlc_chk.c failed'
  684. Wc_c="`wc -c < 'dmake/dbug/malloc/mlc_chk.c'`"
  685. test 6308 -eq "$Wc_c" ||
  686.     echo 'dmake/dbug/malloc/mlc_chk.c: original size 6308, current size' "$Wc_c"
  687. rm -f _shar_wnt_.tmp
  688. fi
  689. # ============= dmake/dbug/malloc/mlc_chn.c ==============
  690. if test -f 'dmake/dbug/malloc/mlc_chn.c' -a X"$1" != X"-c"; then
  691.     echo 'x - skipping dmake/dbug/malloc/mlc_chn.c (File already exists)'
  692.     rm -f _shar_wnt_.tmp
  693. else
  694. > _shar_wnt_.tmp
  695. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/mlc_chn.c' &&
  696. /*
  697. X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  698. X * You may copy, distribute, and use this software as long as this
  699. X * copyright statement is not removed.
  700. X */
  701. #include <stdio.h>
  702. #include <fcntl.h>
  703. #include "malloc.h"
  704. X
  705. /*
  706. X * Function:    malloc_chain_check()
  707. X *
  708. X * Purpose:    to verify malloc chain is intact
  709. X *
  710. X * Arguments:    todo    - 0 - just check and return status
  711. X *              1 - call malloc_warn if error detected
  712. X *
  713. X * Returns:    0    - malloc chain intact & no overflows
  714. X *        other    - problems detected in malloc chain
  715. X *
  716. X * Narrative:
  717. X *
  718. X * Notes:    If todo is non-zero the malloc_warn function, when called
  719. X *        may not return (i.e. it may exit)
  720. X *
  721. X */
  722. #ifndef lint
  723. static
  724. char rcs_hdr[] = "$Id: malloc_chn.c,v 1.4 90/05/11 00:13:09 cpcahil Exp $";
  725. #endif
  726. X
  727. X
  728. int
  729. malloc_chain_check(todo)
  730. X    int          todo;
  731. {
  732. X    char            * func = "malloc_chain_check";
  733. X    int              i;
  734. X    extern char        * malloc_data_start;
  735. X    extern char        * malloc_data_end;
  736. X    extern struct mlist     * malloc_end;
  737. X    extern int          malloc_errno;
  738. X    extern struct mlist      malloc_start;
  739. X    struct mlist        * oldptr;
  740. X    struct mlist        * ptr;
  741. X    int              rtn = 0;
  742. X
  743. X    oldptr = &malloc_start;
  744. X    for(ptr = malloc_start.next; ; ptr = ptr->next)
  745. X    {
  746. X        /*
  747. X         * Since the malloc chain is a forward only chain, any
  748. X         * pointer that we get should always be positioned in 
  749. X         * memory following the previous pointer.  If this is not
  750. X         * so, we must have a corrupted chain.
  751. X         */
  752. X        if( ptr )
  753. X        {
  754. X            if(ptr < oldptr )
  755. X            {
  756. X                malloc_errno = M_CODE_CHAIN_BROKE;
  757. X                if( todo )
  758. X                {
  759. X                    malloc_fatal(func);
  760. X                }
  761. X                rtn++;
  762. X                break;
  763. X            }
  764. X            oldptr = ptr;
  765. X        }
  766. X        else
  767. X        {
  768. X            if( oldptr != malloc_end )
  769. X            {
  770. X                /*
  771. X                 * This should never happen.  If it does, then
  772. X                 * we got a real problem.
  773. X                 */
  774. X                malloc_errno = M_CODE_NO_END;
  775. X                if( todo )
  776. X                {
  777. X                    malloc_fatal(func);
  778. X                }
  779. X                rtn++;
  780. X            }
  781. X            break;
  782. X        }
  783. X        
  784. X        /*
  785. X         * verify that ptr is within the malloc region...
  786. X         * since we started within the malloc chain this should never
  787. X         * happen.
  788. X         */
  789. X
  790. X        if( ((char *)ptr < malloc_data_start) ||
  791. X            ((char *)ptr > malloc_data_end) )
  792. X        {
  793. X            malloc_errno = M_CODE_BAD_PTR;
  794. X            if( todo )
  795. X            {
  796. X                malloc_fatal(func);
  797. X            }
  798. X            rtn++;
  799. X            break;
  800. X        }
  801. X
  802. X        /* 
  803. X         * verify magic flag is set
  804. X         */
  805. X        
  806. X        if( (ptr->flag&M_MAGIC) != M_MAGIC )
  807. X        {
  808. X            malloc_errno = M_CODE_BAD_MAGIC;
  809. X            if( todo )
  810. X            {
  811. X                malloc_warning(func);
  812. X            }
  813. X            rtn++;
  814. X            continue;
  815. X        }
  816. X
  817. X        /* 
  818. X         * verify segments are correctly linked together
  819. X         */
  820. X        
  821. X        if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  822. X            (ptr->next && (ptr->next->prev != ptr) ) ||
  823. X            ((ptr->next == NULL) && (ptr->prev == NULL)) )
  824. X        {
  825. X            malloc_errno = M_CODE_BAD_CONNECT;
  826. X            if( todo )
  827. X            {
  828. X                malloc_warning(func);
  829. X            }
  830. X            rtn++;
  831. X            continue;
  832. X        }
  833. X
  834. X        /*
  835. X         * If this segment is allocated
  836. X         */
  837. X
  838. X        if( (ptr->flag & M_INUSE) != 0 )
  839. X        {
  840. X            /*
  841. X             * verify no overflow of data area
  842. X             */
  843. X
  844. X            for(i=ptr->r_size; i < ptr->s.size; i++)
  845. X            {
  846. X                if( ptr->data[i] != M_FILL )
  847. X                {
  848. X                    malloc_errno = M_CODE_OVERRUN;
  849. X                    if( todo )
  850. X                    {
  851. X                        malloc_warning(func);
  852. X                    }
  853. X                    rtn++;
  854. X                    break;
  855. X                }
  856. X            }
  857. X        }
  858. X        else /* it's not allocated so */
  859. X        {
  860. X            /*
  861. X             * verify no reuse of freed data blocks
  862. X             */
  863. X
  864. X            for(i=0; i < ptr->s.size; i++)
  865. X            {
  866. X                if( ptr->data[i] != M_FREE_FILL )
  867. X                {
  868. X                    malloc_errno = M_CODE_REUSE;
  869. X                    if( todo )
  870. X                    {
  871. X                        malloc_warning(func);
  872. X                    }
  873. X                    rtn++;
  874. X                    break;
  875. X                }
  876. X            }
  877. X        }
  878. X
  879. X    } /* for(... */
  880. X
  881. X    return(rtn);
  882. X
  883. } /* malloc_chain_check(... */
  884. SHAR_EOF
  885. chmod 0640 dmake/dbug/malloc/mlc_chn.c ||
  886. echo 'restore of dmake/dbug/malloc/mlc_chn.c failed'
  887. Wc_c="`wc -c < 'dmake/dbug/malloc/mlc_chn.c'`"
  888. test 3449 -eq "$Wc_c" ||
  889.     echo 'dmake/dbug/malloc/mlc_chn.c: original size 3449, current size' "$Wc_c"
  890. rm -f _shar_wnt_.tmp
  891. fi
  892. # ============= dmake/dbug/malloc/patchlevel ==============
  893. if test -f 'dmake/dbug/malloc/patchlevel' -a X"$1" != X"-c"; then
  894.     echo 'x - skipping dmake/dbug/malloc/patchlevel (File already exists)'
  895.     rm -f _shar_wnt_.tmp
  896. else
  897. > _shar_wnt_.tmp
  898. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/patchlevel' &&
  899. 3
  900. SHAR_EOF
  901. chmod 0640 dmake/dbug/malloc/patchlevel ||
  902. echo 'restore of dmake/dbug/malloc/patchlevel failed'
  903. Wc_c="`wc -c < 'dmake/dbug/malloc/patchlevel'`"
  904. test 2 -eq "$Wc_c" ||
  905.     echo 'dmake/dbug/malloc/patchlevel: original size 2, current size' "$Wc_c"
  906. rm -f _shar_wnt_.tmp
  907. fi
  908. # ============= dmake/dbug/malloc/realloc.c ==============
  909. if test -f 'dmake/dbug/malloc/realloc.c' -a X"$1" != X"-c"; then
  910.     echo 'x - skipping dmake/dbug/malloc/realloc.c (File already exists)'
  911.     rm -f _shar_wnt_.tmp
  912. else
  913. > _shar_wnt_.tmp
  914. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/realloc.c' &&
  915. /*
  916. X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  917. X * You may copy, distribute, and use this software as long as this
  918. X * copyright statement is not removed.
  919. X */
  920. #include <stdio.h>
  921. #include "malloc.h"
  922. X
  923. /*
  924. X * Function:    realloc()
  925. X *
  926. X * Purpose:    to re-allocate a data area.
  927. X *
  928. X * Arguments:    cptr    - pointer to area to reallocate
  929. X *        size    - size to change area to
  930. X *
  931. X * Returns:    pointer to new area (may be same area)
  932. X *
  933. X * Narrative:    verify pointer is within malloc region
  934. X *        obtain mlist pointer from cptr
  935. X *        verify magic number is correct
  936. X *        verify inuse flag is set
  937. X *        verify connection to adjoining segments is correct
  938. X *        save requested size
  939. X *        round-up size to appropriate boundry
  940. X *        IF size is bigger than what is in this segment
  941. X *            try to join next segment to this segment
  942. X *        IF size is less than what is is this segment
  943. X *            determine leftover amount of space
  944. X *        ELSE
  945. X *            allocate new segment of size bites
  946. X *            IF allocation failed
  947. X *                return NULL
  948. X *            copy previous data to new segment
  949. X *            free previous segment
  950. X *            return new pointer
  951. X *        split of extra space in this segment (if any)
  952. X *        clear bytes beyound what they had before
  953. X *        return pointer to data 
  954. X */
  955. #ifndef lint
  956. static
  957. char rcs_hdr[] = "$Id: realloc.c,v 1.8 90/08/29 21:22:52 cpcahil Exp $";
  958. #endif
  959. X
  960. char *
  961. realloc(cptr,size)
  962. X    char            * cptr;
  963. X    unsigned int          size;
  964. {
  965. X    void              free();
  966. X    char            * func = "realloc";
  967. X    int              i;
  968. X    char            * malloc();
  969. X    extern int          malloc_checking;
  970. X    extern struct mlist     * malloc_end;
  971. X    extern int          malloc_errno;
  972. X    extern char        * malloc_data_end;
  973. X    extern char        * malloc_data_start;
  974. X    void              malloc_join();
  975. X    void              malloc_memset();
  976. X    void              malloc_split();
  977. X    char            * memcpy();
  978. X    char            * new_cptr;
  979. X    struct mlist        * ptr;
  980. X    int              r_size;
  981. X
  982. X    /*
  983. X     * IF malloc chain checking is on, go do it.
  984. X     */
  985. X    if( malloc_checking )
  986. X    {
  987. X        (void) malloc_chain_check(1);
  988. X    }
  989. X
  990. X    /*
  991. X     * verify that cptr is within the malloc region...
  992. X     */
  993. X    if( cptr < malloc_data_start || cptr > malloc_data_end )
  994. X    {
  995. X        malloc_errno = M_CODE_BAD_PTR;
  996. X        malloc_warning(func);
  997. X        return (NULL);
  998. X    }
  999. X
  1000. X    /* 
  1001. X     * convert pointer to mlist struct pointer.  To do this we must 
  1002. X     * move the pointer backwards the correct number of bytes...
  1003. X     */
  1004. X    
  1005. X    ptr = (struct mlist *) (cptr - M_SIZE);
  1006. X    
  1007. X    if( (ptr->flag&M_MAGIC) != M_MAGIC )
  1008. X    {
  1009. X        malloc_errno = M_CODE_BAD_MAGIC;
  1010. X        malloc_warning(func);
  1011. X        return(NULL);
  1012. X    }
  1013. X
  1014. X    if( ! (ptr->flag & M_INUSE) )
  1015. X    {
  1016. X        malloc_errno = M_CODE_NOT_INUSE ;
  1017. X        malloc_warning(func);
  1018. X        return(NULL);
  1019. X    }
  1020. X
  1021. X     if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  1022. X        (ptr->next && (ptr->next->prev != ptr) ) ||
  1023. X        ((ptr->next == NULL) && (ptr->prev == NULL)) )
  1024. X    {
  1025. X        malloc_errno = M_CODE_BAD_CONNECT;
  1026. X        malloc_warning(func);
  1027. X        return(NULL);
  1028. X    }
  1029. X
  1030. X    r_size = ++size;
  1031. X
  1032. X    M_ROUNDUP(size);
  1033. X
  1034. X    if( size > ptr->s.size )
  1035. X    {
  1036. X        malloc_join(ptr,ptr->next,1,1);
  1037. X    }
  1038. X
  1039. X    if( size > ptr->s.size )
  1040. X    {
  1041. X        /*
  1042. X         * else we can't combine it, so lets allocate a new chunk,
  1043. X         * copy the data and free the old chunk...
  1044. X         */
  1045. X        new_cptr = malloc(size);
  1046. X
  1047. X        if( new_cptr == (char *) 0)
  1048. X        {
  1049. X            return(new_cptr);
  1050. X        }
  1051. X
  1052. X        if( r_size < ptr->r_size )
  1053. X        {
  1054. X            i = r_size;
  1055. X        }
  1056. X        else
  1057. X        {
  1058. X            i = ptr->r_size;
  1059. X        }
  1060. X        (void)memcpy(new_cptr,ptr->data,i);
  1061. X        free(cptr);
  1062. X        return(new_cptr);
  1063. X
  1064. X    } /* else... */
  1065. X
  1066. X    /*
  1067. X     * save amount of real data in new segment (this will be used in the
  1068. X     * memset later) and then save requested size of this segment.
  1069. X     */
  1070. X
  1071. X    if( ptr->r_size < r_size )
  1072. X    {
  1073. X        i = ptr->r_size;
  1074. X    }
  1075. X    else
  1076. X    {
  1077. X        i = r_size;
  1078. X    }
  1079. X
  1080. X    ptr->r_size = r_size;
  1081. X
  1082. X    /*
  1083. X     * split off extra free space at end of this segment, if possible...
  1084. X     */
  1085. X
  1086. X    malloc_split(ptr);
  1087. X
  1088. X    malloc_memset( ptr->data+i, M_FILL, (int) (ptr->s.size - i));
  1089. X        
  1090. X    return(ptr->data);
  1091. X
  1092. } /* realloc(... */
  1093. X
  1094. X
  1095. /*
  1096. X * $Log:    realloc.c,v $
  1097. X * Revision 1.8  90/08/29  21:22:52  cpcahil
  1098. X * miscellaneous lint fixes
  1099. X * 
  1100. X * Revision 1.7  90/05/11  00:13:10  cpcahil
  1101. X * added copyright statment
  1102. X * 
  1103. X * Revision 1.6  90/02/25  11:01:20  cpcahil
  1104. X * added support for malloc chain checking.
  1105. X * 
  1106. X * Revision 1.5  90/02/24  21:50:31  cpcahil
  1107. X * lots of lint fixes
  1108. X * 
  1109. X * Revision 1.4  90/02/24  17:29:39  cpcahil
  1110. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  1111. X * id string
  1112. X * 
  1113. X * Revision 1.3  90/02/24  17:20:00  cpcahil
  1114. X * attempt to get rid of full path in rcs header.
  1115. X * 
  1116. X * Revision 1.2  90/02/24  15:14:20  cpcahil
  1117. X * 1. added function header 
  1118. X * 2. changed calls to malloc_warning to conform to new usage
  1119. X * 3. added setting of malloc_errno
  1120. X *  4. broke up bad pointer determination so that errno's would be more
  1121. X *    descriptive
  1122. X * 
  1123. X * Revision 1.1  90/02/22  23:17:43  cpcahil
  1124. X * Initial revision
  1125. X * 
  1126. X */
  1127. SHAR_EOF
  1128. chmod 0640 dmake/dbug/malloc/realloc.c ||
  1129. echo 'restore of dmake/dbug/malloc/realloc.c failed'
  1130. Wc_c="`wc -c < 'dmake/dbug/malloc/realloc.c'`"
  1131. test 4659 -eq "$Wc_c" ||
  1132.     echo 'dmake/dbug/malloc/realloc.c: original size 4659, current size' "$Wc_c"
  1133. rm -f _shar_wnt_.tmp
  1134. fi
  1135. # ============= dmake/dbug/malloc/string.c ==============
  1136. if test -f 'dmake/dbug/malloc/string.c' -a X"$1" != X"-c"; then
  1137.     echo 'x - skipping dmake/dbug/malloc/string.c (File already exists)'
  1138.     rm -f _shar_wnt_.tmp
  1139. else
  1140. > _shar_wnt_.tmp
  1141. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/string.c' &&
  1142. /*
  1143. X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  1144. X * You may copy, distribute, and use this software as long as this
  1145. X * copyright statement is not removed.
  1146. X */
  1147. X
  1148. #include <stdio.h>
  1149. #include <string.h>
  1150. #include <sys/types.h>
  1151. #include "malloc.h"
  1152. X
  1153. #ifndef lint
  1154. static
  1155. char rcs_hdr[] = "$Id: string.c,v 1.7 90/08/29 22:24:19 cpcahil Exp $";
  1156. #endif
  1157. X
  1158. int    malloc_checking = 0;
  1159. X
  1160. char *
  1161. strcat(str1,str2)
  1162. X    register char    * str1;
  1163. X    register char    * str2;
  1164. {
  1165. X    char        * rtn;
  1166. X    int      len;
  1167. X
  1168. X    /* 
  1169. X     * check pointers agains malloc region.  The malloc* functions
  1170. X     * will properly handle the case where a pointer does not
  1171. X     * point into malloc space.
  1172. X     */
  1173. X    malloc_checking = 1;
  1174. X
  1175. X    len = strlen(str2);
  1176. X    malloc_check_str("strcat", str2);
  1177. X
  1178. X    len += strlen(str1) + 1;
  1179. X    malloc_checking = 0;
  1180. X
  1181. X    malloc_check_data("strcat", str1, len);
  1182. X
  1183. X    rtn = str1;
  1184. X
  1185. X    while( *str1 )
  1186. X    {
  1187. X        str1++;
  1188. X    }
  1189. X    
  1190. X    while( (*str1 = *str2) != '\0' )
  1191. X    {
  1192. X        str1++;
  1193. X        str2++;
  1194. X    }
  1195. X    
  1196. X    return(rtn);
  1197. }
  1198. X
  1199. char *
  1200. strdup(str1)
  1201. X    register char    * str1;
  1202. {
  1203. X    char        * malloc();
  1204. X    char        * rtn;
  1205. X    register char    * str2;
  1206. X
  1207. X    malloc_check_str("strdup", str1);
  1208. X
  1209. X    rtn = str2 = malloc((unsigned)strlen(str1)+1);
  1210. X
  1211. X    if( rtn != (char *) 0)
  1212. X    {
  1213. X        while( (*str2 = *str1) != '\0' )
  1214. X        {
  1215. X            str1++;
  1216. X            str2++;
  1217. X        }
  1218. X    }
  1219. X
  1220. X    return(rtn);
  1221. }
  1222. X
  1223. char *
  1224. strncat(str1,str2,len)
  1225. X    register char    * str1;
  1226. X    register char    * str2;
  1227. X    register int      len;
  1228. {
  1229. X    int           len1;
  1230. X    int           len2;
  1231. X    char        * rtn;
  1232. X
  1233. X    malloc_check_strn("strncat", str2, len);
  1234. X
  1235. X    malloc_checking = 1;
  1236. X
  1237. X    len2 = strlen(str2) + 1;
  1238. X    len1 = strlen(str1);
  1239. X
  1240. X    malloc_checking = 0;
  1241. X
  1242. X
  1243. X    if( (len+1) < len2 )
  1244. X    {
  1245. X        len1 += len + 1;
  1246. X    }
  1247. X    else
  1248. X    {
  1249. X        len1 += len2;
  1250. X    }
  1251. X    malloc_check_data("strncat", str1, len1);
  1252. X
  1253. X    rtn = str1;
  1254. X
  1255. X    while( *str1 )
  1256. X    {
  1257. X        str1++;
  1258. X    }
  1259. X
  1260. X    while( len-- && ((*str1++ = *str2++) != '\0') )
  1261. X    {
  1262. X    }
  1263. X    
  1264. X    if( ! len )
  1265. X    {
  1266. X        *str1 = '\0';
  1267. X    }
  1268. X
  1269. X    return(rtn);
  1270. }
  1271. X
  1272. int
  1273. strcmp(str1,str2)
  1274. X    register char    * str1;
  1275. X    register char    * str2;
  1276. {
  1277. X    malloc_check_str("strcmp", str1);
  1278. X    malloc_check_str("strcmp", str2);
  1279. X
  1280. X    while( *str1 && (*str1 == *str2) )
  1281. X    {
  1282. X        str1++;
  1283. X        str2++;
  1284. X    }
  1285. X
  1286. X
  1287. X    /*
  1288. X     * in order to deal with the case of a negative last char of either
  1289. X     * string when the other string has a null
  1290. X     */
  1291. X    if( (*str2 == '\0') && (*str1 == '\0') )
  1292. X    {
  1293. X        return(0);
  1294. X    }
  1295. X    else if( *str2 == '\0' )
  1296. X    {
  1297. X        return(1);
  1298. X    }
  1299. X    else if( *str1 == '\0' )
  1300. X    {
  1301. X        return(-1);
  1302. X    }
  1303. X    
  1304. X    return( *str1 - *str2 );
  1305. }
  1306. X
  1307. int
  1308. strncmp(str1,str2,len)
  1309. X    register char    * str1;
  1310. X    register char    * str2;
  1311. X    register int      len;
  1312. {
  1313. X    malloc_check_strn("strncmp", str1, len);
  1314. X    malloc_check_strn("strncmp", str2, len);
  1315. X
  1316. X    while( --len >= 0 && *str1 && (*str1 == *str2) )
  1317. X    {
  1318. X        str1++;
  1319. X        str2++;
  1320. X    }
  1321. X
  1322. X    if( len < 0 )
  1323. X    {
  1324. X        return(0);
  1325. X    }
  1326. X    /*
  1327. X     * in order to deal with the case of a negative last char of either
  1328. X     * string when the other string has a null
  1329. X     */
  1330. X    if( (*str2 == '\0') && (*str1 == '\0') )
  1331. X    {
  1332. X        return(0);
  1333. X    }
  1334. X    else if( *str2 == '\0' )
  1335. X    {
  1336. X        return(1);
  1337. X    }
  1338. X    else if( *str1 == '\0' )
  1339. X    {
  1340. X        return(-1);
  1341. X    }
  1342. X    
  1343. X    return( *str1 - *str2 );
  1344. }
  1345. X
  1346. char *
  1347. strcpy(str1,str2)
  1348. X    register char    * str1;
  1349. X    register char    * str2;
  1350. {
  1351. X    char        * rtn;
  1352. X    int          len;
  1353. X
  1354. X    malloc_checking = 1;
  1355. X    len = strlen(str2) + 1;
  1356. X    malloc_checking = 0;
  1357. X
  1358. X    malloc_check_data("strcpy", str1, len);
  1359. X    malloc_check_data("strcpy", str2, len);
  1360. X
  1361. X    rtn = str1;
  1362. X
  1363. X    while( (*str1++ = *str2++) != '\0')
  1364. X    {
  1365. X    }
  1366. X
  1367. X    return(rtn);
  1368. }
  1369. X
  1370. char *
  1371. strncpy(str1,str2,len)
  1372. X    register char    * str1;
  1373. X    register char    * str2;
  1374. X    register int      len;
  1375. {
  1376. X    extern int      malloc_checking;
  1377. X    char        * rtn;
  1378. X
  1379. X    malloc_check_data("strncpy", str1, len);
  1380. X    malloc_check_strn("strncpy", str2, len);
  1381. X
  1382. X    rtn = str1;
  1383. X
  1384. X    while((len-- > 0) && (*str1++ = *str2++) != '\0')
  1385. X    {
  1386. X    }
  1387. X    while( (len-- > 0) )
  1388. X    {
  1389. X        *str1++ = '\0';
  1390. X    }
  1391. X
  1392. X    return(rtn);
  1393. }
  1394. X
  1395. int
  1396. strlen(str1)
  1397. X    register char    * str1;
  1398. {
  1399. X    register char    * s;
  1400. X
  1401. X    if(! malloc_checking )
  1402. X    {
  1403. X        malloc_check_str("strlen", str1);
  1404. X    }
  1405. X
  1406. X    for( s = str1; *s; s++)
  1407. X    {
  1408. X    }
  1409. X
  1410. X    return( s - str1 );
  1411. }
  1412. X
  1413. char *
  1414. strchr(str1,c)
  1415. X    register char    * str1;
  1416. X    register int      c;
  1417. {
  1418. X    malloc_check_str("strchr", str1);
  1419. X
  1420. X    while( *str1 && (*str1 != (char) c) )
  1421. X    {
  1422. X        str1++;
  1423. X    }
  1424. X
  1425. X    if(*str1 != (char) c)
  1426. X    {
  1427. X        str1 = (char *) 0;
  1428. X    }
  1429. X
  1430. X    return(str1);
  1431. }
  1432. X
  1433. char *
  1434. strrchr(str1,c)
  1435. X    register char    * str1;
  1436. X    register int      c;
  1437. {
  1438. X    register char    * rtn = (char *) 0;
  1439. X
  1440. X    malloc_check_str("strrchr", str1);
  1441. X
  1442. X    while( *str1 )
  1443. X    {
  1444. X        if(*str1 == (char) c )
  1445. X        {
  1446. X            rtn = str1;
  1447. X        }
  1448. X        str1++;
  1449. X    }
  1450. X
  1451. X    if( *str1 == (char) c)
  1452. X    {
  1453. X        rtn = str1;
  1454. X    }
  1455. X
  1456. X    return(rtn);
  1457. }
  1458. X
  1459. char *
  1460. index(str1,c)
  1461. X    char        * str1;
  1462. X    char          c;
  1463. {
  1464. X    return( strchr(str1,c) );
  1465. }
  1466. X
  1467. char *
  1468. rindex(str1,c)
  1469. X    char        * str1;
  1470. X    char          c;
  1471. {
  1472. X    return( strrchr(str1,c) );
  1473. }
  1474. X
  1475. char *
  1476. strpbrk(str1,str2)
  1477. X    register char    * str1;
  1478. X    register char    * str2;
  1479. {
  1480. X    register char    * tmp;
  1481. X
  1482. X    malloc_check_str("strpbrk", str1);
  1483. X    malloc_check_str("strpbrk", str2);
  1484. X
  1485. X    while(*str1)
  1486. X    {
  1487. X        for( tmp=str2; *tmp && *tmp != *str1; tmp++)
  1488. X        {
  1489. X        }
  1490. X        if( *tmp )
  1491. X        {
  1492. X            break;
  1493. X        }
  1494. X        str1++;
  1495. X    }
  1496. X
  1497. X    if( ! *str1 )
  1498. X    {
  1499. X        str1 = (char *) 0;
  1500. X    }
  1501. X
  1502. X    return(str1);
  1503. }
  1504. X
  1505. int
  1506. strspn(str1,str2)
  1507. X    register char    * str1;
  1508. X    register char    * str2;
  1509. {
  1510. X    register char    * tmp;
  1511. X    char        * orig = str1;
  1512. X
  1513. X    malloc_check_str("strspn", str1);
  1514. X    malloc_check_str("strspn", str2);
  1515. X
  1516. X    while(*str1)
  1517. X    {
  1518. X        for( tmp=str2; *tmp && *tmp != *str1; tmp++)
  1519. X        {
  1520. X        }
  1521. X        if(! *tmp )
  1522. X        {
  1523. X            break;
  1524. X        }
  1525. X        str1++;
  1526. X    }
  1527. X
  1528. X    return( (int) (str1 - orig) );
  1529. }
  1530. X
  1531. int
  1532. strcspn(str1,str2)
  1533. X    register char    * str1;
  1534. X    register char    * str2;
  1535. {
  1536. X    register char    * tmp;
  1537. X    char        * orig = str1;
  1538. X
  1539. X    malloc_check_str("strcspn", str1);
  1540. X    malloc_check_str("strcspn", str2);
  1541. X
  1542. X    while(*str1)
  1543. X    {
  1544. X        for( tmp=str2; *tmp && *tmp != *str1; tmp++)
  1545. X        {
  1546. X        }
  1547. X        if( *tmp )
  1548. X        {
  1549. X            break;
  1550. X        }
  1551. X        str1++;
  1552. X    }
  1553. X
  1554. X    return( (int) (str1 - orig) );
  1555. }
  1556. X
  1557. /*
  1558. X * strtok() source taken from that posted to comp.lang.c by Chris Torek
  1559. X * in Jan 1990.
  1560. X */
  1561. X
  1562. /*
  1563. X * Copyright (c) 1989 The Regents of the University of California.
  1564. X * All rights reserved.
  1565. X *
  1566. X * Redistribution and use in source and binary forms are permitted
  1567. X * provided that the above copyright notice and this paragraph are
  1568. X * duplicated in all such forms and that any documentation,
  1569. X * advertising materials, and other materials related to such
  1570. X * distribution and use acknowledge that the software was developed
  1571. X * by the University of California, Berkeley.  The name of the
  1572. X * University may not be used to endorse or promote products derived
  1573. X * from this software without specific prior written permission.
  1574. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1575. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1576. X * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1577. X */
  1578. X
  1579. /*
  1580. X * Get next token from string s (NULL on 2nd, 3rd, etc. calls),
  1581. X * where tokens are nonempty strings separated by runs of
  1582. X * chars from delim.  Writes NULs into s to end tokens.  delim need not
  1583. X * remain constant from call to call.
  1584. X *
  1585. X * Modified by cpc:     changed variable names to conform with naming
  1586. X *            conventions used in rest of code.  Added malloc pointer
  1587. X *            check calls.
  1588. X */
  1589. char *
  1590. strtok(str1, str2)
  1591. X    char     * str1;
  1592. X    char    * str2;
  1593. {
  1594. X    static char     * last;
  1595. X    char        * strtoken();
  1596. X
  1597. X    if( str1 )
  1598. X    {
  1599. X        malloc_check_str("strtok", str1);
  1600. X        last = str1;
  1601. X    }
  1602. X    malloc_check_str("strtok", str2);
  1603. X
  1604. X    return (strtoken(&last, str2, 1));
  1605. }
  1606. X
  1607. X
  1608. /*
  1609. X * Get next token from string *stringp, where tokens are (possibly empty)
  1610. X * strings separated by characters from delim.  Tokens are separated
  1611. X * by exactly one delimiter iff the skip parameter is false; otherwise
  1612. X * they are separated by runs of characters from delim, because we
  1613. X * skip over any initial `delim' characters.
  1614. X *
  1615. X * Writes NULs into the string at *stringp to end tokens.
  1616. X * delim will usually, but need not, remain constant from call to call.
  1617. X * On return, *stringp points past the last NUL written (if there might
  1618. X * be further tokens), or is NULL (if there are definitely no more tokens).
  1619. X *
  1620. X * If *stringp is NULL, strtoken returns NULL.
  1621. X */
  1622. char *
  1623. strtoken(stringp, delim, skip)
  1624. X    register char **stringp;
  1625. X    register char *delim;
  1626. X    int skip;
  1627. {
  1628. X    register char *s;
  1629. X    register char *spanp;
  1630. X    register int c, sc;
  1631. X    char *tok;
  1632. X
  1633. X    if ((s = *stringp) == NULL)
  1634. X        return (NULL);
  1635. X
  1636. X    if (skip) {
  1637. X        /*
  1638. X         * Skip (span) leading delimiters (s += strspn(s, delim)).
  1639. X         */
  1640. X    cont:
  1641. X        c = *s;
  1642. X        for (spanp = delim; (sc = *spanp++) != 0;) {
  1643. X            if (c == sc) {
  1644. X                s++;
  1645. X                goto cont;
  1646. X            }
  1647. X        }
  1648. X        if (c == 0) {        /* no token found */
  1649. X            *stringp = NULL;
  1650. X            return (NULL);
  1651. X        }
  1652. X    }
  1653. X
  1654. X    /*
  1655. X     * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  1656. X     * Note that delim must have one NUL; we stop if we see that, too.
  1657. X     */
  1658. X    for (tok = s;;) {
  1659. X        c = *s++;
  1660. X        spanp = delim;
  1661. X        do {
  1662. X            if ((sc = *spanp++) == c) {
  1663. X                if (c == 0)
  1664. X                    s = NULL;
  1665. X                else
  1666. X                    s[-1] = 0;
  1667. X                *stringp = s;
  1668. X                return (tok);
  1669. X            }
  1670. X        } while (sc != 0);
  1671. X    }
  1672. X    /* NOTREACHED */
  1673. }
  1674. X
  1675. /*
  1676. X * $Log:    string.c,v $
  1677. X * Revision 1.7  90/08/29  22:24:19  cpcahil
  1678. X * added new function to check on strings up to a specified length 
  1679. X * and used it within several strn* functions.
  1680. X * 
  1681. X * Revision 1.6  90/07/16  20:06:56  cpcahil
  1682. X * fixed several minor bugs found with Henry Spencer's string/mem function
  1683. X * tester program.
  1684. X * 
  1685. X * Revision 1.5  90/06/10  14:59:49  cpcahil
  1686. X * Fixed a couple of bugs in strncpy & strdup
  1687. X * 
  1688. X * Revision 1.4  90/05/11  00:13:10  cpcahil
  1689. X * added copyright statment
  1690. X * 
  1691. X * Revision 1.3  90/02/24  21:50:32  cpcahil
  1692. X * lots of lint fixes
  1693. X * 
  1694. X * Revision 1.2  90/02/24  17:29:40  cpcahil
  1695. X * changed $Header to $Id so full path wouldnt be included as part of rcs 
  1696. X * id string
  1697. X * 
  1698. X * Revision 1.1  90/02/22  23:17:44  cpcahil
  1699. X * Initial revision
  1700. X * 
  1701. X */
  1702. SHAR_EOF
  1703. chmod 0640 dmake/dbug/malloc/string.c ||
  1704. echo 'restore of dmake/dbug/malloc/string.c failed'
  1705. Wc_c="`wc -c < 'dmake/dbug/malloc/string.c'`"
  1706. test 9382 -eq "$Wc_c" ||
  1707.     echo 'dmake/dbug/malloc/string.c: original size 9382, current size' "$Wc_c"
  1708. rm -f _shar_wnt_.tmp
  1709. fi
  1710. # ============= dmake/dbug/malloc/testmem.c ==============
  1711. if test -f 'dmake/dbug/malloc/testmem.c' -a X"$1" != X"-c"; then
  1712.     echo 'x - skipping dmake/dbug/malloc/testmem.c (File already exists)'
  1713.     rm -f _shar_wnt_.tmp
  1714. else
  1715. > _shar_wnt_.tmp
  1716. sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/testmem.c' &&
  1717. /* 
  1718. X * This stuff is all stolen (with permission, since it was in the public
  1719. X * domain) from Henry Spencer's string and memory library.  Thanks Henry.
  1720. X */
  1721. X
  1722. /*
  1723. X * Test program for string(3) routines.
  1724. X * 
  1725. X * Note that at least one Bell Labs implementation of the string
  1726. X * routines flunks a couple of these tests -- the ones which test
  1727. X * behavior on "negative" characters.
  1728. X */
  1729. X
  1730. #include <stdio.h>
  1731. #include <string.h>
  1732. X
  1733. char * index();
  1734. char * rindex();
  1735. X
  1736. #define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  1737. X
  1738. char *it = "<UNSET>";        /* Routine name for message routines. */
  1739. int waserror = 0;        /* For exit status. */
  1740. X
  1741. char uctest[] = "\004\203";    /* For testing signedness of chars. */
  1742. int charsigned;            /* Result. */
  1743. X
  1744. /*
  1745. X - check - complain if condition is not true
  1746. X */
  1747. void
  1748. check(thing, number)
  1749. int thing;
  1750. int number;            /* Test number for error message. */
  1751. {
  1752. X    if (!thing) {
  1753. X        printf("%s flunked test %d\n", it, number);
  1754. X        waserror = 1;
  1755. X    }
  1756. }
  1757. X
  1758. /*
  1759. X - equal - complain if first two args don't strcmp as equal
  1760. X */
  1761. void
  1762. equal(a, b, number)
  1763. char *a;
  1764. char *b;
  1765. int number;            /* Test number for error message. */
  1766. {
  1767. X    check(a != NULL && b != NULL && STREQ(a, b), number);
  1768. }
  1769. X
  1770. char one[50];
  1771. char two[50];
  1772. X
  1773. #ifdef UNIXERR
  1774. #define ERR 1
  1775. #endif
  1776. #ifdef BERKERR
  1777. #define ERR 1
  1778. #endif
  1779. #ifdef ERR
  1780. int f;
  1781. extern char *sys_errlist[];
  1782. extern int sys_nerr;
  1783. extern int errno;
  1784. #endif
  1785. X
  1786. /* ARGSUSED */
  1787. main(argc, argv)
  1788. int argc;
  1789. char *argv[];
  1790. {
  1791. X    /*
  1792. X     * First, establish whether chars are signed.
  1793. X     */
  1794. X    if (uctest[0] < uctest[1])
  1795. X        charsigned = 0;
  1796. X    else
  1797. X        charsigned = 1;
  1798. X
  1799. X    /*
  1800. X     * Then, do the rest of the work.  Split into two functions because
  1801. X     * some compilers get unhappy about a single immense function.
  1802. X     */
  1803. X    first();
  1804. X    second();
  1805. X
  1806. X    exit((waserror) ? 1 : 0);
  1807. }
  1808. X
  1809. first()
  1810. {
  1811. X    /*
  1812. X     * Test strcmp first because we use it to test other things.
  1813. X     */
  1814. X    it = "strcmp";
  1815. X    check(strcmp("", "") == 0, 1);        /* Trivial case. */
  1816. X    check(strcmp("a", "a") == 0, 2);    /* Identity. */
  1817. X    check(strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  1818. X    check(strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  1819. X    check(strcmp("abcd", "abc") > 0, 5);
  1820. X    check(strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  1821. X    check(strcmp("abce", "abcd") > 0, 7);
  1822. X    check(strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  1823. X    if (charsigned)                /* Sign-bit comparison. */
  1824. X        check(strcmp("a\203", "a\003") < 0, 9);
  1825. X    else
  1826. X        check(strcmp("a\203", "a\003") > 0, 9);
  1827. X    check(strcmp("a", "a\203") < 0, 10);    /* Tricky if char signed. */
  1828. X
  1829. X    /*
  1830. X     * Test strcpy next because we need it to set up other tests.
  1831. X     */
  1832. X    it = "strcpy";
  1833. X    check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  1834. X    equal(one, "abcd", 2);            /* Basic test. */
  1835. X
  1836. X    (void) strcpy(one, "x");
  1837. X    equal(one, "x", 3);            /* Writeover. */
  1838. X    equal(one+2, "cd", 4);            /* Wrote too much? */
  1839. X
  1840. X    (void) strcpy(two, "hi there");
  1841. X    (void) strcpy(one, two);
  1842. X    equal(one, "hi there", 5);        /* Basic test encore. */
  1843. X    equal(two, "hi there", 6);        /* Stomped on source? */
  1844. X
  1845. X    (void) strcpy(one, "");
  1846. X    equal(one, "", 7);            /* Boundary condition. */
  1847. X
  1848. X    /*
  1849. X     * strcat
  1850. X     */
  1851. X    it = "strcat";
  1852. X    (void) strcpy(one, "ijk");
  1853. X    check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  1854. X    equal(one, "ijklmn", 2);        /* Basic test. */
  1855. X
  1856. X    (void) strcpy(one, "x");
  1857. X    (void) strcat(one, "yz");
  1858. X    equal(one, "xyz", 3);            /* Writeover. */
  1859. X    equal(one+4, "mn", 4);            /* Wrote too much? */
  1860. X
  1861. X    (void) strcpy(one, "gh");
  1862. X    (void) strcpy(two, "ef");
  1863. X    (void) strcat(one, two);
  1864. X    equal(one, "ghef", 5);            /* Basic test encore. */
  1865. X    equal(two, "ef", 6);            /* Stomped on source? */
  1866. X
  1867. X    (void) strcpy(one, "");
  1868. X    (void) strcat(one, "");
  1869. X    equal(one, "", 7);            /* Boundary conditions. */
  1870. X    (void) strcpy(one, "ab");
  1871. X    (void) strcat(one, "");
  1872. X    equal(one, "ab", 8);
  1873. X    (void) strcpy(one, "");
  1874. X    (void) strcat(one, "cd");
  1875. X    equal(one, "cd", 9);
  1876. X
  1877. X    /*
  1878. X     * strncat - first test it as strcat, with big counts, then
  1879. X     * test the count mechanism.
  1880. X     */
  1881. X    it = "strncat";
  1882. X    (void) strcpy(one, "ijk");
  1883. X    check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  1884. X    equal(one, "ijklmn", 2);        /* Basic test. */
  1885. X
  1886. X    (void) strcpy(one, "x");
  1887. X    (void) strncat(one, "yz", 99);
  1888. X    equal(one, "xyz", 3);            /* Writeover. */
  1889. X    equal(one+4, "mn", 4);            /* Wrote too much? */
  1890. X
  1891. X    (void) strcpy(one, "gh");
  1892. X    (void) strcpy(two, "ef");
  1893. X    (void) strncat(one, two, 99);
  1894. X    equal(one, "ghef", 5);            /* Basic test encore. */
  1895. X    equal(two, "ef", 6);            /* Stomped on source? */
  1896. X
  1897. X    (void) strcpy(one, "");
  1898. X    (void) strncat(one, "", 99);
  1899. X    equal(one, "", 7);            /* Boundary conditions. */
  1900. X    (void) strcpy(one, "ab");
  1901. X    (void) strncat(one, "", 99);
  1902. X    equal(one, "ab", 8);
  1903. X    (void) strcpy(one, "");
  1904. X    (void) strncat(one, "cd", 99);
  1905. X    equal(one, "cd", 9);
  1906. X
  1907. X    (void) strcpy(one, "ab");
  1908. SHAR_EOF
  1909. true || echo 'restore of dmake/dbug/malloc/testmem.c failed'
  1910. fi
  1911. echo 'End of part 6, continue with part 7'
  1912. echo 7 > _shar_seq_.tmp
  1913. exit 0
  1914.  
  1915. exit 0 # Just in case...
  1916. -- 
  1917. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1918. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1919. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1920. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1921.