/[dynamips]/upstream/dynamips-0.2.6-RC1/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-RC1/utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Sat Oct 6 16:03:58 2007 UTC (16 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 5765 byte(s)
import dynamips-0.2.6-RC1

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

  ViewVC Help
Powered by ViewVC 1.1.26