00001
00002 #include "edt_utils.h"
00003
00004 #include "edt_error.h"
00005
00006 #include <time.h>
00007
00008
00009 HANDLE edt_open_datafile(const char *path,
00010 const char *szFileName,
00011 u_char writing,
00012 u_char direct,
00013 u_char truncate)
00014
00015 {
00016
00017 int ok = 0;
00018
00019 HANDLE pFile;
00020
00021 int rwmode;
00022
00023 int createmode;
00024
00025 int flags = 0;
00026
00027 char name[256];
00028
00029 if (path)
00030 sprintf(name, "%s/%s", path, szFileName);
00031 else
00032 sprintf(name, "%s",szFileName);
00033
00034
00035 if (direct)
00036 flags = F_NOCACHE;
00037
00038 if (writing)
00039 {
00040 if (truncate)
00041 flags |= O_TRUNC;
00042 pFile = open(name, flags | (O_CREAT | O_RDWR), 0777);
00043 }
00044 else
00045 pFile = open(name, flags | O_RDONLY);
00046
00047 if (pFile == -1)
00048 {
00049 perror("Open");
00050 edt_msg(EDT_MSG_FATAL,
00051 "Failed to open %s for %s\n", name, (writing)?"write":"read");
00052 }
00053 return pFile;
00054 }
00055
00056 void edt_close_datafile(HANDLE f)
00057
00058 {
00059
00060
00061 close(f);
00062
00063
00064
00065 }
00066
00067 int edt_write_datafile(HANDLE f, void *p, int bufsize)
00068
00069 {
00070
00071 int rc;
00072
00073
00074 rc = write(f,p,bufsize);
00075
00076 if (rc == -1)
00077 {
00078 printf("Write Error");
00079 printf("Error on file %d\n", f);
00080 }
00081
00082 return rc;
00083
00084 }
00085
00086 int edt_read_datafile(HANDLE f, void *p, int bufsize)
00087
00088 {
00089
00090 int rc;
00091
00092
00093 rc = read(f,p,bufsize);
00094
00095 return rc;
00096
00097 }
00098
00099 void *edt_alloc_aligned(int size)
00100
00101 {
00102 void *p;
00103 volatile int throwaway_int = posix_memalign(&p,512,size);
00104 return p;
00105 }
00106
00107 void edt_free_aligned(void *p)
00108
00109 {
00110 free(p);
00111 }
00112
00122 extern int dumping_sleep;
00123
00124 #include <semaphore.h>
00125
00126 int
00127 edt_wait_event_timeout(sem_t *p, int timeout)
00128
00129 {
00130
00131 int t = 0;
00132
00133 while (t < timeout && sem_trywait(p))
00134 {
00135 edt_msleep(10);
00136 t += 10;
00137 }
00138
00139 if (t < timeout)
00140 return 0;
00141 else
00142 return 1;
00143
00144 }
00145
00146
00147 #include <sys/ioctl.h>
00148
00149 static int kbhitedt()
00150 {
00151 int numchars;
00152
00153 ioctl(0, FIONREAD, &numchars) ;
00154
00155
00156 return(numchars);
00157 }
00158
00159 #include <string.h>
00160
00161 u_char edt_wait_for_console_input(char *line,
00162 const int timeout,
00163 const int maxlen)
00164
00165 {
00166
00167
00168 int t = 0;
00169
00170 while (t < timeout && !kbhitedt())
00171 {
00172 edt_msleep(10);
00173 t += 10;
00174 }
00175
00176 if (t < timeout)
00177 {
00178 volatile char *throwaway_charp = fgets(line, maxlen, stdin);
00179 if (strlen(line))
00180 if (line[strlen(line)-1] == '\n')
00181 line[strlen(line)-1] = 0;
00182
00183 return TRUE;
00184 }
00185 else
00186 return FALSE;
00187
00188
00189 }
00190 #include <sys/timeb.h>
00191
00192 #include <time.h>
00193
00194 int edt_get_datestr(char *s, int maxlen)
00195
00196 {
00197 time_t tm;
00198
00199 time(&tm);
00200
00201 strncpy(s,ctime(&tm),maxlen);
00202
00203 if (s[strlen(s)-1] == '\n')
00204 s[strlen(s)-1] = 0;
00205
00206 return 0;
00207 }
00208
00209 uint64_t
00210 edt_get_file_position(HANDLE f)
00211
00212 {
00213 return lseek(f, 0, SEEK_CUR);
00214
00215 }
00216
00217 #include <sys/stat.h>
00218
00219 uint64_t
00220 edt_get_file_size(HANDLE f)
00221
00222 {
00223
00224
00225 struct stat buf;
00226
00227
00228
00229 fstat(f, &buf);
00230
00231 return buf.st_size;
00232
00233 }
00234
00235 uint64_t
00236 edt_file_seek(HANDLE f, uint64_t to)
00237
00238 {
00239 uint64_t rc;
00240
00241 rc = lseek(f, to, SEEK_SET);
00242
00243 return (rc != to);
00244
00245 }
00246
00258 unsigned char *
00259 edt_alloc(int size)
00260 {
00261 unsigned char *ret;
00262
00263 ret = (unsigned char *) valloc(size);
00264 if (ret)
00265 {
00266 u_char *tmp_p;
00267
00268 for (tmp_p = ret; tmp_p < ret + size; tmp_p += 4096)
00269 {
00270
00271 *tmp_p = 0xa5;
00272 }
00273 }
00274 return (ret);
00275 }
00276
00285 void
00286 edt_free(uchar_t *ptr)
00287 {
00288 free(ptr);
00289 }
00290
00291
00298 double
00299 edt_timestamp()
00300 {
00301 double dtime;
00302
00303 struct timeval endtime;
00304
00305 gettimeofday(&endtime, (void *) NULL);
00306
00307 dtime = (double) endtime.tv_sec
00308 + (((double) endtime.tv_usec) / 1000000.0);
00309
00310 return (dtime);
00311
00312 }
00313
00325 double
00326 edt_dtime()
00327 {
00328 double dtime;
00329
00330 static struct timeval starttime;
00331 struct timeval endtime;
00332 double start;
00333 double end;
00334
00335 gettimeofday(&endtime, (void *) NULL);
00336
00337 start = (double) starttime.tv_sec
00338 + (((double) starttime.tv_usec) / 1000000.0);
00339 end = (double) endtime.tv_sec
00340 + (((double) endtime.tv_usec) / 1000000.0);
00341 dtime = end - start;
00342
00343
00344 starttime = endtime;
00345 return (dtime);
00346 }
00347
00348 static double elapsed_start = 0.0;
00349
00355 double
00356 edt_elapsed(u_char reset)
00357 {
00358
00359 if (reset || elapsed_start == 0.0)
00360 {
00361 elapsed_start = edt_timestamp();
00362 }
00363
00364 return edt_timestamp() - elapsed_start;
00365 }
00366
00375 void
00376 edt_msleep(int msecs)
00377 {
00378
00379
00380 struct timespec ts;
00381 double farg = (double)msecs;
00382
00383 ts.tv_sec = (time_t) ((farg * 1000000.0) / 1000000000.0);
00384 ts.tv_nsec = (long) (msecs % 1000) * 1000000;
00385
00386 (void)nanosleep(&ts, NULL);
00387 }
00388
00389
00397 void
00398 edt_usleep(int usecs)
00399 {
00400 struct timespec ts;
00401
00402 ts.tv_sec = usecs / 1000000;
00403 ts.tv_nsec = (long) (usecs % 1000000) * 1000;
00404
00405 (void)nanosleep(&ts, NULL);
00406 }
00407
00408 void
00409 edt_usec_busywait(u_int usec)
00410 {
00411 double t = 0.0, f_usec = usec / 1000000;
00412
00413 edt_dtime();
00414 while (t < f_usec)
00415 t += edt_dtime();
00416 }
00417
00418
00419
00420
00421
00422
00426 void
00427 edt_fwd_to_back(char *str)
00428 {
00429 char *p = str;
00430
00431 while (*p != '\0')
00432 {
00433 if (*p == '/')
00434 *p = '\\';
00435 ++p;
00436 }
00437 }
00438
00442 void
00443 edt_back_to_fwd(char *str)
00444 {
00445 char *p = str;
00446
00447 while (*p != '\0')
00448 {
00449 if (*p == '\\')
00450 *p = '/';
00451 ++p;
00452 }
00453 }
00454
00455
00456
00461 void
00462 edt_correct_slashes(char *str)
00463 {
00464 edt_back_to_fwd(str);
00465 }
00466
00486 int
00487 edt_access(char *fname, int perm)
00488 {
00489 int ret;
00490
00491 edt_correct_slashes(fname);
00492 ret = access(fname, perm);
00493 return ret;
00494 }
00495
00523 #include <dirent.h>
00524
00525 DIRHANDLE
00526 edt_opendir(const char *dirname)
00527 {
00528 return (DIRHANDLE) opendir(dirname);
00529 }
00530
00531 int
00532 edt_readdir(DIRHANDLE dirphandle, char *name)
00533 {
00534 DIR *dirp = (DIR *) dirphandle;
00535
00536 struct dirent *dp;
00537
00538 if ((dp = readdir(dirp)) == NULL)
00539 return 0;
00540 strcpy(name, dp->d_name);
00541 return 1;
00542 }
00543
00544 void
00545 edt_closedir(DIRHANDLE dirphandle)
00546 {
00547 DIR *dirp = (DIR *) dirphandle;
00548 closedir(dirp);
00549 }
00550
00551 #include <sys/statvfs.h>
00552
00553 int64_t
00554 edt_disk_free(const char *name)
00555
00556 {
00557 int rc;
00558 struct statvfs stat;
00559
00560 rc = statvfs(name, &stat);
00561
00562 if (rc == 0)
00563 {
00564 return stat.f_bsize * stat.f_bavail;
00565 }
00566 else
00567 return 0;
00568 }
00569