home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE2.TAR / stcopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-02  |  2.1 KB  |  101 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.  * <copyright.h>.
  6.  *
  7.  * v1.2.0 - 09/17/91 (bpk) - added BULL & USG stuff, thanks to Jim Sillas
  8.  */
  9.  
  10. #include <copyright.h>
  11. #include <stdio.h>
  12. #if defined(USG) || defined(UTS) || defined(_AIX)
  13. # include <string.h>
  14. #else
  15. # include <strings.h>
  16. #endif
  17.  
  18. char    *stcopyr();
  19.  
  20. int    string_count = 0;
  21. int    string_max = 0;
  22.  
  23. /*
  24.  * stcopy - allocate space for and copy a string
  25.  *
  26.  *     STCOPY takes a string as an argument, allocates space for
  27.  *     a copy of the string, copies the string to the allocated space,
  28.  *     and returns a pointer to the copy.
  29.  */
  30.  
  31. char *
  32. stcopy(st)
  33.     char    *st;
  34.     {
  35.       if (!st) return(NULL);
  36.       if (string_max < ++string_count) string_max = string_count;
  37.  
  38.       return strcpy((char *)malloc(strlen(st) + 1), st);
  39.     }
  40.  
  41. /*
  42.  * stcopyr - copy a string allocating space if necessary
  43.  *
  44.  *     STCOPYR takes a string, S, as an argument, and a pointer to a second
  45.  *     string, R, which is to be replaced by S.  If R is long enough to
  46.  *     hold S, S is copied.  Otherwise, new space is allocated, and R is
  47.  *     freed.  S is then copied to the newly allocated space.  If S is
  48.  *     NULL, then R is freed and NULL is returned.
  49.  *
  50.  *     In any event, STCOPYR returns a pointer to the new copy of S,
  51.  *     or a NULL pointer.
  52.  */
  53. char *
  54. stcopyr(s,r)
  55.     char    *s;
  56.     char    *r;
  57.     {
  58.     int    sl;
  59.  
  60.     if(!s && r) {
  61.         free(r);
  62.         string_count--;
  63.         return(NULL);
  64.     }
  65.     else if (!s) return(NULL);
  66.  
  67.     sl = strlen(s) + 1;
  68.  
  69.     if(r) {
  70.         if ((strlen(r) + 1) < sl) {
  71.         free(r);
  72.         r = (char *) malloc(sl);
  73.         }
  74.     }
  75.     else {
  76.         r = (char *) malloc(sl);
  77.         string_count++;
  78.         if(string_max < string_count) string_max = string_count;
  79.     }
  80.         
  81.     return strcpy(r,s);
  82.     }
  83.  
  84. /*
  85.  * stfree - free space allocated by stcopy or stalloc
  86.  *
  87.  *     STFREE takes a string that was returned by stcopy or stalloc 
  88.  *     and frees the space that was allocated for the string.
  89.  */
  90. void
  91. stfree(st)
  92.     char *st;
  93.     {
  94.     if(st) {
  95.         free(st);
  96.         string_count--;
  97.     }
  98.     }
  99.  
  100.  
  101.