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