Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/vczip/tests/terror.h
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* Glenn Fowler <[email protected]> *
19
* *
20
***********************************************************************/
21
#include <ast_common.h>
22
23
#ifndef NIL
24
#define NIL(t) ((t)0)
25
#endif
26
27
#if __STD_C
28
#include <stdarg.h>
29
#else
30
#include <varargs.h>
31
#endif
32
33
_BEGIN_EXTERNS_
34
35
#if !_SFIO_H
36
extern int sprintf _ARG_((char*, const char*, ...));
37
extern int vsprintf _ARG_((char*, const char*, va_list));
38
#endif
39
40
extern int atexit _ARG_((void (*)(void)));
41
extern void exit _ARG_((int));
42
extern size_t strlen _ARG_((const char*));
43
extern Void_t* malloc _ARG_((size_t));
44
extern char* getenv _ARG_((const char*));
45
46
extern int strncmp _ARG_((const char*, const char*, size_t));
47
extern int strcmp _ARG_((const char*, const char*));
48
extern int system _ARG_((const char*));
49
50
#if !_hdr_unistd
51
extern int alarm _ARG_((int));
52
extern int sleep _ARG_((int));
53
extern int fork();
54
extern int wait _ARG_((int*));
55
extern int access _ARG_((const char*, int));
56
extern int write _ARG_((int, const void*, int));
57
extern int unlink _ARG_((const char*));
58
extern Void_t* sbrk _ARG_((int));
59
extern int getpid();
60
#endif
61
62
extern void tsterror _ARG_((char*, ...));
63
extern void tstwarn _ARG_((char*, ...));
64
extern void tstsuccess _ARG_((char*, ...));
65
66
_END_EXTERNS_
67
68
static int Tstline;
69
static char Tstfile[16][256];
70
71
#ifdef __LINE__
72
#define terror (Tstline=__LINE__),tsterror
73
#else
74
#define terror (Tstline=-1),tsterror
75
#endif
76
77
#ifdef __LINE__
78
#define twarn (Tstline=__LINE__),tstwarn
79
#else
80
#define twarn (Tstline=-1),tstwarn
81
#endif
82
83
#ifdef __LINE__
84
#define tsuccess (Tstline=__LINE__),tstsuccess
85
#else
86
#define tsuccess (Tstline=-1),tstsuccess
87
#endif
88
89
#define tmesg (Tstline=-1),tstwarn
90
91
#ifdef DEBUG
92
#ifdef __LINE__
93
#define TSTDEBUG(x) (Tstline=__LINE__),tstwarn x
94
#else
95
#define TSTDEBUG(x) (Tstline=-1),tstwarn x
96
#endif
97
#else
98
#define TSTDEBUG(x)
99
#endif
100
101
#ifndef MAIN
102
#if __STD_C
103
#define MAIN() main(int argc, char** argv)
104
#else
105
#define MAIN() main(argc, argv) int argc; char** argv;
106
#endif
107
#endif /*MAIN*/
108
109
#ifndef TSTEXIT
110
#define TSTEXIT(v) { tstcleanup(); exit(v); }
111
#endif
112
113
static void tstcleanup()
114
{
115
#ifdef DEBUG
116
twarn("Temp files will not be removed");
117
#else
118
int i;
119
for(i = 0; i < sizeof(Tstfile)/sizeof(Tstfile[0]); ++i)
120
if(Tstfile[i][0])
121
unlink(Tstfile[i]);
122
#endif
123
}
124
125
#if __STD_C
126
static void tstputmesg(int line, char* form, va_list args)
127
#else
128
static void tstputmesg(line, form, args)
129
int line;
130
char* form;
131
va_list args;
132
#endif
133
{
134
char *s, buf[1024];
135
int n;
136
137
for(n = 0; n < sizeof(buf); ++n)
138
buf[n] = 0;
139
140
s = buf; n = 0;
141
if(line >= 0)
142
{
143
#if _SFIO_H
144
sfsprintf(s, sizeof(buf), "\tLine=%d: ", line);
145
#else
146
sprintf(s, "\tLine=%d: ", line);
147
#endif
148
s += (n = strlen(s));
149
}
150
#if _SFIO_H
151
sfvsprintf(s, sizeof(buf)-n, form, args);
152
#else
153
vsprintf(s, form, args);
154
#endif
155
156
if((n = strlen(buf)) > 0)
157
{ if(buf[n-1] != '\n')
158
{ buf[n] = '\n';
159
n += 1;
160
}
161
write(2,buf,n);
162
}
163
}
164
165
166
#if __STD_C
167
void tsterror(char* form, ...)
168
#else
169
void tsterror(va_alist)
170
va_dcl
171
#endif
172
{
173
char failform[1024];
174
175
va_list args;
176
#if __STD_C
177
va_start(args,form);
178
#else
179
char* form;
180
va_start(args);
181
form = va_arg(args,char*);
182
#endif
183
184
#if _SFIO_H
185
sfsprintf(failform, sizeof(failform), "Failure: %s", form);
186
#else
187
sprintf(failform, "Failure: %s", form);
188
#endif
189
190
tstputmesg(Tstline,failform,args);
191
192
va_end(args);
193
194
tstcleanup();
195
exit(1);
196
}
197
198
199
#if __STD_C
200
void tstsuccess(char* form, ...)
201
#else
202
void tstsuccess(va_alist)
203
va_dcl
204
#endif
205
{
206
va_list args;
207
#if __STD_C
208
va_start(args,form);
209
#else
210
char* form;
211
va_start(args);
212
form = va_arg(args,char*);
213
#endif
214
215
tstputmesg(Tstline,form,args);
216
217
va_end(args);
218
219
tstcleanup();
220
exit(0);
221
}
222
223
224
#if __STD_C
225
void tstwarn(char* form, ...)
226
#else
227
void tstwarn(va_alist)
228
va_dcl
229
#endif
230
{
231
#if TEST_VERBOSE
232
va_list args;
233
#if __STD_C
234
va_start(args,form);
235
#else
236
char* form;
237
va_start(args);
238
form = va_arg(args,char*);
239
#endif
240
241
tstputmesg(Tstline,form,args);
242
243
va_end(args);
244
#endif
245
}
246
247
248
#if __STD_C
249
static char* tstfile(int n)
250
#else
251
static char* tstfile(n)
252
int n;
253
#endif
254
{
255
static int Setatexit = 0;
256
257
if(!Setatexit)
258
{ Setatexit = 1;
259
atexit(tstcleanup);
260
}
261
262
if(n >= sizeof(Tstfile)/sizeof(Tstfile[0]))
263
terror("Bad temporary file request:%d\n", n);
264
265
if(!Tstfile[n][0])
266
{
267
#ifdef DEBUG
268
#if _SFIO_H
269
sfsprintf(Tstfile[n], sizeof(Tstfile[0]), "Tstfile.%c%c%c", '0'+n, '0'+n, '0'+n);
270
#else
271
sprintf(Tstfile[n], "Tstfile.%c%c%c", '0'+n, '0'+n, '0'+n);
272
#endif
273
#else
274
static int pid;
275
static char* tmp;
276
if (!tmp)
277
{ if (!(tmp = (char*)getenv("TMPDIR")) || access(tmp, 0) != 0)
278
tmp = "/tmp";
279
pid = (int)getpid() % 10000;
280
}
281
#if _SFIO_H
282
sfsprintf(Tstfile[n], sizeof(Tstfile[0]), "%s/sft.%c.%d", tmp, '0'+n, pid);
283
#else
284
sprintf(Tstfile[n], "%s/sft.%c.%d", tmp, '0'+n, pid);
285
#endif
286
#endif
287
}
288
289
return Tstfile[n];
290
}
291
292
unsigned int trandom()
293
{
294
static unsigned int rand = 0xdeadbeef;
295
rand = rand*17109811 + 751;
296
return rand;
297
}
298
299