/* See js.h */ /* Last edited on 2017-06-21 05:33:05 by stolfilocal */ #include #include #include #include #include #include #include #include #include #include void error (char *msg) { fprintf (stderr, "*** %s\n", msg); exit(1); } void *programerror (char *msg, char *file, unsigned int line, char* proc) { fprintf (stderr, "*** %s:%u: (%s) %s\n", file, line, proc, msg); exit(1); return NULL; } char *txtcat (char *a, char *b) { char *r = malloc(strlen(a)+strlen(b)+1); affirm (r != NULL, "memory exhausted"); strcpy(r, a); strcat(r, b); return(r); } char *today(void) { time_t today_secs = time(NULL); struct tm today; char *buf = (char *) malloc(20); today = *localtime(&today_secs); sprintf(buf, "%02d-%02d-%02d %02d:%02d:%02d", today.tm_year % 100, today.tm_mon, today.tm_mday, today.tm_hour, today.tm_min, today.tm_sec ); return(buf); } #if (defined(SunOS4)) double now(void) { struct rusage ru; getrusage(RUSAGE_SELF, &ru); return(((double)ru.ru_utime.tv_sec)*1000000.0 + ((double)ru.ru_utime.tv_usec)); } #endif #if (defined(SunOS5)) extern long int __sysconf (int); double now(void) { struct tms buf; clock_t t; times(&buf); t = buf.tms_utime; return(1000000.0L * ((double) t)/((double) _sysconf(3))); } #endif #if (defined(Linux)) #include extern long int __sysconf (int); double now(void) { struct tms buf; clock_t t; times(&buf); t = buf.tms_utime; return(1000000.0L * ((double) t)/((double) __sysconf(2))); } #endif #if (defined(OSF1V4)) extern long int __sysconf (int); double now(void) { struct tms buf; clock_t t; times(&buf); t = buf.tms_utime; return(1000000.0L * ((double) t)/((double) CLK_TCK)); } #endif FILE *open_write(char *name) { FILE *f; fprintf(stderr, "opening %s\n", name); fflush(stderr); f = fopen(name, "w"); affirm (f != NULL, txtcat("can't create file ", name)); return (f); } FILE *open_read(char *name) { FILE *f; fprintf(stderr, "opening %s\n", name); fflush(stderr); f = fopen(name, "r"); affirm (f != NULL, txtcat("can't find file ", name)); return (f); } char *read_line(FILE *f) { int mc = 0; int nc = 0; char *s = NULL; int c; do { c = getc(f); if ((c == EOF) && (nc == 0)) return(NULL); if ((c == EOF) || (c == '\n')) c = '\000'; if (c == '\t') c = ' '; if (nc >= mc) { if (mc == 0) { mc = 40; s = (char *) malloc(mc*sizeof(char)); } else { mc *= 2; s = (char *) realloc ((void *) s, mc*sizeof(char)); } } affirm (s != NULL, "alloc failed"); s[nc] = c; nc++; } while (c != '\000'); return (s); } /* The implementations of $frandom$ and $drandom$ below are not entirely correct, because normalization may produce trailing zero bits with probability slightly larger than 1/2. */ float frandom(void) { float f = 0.0; f = (float)((f + (random()&8388607))/8388608.0); return (f); } double drandom(void) { double d = 0.0; d = (d + (random()&536870911))/536870912.0; d = (d + (random()&8388607))/8388608.0; return (d); }