Annotation of GNUtools/cctools/libstuff/allocate.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.