1 /*
2 * util.c --
3 *
4 * Misc utility functions.
5 *
6 * Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
7 *
8 * See the file "COPYING" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * @(#) $Id: util.c 1470 2002-11-13 13:15:03Z strauss $
12 */
13
14 #include <config.h>
15
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21
22 #include "util.h"
23 #include "snprintf.h"
24
25 #ifdef HAVE_DMALLOC_H
26 #include <dmalloc.h>
27 #endif
28
29
30
31 #ifdef HAVE_DMALLOC_H
32
_smiMalloc(char * file,int line,size_t size)33 void *_smiMalloc(char *file, int line, size_t size)
34 {
35 char *m = _calloc_leap(file, line, 1, size);
36 if (! m) {
37 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
38 }
39 return m;
40 }
41
_smiRealloc(char * file,int line,void * ptr,size_t size)42 void *_smiRealloc(char *file, int line, void *ptr, size_t size)
43 {
44 char *m = _realloc_leap(file, line, ptr, size);
45 if (! m) {
46 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
47 }
48 return m;
49 }
50
_smiStrdup(char * file,int line,const char * s1)51 char *_smiStrdup(char *file, int line, const char *s1)
52 {
53 if (s1) {
54 char *m = _strdup_leap(file, line, s1);
55 if (! m) {
56 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
57 }
58 return m;
59 } else {
60 return NULL;
61 }
62 }
63
_smiStrndup(char * file,int line,const char * s1,size_t n)64 char *_smiStrndup(char *file, int line, const char *s1, size_t n)
65 {
66 char *m;
67
68 m = _smiMalloc(file, line, n+1);
69 if (! m) {
70 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
71 }
72 strncpy(m, s1, n);
73 m[n] = 0;
74 return m;
75 }
76
_smiFree(char * file,int line,void * ptr)77 void _smiFree(char *file, int line, void *ptr)
78 {
79 if (ptr) {
80 _free_leap(file, line, ptr);
81 }
82 }
83
84 #else
85
smiMalloc(size_t size)86 void *smiMalloc(size_t size)
87 {
88 char *m = calloc(1, size);
89 if (! m) {
90 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
91 }
92 return m;
93 }
94
smiRealloc(void * ptr,size_t size)95 void *smiRealloc(void *ptr, size_t size)
96 {
97 char *m = realloc(ptr, size);
98 if (! m) {
99 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
100 }
101 return m;
102 }
103
smiStrdup(const char * s1)104 char *smiStrdup(const char *s1)
105 {
106 if (s1) {
107 char *m = strdup(s1);
108 if (! m) {
109 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
110 }
111 return m;
112 } else {
113 return NULL;
114 }
115 }
116
smiStrndup(const char * s1,size_t n)117 char *smiStrndup(const char *s1, size_t n)
118 {
119 char *m;
120
121 m = smiMalloc(n+1);
122 if (! m) {
123 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
124 }
125 strncpy(m, s1, n);
126 m[n] = 0;
127 return m;
128 }
129
smiFree(void * ptr)130 void smiFree(void *ptr)
131 {
132 if (ptr) {
133 free(ptr);
134 }
135 }
136
137 #endif
138
139
140
smiIsPath(const char * s)141 int smiIsPath(const char *s)
142 {
143 return (strchr(s, '.') || strchr(s, DIR_SEPARATOR));
144 }
145
146
147
148 #ifndef HAVE_TIMEGM
timegm(struct tm * tm)149 time_t timegm(struct tm *tm)
150 {
151 char *tz;
152 static char *s = NULL;
153 char *tofree = NULL;
154 time_t t;
155
156 /* ensure to call mktime() for UTC */
157 tz = getenv("TZ");
158 if (tz) {
159 tofree = s;
160 smiAsprintf(&s, "TZ=%s", tz);
161 }
162 putenv("TZ=NULL");
163 t = mktime(tm);
164 if (tz) {
165 putenv(s);
166 } else {
167 putenv("TZ=");
168 }
169 if (tofree) smiFree(tofree);
170
171 return t;
172 }
173 #endif
174
175
176
smiTypeDerivedFrom(Type * typePtr,Type * parentTypePtr)177 int smiTypeDerivedFrom(Type *typePtr, Type *parentTypePtr)
178 {
179 Type *t;
180
181 if ((!typePtr) || (!parentTypePtr)) {
182 return 0;
183 }
184
185 for (t = typePtr; t != NULL; t = t->parentPtr) {
186 if (parentTypePtr == t) {
187 return 1;
188 }
189 }
190
191 return 0;
192 }
193