X-Git-Url: http://andersk.mit.edu/gitweb/moira.git/blobdiff_plain/1889bd72e39067dd75db5a9013b298956e0fc3f3..79d0bab0f8c75cf6dab288751a106b1d995550c1:/gen/util.c diff --git a/gen/util.c b/gen/util.c index 70be012f..d56ea3b2 100644 --- a/gen/util.c +++ b/gen/util.c @@ -1,122 +1,105 @@ -/* $Header$ +/* $Id$ * - * Utility routines used by the SMS extraction programs. + * Utility routines used by the MOIRA extraction programs. + * + * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. + * For copying and distribution information, please see the file + * . */ +#include +#include +#include #include -#include -#include -#include - +#include -/* ingres_date_and_time: passed a unix time_t, returns a string that ingres - * can parse to obtain that time. - */ +#include "util.h" -static char *month_name[] = { - "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", - "nov", "dec" - }; +RCSID("$Header$"); +/* Julian day of the UNIX epoch (January 1, 1970) */ +#define UNIX_EPOCH 2440588 -char *ingres_date(), *ingres_time(); +extern void sqlglm(char buf[], int *, int *); -char *ingres_date_and_time(l) -long l; +void fix_file(char *targetfile) { - char *ans = NULL, *date, *time; - - if ((date = ingres_date(l)) && (time = ingres_time(l))) { - char buf[BUFSIZ]; - sprintf(buf, "%s %s", date, time); - ans = strsave(buf); + char oldfile[64], filename[64]; + + sprintf(oldfile, "%s.old", targetfile); + sprintf(filename, "%s~", targetfile); + if (rename(targetfile, oldfile) == 0) + { + if (rename(filename, targetfile) < 0) + { + rename(oldfile, targetfile); + perror("Unable to install new file (rename failed)\n"); + fprintf(stderr, "Filename = %s\n", targetfile); + exit(MR_CCONFIG); } - if (date) - free(date); - if (time) - free(time); - return ans; + } + else + { + if (rename(filename, targetfile) < 0) + { + perror("Unable to rename old file\n"); + fprintf(stderr, "Filename = %s\n", targetfile); + exit(MR_CCONFIG); + } + } + unlink(oldfile); } -char *ingres_time(t) - long t; -{ - struct tm *tm; - - if (t == (long) 0) - (void) time(&t); - - if ((tm = localtime(&t)) == (struct tm *) NULL) { - return NULL; - } else { - char buf[BUFSIZ]; - sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, - tm->tm_sec); - return strsave(buf); - } +char *dequote(char *s) +{ + char *last = s; + + while (*s) + { + if (*s == '"') + *s = '\''; + else if (*s != ' ') + last = s; + s++; + } + if (*last == ' ') + *last = '\0'; + else + *(++last) = '\0'; + return s; } -char *ingres_date(t) - long t; +time_t unixtime(char *timestring) { - struct tm *tm; - - if (t == (long) 0) - (void) time(&t); + time_t t; - if ((tm = localtime(&t)) == (struct tm *) NULL) { - return NULL; - } else { - char buf[BUFSIZ]; + t = strtol(timestring, ×tring, 10) - UNIX_EPOCH; + t = t * 24 + strtol(timestring, ×tring, 10); + t = t * 60 + strtol(timestring, ×tring, 10); + t = t * 60 + strtol(timestring, ×tring, 10); - sprintf(buf, "%02d-%3.3s-%04d", tm->tm_mday, - month_name[tm->tm_mon], 1900 + tm->tm_year); - return strsave(buf); - } + return t; } - -fix_file(targetfile) -char *targetfile; +void db_error(int code) { - char oldfile[64], filename[64]; - - sprintf(oldfile, "%s.old", targetfile); - sprintf(filename, "%s~", targetfile); - if (rename(targetfile, oldfile) == 0) { - if (rename(filename, targetfile) < 0) { - rename(oldfile, targetfile); - perror("Unable to install new file (rename failed)\n"); - fprintf(stderr, "Filename = %s\n", targetfile); - exit(SMS_CCONFIG); - } - } else { - if (rename(filename, targetfile) < 0) { - perror("Unable to rename old file\n"); - fprintf(stderr, "Filename = %s\n", targetfile); - exit(SMS_CCONFIG); - } + extern char *whoami; + char buf[256]; + int bufsize = 256, len = 0; + + if (code == -1013) + { + com_err(whoami, 0, "build cancelled by user"); + exit(MR_ABORT); } - unlink(oldfile); -} - -char *dequote(s) -register char *s; -{ - char *last = s; - - while (*s) { - if (*s == '"') - *s = '\''; - else if (*s != ' ') - last = s; - s++; - } - if (*last == ' ') - *last = '\0'; - else - *(++last) = '\0'; + com_err(whoami, MR_DBMS_ERR, " code %d\n", code); + sqlglm(buf, &bufsize, &len); + buf[len] = 0; + com_err(whoami, 0, "SQL error text = %s", buf); + critical_alert("DCM", "%s build encountered DATABASE ERROR %d\n%s", + whoami, code, buf); + exit(MR_DBMS_ERR); }