#include "utils.h" void waitfor (double seconds) { struct timeval time; time.tv_sec = floor(seconds); time.tv_usec = rint((seconds-time.tv_sec)*1E6); select (0, NULL, NULL, NULL, &time); return; } /* end waitfor() */ /* return pseudo random number on (0,1) */ double frandom() { return ((random()+0.5)/pow(2.,31.)); } /* end frandom() */ /* generate n normal variables and store in x[] */ void randnorm (int n, double x[]) { int i; double s1, s2; for (i=0; inext != NULL) && strncmp(p->next->token, token, MAX_TOKEN_SIZE-1); p = p->next ); if ( p->next == NULL ) { /* look for free space */ for (q=mempool+1; qtoken)==(char)0 ) { p->next = q; q->next = NULL; strncpy (q->token, token, MAX_TOKEN_SIZE-1); q->init_time = TIME; return(TIME); } printf("**error watch(): mempool %d records all used up.\n", MAX_RECORD-1); return(-1.); } else { printf("**error watch(): token name \"%s\" already used.\n", token); return(-1.); } /* return the time elapsed in seconds */ case 1: for ( p = mempool; (p->next != NULL) && strncmp(p->next->token, token, MAX_TOKEN_SIZE-1); p = p->next); if ( p->next == NULL ) { printf("**error watch(): token name \"%s\" not found.\n", token); return(-1.); } else return (TIME - p->next->init_time); /* return the time elapsed in seconds */ /* and refresh the watch's startpoint */ case 2: for ( p = mempool; (p->next != NULL) && strncmp(p->next->token, token, MAX_TOKEN_SIZE-1); p = p->next); if ( p->next == NULL ) { printf("**error watch(): token name \"%s\" not found.\n", token); return(-1.); } else { cc = p->next->init_time; p->next->init_time = TIME; return (TIME - cc); } /* destroy the record and free resources */ case 3: for ( p = mempool; (p->next != NULL) && strncmp(p->next->token, token, MAX_TOKEN_SIZE-1); p = p->next); if ( p->next == NULL ) { printf("**error watch(): token name \"%s\" not found.\n", token); return(-1.); } else { cc = p->next->init_time; *(p->next->token) = (char)0; p->next = p->next->next; return (TIME - cc); } default: printf("**error watch(): 0: init, 1: check, 2: refresh, 3:destroy.\n"); return(-1.); } } /* watch() */ #undef TIME #undef MAX_RECORD 512 #undef MAX_TOKEN_SIZE 16 char *earth_time (double seconds) { const struct time_unit { char *name; double scale; } earthman[] = { {"s", 1}, {"min", 60}, {"hr", 60}, {"day", 24}, {"month", 30}, {"year", 365} }; static char time[128], *p; int i, max=sizeof(earthman)/sizeof(struct time_unit); double scale; int denom; seconds /= earthman[0].scale; for (scale=1,i=1; i0; i--) { denom = floor(seconds/scale); if (denom>0) { sprintf(p, "%d %s, ", denom, earthman[i].name); while((*p)!=(char)0) p++; } seconds -= denom*scale; scale /= earthman[i].scale; } sprintf(p, "%.3f %s", seconds, earthman[0].name); return(time); } /* earth_time() */