os_unix.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "os.h"
00019 
00020 void os_sleep(os_time_t sec, os_time_t usec)
00021 {
00022         if (sec)
00023                 sleep(sec);
00024         if (usec)
00025                 usleep(usec);
00026 }
00027 
00028 
00029 int os_get_time(struct os_time *t)
00030 {
00031         int res;
00032         struct timeval tv;
00033         res = gettimeofday(&tv, NULL);
00034         t->sec = tv.tv_sec;
00035         t->usec = tv.tv_usec;
00036         return res;
00037 }
00038 
00039 
00040 int os_mktime(int year, int month, int day, int hour, int min, int sec,
00041               os_time_t *t)
00042 {
00043         struct tm tm;
00044 
00045         if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
00046             hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
00047             sec > 60)
00048                 return -1;
00049 
00050         memset(&tm, 0, sizeof(tm));
00051         tm.tm_year = year - 1900;
00052         tm.tm_mon = month - 1;
00053         tm.tm_mday = day;
00054         tm.tm_hour = hour;
00055         tm.tm_min = min;
00056         tm.tm_sec = sec;
00057 
00058         *t = (os_time_t) mktime(&tm);
00059         return 0;
00060 }
00061 
00062 
00063 int os_daemonize(const char *pid_file)
00064 {
00065         if (daemon(0, 0)) {
00066                 perror("daemon");
00067                 return -1;
00068         }
00069 
00070         if (pid_file) {
00071                 FILE *f = fopen(pid_file, "w");
00072                 if (f) {
00073                         fprintf(f, "%u\n", getpid());
00074                         fclose(f);
00075                 }
00076         }
00077 
00078         return -0;
00079 }
00080 
00081 
00082 void os_daemonize_terminate(const char *pid_file)
00083 {
00084         if (pid_file)
00085                 unlink(pid_file);
00086 }
00087 
00088 
00089 int os_get_random(unsigned char *buf, size_t len)
00090 {
00091         FILE *f;
00092         size_t rc;
00093 
00094         f = fopen("/dev/urandom", "rb");
00095         if (f == NULL) {
00096                 printf("Could not open /dev/urandom.\n");
00097                 return -1;
00098         }
00099 
00100         rc = fread(buf, 1, len, f);
00101         fclose(f);
00102 
00103         return rc != len ? -1 : 0;
00104 }
00105 
00106 
00107 unsigned long os_random(void)
00108 {
00109         return random();
00110 }
00111 
00112 
00113 char * os_rel2abs_path(const char *rel_path)
00114 {
00115         char *buf = NULL, *cwd, *ret;
00116         size_t len = 128, cwd_len, rel_len, ret_len;
00117         int last_errno;
00118 
00119         if (rel_path[0] == '/')
00120                 return strdup(rel_path);
00121 
00122         for (;;) {
00123                 buf = malloc(len);
00124                 if (buf == NULL)
00125                         return NULL;
00126                 cwd = getcwd(buf, len);
00127                 if (cwd == NULL) {
00128                         last_errno = errno;
00129                         free(buf);
00130                         if (last_errno != ERANGE)
00131                                 return NULL;
00132                         len *= 2;
00133                         if (len > 2000)
00134                                 return NULL;
00135                 } else {
00136                         buf[len - 1] = '\0';
00137                         break;
00138                 }
00139         }
00140 
00141         cwd_len = strlen(cwd);
00142         rel_len = strlen(rel_path);
00143         ret_len = cwd_len + 1 + rel_len + 1;
00144         ret = malloc(ret_len);
00145         if (ret) {
00146                 memcpy(ret, cwd, cwd_len);
00147                 ret[cwd_len] = '/';
00148                 memcpy(ret + cwd_len + 1, rel_path, rel_len);
00149                 ret[ret_len - 1] = '\0';
00150         }
00151         free(buf);
00152         return ret;
00153 }
00154 
00155 
00156 int os_program_init(void)
00157 {
00158         return 0;
00159 }
00160 
00161 
00162 void os_program_deinit(void)
00163 {
00164 }
00165 
00166 
00167 int os_setenv(const char *name, const char *value, int overwrite)
00168 {
00169         return setenv(name, value, overwrite);
00170 }
00171 
00172 
00173 int os_unsetenv(const char *name)
00174 {
00175 #if defined(__FreeBSD__) || defined(__NetBSD__)
00176         unsetenv(name);
00177         return 0;
00178 #else
00179         return unsetenv(name);
00180 #endif
00181 }
00182 
00183 
00184 char * os_readfile(const char *name, size_t *len)
00185 {
00186         FILE *f;
00187         char *buf;
00188 
00189         f = fopen(name, "rb");
00190         if (f == NULL)
00191                 return NULL;
00192 
00193         fseek(f, 0, SEEK_END);
00194         *len = ftell(f);
00195         fseek(f, 0, SEEK_SET);
00196 
00197         buf = malloc(*len);
00198         if (buf == NULL) {
00199                 fclose(f);
00200                 return NULL;
00201         }
00202 
00203         fread(buf, 1, *len, f);
00204         fclose(f);
00205 
00206         return buf;
00207 }
00208 
00209 
00210 void * os_zalloc(size_t size)
00211 {
00212         return calloc(1, size);
00213 }
00214 

Generated on Sun Dec 31 13:43:28 2006 for hostapd by  doxygen 1.4.2