Do not include <windows.h>, to avoid duplicate declaration of
[wine/wine64.git] / programs / winetest / util.c
blob1d6a47454c40044aed7d972db7faf552e9c8bd7e
1 /*
2 * Utility functions.
4 * Copyright 2003 Dimitrie O. Paun
5 * Copyright 2003 Ferenc Wagner
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.
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.
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
21 #include <unistd.h>
22 #include <errno.h>
24 #include "winetest.h"
26 void *xmalloc (size_t len)
28 void *p = malloc (len);
30 if (!p) report (R_FATAL, "Out of memory.");
31 return p;
34 void *xrealloc (void *op, size_t len)
36 void *p = realloc (op, len);
38 if (!p) report (R_FATAL, "Out of memory.");
39 return p;
42 char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
44 size_t size = 1000;
45 char *p, *q;
46 int n;
48 p = malloc (size);
49 if (!p) return NULL;
50 while (1) {
51 n = vsnprintf (p, size, fmt, ap);
52 if (n < 0) size *= 2; /* Windows */
53 else if ((unsigned)n >= size) size = n+1; /* glibc */
54 else break;
55 q = realloc (p, size);
56 if (!q) {
57 free (p);
58 return NULL;
60 p = q;
62 if (lenp) *lenp = n;
63 return p;
66 char *vstrmake (size_t *lenp, va_list ap)
68 const char *fmt;
70 fmt = va_arg (ap, const char*);
71 return vstrfmtmake (lenp, fmt, ap);
74 char *strmake (size_t *lenp, ...)
76 va_list ap;
77 char *p;
79 va_start (ap, lenp);
80 p = vstrmake (lenp, ap);
81 if (!p) report (R_FATAL, "Out of memory.");
82 va_end (ap);
83 return p;
86 void xprintf (const char *fmt, ...)
88 va_list ap;
89 size_t size;
90 ssize_t written;
91 char *buffer, *head;
93 va_start (ap, fmt);
94 buffer = vstrfmtmake (&size, fmt, ap);
95 head = buffer;
96 va_end (ap);
97 while ((written = write (1, head, size)) != size) {
98 if (written == -1)
99 report (R_FATAL, "Can't write logs: %d", errno);
100 head += written;
101 size -= written;
103 free (buffer);
106 char *
107 badtagchar (char *tag)
109 while (*tag)
110 if (('a'<=*tag && *tag<='z') ||
111 ('A'<=*tag && *tag<='Z') ||
112 ('0'<=*tag && *tag<='9') ||
113 *tag=='-' || *tag=='.')
114 tag++;
115 else return tag;
116 return NULL;