summaryrefslogtreecommitdiff
path: root/other/zylyx/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/zylyx/src/common.c')
-rw-r--r--other/zylyx/src/common.c290
1 files changed, 290 insertions, 0 deletions
diff --git a/other/zylyx/src/common.c b/other/zylyx/src/common.c
new file mode 100644
index 0000000..3f3a99a
--- /dev/null
+++ b/other/zylyx/src/common.c
@@ -0,0 +1,290 @@
1
2#include <sys/time.h>
3#include <netinet/in.h>
4#include <unistd.h>
5#include <pthread.h>
6#include <time.h>
7#include <stdarg.h>
8#include <stdio.h>
9#include <string.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include "common.h"
13
14#ifdef DEBUG
15void
16debugp (char *filename, const char *str, ...)
17{
18 FILE *fp; /* temporary file pointer */
19 va_list vl;
20
21 fp = fopen (filename, "a");
22 if (fp == NULL)
23 return;
24
25 va_start (vl, str);
26 vfprintf (fp, str, vl);
27 va_end (vl);
28
29 fclose (fp);
30
31 return;
32}
33
34void
35hexdump (char *filename, unsigned char *data, unsigned int amount)
36{
37 FILE *fp; /* temporary file pointer */
38 unsigned int dp, p; /* data pointer */
39 const char trans[] = "................................ !\"#$%&'()*+,-./0123456789"
40 ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
41 "nopqrstuvwxyz{|}~...................................."
42 "....................................................."
43 "........................................";
44
45 fp = fopen (filename, "a");
46 if (fp == NULL)
47 return;
48
49 fprintf (fp, "\n-packet-\n");
50
51 for (dp = 1; dp <= amount; dp++) {
52 fprintf (fp, "%02x ", data[dp-1]);
53 if ((dp % 8) == 0)
54 fprintf (fp, " ");
55 if ((dp % 16) == 0) {
56 fprintf (fp, "| ");
57 p = dp;
58 for (dp -= 16; dp < p; dp++)
59 fprintf (fp, "%c", trans[data[dp]]);
60 fflush (fp);
61 fprintf (fp, "\n");
62 }
63 fflush (fp);
64 }
65 if ((amount % 16) != 0) {
66 p = dp = 16 - (amount % 16);
67 for (dp = p; dp > 0; dp--) {
68 fprintf (fp, " ");
69 if (((dp % 8) == 0) && (p != 8))
70 fprintf (fp, " ");
71 fflush (fp);
72 }
73 fprintf (fp, " | ");
74 for (dp = (amount - (16 - p)); dp < amount; dp++)
75 fprintf (fp, "%c", trans[data[dp]]);
76 fflush (fp);
77 }
78 fprintf (fp, "\n");
79
80 fclose (fp);
81 return;
82}
83
84#endif
85
86
87/* m_random
88 *
89 * return a random number between `lowmark´ and `highmark´
90 */
91
92int
93m_random (int lowmark, int highmark)
94{
95 long int rnd;
96
97 /* flip/swap them in case user messed up
98 */
99 if (lowmark > highmark) {
100 lowmark ^= highmark;
101 highmark ^= lowmark;
102 lowmark ^= highmark;
103 }
104 rnd = lowmark;
105
106 srandom ((unsigned int) time (NULL));
107 rnd += (random () % (highmark - lowmark));
108
109 /* this is lame, i know :)
110 */
111 return (rnd);
112}
113
114
115/* set_tv
116 *
117 * initializes a struct timeval pointed to by `tv' to a second value of
118 * `seconds'
119 *
120 * return in any case
121 */
122
123void
124set_tv (struct timeval *tv, int seconds)
125{
126 tv->tv_sec = seconds;
127 tv->tv_usec = 0;
128
129 return;
130}
131
132
133/* xstrupper
134 *
135 * uppercase a string `str'
136 *
137 * return in any case
138 */
139
140void
141xstrupper (char *str)
142{
143 for (; *str != '\0'; ++str) {
144 if (*str >= 'a' && *str <= 'z') {
145 *str -= ('a' - 'A');
146 }
147 }
148
149 return;
150}
151
152
153/* concating snprintf
154 *
155 * determines the length of the string pointed to by `os', appending formatted
156 * string to a maximium length of `len'.
157 *
158 */
159
160void
161scnprintf (char *os, size_t len, const char *str, ...)
162{
163 va_list vl;
164 char *ostmp = os + strlen (os);
165
166 va_start (vl, str);
167 vsnprintf (ostmp, len - strlen (os) - 1, str, vl);
168 va_end (vl);
169
170 return;
171}
172
173
174unsigned long int
175t_passed (struct timeval *old)
176{
177 unsigned long int tp;
178 struct timeval tv;
179
180 if (old == NULL)
181 return (0);
182 if (old->tv_sec == 0 && old->tv_usec == 0)
183 return (0);
184
185 gettimeofday (&tv, NULL);
186
187 tp = ((tv.tv_sec * 1000000) + tv.tv_usec) -
188 ((old->tv_sec * 1000000) + old->tv_usec);
189
190 return (tp);
191}
192
193
194unsigned long int
195tdiff (struct timeval *old, struct timeval *new)
196{
197 unsigned long int time1;
198
199 if (new->tv_sec >= old->tv_sec) {
200 time1 = new->tv_sec - old->tv_sec;
201 if ((new->tv_usec - 500000) >= old->tv_usec)
202 time1++;
203 } else {
204 time1 = old->tv_sec - new->tv_sec;
205 if ((old->tv_usec - 500000) >= new->tv_usec)
206 time1++;
207 }
208
209 return (time1);
210}
211
212
213/* ipv4_print
214 *
215 * padding = 0 -> don't padd
216 * padding = 1 -> padd with zeros
217 * padding = 2 -> padd with spaces
218 */
219
220char *
221ipv4_print (char *dest, struct in_addr in, int padding)
222{
223 unsigned char *ipp;
224
225 ipp = (unsigned char *) &in.s_addr;
226
227 strcpy (dest, "");
228
229 switch (padding) {
230 case (0):
231 sprintf (dest, "%d.%d.%d.%d", ipp[0], ipp[1], ipp[2], ipp[3]);
232 break;
233 case (1):
234 sprintf (dest, "%03d.%03d.%03d.%03d", ipp[0], ipp[1], ipp[2], ipp[3]);
235 break;
236 case (2):
237 sprintf (dest, "%3d.%3d.%3d.%3d", ipp[0], ipp[1], ipp[2], ipp[3]);
238 break;
239 default:
240 break;
241 }
242
243 return (dest);
244}
245
246
247void *
248xrealloc (void *m_ptr, size_t newsize)
249{
250 void *n_ptr;
251
252 n_ptr = realloc (m_ptr, newsize);
253 if (n_ptr == NULL) {
254 fprintf (stderr, "realloc failed\n");
255 exit (EXIT_FAILURE);
256 }
257
258 return (n_ptr);
259}
260
261
262char *
263xstrdup (char *str)
264{
265 char *b;
266
267 b = strdup (str);
268 if (b == NULL) {
269 fprintf (stderr, "strdup failed\n");
270 exit (EXIT_FAILURE);
271 }
272
273 return (b);
274}
275
276
277void *
278xcalloc (int factor, size_t size)
279{
280 void *bla;
281
282 bla = calloc (factor, size);
283
284 if (bla == NULL) {
285 fprintf (stderr, "no memory left\n");
286 exit (EXIT_FAILURE);
287 }
288
289 return (bla);
290}