/[dynamips]/upstream/dynamips-0.2.6-RC2/utils.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/dynamips-0.2.6-RC2/utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations)
Sat Oct 6 16:05:34 2007 UTC (13 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 6451 byte(s)
dynamips-0.2.6-RC2

1 /*
2 * Cisco C7200 (Predator) simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot. All rights reserved.
4 *
5 * Utility functions.
6 */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdarg.h>
13 #include <unistd.h>
14 #include <time.h>
15 #include <signal.h>
16 #include <sys/time.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <assert.h>
25 #ifdef __CYGWIN__
26 #include <malloc.h>
27 #endif
28
29 #include "utils.h"
30
31 extern FILE *log_file;
32
33 /* Add an element to a list */
34 m_list_t *m_list_add(m_list_t **head,void *data)
35 {
36 m_list_t *item;
37
38 if ((item = malloc(sizeof(*item))) != NULL) {
39 item->data = data;
40 item->next = *head;
41 *head = item;
42 }
43
44 return item;
45 }
46
47 /* Dynamic sprintf */
48 char *dyn_sprintf(const char *fmt,...)
49 {
50 int n,size = 512;
51 va_list ap;
52 char *p,*p2;
53
54 if ((p = malloc(size)) == NULL) {
55 perror("dyn_sprintf: malloc");
56 return NULL;
57 }
58
59 for(;;)
60 {
61 /* Try to print in the allocated space */
62 va_start(ap,fmt);
63 n = vsnprintf(p,size,fmt,ap);
64 va_end(ap);
65
66 /* If that worked, return the string */
67 if ((n > -1) && (n < size))
68 return p;
69
70 /* Else try again with more space. */
71 if (n > -1)
72 size = n + 1;
73 else
74 size *= 2;
75
76 if ((p2 = realloc(p,size)) == NULL) {
77 perror("dyn_sprintf: realloc");
78 free(p);
79 return NULL;
80 }
81
82 p = p2;
83 }
84 }
85
86 /* Split a string */
87 int m_strsplit(char *str,char delim,char **array,int max_count)
88 {
89 int i,pos = 0;
90 size_t len;
91 char *ptr;
92
93 for(i=0;i<max_count;i++)
94 array[i] = NULL;
95
96 do {
97 if (pos == max_count)
98 goto error;
99
100 ptr = strchr(str,delim);
101 if (!ptr)
102 ptr = str + strlen(str);
103
104 len = ptr - str;
105
106 if (!(array[pos] = malloc(len+1)))
107 goto error;
108
109 memcpy(array[pos],str,len);
110 array[pos][len] = 0;
111
112 str = ptr + 1;
113 pos++;
114 }while(*ptr);
115
116 return(pos);
117
118 error:
119 for(i=0;i<max_count;i++)
120 free(array[i]);
121 return(-1);
122 }
123
124 /* Tokenize a string */
125 int m_strtok(char *str,char delim,char **array,int max_count)
126 {
127 int i,pos = 0;
128 size_t len;
129 char *ptr;
130
131 for(i=0;i<max_count;i++)
132 array[i] = NULL;
133
134 do {
135 if (pos == max_count)
136 goto error;
137
138 ptr = strchr(str,delim);
139 if (!ptr)
140 ptr = str + strlen(str);
141
142 len = ptr - str;
143
144 if (!(array[pos] = malloc(len+1)))
145 goto error;
146
147 memcpy(array[pos],str,len);
148 array[pos][len] = 0;
149
150 while(*ptr == delim)
151 ptr++;
152
153 str = ptr;
154 pos++;
155 }while(*ptr);
156
157 return(pos);
158
159 error:
160 for(i=0;i<max_count;i++)
161 free(array[i]);
162 return(-1);
163 }
164
165 /* Quote a string */
166 char *m_strquote(char *buffer,size_t buf_len,char *str)
167 {
168 char *p;
169
170 if (!(p = strpbrk(str," \t\"'")))
171 return str;
172
173 snprintf(buffer,buf_len,"\"%s\"",str);
174 return buffer;
175 }
176
177 /* Ugly function that dumps a structure in hexa and ascii. */
178 void mem_dump(FILE *f_output,u_char *pkt,u_int len)
179 {
180 u_int x,i = 0, tmp;
181
182 while (i < len)
183 {
184 if ((len - i) > 16)
185 x = 16;
186 else x = len - i;
187
188 fprintf(f_output,"%4.4x: ",i);
189
190 for (tmp=0;tmp<x;tmp++)
191 fprintf(f_output,"%2.2x ",pkt[i+tmp]);
192 for (tmp=x;tmp<16;tmp++) fprintf(f_output," ");
193
194 for (tmp=0;tmp<x;tmp++) {
195 char c = pkt[i+tmp];
196
197 if (((c >= 'A') && (c <= 'Z')) ||
198 ((c >= 'a') && (c <= 'z')) ||
199 ((c >= '0') && (c <= '9')))
200 fprintf(f_output,"%c",c);
201 else
202 fputs(".",f_output);
203 }
204
205 i += x;
206 fprintf(f_output,"\n");
207 }
208
209 fprintf(f_output,"\n");
210 }
211
212 /* Logging function */
213 void m_flog(FILE *fd,char *module,char *fmt,va_list ap)
214 {
215 struct timeval now;
216 static char buf[256];
217 time_t ct;
218
219 gettimeofday(&now,0);
220 ct = now.tv_sec;
221 strftime(buf,sizeof(buf),"%b %d %H:%M:%S",localtime(&ct));
222 if (fd) {
223 fprintf(fd,"%s.%03ld %s: ",buf,(long)now.tv_usec/1000,module);
224 vfprintf(fd,fmt,ap);
225 fflush(fd);
226 }
227 }
228
229 /* Logging function */
230 void m_log(char *module,char *fmt,...)
231 {
232 va_list ap;
233
234 va_start(ap,fmt);
235 m_flog(log_file,module,fmt,ap);
236 va_end(ap);
237 }
238
239 /* Returns a line from specified file (remove trailing '\n') */
240 char *m_fgets(char *buffer,int size,FILE *fd)
241 {
242 int len;
243
244 buffer[0] = '\0';
245 fgets(buffer,size,fd);
246
247 if ((len = strlen(buffer)) == 0)
248 return NULL;
249
250 /* remove trailing '\n' */
251 if (buffer[len-1] == '\n')
252 buffer[len-1] = '\0';
253
254 return buffer;
255 }
256
257 /* Read a file and returns it in a buffer */
258 ssize_t m_read_file(char *filename,char **buffer)
259 {
260 char tmp[256],*ptr,*nptr;
261 size_t len,tot_len;
262 FILE *fd;
263
264 *buffer = ptr = NULL;
265 tot_len = 0;
266
267 /* Open file for reading */
268 if ((fd = fopen(filename,"r")) == NULL)
269 return(-1);
270
271 while((len = fread(tmp,1,sizeof(tmp),fd)) > 0)
272 {
273 /* Reallocate memory */
274 nptr = realloc(ptr,tot_len+len+1);
275 if (nptr == NULL) {
276 if (ptr) free(ptr);
277 fclose(fd);
278 return(-1);
279 }
280
281 ptr = nptr;
282
283 /* Ok, memory could be allocated */
284 memcpy(&ptr[tot_len],tmp,len);
285 tot_len += len;
286 }
287
288 fclose(fd);
289 *buffer = ptr;
290 return(tot_len);
291 }
292
293 /* Allocate aligned memory */
294 void *m_memalign(size_t boundary,size_t size)
295 {
296 void *p;
297
298 #ifdef __linux__
299 if (posix_memalign((void *)&p,boundary,size))
300 #else
301 #if defined(__CYGWIN__) || defined(SUNOS)
302 if (!(p = memalign(boundary,size)))
303 #else
304 if (!(p = malloc(size)))
305 #endif
306 #endif
307 return NULL;
308
309 assert(((m_iptr_t)p & (boundary-1)) == 0);
310 return p;
311 }
312
313 /* Block specified signal for calling thread */
314 int m_signal_block(int sig)
315 {
316 sigset_t sig_mask;
317 sigemptyset(&sig_mask);
318 sigaddset(&sig_mask,sig);
319 return(pthread_sigmask(SIG_BLOCK,&sig_mask,NULL));
320 }
321
322 /* Unblock specified signal for calling thread */
323 int m_signal_unblock(int sig)
324 {
325 sigset_t sig_mask;
326 sigemptyset(&sig_mask);
327 sigaddset(&sig_mask,sig);
328 return(pthread_sigmask(SIG_UNBLOCK,&sig_mask,NULL));
329 }
330
331 /* Set non-blocking mode on a file descriptor */
332 int m_fd_set_non_block(int fd)
333 {
334 int flags;
335
336 if ((flags = fcntl(fd,F_GETFL,0)) < 1)
337 return(-1);
338
339 return(fcntl(fd,F_SETFL, flags | O_NONBLOCK));
340 }

  ViewVC Help
Powered by ViewVC 1.1.26