os_internal.c

Go to the documentation of this file.
00001 
00026 #include "includes.h"
00027 
00028 #undef OS_REJECT_C_LIB_FUNCTIONS
00029 #include "os.h"
00030 
00031 void os_sleep(os_time_t sec, os_time_t usec)
00032 {
00033         if (sec)
00034                 sleep(sec);
00035         if (usec)
00036                 usleep(usec);
00037 }
00038 
00039 
00040 int os_get_time(struct os_time *t)
00041 {
00042         int res;
00043         struct timeval tv;
00044         res = gettimeofday(&tv, NULL);
00045         t->sec = tv.tv_sec;
00046         t->usec = tv.tv_usec;
00047         return res;
00048 }
00049 
00050 
00051 int os_mktime(int year, int month, int day, int hour, int min, int sec,
00052               os_time_t *t)
00053 {
00054         struct tm tm;
00055 
00056         if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
00057             hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
00058             sec > 60)
00059                 return -1;
00060 
00061         os_memset(&tm, 0, sizeof(tm));
00062         tm.tm_year = year - 1900;
00063         tm.tm_mon = month - 1;
00064         tm.tm_mday = day;
00065         tm.tm_hour = hour;
00066         tm.tm_min = min;
00067         tm.tm_sec = sec;
00068 
00069         *t = (os_time_t) mktime(&tm);
00070         return 0;
00071 }
00072 
00073 
00074 int os_daemonize(const char *pid_file)
00075 {
00076         if (daemon(0, 0)) {
00077                 perror("daemon");
00078                 return -1;
00079         }
00080 
00081         if (pid_file) {
00082                 FILE *f = fopen(pid_file, "w");
00083                 if (f) {
00084                         fprintf(f, "%u\n", getpid());
00085                         fclose(f);
00086                 }
00087         }
00088 
00089         return -0;
00090 }
00091 
00092 
00093 void os_daemonize_terminate(const char *pid_file)
00094 {
00095         if (pid_file)
00096                 unlink(pid_file);
00097 }
00098 
00099 
00100 int os_get_random(unsigned char *buf, size_t len)
00101 {
00102         FILE *f;
00103         size_t rc;
00104 
00105         f = fopen("/dev/urandom", "rb");
00106         if (f == NULL) {
00107                 printf("Could not open /dev/urandom.\n");
00108                 return -1;
00109         }
00110 
00111         rc = fread(buf, 1, len, f);
00112         fclose(f);
00113 
00114         return rc != len ? -1 : 0;
00115 }
00116 
00117 
00118 unsigned long os_random(void)
00119 {
00120         return random();
00121 }
00122 
00123 
00124 char * os_rel2abs_path(const char *rel_path)
00125 {
00126         char *buf = NULL, *cwd, *ret;
00127         size_t len = 128, cwd_len, rel_len, ret_len;
00128 
00129         if (rel_path[0] == '/')
00130                 return os_strdup(rel_path);
00131 
00132         for (;;) {
00133                 buf = os_malloc(len);
00134                 if (buf == NULL)
00135                         return NULL;
00136                 cwd = getcwd(buf, len);
00137                 if (cwd == NULL) {
00138                         os_free(buf);
00139                         if (errno != ERANGE) {
00140                                 return NULL;
00141                         }
00142                         len *= 2;
00143                 } else {
00144                         break;
00145                 }
00146         }
00147 
00148         cwd_len = strlen(cwd);
00149         rel_len = strlen(rel_path);
00150         ret_len = cwd_len + 1 + rel_len + 1;
00151         ret = os_malloc(ret_len);
00152         if (ret) {
00153                 os_memcpy(ret, cwd, cwd_len);
00154                 ret[cwd_len] = '/';
00155                 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
00156                 ret[ret_len - 1] = '\0';
00157         }
00158         os_free(buf);
00159         return ret;
00160 }
00161 
00162 
00163 int os_program_init(void)
00164 {
00165         return 0;
00166 }
00167 
00168 
00169 void os_program_deinit(void)
00170 {
00171 }
00172 
00173 
00174 int os_setenv(const char *name, const char *value, int overwrite)
00175 {
00176         return setenv(name, value, overwrite);
00177 }
00178 
00179 
00180 int os_unsetenv(const char *name)
00181 {
00182 #if defined(__FreeBSD__) || defined(__NetBSD__)
00183         unsetenv(name);
00184         return 0;
00185 #else
00186         return unsetenv(name);
00187 #endif
00188 }
00189 
00190 
00191 char * os_readfile(const char *name, size_t *len)
00192 {
00193         FILE *f;
00194         char *buf;
00195 
00196         f = fopen(name, "rb");
00197         if (f == NULL)
00198                 return NULL;
00199 
00200         fseek(f, 0, SEEK_END);
00201         *len = ftell(f);
00202         fseek(f, 0, SEEK_SET);
00203 
00204         buf = os_malloc(*len);
00205         if (buf == NULL) {
00206                 fclose(f);
00207                 return NULL;
00208         }
00209 
00210         fread(buf, 1, *len, f);
00211         fclose(f);
00212 
00213         return buf;
00214 }
00215 
00216 
00217 void * os_zalloc(size_t size)
00218 {
00219         void *n = os_malloc(size);
00220         if (n)
00221                 os_memset(n, 0, size);
00222         return n;
00223 }
00224 
00225 
00226 void * os_malloc(size_t size)
00227 {
00228         return malloc(size);
00229 }
00230 
00231 
00232 void * os_realloc(void *ptr, size_t size)
00233 {
00234         return realloc(ptr, size);
00235 }
00236 
00237 
00238 void os_free(void *ptr)
00239 {
00240         free(ptr);
00241 }
00242 
00243 
00244 void * os_memcpy(void *dest, const void *src, size_t n)
00245 {
00246         char *d = dest;
00247         const char *s = src;
00248         while (n--)
00249                 *d++ = *s++;
00250         return dest;
00251 }
00252 
00253 
00254 void * os_memmove(void *dest, const void *src, size_t n)
00255 {
00256         if (dest < src)
00257                 os_memcpy(dest, src, n);
00258         else {
00259                 /* overlapping areas */
00260                 char *d = (char *) dest + n;
00261                 const char *s = (const char *) src + n;
00262                 while (n--)
00263                         *--d = *--s;
00264         }
00265         return dest;
00266 }
00267 
00268 
00269 void * os_memset(void *s, int c, size_t n)
00270 {
00271         char *p = s;
00272         while (n--)
00273                 *p++ = c;
00274         return s;
00275 }
00276 
00277 
00278 int os_memcmp(const void *s1, const void *s2, size_t n)
00279 {
00280         const unsigned char *p1 = s1, *p2 = s2;
00281 
00282         if (n == 0)
00283                 return 0;
00284 
00285         while (*p1 == *p2) {
00286                 p1++;
00287                 p2++;
00288                 n--;
00289                 if (n == 0)
00290                         return 0;
00291         }
00292 
00293         return *p1 - *p2;
00294 }
00295 
00296 
00297 char * os_strdup(const char *s)
00298 {
00299         char *res;
00300         size_t len;
00301         if (s == NULL)
00302                 return NULL;
00303         len = os_strlen(s);
00304         res = os_malloc(len + 1);
00305         if (res)
00306                 os_memcpy(res, s, len + 1);
00307         return res;
00308 }
00309 
00310 
00311 size_t os_strlen(const char *s)
00312 {
00313         const char *p = s;
00314         while (*p)
00315                 p++;
00316         return p - s;
00317 }
00318 
00319 
00320 int os_strcasecmp(const char *s1, const char *s2)
00321 {
00322         /*
00323          * Ignoring case is not required for main functionality, so just use
00324          * the case sensitive version of the function.
00325          */
00326         return os_strcmp(s1, s2);
00327 }
00328 
00329 
00330 int os_strncasecmp(const char *s1, const char *s2, size_t n)
00331 {
00332         /*
00333          * Ignoring case is not required for main functionality, so just use
00334          * the case sensitive version of the function.
00335          */
00336         return os_strncmp(s1, s2, n);
00337 }
00338 
00339 
00340 char * os_strchr(const char *s, int c)
00341 {
00342         while (*s) {
00343                 if (*s == c)
00344                         return (char *) s;
00345                 s++;
00346         }
00347         return NULL;
00348 }
00349 
00350 
00351 char * os_strrchr(const char *s, int c)
00352 {
00353         const char *p = s;
00354         while (*p)
00355                 p++;
00356         p--;
00357         while (p >= s) {
00358                 if (*p == c)
00359                         return (char *) p;
00360                 p--;
00361         }
00362         return NULL;
00363 }
00364 
00365 
00366 int os_strcmp(const char *s1, const char *s2)
00367 {
00368         while (*s1 == *s2) {
00369                 if (*s1 == '\0')
00370                         break;
00371                 s1++;
00372                 s2++;
00373         }
00374 
00375         return *s1 - *s2;
00376 }
00377 
00378 
00379 int os_strncmp(const char *s1, const char *s2, size_t n)
00380 {
00381         if (n == 0)
00382                 return 0;
00383 
00384         while (*s1 == *s2) {
00385                 if (*s1 == '\0')
00386                         break;
00387                 s1++;
00388                 s2++;
00389                 n--;
00390                 if (n == 0)
00391                         return 0;
00392         }
00393 
00394         return *s1 - *s2;
00395 }
00396 
00397 
00398 char * os_strncpy(char *dest, const char *src, size_t n)
00399 {
00400         char *d = dest;
00401 
00402         while (n--) {
00403                 *d = *src;
00404                 if (*src == '\0')
00405                         break;
00406                 d++;
00407                 src++;
00408         }
00409 
00410         return dest;
00411 }
00412 
00413 
00414 char * os_strstr(const char *haystack, const char *needle)
00415 {
00416         size_t len = os_strlen(needle);
00417         while (*haystack) {
00418                 if (os_strncmp(haystack, needle, len) == 0)
00419                         return (char *) haystack;
00420                 haystack++;
00421         }
00422 
00423         return NULL;
00424 }
00425 
00426 
00427 int os_snprintf(char *str, size_t size, const char *format, ...)
00428 {
00429         va_list ap;
00430         int ret;
00431 
00432         /* See http://www.ijs.si/software/snprintf/ for portable
00433          * implementation of snprintf.
00434          */
00435 
00436         va_start(ap, format);
00437         ret = vsnprintf(str, size, format, ap);
00438         va_end(ap);
00439         if (size > 0)
00440                 str[size - 1] = '\0';
00441         return ret;
00442 }
00443 

Generated on Sun Dec 31 13:48:55 2006 for wpa_supplicant by  doxygen 1.4.2