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