List.mui: Update entries count prior to range change
[AROS.git] / workbench / network / WirelessManager / src / utils / os_amiga.c
blob4a3bc7e751eb6d0d1ef1cdaf9aac3c3d3a69a83f
1 /*
2 * wpa_supplicant/hostapd / OS specific functions for Amiga-like systems
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2010-2011, Neil Cafferkey
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
11 * license.
13 * See README and COPYING for more details.
16 #include "includes.h"
18 #include "os.h"
20 #include <dos/dos.h>
22 #include <proto/dos.h>
25 void os_sleep(os_time_t sec, os_time_t usec)
27 ULONG ticks = sec * 50 + (usec + 19999) / 20000;
29 Delay(ticks);
33 int os_get_time(struct os_time *t)
35 struct DateStamp ds;
36 DateStamp(&ds);
37 time((time_t *)&t->sec);
38 t->usec = ds.ds_Tick % TICKS_PER_SECOND * 20000;
39 return 0;
43 int os_mktime(int year, int month, int day, int hour, int min, int sec,
44 os_time_t *t)
46 struct tm tm, *tm1;
47 time_t t_local, t1, t2;
48 os_time_t tz_offset;
50 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
51 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
52 sec > 60)
53 return -1;
55 memset(&tm, 0, sizeof(tm));
56 tm.tm_year = year - 1900;
57 tm.tm_mon = month - 1;
58 tm.tm_mday = day;
59 tm.tm_hour = hour;
60 tm.tm_min = min;
61 tm.tm_sec = sec;
63 t_local = mktime(&tm);
65 /* figure out offset to UTC */
66 tm1 = localtime(&t_local);
67 if (tm1) {
68 t1 = mktime(tm1);
69 tm1 = gmtime(&t_local);
70 if (tm1) {
71 t2 = mktime(tm1);
72 tz_offset = t2 - t1;
73 } else
74 tz_offset = 0;
75 } else
76 tz_offset = 0;
78 *t = (os_time_t) t_local - tz_offset;
79 return 0;
83 int os_daemonize(const char *pid_file)
85 return -1;
89 void os_daemonize_terminate(const char *pid_file)
91 if (pid_file)
92 unlink(pid_file);
96 int os_get_random(unsigned char *buf, size_t len)
98 while (len-- > 0)
99 *buf++ = rand();
101 return 0;
105 unsigned long os_random(void)
107 unsigned long val;
109 os_get_random((unsigned char *) &val, sizeof(unsigned long));
111 return val;
115 char * os_rel2abs_path(const char *rel_path)
117 char *buf = NULL, *cwd, *ret;
118 size_t len = 128, cwd_len, rel_len, ret_len;
119 int last_errno;
121 if (strchr(rel_path, ':') != NULL)
122 return strdup(rel_path);
124 for (;;) {
125 buf = malloc(len);
126 if (buf == NULL)
127 return NULL;
128 cwd = getcwd(buf, len);
129 if (cwd == NULL) {
130 last_errno = errno;
131 free(buf);
132 if (last_errno != ERANGE)
133 return NULL;
134 len *= 2;
135 if (len > 2000)
136 return NULL;
137 } else {
138 buf[len - 1] = '\0';
139 break;
143 cwd_len = strlen(cwd);
144 rel_len = strlen(rel_path);
145 ret_len = cwd_len + 1 + rel_len + 1;
146 ret = malloc(ret_len);
147 if (ret) {
148 memcpy(ret, cwd, cwd_len);
149 if (ret[cwd_len - 1] != ':')
150 ret[cwd_len] = '/';
151 memcpy(ret + cwd_len + 1, rel_path, rel_len);
152 ret[ret_len - 1] = '\0';
154 free(buf);
155 return ret;
159 int os_program_init(void)
161 return 0;
165 void os_program_deinit(void)
170 char * os_readfile(const char *name, size_t *len)
172 FILE *f;
173 char *buf;
175 f = fopen(name, "rb");
176 if (f == NULL)
177 return NULL;
179 fseek(f, 0, SEEK_END);
180 *len = ftell(f);
181 fseek(f, 0, SEEK_SET);
183 buf = malloc(*len);
184 if (buf == NULL) {
185 fclose(f);
186 return NULL;
189 if (fread(buf, 1, *len, f) != *len) {
190 fclose(f);
191 free(buf);
192 return NULL;
195 fclose(f);
197 return buf;
201 void * os_zalloc(size_t size)
203 void *n = os_malloc(size);
204 if (n)
205 os_memset(n, 0, size);
206 return n;
210 size_t os_strlcpy(char *dest, const char *src, size_t siz)
212 const char *s = src;
213 size_t left = siz;
215 if (left) {
216 /* Copy string up to the maximum size of the dest buffer */
217 while (--left != 0) {
218 if ((*dest++ = *s++) == '\0')
219 break;
223 if (left == 0) {
224 /* Not enough room for the string; force NUL-termination */
225 if (siz != 0)
226 *dest = '\0';
227 while (*s++)
228 ; /* determine total src string length */
231 return s - src - 1;