LMS 2012
Main Page
Related Pages
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
os_internal.c
Go to the documentation of this file.
1
/*
2
* wpa_supplicant/hostapd / Internal implementation of OS specific functions
3
* Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation.
8
*
9
* Alternatively, this software may be distributed under the terms of BSD
10
* license.
11
*
12
* See README and COPYING for more details.
13
*
14
* This file is an example of operating system specific wrapper functions.
15
* This version implements many of the functions internally, so it can be used
16
* to fill in missing functions from the target system C libraries.
17
*
18
* Some of the functions are using standard C library calls in order to keep
19
* this file in working condition to allow the functions to be tested on a
20
* Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for
21
* this file to work correctly. Note that these implementations are only
22
* examples and are not optimized for speed.
23
*/
24
25
#include "
includes.h
"
26
27
#undef OS_REJECT_C_LIB_FUNCTIONS
28
#include "
os.h
"
29
/*
30
void os_sleep(os_time_t sec, os_time_t usec)
31
{
32
if (sec)
33
sleep(sec);
34
if (usec)
35
usleep(usec);
36
}
37
38
39
int os_get_time(struct os_time *t)
40
{
41
int res;
42
struct timeval tv;
43
res = gettimeofday(&tv, NULL);
44
t->sec = tv.tv_sec;
45
t->usec = tv.tv_usec;
46
return res;
47
}
48
*/
49
/*
50
int os_mktime(int year, int month, int day, int hour, int min, int sec,
51
os_time_t *t)
52
{
53
struct tm tm;
54
55
if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
56
hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
57
sec > 60)
58
return -1;
59
60
os_memset(&tm, 0, sizeof(tm));
61
tm.tm_year = year - 1900;
62
tm.tm_mon = month - 1;
63
tm.tm_mday = day;
64
tm.tm_hour = hour;
65
tm.tm_min = min;
66
tm.tm_sec = sec;
67
68
*t = (os_time_t) mktime(&tm);
69
return 0;
70
}
71
72
73
int os_daemonize(const char *pid_file)
74
{
75
if (daemon(0, 0)) {
76
perror("daemon");
77
return -1;
78
}
79
80
if (pid_file) {
81
FILE *f = fopen(pid_file, "w");
82
if (f) {
83
fprintf(f, "%u\n", getpid());
84
fclose(f);
85
}
86
}
87
88
return -0;
89
}
90
91
92
void os_daemonize_terminate(const char *pid_file)
93
{
94
if (pid_file)
95
unlink(pid_file);
96
}
97
*/
98
/*
99
int os_get_random(unsigned char *buf, size_t len)
100
{
101
FILE *f;
102
size_t rc;
103
104
f = fopen("/dev/urandom", "rb");
105
if (f == NULL) {
106
printf("Could not open /dev/urandom.\n");
107
return -1;
108
}
109
110
rc = fread(buf, 1, len, f);
111
fclose(f);
112
113
return rc != len ? -1 : 0;
114
}
115
116
117
unsigned long os_random(void)
118
{
119
return random();
120
}
121
122
*/
123
/*
124
char * os_rel2abs_path(const char *rel_path)
125
{
126
char *buf = NULL, *cwd, *ret;
127
size_t len = 128, cwd_len, rel_len, ret_len;
128
129
if (rel_path[0] == '/')
130
return os_strdup(rel_path);
131
132
for (;;) {
133
buf = os_malloc(len);
134
if (buf == NULL)
135
return NULL;
136
cwd = getcwd(buf, len);
137
if (cwd == NULL) {
138
os_free(buf);
139
if (errno != ERANGE) {
140
return NULL;
141
}
142
len *= 2;
143
} else {
144
break;
145
}
146
}
147
148
cwd_len = strlen(cwd);
149
rel_len = strlen(rel_path);
150
ret_len = cwd_len + 1 + rel_len + 1;
151
ret = os_malloc(ret_len);
152
if (ret) {
153
os_memcpy(ret, cwd, cwd_len);
154
ret[cwd_len] = '/';
155
os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
156
ret[ret_len - 1] = '\0';
157
}
158
os_free(buf);
159
return ret;
160
}
161
162
*/
163
164
/*
165
int os_program_init(void)
166
{
167
return 0;
168
}
169
170
171
void os_program_deinit(void)
172
{
173
}
174
175
176
int os_setenv(const char *name, const char *value, int overwrite)
177
{
178
return setenv(name, value, overwrite);
179
}
180
181
182
int os_unsetenv(const char *name)
183
{
184
#if defined(__FreeBSD__) || defined(__NetBSD__)
185
unsetenv(name);
186
return 0;
187
#else
188
return unsetenv(name);
189
#endif
190
}
191
192
193
char * os_readfile(const char *name, size_t *len)
194
{
195
FILE *f;
196
char *buf;
197
198
f = fopen(name, "rb");
199
if (f == NULL)
200
return NULL;
201
202
fseek(f, 0, SEEK_END);
203
*len = ftell(f);
204
fseek(f, 0, SEEK_SET);
205
206
buf = os_malloc(*len);
207
if (buf == NULL) {
208
fclose(f);
209
return NULL;
210
}
211
212
if (fread(buf, 1, *len, f) != *len) {
213
fclose(f);
214
os_free(buf);
215
return NULL;
216
}
217
218
fclose(f);
219
220
return buf;
221
}
222
*/
223
/*
224
void * os_zalloc(size_t size)
225
{
226
void *n = os_malloc(size);
227
if (n)
228
os_memset(n, 0, size);
229
return n;
230
}
231
232
233
void * os_malloc(size_t size)
234
{
235
return malloc(size);
236
}
237
238
239
void * os_realloc(void *ptr, size_t size)
240
{
241
return realloc(ptr, size);
242
}
243
244
245
void os_free(void *ptr)
246
{
247
free(ptr);
248
}
249
250
251
void * os_memcpy(void *dest, const void *src, size_t n)
252
{
253
char *d = dest;
254
const char *s = src;
255
while (n--)
256
*d++ = *s++;
257
return dest;
258
}
259
*/
260
/*
261
void * os_memmove(void *dest, const void *src, size_t n)
262
{
263
if (dest < src)
264
os_memcpy(dest, src, n);
265
else {*/
266
/* overlapping areas */
267
/*char *d = (char *) dest + n;
268
const char *s = (const char *) src + n;
269
while (n--)
270
*--d = *--s;
271
}
272
return dest;
273
}
274
*/
275
/*
276
void * os_memset(void *s, int c, size_t n)
277
{
278
char *p = s;
279
while (n--)
280
*p++ = c;
281
return s;
282
}
283
284
285
int os_memcmp(const void *s1, const void *s2, size_t n)
286
{
287
const unsigned char *p1 = s1, *p2 = s2;
288
289
if (n == 0)
290
return 0;
291
292
while (*p1 == *p2) {
293
p1++;
294
p2++;
295
n--;
296
if (n == 0)
297
return 0;
298
}
299
300
return *p1 - *p2;
301
}
302
303
304
char * os_strdup(const char *s)
305
{
306
char *res;
307
size_t len;
308
if (s == NULL)
309
return NULL;
310
len = os_strlen(s);
311
res = os_malloc(len + 1);
312
if (res)
313
os_memcpy(res, s, len + 1);
314
return res;
315
}
316
317
318
size_t os_strlen(const char *s)
319
{
320
const char *p = s;
321
while (*p)
322
p++;
323
return p - s;
324
}
325
*/
326
327
//int os_strcasecmp(const char *s1, const char *s2)
328
//{
329
/*
330
* Ignoring case is not required for main functionality, so just use
331
* the case sensitive version of the function.
332
*/
333
// return os_strcmp(s1, s2);
334
//}
335
336
337
//int os_strncasecmp(const char *s1, const char *s2, size_t n)
338
//{
339
/*
340
* Ignoring case is not required for main functionality, so just use
341
* the case sensitive version of the function.
342
*/
343
// return os_strncmp(s1, s2, n);
344
//}
345
346
/*
347
char * os_strchr(const char *s, int c)
348
{
349
while (*s) {
350
if (*s == c)
351
return (char *) s;
352
s++;
353
}
354
return NULL;
355
}
356
*/
357
/*
358
char * os_strrchr(const char *s, int c)
359
{
360
const char *p = s;
361
while (*p)
362
p++;
363
p--;
364
while (p >= s) {
365
if (*p == c)
366
return (char *) p;
367
p--;
368
}
369
return NULL;
370
}
371
372
373
int os_strcmp(const char *s1, const char *s2)
374
{
375
while (*s1 == *s2) {
376
if (*s1 == '\0')
377
break;
378
s1++;
379
s2++;
380
}
381
382
return *s1 - *s2;
383
}
384
385
386
int os_strncmp(const char *s1, const char *s2, size_t n)
387
{
388
if (n == 0)
389
return 0;
390
391
while (*s1 == *s2) {
392
if (*s1 == '\0')
393
break;
394
s1++;
395
s2++;
396
n--;
397
if (n == 0)
398
return 0;
399
}
400
401
return *s1 - *s2;
402
}
403
404
405
char * os_strncpy(char *dest, const char *src, size_t n)
406
{
407
char *d = dest;
408
409
while (n--) {
410
*d = *src;
411
if (*src == '\0')
412
break;
413
d++;
414
src++;
415
}
416
417
return dest;
418
}
419
420
*/
421
/*
422
size_t os_strlcpy(char *dest, const char *src, size_t siz)
423
{
424
const char *s = src;
425
size_t left = siz;
426
427
if (left) {*/
428
/* Copy string up to the maximum size of the dest buffer */
429
/* while (--left != 0) {
430
if ((*dest++ = *s++) == '\0')
431
break;
432
}
433
}
434
435
if (left == 0) {*/
436
/* Not enough room for the string; force NUL-termination */
437
/* if (siz != 0)
438
*dest = '\0';
439
while (*s++)
440
; */
/* determine total src string length */
441
/*}
442
443
return s - src - 1;
444
}
445
*/
446
/*
447
char * os_strstr(const char *haystack, const char *needle)
448
{
449
size_t len = os_strlen(needle);
450
while (*haystack) {
451
if (os_strncmp(haystack, needle, len) == 0)
452
return (char *) haystack;
453
haystack++;
454
}
455
456
return NULL;
457
}
458
459
*/
460
//int os_snprintf(char *str, size_t size, const char *format, ...)
461
//{
462
// va_list ap;
463
// int ret;
464
465
/* See http://www.ijs.si/software/snprintf/ for portable
466
* implementation of snprintf.
467
*/
468
/*
469
va_start(ap, format);
470
ret = vsnprintf(str, size, format, ap);
471
va_end(ap);
472
if (size > 0)
473
str[size - 1] = '\0';
474
return ret;
475
}
476
*/
includes.h
os.h
ev3sources-master-2
lms2012
c_com
source
os_internal.c
Generated on Tue Jul 29 2014 19:20:26 for LMS 2012 by
1.8.7