2 * wpa_supplicant/hostapd / OS specific functions for UNIX/POSIX systems
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 void os_sleep(os_time_t sec
, os_time_t usec
)
28 int os_get_time(struct os_time
*t
)
32 res
= gettimeofday(&tv
, NULL
);
39 int os_mktime(int year
, int month
, int day
, int hour
, int min
, int sec
,
44 if (year
< 1970 || month
< 1 || month
> 12 || day
< 1 || day
> 31 ||
45 hour
< 0 || hour
> 23 || min
< 0 || min
> 59 || sec
< 0 ||
49 memset(&tm
, 0, sizeof(tm
));
50 tm
.tm_year
= year
- 1900;
51 tm
.tm_mon
= month
- 1;
57 *t
= (os_time_t
) mktime(&tm
);
62 int os_daemonize(const char *pid_file
)
70 FILE *f
= fopen(pid_file
, "w");
72 fprintf(f
, "%u\n", getpid());
81 void os_daemonize_terminate(const char *pid_file
)
88 int os_get_random(unsigned char *buf
, size_t len
)
93 f
= fopen("/dev/urandom", "rb");
95 printf("Could not open /dev/urandom.\n");
99 rc
= fread(buf
, 1, len
, f
);
102 return rc
!= len
? -1 : 0;
106 unsigned long os_random(void)
112 char * os_rel2abs_path(const char *rel_path
)
114 char *buf
= NULL
, *cwd
, *ret
;
115 size_t len
= 128, cwd_len
, rel_len
, ret_len
;
118 if (rel_path
[0] == '/')
119 return strdup(rel_path
);
125 cwd
= getcwd(buf
, len
);
129 if (last_errno
!= ERANGE
)
140 cwd_len
= strlen(cwd
);
141 rel_len
= strlen(rel_path
);
142 ret_len
= cwd_len
+ 1 + rel_len
+ 1;
143 ret
= malloc(ret_len
);
145 memcpy(ret
, cwd
, cwd_len
);
147 memcpy(ret
+ cwd_len
+ 1, rel_path
, rel_len
);
148 ret
[ret_len
- 1] = '\0';
155 int os_program_init(void)
161 void os_program_deinit(void)
166 int os_setenv(const char *name
, const char *value
, int overwrite
)
168 return setenv(name
, value
, overwrite
);
172 int os_unsetenv(const char *name
)
174 #if defined(__FreeBSD__) || defined(__NetBSD__)
178 return unsetenv(name
);
183 char * os_readfile(const char *name
, size_t *len
)
188 f
= fopen(name
, "rb");
192 fseek(f
, 0, SEEK_END
);
194 fseek(f
, 0, SEEK_SET
);
202 fread(buf
, 1, *len
, f
);
209 void * os_zalloc(size_t size
)
211 return calloc(1, size
);