home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d156 / flex.lha / Flex / Flex2 / gnu.lib.src / realloc.c < prev    next >
C/C++ Source or Header  |  1988-10-02  |  1KB  |  67 lines

  1. /*
  2.  * realloc() -- re-size the block of storage pointed to by 'root'
  3.  *              to 'new_size' (in bytes). The old values are copied
  4.  *              into the new space, up to the smaller of the two.
  5.  *
  6.  *              Author: Scott Henry, 22 Feb 88
  7.  */
  8.  
  9. #ifndef NULL
  10. #define NULL ((void *)0)
  11. #endif
  12.  
  13. struct mem {
  14.     struct mem *next;
  15.     long size;
  16. };
  17.  
  18. void *
  19. realloc( root, new_size)
  20.     register unsigned char *root;
  21.     register unsigned int new_size;
  22. {
  23.    extern unsigned char *malloc();
  24.    register struct mem *mp;
  25.    register unsigned char *ptr;
  26.    register long old_size;
  27.    register unsigned int i;
  28.  
  29.    if (root == NULL)
  30.     {
  31.       return( new_size > 0 ? malloc( new_size) : NULL);
  32.     }
  33.  
  34.    if (new_size <= 0)
  35.     {
  36.       free( root);
  37.       return (NULL);
  38.     }
  39.  
  40. #ifdef AZTEC_C
  41.    mp = (struct mem *)root -1;
  42.    old_size = mp->size;
  43.    if ((ptr = malloc( new_size)) == NULL)
  44.     {
  45.       return NULL;
  46.     }
  47.    for (i=0; i<old_size && i<new_size; ++i)
  48.     {
  49.       ptr[i] = root[i];
  50.     }
  51.    for (i=old_size; i<new_size; ++i)
  52.     {
  53.       ptr[i] = '\0';
  54.     }
  55.    if (free( root) != 0)
  56.     {
  57.       free( ptr);
  58.       return NULL;
  59.     }
  60.    return ptr;
  61. #else
  62.     I haven't taken the time to make this work under Lattice
  63.     (since I don't have it), so you are on your own, here. If
  64.     you do make it work, please send the fix back to me.
  65. #endif
  66. }
  67.