/* See js.h */ /****************************************************************************/ /* (C) Copyright 1993 Universidade Estadual de Campinas (UNICAMP) */ /* Campinas, SP, Brazil */ /* */ /* This file can be freely distributed, modified, and used for any */ /* non-commercial purpose, provided that this copyright and authorship */ /* notice be included in any copy or derived version of this file. */ /* */ /* DISCLAIMER: This software is offered ``as is'', without any guarantee */ /* as to fitness for any particular purpose. Neither the copyright */ /* holder nor the authors or their employers can be held responsible for */ /* any damages that may result from its use. */ /****************************************************************************/ #include "js.h" #include #include #include #include #include #include #include #include void error (char *msg) { fprintf (stderr, "*** %s\n", msg); abort(); } void assert(int test, char *msg) { if (! test) { fprintf(stderr, "\n*** assertion is false: %s ***\n", msg); abort(); } } char *txtcat (char *a, char *b) { char *r = malloc(strlen(a)+strlen(b)+1); if (r) { strcpy(r, a); strcat(r, b); return(r); } else { error ("textcat: memory exhausted"); return(NULL); } } char *today(void) { time_t today_secs = time(NULL); struct tm today = *localtime(&today_secs); char *buf = (char *) malloc(20); 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); } 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)); } FILE *open_write(char *name) { FILE *f; fprintf(stderr, "opening %s\n", name); fflush(stderr); f = fopen(name, "w"); if (f == NULL) { error (txtcat("open_write: 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"); if (f == NULL) { error (txtcat("open_read: 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)); } } assert (s != NULL, "read_line: alloc failed"); s[nc] = c; nc++; } while (c != '\000'); return (s); } float frandom(void) { float f = 0.0; f = (f + (random()&65535))/65536.0; f = (f + (random()&65535))/65536.0; return (f); } double drandom(void) { double d = 0.0; d = (d + (random()&65535))/65536.0; d = (d + (random()&65535))/65536.0; d = (d + (random()&65535))/65536.0; d = (d + (random()&65535))/65536.0; return (d); }