[BACK]Return to mandoc_aux.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mandoc_aux.c, Revision 1.2

1.2     ! schwarze    1: /*     $Id: mandoc_aux.c,v 1.1 2014/03/21 22:17:01 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.2     ! schwarze    4:  * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #include <sys/types.h>
                     19:
1.2     ! schwarze   20: #include <stdarg.h>
1.1       schwarze   21: #include <stdlib.h>
                     22: #include <stdio.h>
                     23: #include <string.h>
                     24:
                     25: #include "mandoc.h"
                     26: #include "mandoc_aux.h"
1.2     ! schwarze   27:
        !            28: int
        !            29: mandoc_asprintf(char **dest, const char *fmt, ...)
        !            30: {
        !            31:        va_list  ap;
        !            32:        int      ret;
        !            33:
        !            34:        va_start(ap, fmt);
        !            35:        ret = vasprintf(dest, fmt, ap);
        !            36:        va_end(ap);
        !            37:
        !            38:        if (-1 == ret) {
        !            39:                perror(NULL);
        !            40:                exit((int)MANDOCLEVEL_SYSERR);
        !            41:        }
        !            42:        return(ret);
        !            43: }
1.1       schwarze   44:
                     45: void *
                     46: mandoc_calloc(size_t num, size_t size)
                     47: {
                     48:        void    *ptr;
                     49:
                     50:        ptr = calloc(num, size);
                     51:        if (NULL == ptr) {
                     52:                perror(NULL);
                     53:                exit((int)MANDOCLEVEL_SYSERR);
                     54:        }
                     55:        return(ptr);
                     56: }
                     57:
                     58: void *
                     59: mandoc_malloc(size_t size)
                     60: {
                     61:        void    *ptr;
                     62:
                     63:        ptr = malloc(size);
                     64:        if (NULL == ptr) {
                     65:                perror(NULL);
                     66:                exit((int)MANDOCLEVEL_SYSERR);
                     67:        }
                     68:        return(ptr);
                     69: }
                     70:
                     71: void *
                     72: mandoc_realloc(void *ptr, size_t size)
                     73: {
                     74:
                     75:        ptr = realloc(ptr, size);
                     76:        if (NULL == ptr) {
                     77:                perror(NULL);
                     78:                exit((int)MANDOCLEVEL_SYSERR);
                     79:        }
                     80:        return(ptr);
                     81: }
                     82:
                     83: char *
                     84: mandoc_strdup(const char *ptr)
                     85: {
                     86:        char    *p;
                     87:
                     88:        p = strdup(ptr);
                     89:        if (NULL == p) {
                     90:                perror(NULL);
                     91:                exit((int)MANDOCLEVEL_SYSERR);
                     92:        }
                     93:        return(p);
                     94: }
                     95:
                     96: char *
                     97: mandoc_strndup(const char *ptr, size_t sz)
                     98: {
                     99:        char    *p;
                    100:
                    101:        p = mandoc_malloc(sz + 1);
                    102:        memcpy(p, ptr, sz);
                    103:        p[(int)sz] = '\0';
                    104:        return(p);
                    105: }