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, *tm1;
00044 time_t t_local, t1, t2;
00045 os_time_t tz_offset;
00046
00047 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
00048 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
00049 sec > 60)
00050 return -1;
00051
00052 memset(&tm, 0, sizeof(tm));
00053 tm.tm_year = year - 1900;
00054 tm.tm_mon = month - 1;
00055 tm.tm_mday = day;
00056 tm.tm_hour = hour;
00057 tm.tm_min = min;
00058 tm.tm_sec = sec;
00059
00060 t_local = mktime(&tm);
00061
00062
00063 tm1 = localtime(&t_local);
00064 if (tm1) {
00065 t1 = mktime(tm1);
00066 tm1 = gmtime(&t_local);
00067 if (tm1) {
00068 t2 = mktime(tm1);
00069 tz_offset = t2 - t1;
00070 } else
00071 tz_offset = 0;
00072 } else
00073 tz_offset = 0;
00074
00075 *t = (os_time_t) t_local - tz_offset;
00076 return 0;
00077 }
00078
00079
00080 #ifdef __APPLE__
00081 #include <fcntl.h>
00082 static int os_daemon(int nochdir, int noclose)
00083 {
00084 int devnull;
00085
00086 if (chdir("/") < 0)
00087 return -1;
00088
00089 devnull = open("/dev/null", O_RDWR);
00090 if (devnull < 0)
00091 return -1;
00092
00093 if (dup2(devnull, STDIN_FILENO) < 0) {
00094 close(devnull);
00095 return -1;
00096 }
00097
00098 if (dup2(devnull, STDOUT_FILENO) < 0) {
00099 close(devnull);
00100 return -1;
00101 }
00102
00103 if (dup2(devnull, STDERR_FILENO) < 0) {
00104 close(devnull);
00105 return -1;
00106 }
00107
00108 return 0;
00109 }
00110 #else
00111 #define os_daemon daemon
00112 #endif
00113
00114
00115 int os_daemonize(const char *pid_file)
00116 {
00117 #ifdef __uClinux__
00118 return -1;
00119 #else
00120 if (os_daemon(0, 0)) {
00121 perror("daemon");
00122 return -1;
00123 }
00124
00125 if (pid_file) {
00126 FILE *f = fopen(pid_file, "w");
00127 if (f) {
00128 fprintf(f, "%u\n", getpid());
00129 fclose(f);
00130 }
00131 }
00132
00133 return -0;
00134 #endif
00135 }
00136
00137
00138 void os_daemonize_terminate(const char *pid_file)
00139 {
00140 if (pid_file)
00141 unlink(pid_file);
00142 }
00143
00144
00145 int os_get_random(unsigned char *buf, size_t len)
00146 {
00147 FILE *f;
00148 size_t rc;
00149
00150 f = fopen("/dev/urandom", "rb");
00151 if (f == NULL) {
00152 printf("Could not open /dev/urandom.\n");
00153 return -1;
00154 }
00155
00156 rc = fread(buf, 1, len, f);
00157 fclose(f);
00158
00159 return rc != len ? -1 : 0;
00160 }
00161
00162
00163 unsigned long os_random(void)
00164 {
00165 return random();
00166 }
00167
00168
00169 char * os_rel2abs_path(const char *rel_path)
00170 {
00171 char *buf = NULL, *cwd, *ret;
00172 size_t len = 128, cwd_len, rel_len, ret_len;
00173 int last_errno;
00174
00175 if (rel_path[0] == '/')
00176 return strdup(rel_path);
00177
00178 for (;;) {
00179 buf = malloc(len);
00180 if (buf == NULL)
00181 return NULL;
00182 cwd = getcwd(buf, len);
00183 if (cwd == NULL) {
00184 last_errno = errno;
00185 free(buf);
00186 if (last_errno != ERANGE)
00187 return NULL;
00188 len *= 2;
00189 if (len > 2000)
00190 return NULL;
00191 } else {
00192 buf[len - 1] = '\0';
00193 break;
00194 }
00195 }
00196
00197 cwd_len = strlen(cwd);
00198 rel_len = strlen(rel_path);
00199 ret_len = cwd_len + 1 + rel_len + 1;
00200 ret = malloc(ret_len);
00201 if (ret) {
00202 memcpy(ret, cwd, cwd_len);
00203 ret[cwd_len] = '/';
00204 memcpy(ret + cwd_len + 1, rel_path, rel_len);
00205 ret[ret_len - 1] = '\0';
00206 }
00207 free(buf);
00208 return ret;
00209 }
00210
00211
00212 int os_program_init(void)
00213 {
00214 return 0;
00215 }
00216
00217
00218 void os_program_deinit(void)
00219 {
00220 }
00221
00222
00223 int os_setenv(const char *name, const char *value, int overwrite)
00224 {
00225 return setenv(name, value, overwrite);
00226 }
00227
00228
00229 int os_unsetenv(const char *name)
00230 {
00231 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
00232 defined(__OpenBSD__)
00233 unsetenv(name);
00234 return 0;
00235 #else
00236 return unsetenv(name);
00237 #endif
00238 }
00239
00240
00241 char * os_readfile(const char *name, size_t *len)
00242 {
00243 FILE *f;
00244 char *buf;
00245
00246 f = fopen(name, "rb");
00247 if (f == NULL)
00248 return NULL;
00249
00250 fseek(f, 0, SEEK_END);
00251 *len = ftell(f);
00252 fseek(f, 0, SEEK_SET);
00253
00254 buf = malloc(*len);
00255 if (buf == NULL) {
00256 fclose(f);
00257 return NULL;
00258 }
00259
00260 if (fread(buf, 1, *len, f) != *len) {
00261 fclose(f);
00262 free(buf);
00263 return NULL;
00264 }
00265
00266 fclose(f);
00267
00268 return buf;
00269 }
00270
00271
00272 void * os_zalloc(size_t size)
00273 {
00274 return calloc(1, size);
00275 }
00276
00277
00278 size_t os_strlcpy(char *dest, const char *src, size_t siz)
00279 {
00280 const char *s = src;
00281 size_t left = siz;
00282
00283 if (left) {
00284
00285 while (--left != 0) {
00286 if ((*dest++ = *s++) == '\0')
00287 break;
00288 }
00289 }
00290
00291 if (left == 0) {
00292
00293 if (siz != 0)
00294 *dest = '\0';
00295 while (*s++)
00296 ;
00297 }
00298
00299 return s - src - 1;
00300 }
00301