This is allocate.c in view mode; [Download] [Up]
#include <stdlib.h> #include <stdarg.h> #include <string.h> #include <mach/mach.h> #include "stuff/allocate.h" #include "stuff/errors.h" /* * allocate() is just a wrapper around malloc that prints an error message and * exits if the malloc fails. */ void * allocate( unsigned long size) { void *p; if(size == 0) return(NULL); if((p = malloc(size)) == NULL) system_fatal("virtual memory exhausted (malloc failed)"); return(p); } /* * reallocate() is just a wrapper around realloc that prints and error message * and exits if the realloc fails. */ void * reallocate( void *p, unsigned long size) { if(p == NULL) return(allocate(size)); if((p = realloc(p, size)) == NULL) system_fatal("virtual memory exhausted (realloc failed)"); return(p); } /* * savestr() malloc's space for the string passed to it, copys the string into * the space and returns a pointer to that space. */ char * savestr( const char *s) { long len; char *r; len = strlen(s) + 1; r = (char *)allocate(len); strcpy(r, s); return(r); } /* * Makestr() creates a string that is the concatenation of a variable number of * strings. It is pass a variable number of pointers to strings and the last * pointer is NULL. It returns the pointer to the string it created. The * storage for the string is malloc()'ed can be free()'ed when nolonger needed. */ char * makestr( const char *args, ...) { va_list ap; char *s, *p; long size; size = 0; if(args != NULL){ size += strlen(args); va_start(ap, args); p = (char *)va_arg(ap, char *); while(p != NULL){ size += strlen(p); p = (char *)va_arg(ap, char *); } } s = allocate(size + 1); *s = '\0'; if(args != NULL){ (void)strcat(s, args); va_start(ap, args); p = (char *)va_arg(ap, char *); while(p != NULL){ (void)strcat(s, p); p = (char *)va_arg(ap, char *); } va_end(ap); } return(s); }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.