home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume22 / archie / part01 / stcopy.c < prev    next >
C/C++ Source or Header  |  1991-08-21  |  2KB  |  94 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <uw-copyright.h>.
  6.  */
  7.  
  8. #include <uw-copyright.h>
  9. #include <stdio.h>
  10. #include <strings.h>
  11.  
  12. char    *stcopyr();
  13.  
  14. int    string_count = 0;
  15. int    string_max = 0;
  16.  
  17. /*
  18.  * stcopy - allocate space for and copy a string
  19.  *
  20.  *     STCOPY takes a string as an argument, allocates space for
  21.  *     a copy of the string, copies the string to the allocated space,
  22.  *     and returns a pointer to the copy.
  23.  */
  24.  
  25. char *
  26. stcopy(st)
  27.     char    *st;
  28.     {
  29.       if (!st) return(NULL);
  30.       if (string_max < ++string_count) string_max = string_count;
  31.  
  32.       return strcpy((char *)malloc(strlen(st) + 1), st);
  33.     }
  34.  
  35. /*
  36.  * stcopyr - copy a string allocating space if necessary
  37.  *
  38.  *     STCOPYR takes a string, S, as an argument, and a pointer to a second
  39.  *     string, R, which is to be replaced by S.  If R is long enough to
  40.  *     hold S, S is copied.  Otherwise, new space is allocated, and R is
  41.  *     freed.  S is then copied to the newly allocated space.  If S is
  42.  *     NULL, then R is freed and NULL is returned.
  43.  *
  44.  *     In any event, STCOPYR returns a pointer to the new copy of S,
  45.  *     or a NULL pointer.
  46.  */
  47. char *
  48. stcopyr(s,r)
  49.     char    *s;
  50.     char    *r;
  51.     {
  52.     int    sl;
  53.  
  54.     if(!s && r) {
  55.         free(r);
  56.         string_count--;
  57.         return(NULL);
  58.     }
  59.     else if (!s) return(NULL);
  60.  
  61.     sl = strlen(s) + 1;
  62.  
  63.     if(r) {
  64.         if ((strlen(r) + 1) < sl) {
  65.         free(r);
  66.         r = (char *) malloc(sl);
  67.         }
  68.     }
  69.     else {
  70.         r = (char *) malloc(sl);
  71.         string_count++;
  72.         if(string_max < string_count) string_max = string_count;
  73.     }
  74.         
  75.     return strcpy(r,s);
  76.     }
  77.  
  78. /*
  79.  * stfree - free space allocated by stcopy or stalloc
  80.  *
  81.  *     STFREE takes a string that was returned by stcopy or stalloc 
  82.  *     and frees the space that was allocated for the string.
  83.  */
  84. stfree(st)
  85.     char *st;
  86.     {
  87.     if(st) {
  88.         free(st);
  89.         string_count--;
  90.     }
  91.     }
  92.  
  93.  
  94.