ReactOS 0.4.15-dev-7674-gc0b4db1
util.c
Go to the documentation of this file.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22#include <unistd.h>
23#include <errno.h>
24
25#include "winetest.h"
26
27void *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
35void *xrealloc (void *op, size_t len)
36{
37 void *p = realloc (op, len);
38
39 if (len && !p) report (R_FATAL, "Out of memory.");
40 return p;
41}
42
43char *xstrdup( const char *str )
44{
45 char *res = strdup( str );
46 if (!res) report (R_FATAL, "Out of memory.");
47 return res;
48}
49
50static char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
51{
52 size_t size = 1000;
53 char *p, *q;
54 int n;
55
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;
67 }
68 p = q;
69 }
70 if (lenp) *lenp = n;
71 return p;
72}
73
74char *vstrmake (size_t *lenp, va_list ap)
75{
76 const char *fmt;
77
78 fmt = va_arg (ap, const char*);
79 return vstrfmtmake (lenp, fmt, ap);
80}
81
82char *strmake (size_t *lenp, ...)
83{
84 va_list ap;
85 char *p;
86
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;
92}
93
94void xprintf (const char *fmt, ...)
95{
96 va_list ap;
97 size_t size;
98 ssize_t written;
99 char *buffer, *head;
100
101 va_start (ap, fmt);
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;
110 }
111 free (buffer);
112}
113
114int
116{
117 return (('a'<=c && c<='z') ||
118 ('A'<=c && c<='Z') ||
119 ('0'<=c && c<='9') ||
120 c=='-' || c=='.');
121}
122
123const char *
124findbadtagchar (const char *tag)
125{
126 while (*tag)
127 if (goodtagchar (*tag)) tag++;
128 else return tag;
129 return NULL;
130}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define va_arg(ap, T)
Definition: acmsvcex.h:89
#define write
Definition: acwin.h:97
struct outqueuenode * head
Definition: adnsresfilter.c:66
#define realloc
Definition: debug_ros.c:6
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
UINT op
Definition: effect.c:236
static void report(const DATA_BLOB *pDataIn, const DATA_BLOB *pOptionalEntropy, CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct, DWORD dwFlags)
Definition: protectdata.c:769
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLsizeiptr size
Definition: glext.h:5919
GLdouble n
Definition: glext.h:7729
GLuint res
Definition: glext.h:9613
GLuint buffer
Definition: glext.h:5915
const GLubyte * c
Definition: glext.h:8905
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
int goodtagchar(char c)
Definition: util.c:115
static char * vstrfmtmake(size_t *lenp, const char *fmt, va_list ap)
Definition: util.c:50
void * xrealloc(void *op, size_t len)
Definition: util.c:35
char * vstrmake(size_t *lenp, va_list ap)
Definition: util.c:74
char * xstrdup(const char *str)
Definition: util.c:43
void xprintf(const char *fmt,...)
Definition: util.c:94
void * xmalloc(size_t len)
Definition: util.c:27
char * strmake(size_t *lenp,...)
Definition: util.c:82
const char * findbadtagchar(const char *tag)
Definition: util.c:124
int ssize_t
Definition: rosdhcp.h:48
const WCHAR * str
#define errno
Definition: errno.h:18
_Check_return_ _CRTIMP char *__cdecl strdup(_In_opt_z_ const char *_Src)
Definition: dsound.c:943
Definition: ecma_167.h:138
#define vsnprintf
Definition: tif_win32.c:406
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36
@ R_FATAL
Definition: winetest.h:56