util.c 2.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Utility functions.
 *
 * Copyright 2003 Dimitrie O. Paun
 * Copyright 2003 Ferenc Wagner
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
 */
21

22
#include <unistd.h>
23
#include <errno.h>
24 25 26 27 28 29 30

#include "winetest.h"

void *xmalloc (size_t len)
{
    void *p = malloc (len);

31
    if (!p) report (R_FATAL, "Out of memory.");
32 33 34 35 36 37 38
    return p;
}

void *xrealloc (void *op, size_t len)
{
    void *p = realloc (op, len);

39
    if (len && !p) report (R_FATAL, "Out of memory.");
40 41 42
    return p;
}

43 44 45 46 47 48 49
char *xstrdup( const char *str )
{
    char *res = strdup( str );
    if (!res) report (R_FATAL, "Out of memory.");
    return res;
}

50
static char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
{
    size_t size = 1000;
    char *p, *q;
    int n;

    p = malloc (size);
    if (!p) return NULL;
    while (1) {
        n = vsnprintf (p, size, fmt, ap);
        if (n < 0) size *= 2;   /* Windows */
        else if ((unsigned)n >= size) size = n+1; /* glibc */
        else break;
        q = realloc (p, size);
        if (!q) {
          free (p);
          return NULL;
       }
       p = q;
    }
    if (lenp) *lenp = n;
    return p;
}

74 75 76 77 78 79 80 81
char *vstrmake (size_t *lenp, va_list ap)
{
    const char *fmt;

    fmt = va_arg (ap, const char*);
    return vstrfmtmake (lenp, fmt, ap);
}

82
char *strmake (size_t *lenp, ...)
83 84 85 86
{
    va_list ap;
    char *p;

87 88 89
    va_start (ap, lenp);
    p = vstrmake (lenp, ap);
    if (!p) report (R_FATAL, "Out of memory.");
90 91 92
    va_end (ap);
    return p;
}
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
void xprintf (const char *fmt, ...)
{
    va_list ap;
    size_t size;
    ssize_t written;
    char *buffer, *head;

    va_start (ap, fmt);
    buffer = vstrfmtmake (&size, fmt, ap);
    head = buffer;
    va_end (ap);
    while ((written = write (1, head, size)) != size) {
        if (written == -1)
            report (R_FATAL, "Can't write logs: %d", errno);
        head += written;
        size -= written;
    }
    free (buffer);
}

114 115 116 117 118 119 120 121 122
int
goodtagchar (char c)
{
    return (('a'<=c && c<='z') ||
            ('A'<=c && c<='Z') ||
            ('0'<=c && c<='9') ||
            c=='-' || c=='.');
}

Ferenc Wagner's avatar
Ferenc Wagner committed
123
const char *
124
findbadtagchar (const char *tag)
125 126
{
    while (*tag)
127
        if (goodtagchar (*tag)) tag++;
128 129 130
        else return tag;
    return NULL;
}