ntdll: Initialize needs_close to FALSE.
[wine/hacks.git] / programs / winetest / util.c
blob12c607347f988f1bfa3c8517ae6a43011eae3053
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
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <unistd.h>
23 #include <errno.h>
25 #include "winetest.h"
27 void *xmalloc (size_t len)
29 void *p = malloc (len);
31 if (!p) report (R_FATAL, "Out of memory.");
32 return p;
35 void *xrealloc (void *op, size_t len)
37 void *p = realloc (op, len);
39 if (len && !p) report (R_FATAL, "Out of memory.");
40 return p;
43 char *xstrdup( const char *str )
45 char *res = strdup( str );
46 if (!res) report (R_FATAL, "Out of memory.");
47 return res;
50 static char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
52 size_t size = 1000;
53 char *p, *q;
54 int n;
56 p = malloc (size);
57 if (!p) return NULL;
58 while (1) {
59 n = vsnprintf (p, size, fmt, ap);
60 if (n < 0) size *= 2; /* Windows */
61 else if ((unsigned)n >= size) size = n+1; /* glibc */
62 else break;
63 q = realloc (p, size);
64 if (!q) {
65 free (p);
66 return NULL;
68 p = q;
70 if (lenp) *lenp = n;
71 return p;
74 char *vstrmake (size_t *lenp, va_list ap)
76 const char *fmt;
78 fmt = va_arg (ap, const char*);
79 return vstrfmtmake (lenp, fmt, ap);
82 char *strmake (size_t *lenp, ...)
84 va_list ap;
85 char *p;
87 va_start (ap, lenp);
88 p = vstrmake (lenp, ap);
89 if (!p) report (R_FATAL, "Out of memory.");
90 va_end (ap);
91 return p;
94 void xprintf (const char *fmt, ...)
96 va_list ap;
97 size_t size;
98 ssize_t written;
99 char *buffer, *head;
101 va_start (ap, fmt);
102 buffer = vstrfmtmake (&size, fmt, ap);
103 head = buffer;
104 va_end (ap);
105 while ((written = write (1, head, size)) != size) {
106 if (written == -1)
107 report (R_FATAL, "Can't write logs: %d", errno);
108 head += written;
109 size -= written;
111 free (buffer);
115 goodtagchar (char c)
117 return (('a'<=c && c<='z') ||
118 ('A'<=c && c<='Z') ||
119 ('0'<=c && c<='9') ||
120 c=='-' || c=='.');
123 const char *
124 findbadtagchar (const char *tag)
126 while (*tag)
127 if (goodtagchar (*tag)) tag++;
128 else return tag;
129 return NULL;