MFC:
[dragonfly.git] / contrib / wpa_supplicant-0.5.8 / os_unix.c
blobfb8149a7f3b96b69865cbb0f7b36e10cde35f030
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "os.h"
19 void os_sleep(os_time_t sec, os_time_t usec)
21 if (sec)
22 sleep(sec);
23 if (usec)
24 usleep(usec);
28 int os_get_time(struct os_time *t)
30 int res;
31 struct timeval tv;
32 res = gettimeofday(&tv, NULL);
33 t->sec = tv.tv_sec;
34 t->usec = tv.tv_usec;
35 return res;
39 int os_mktime(int year, int month, int day, int hour, int min, int sec,
40 os_time_t *t)
42 struct tm tm;
44 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
45 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
46 sec > 60)
47 return -1;
49 memset(&tm, 0, sizeof(tm));
50 tm.tm_year = year - 1900;
51 tm.tm_mon = month - 1;
52 tm.tm_mday = day;
53 tm.tm_hour = hour;
54 tm.tm_min = min;
55 tm.tm_sec = sec;
57 *t = (os_time_t) mktime(&tm);
58 return 0;
62 int os_daemonize(const char *pid_file)
64 if (daemon(0, 0)) {
65 perror("daemon");
66 return -1;
69 if (pid_file) {
70 FILE *f = fopen(pid_file, "w");
71 if (f) {
72 fprintf(f, "%u\n", getpid());
73 fclose(f);
77 return -0;
81 void os_daemonize_terminate(const char *pid_file)
83 if (pid_file)
84 unlink(pid_file);
88 int os_get_random(unsigned char *buf, size_t len)
90 FILE *f;
91 size_t rc;
93 f = fopen("/dev/urandom", "rb");
94 if (f == NULL) {
95 printf("Could not open /dev/urandom.\n");
96 return -1;
99 rc = fread(buf, 1, len, f);
100 fclose(f);
102 return rc != len ? -1 : 0;
106 unsigned long os_random(void)
108 return random();
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;
116 int last_errno;
118 if (rel_path[0] == '/')
119 return strdup(rel_path);
121 for (;;) {
122 buf = malloc(len);
123 if (buf == NULL)
124 return NULL;
125 cwd = getcwd(buf, len);
126 if (cwd == NULL) {
127 last_errno = errno;
128 free(buf);
129 if (last_errno != ERANGE)
130 return NULL;
131 len *= 2;
132 if (len > 2000)
133 return NULL;
134 } else {
135 buf[len - 1] = '\0';
136 break;
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);
144 if (ret) {
145 memcpy(ret, cwd, cwd_len);
146 ret[cwd_len] = '/';
147 memcpy(ret + cwd_len + 1, rel_path, rel_len);
148 ret[ret_len - 1] = '\0';
150 free(buf);
151 return ret;
155 int os_program_init(void)
157 return 0;
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__)
175 unsetenv(name);
176 return 0;
177 #else
178 return unsetenv(name);
179 #endif
183 char * os_readfile(const char *name, size_t *len)
185 FILE *f;
186 char *buf;
188 f = fopen(name, "rb");
189 if (f == NULL)
190 return NULL;
192 fseek(f, 0, SEEK_END);
193 *len = ftell(f);
194 fseek(f, 0, SEEK_SET);
196 buf = malloc(*len);
197 if (buf == NULL) {
198 fclose(f);
199 return NULL;
202 fread(buf, 1, *len, f);
203 fclose(f);
205 return buf;
209 void * os_zalloc(size_t size)
211 return calloc(1, size);