File: util.c

package info (click to toggle)
xzx 2.9.0-1
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 1,644 kB
  • ctags: 2,195
  • sloc: ansic: 25,375; sh: 1,937; yacc: 714; makefile: 197; lex: 107
file content (456 lines) | stat: -rw-r--r-- 8,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/********************************************************************************
* Copyright (c) Des Herriott 1993, 1994
*               Erik Kunze   1995 - 1999
*
* Permission to use, distribute, and sell this software and its documentation
* for any purpose is hereby granted without fee, provided that the above
* copyright notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that the name
* of the copyright holder not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior permission.  The
* copyright holder makes no representations about the suitability of this
* software for any purpose.  It is provided "as is" without express or implied
* warranty. THE CODE MAY NOT BE MODIFIED OR REUSED WITHOUT PERMISSION!
*
* THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* Authors: Des Herriott
*          Erik Kunze
*******************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifndef lint
static char rcsid[] = "$Id: util.c,v 4.26 1999/04/27 13:55:31 erik Rel $";
#endif
#include <sys/types.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <sys/time.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <assert.h>
#include "resource.h"
#include "main.h"
#include "util.h"
#define NELEM(a)		(sizeof(a) / sizeof(a[0]))
typedef struct _onQuitRecord {
void (*f)(void);
struct _onQuitRecord *next;
} onQuitRecord;
const char *FtExt[] = {
"sna", "z80", "slt",
"tap", "voc", "tzx",
"dat",
"dsk", "trd", "fdi",
"scl", "mgt", "img",
"mdr",
"scr", "pok"
};
static onQuitRecord *onQuitList = NULL;
FILE *
Fopen(char *name, const char *amode)
{
FILE *fp = NULL;
char *buf;
char *path, *element;
if (!name || !name[0])
{
return NULL;
}
if (name[0] == '/' || !strncmp(name, "./", 2) || !strncmp(name, "../", 3))
{
return (fopen(name, amode));
}
path = Strdup(GETCFG(libDir), "Fopen");
element = strtok(path, ":");
while (element)
{
buf = (char *)Malloc(strlen(name) + strlen(element) + 2, "Fopen");
(void)sprintf(buf, "%s/%s", element, name);
fp = fopen(buf, amode);
free(buf);
if (fp)
{
break;
}
element = strtok(NULL, ":");
}
free(path);
return fp;
}
void *
Malloc(size_t size, const char *function)
{
void *p;
if (!(p = calloc(size, 1)))
{
Msg(M_FATAL, "malloc failed in %s()", function);
}
return p;
}
char *
Strdup(const char *src, const char *function)
{
char *dst;
if (!(dst = strdup(src)))
{
Msg(M_FATAL, "strdup failed in %s()", function);
}
return dst;
}
void *
Mmap(char *fname, int regular, int *readonly, size_t *size)
{
FILE *fp;
struct stat sbuf;
void *mmp = (void *)-1;
int res;
if (!(fp = Fopen(fname, "rb")))
{
Msg(M_ERR, "couldn't open <%s> for reading", fname);
return mmp;
}
res = fstat(fileno(fp), &sbuf);
(void)fclose(fp);
if (res < 0)
{
return mmp;
}
if (regular && !S_ISREG(sbuf.st_mode))
{
Msg(M_WARN, "not a regular file");
return mmp;
}
*size = (size_t)sbuf.st_size,
*readonly = (sbuf.st_mode & S_IWRITE) == 0 ? 1 : *readonly;
if (!(fp = Fopen(fname, *readonly ? "rb" : "r+b")))
{
Msg(M_ERR, "couldn't open <%s> for %sing", fname,
*readonly ? "read" : "writ");
return mmp;
}
#ifdef DEMO
mmp = mmap(NULL, *size, PROT_READ | (*readonly ? 0 : PROT_WRITE),
MAP_PRIVATE, fileno(fp), (off_t)0);
#else
mmp = mmap(NULL, *size, PROT_READ | (*readonly ? 0 : PROT_WRITE),
MAP_SHARED, fileno(fp), (off_t)0);
#endif
if (mmp == (void *)-1)
{
Msg(M_PERR, "mmap failed");
}
(void)fclose(fp);
return mmp;
}
void
Munmap(void *start, size_t length)
{
(void)munmap(start, length);
}
void
OnQuit(void (*fn)(void))
{
onQuitRecord *p;
p = (onQuitRecord *)Malloc(sizeof(onQuitRecord), "OnQuit");
p->f = fn;
p->next = onQuitList;
onQuitList = p;
}
void
Quit(int status)
{
onQuitRecord *p = onQuitList;
while (p)
{
p->f();
p = p->next;
}
exit(status);
}
#ifdef nec_ews
void
Msg(va_alist)
va_dcl
{
va_list args;
int errorLevel;
char *fmt;
va_start(args);
errorLevel = (int)va_arg(args, int);
fmt = (char *)va_arg(args, char *);
#else
void
Msg(int errorLevel, ...)
{
va_list args;
char *fmt;
va_start(args, errorLevel);
fmt = (char *)va_arg(args, char *);
#endif
if (!GETCFG(quiet)
|| errorLevel == M_ERR || errorLevel == M_PERR || errorLevel == M_FATAL)
{
(void)fprintf(stderr, "%s: ", ProgName);
switch(errorLevel)
{
case M_WARN:
case M_PWARN:
(void)fprintf(stderr, "warning: ");
break;
case M_ERR:
case M_PERR:
(void)fprintf(stderr, "error: ");
break;
case M_FATAL:
(void)fprintf(stderr, "fatal: ");
break;
case M_DEBUG:
(void)fprintf(stderr, "debug: ");
break;
}
(void)vfprintf(stderr, fmt, args);
(void)putc('\n', stderr);
if (errorLevel == M_PWARN || errorLevel == M_PERR)
{
perror("  -> reason");
}
}
if (errorLevel == M_FATAL)
{
Quit(1);
}
}
void
IntFrequency(int ms)
{
struct itimerval itv;
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = ms;
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = ms;
(void)setitimer(ITIMER_REAL, &itv, NULL);
}
int
ClassifyDescriptor(int fd)
{
struct stat buf;
if (isatty(fd))
{
return FD_TTY;
}
if (!fstat(fd, &buf))
{
if (S_ISREG(buf.st_mode))
{
return FD_FILE;
}
else if (S_ISFIFO(buf.st_mode))
{
return FD_PIPE;
}
}
Msg(M_WARN, "don't know where fd %d is coming from", fd);
return FD_UNKNOWN;
}
void
SetBlocking(int fd, int mode)
{
int status;
if  ((status = fcntl(fd, F_GETFL)) != -1)
{
#ifdef O_NONBLOCK
status = mode ? status | O_NONBLOCK : status & ~O_NONBLOCK;
#else
status = mode ?	status | O_NDELAY : status & ~O_NDELAY;
#endif
if (fcntl(fd, F_SETFL, status) != -1)
{
return;
}
}
Msg(M_PERR, "couldn't make fd %d %sblocking", fd, mode ? "non" : "");
}
const char *
GetBaseName(char *filename)
{
char *s;
if (filename)
{
if ((s = strrchr(filename, '/')))
{
return ++s;
}
else
{
return filename;
}
}
return ("-none-");
}
int
GetFileType(char *fname)
{
char *p;
int i;
assert(NELEM(FtExt) == FT_UNKNOWN);
if ((p = strrchr(fname, '.')))
{
p++;
for (i = 0; i < FT_UNKNOWN; i++)
{
if (!strncasecmp(p, FtExt[i], 4))
{
return i;
}
}
}
return FT_UNKNOWN;
}
#ifndef HAVE_STRCASECMP
int
strcasecmp(const char *s1, const char *s2)
{
char c1, c2;
for ( ; *s1 && *s2; s1++, s2++)
{
c1 = isupper(*s1) ? tolower(*s1) : *s1;
c2 = isupper(*s2) ? tolower(*s2) : *s2;
if (c1 != c2)
{
return (c1 - c2);
}
}
if (*s1 || *s2)
{
return (*s1 - *s2);
}
return 0;
}
int
strncasecmp(const char *s1, const char *s2, int n)
{
int i;
char c1, c2;
for (i = 0; *s1 && *s2 && i < n; s1++, s2++, i++)
{
c1 = isupper(*s1) ? tolower(*s1) : *s1;
c2 = isupper(*s2) ? tolower(*s2) : *s2;
if (c1 != c2)
{
return (c1 - c2);
}
}
if ((i < n) && (*s1 || *s2))
{
return (*s1 - *s2);
}
return 0;
}
#endif
void
DestroyList(Llist *head, void (*pDestroy)(void *))
{
Llist *pNext;
while (head)
{
if (head->item)
{
if (pDestroy)
{
pDestroy(head->item);
}
free(head->item);
}
pNext = head->next;
free(head);
head = pNext;
}
}
Llist *
AppendElemList(Llist *head, void *item)
{
Llist **ppHead = &head;
while (*ppHead)
{
ppHead = &((*ppHead)->next);
}
*ppHead = Malloc(sizeof(Llist), "AppendElemList");
(*ppHead)->item = item;
(*ppHead)->next = NULL;
return head;
}
Llist *
PrependElemList(Llist *head, void *item)
{
Llist *pHead;
pHead = Malloc(sizeof(Llist), "PrependElemList");
pHead->item = item;
pHead->next = head;
return pHead;
}
Llist *
RemoveElemList(void *item, int (*pCompare)(void *, void *), Llist *head,
void (*pDestroy)(void *))
{
Llist **ppHead = &head;
Llist *pElem;
while (*ppHead && (*pCompare)((*ppHead)->item, item))
{
ppHead = &((*ppHead)->next);
}
if (*ppHead)
{
pElem = *ppHead;
*ppHead = (*ppHead)->next;
if (pDestroy)
{
pDestroy(pElem);
}
free(pElem);
}
return head;
}
Llist *
SortedInsertList(void *item, int (*pCompare)(void *, void *), Llist *head)
{
Llist **ppHead = &head;
Llist *pElem = Malloc(sizeof(Llist), "SortedInsertList");
pElem->item = item;
pElem->next = NULL;
while (*ppHead && (*pCompare)(item, (*ppHead)->item) >= 0)
{
ppHead = &(*ppHead)->next;
}
if (*ppHead)
{
pElem->next = *ppHead;
}
*ppHead = pElem;
return head;
}
void *
RetrieveElemList(Llist *head, unsigned int which)
{
while (head && which)
{
head = head->next;
which--;
}
return (head ? head->item : NULL);
}