More safe array tests.
[wine] / programs / winetest / util.c
1 /*
2  * Utility functions.
3  *
4  * Copyright 2003 Dimitrie O. Paun
5  * Copyright 2003 Ferenc Wagner
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <unistd.h>
23 #include <errno.h>
24
25 #include "winetest.h"
26
27 void *xmalloc (size_t len)
28 {
29     void *p = malloc (len);
30
31     if (!p) report (R_FATAL, "Out of memory.");
32     return p;
33 }
34
35 void *xrealloc (void *op, size_t len)
36 {
37     void *p = realloc (op, len);
38
39     if (!p) report (R_FATAL, "Out of memory.");
40     return p;
41 }
42
43 char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
44 {
45     size_t size = 1000;
46     char *p, *q;
47     int n;
48
49     p = malloc (size);
50     if (!p) return NULL;
51     while (1) {
52         n = vsnprintf (p, size, fmt, ap);
53         if (n < 0) size *= 2;   /* Windows */
54         else if ((unsigned)n >= size) size = n+1; /* glibc */
55         else break;
56         q = realloc (p, size);
57         if (!q) {
58           free (p);
59           return NULL;
60        }
61        p = q;
62     }
63     if (lenp) *lenp = n;
64     return p;
65 }
66
67 char *vstrmake (size_t *lenp, va_list ap)
68 {
69     const char *fmt;
70
71     fmt = va_arg (ap, const char*);
72     return vstrfmtmake (lenp, fmt, ap);
73 }
74
75 char *strmake (size_t *lenp, ...)
76 {
77     va_list ap;
78     char *p;
79
80     va_start (ap, lenp);
81     p = vstrmake (lenp, ap);
82     if (!p) report (R_FATAL, "Out of memory.");
83     va_end (ap);
84     return p;
85 }
86
87 void xprintf (const char *fmt, ...)
88 {
89     va_list ap;
90     size_t size;
91     ssize_t written;
92     char *buffer, *head;
93
94     va_start (ap, fmt);
95     buffer = vstrfmtmake (&size, fmt, ap);
96     head = buffer;
97     va_end (ap);
98     while ((written = write (1, head, size)) != size) {
99         if (written == -1)
100             report (R_FATAL, "Can't write logs: %d", errno);
101         head += written;
102         size -= written;
103     }
104     free (buffer);
105 }
106
107 const char *
108 badtagchar (const char *tag)
109 {
110     while (*tag)
111         if (('a'<=*tag && *tag<='z') ||
112             ('A'<=*tag && *tag<='Z') ||
113             ('0'<=*tag && *tag<='9') ||
114             *tag=='-' || *tag=='.')
115             tag++;
116         else return tag;
117     return NULL;
118 }