home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / cctools / libstuff / allocate.c next >
Encoding:
C/C++ Source or Header  |  1995-01-21  |  2.0 KB  |  102 lines

  1. #include <stdlib.h>
  2. #include <stdarg.h>
  3. #include <string.h>
  4. #include <mach/mach.h>
  5.  
  6. #include "stuff/allocate.h"
  7. #include "stuff/errors.h"
  8. /*
  9.  * allocate() is just a wrapper around malloc that prints an error message and
  10.  * exits if the malloc fails.
  11.  */
  12. __private_extern__
  13. void *
  14. allocate(
  15. unsigned long size)
  16. {
  17.     void *p;
  18.  
  19.     if(size == 0)
  20.         return(NULL);
  21.     if((p = malloc(size)) == NULL)
  22.         system_fatal("virtual memory exhausted (malloc failed)");
  23.     return(p);
  24. }
  25.  
  26. /*
  27.  * reallocate() is just a wrapper around realloc that prints and error message
  28.  * and exits if the realloc fails.
  29.  */
  30. __private_extern__
  31. void *
  32. reallocate(
  33. void *p,
  34. unsigned long size)
  35. {
  36.     if(p == NULL)
  37.         return(allocate(size));
  38.     if((p = realloc(p, size)) == NULL)
  39.         system_fatal("virtual memory exhausted (realloc failed)");
  40.     return(p);
  41. }
  42.  
  43. /*
  44.  * savestr() malloc's space for the string passed to it, copys the string into
  45.  * the space and returns a pointer to that space.
  46.  */
  47. __private_extern__
  48. char *
  49. savestr(
  50. const char *s)
  51. {
  52.     long len;
  53.     char *r;
  54.  
  55.     len = strlen(s) + 1;
  56.     r = (char *)allocate(len);
  57.     strcpy(r, s);
  58.     return(r);
  59. }
  60.  
  61. /*
  62.  * Makestr() creates a string that is the concatenation of a variable number of
  63.  * strings.  It is pass a variable number of pointers to strings and the last
  64.  * pointer is NULL.  It returns the pointer to the string it created.  The
  65.  * storage for the string is malloc()'ed can be free()'ed when nolonger needed.
  66.  */
  67. __private_extern__
  68. char *
  69. makestr(
  70. const char *args,
  71. ...)
  72. {
  73.     va_list ap;
  74.     char *s, *p;
  75.     long size;
  76.  
  77.     size = 0;
  78.     if(args != NULL){
  79.         size += strlen(args);
  80.         va_start(ap, args);
  81.         p = (char *)va_arg(ap, char *);
  82.         while(p != NULL){
  83.         size += strlen(p);
  84.         p = (char *)va_arg(ap, char *);
  85.         }
  86.     }
  87.     s = allocate(size + 1);
  88.     *s = '\0';
  89.  
  90.     if(args != NULL){
  91.         (void)strcat(s, args);
  92.         va_start(ap, args);
  93.         p = (char *)va_arg(ap, char *);
  94.         while(p != NULL){
  95.         (void)strcat(s, p);
  96.         p = (char *)va_arg(ap, char *);
  97.         }
  98.         va_end(ap);
  99.     }
  100.     return(s);
  101. }
  102.