[24319] | 1 | /* $Id: util.c 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
[23095] | 2 | * |
---|
| 3 | * Utility routines used by the MOIRA extraction programs. |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. |
---|
| 6 | * For copying and distribution information, please see the file |
---|
| 7 | * <mit-copyright.h>. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <mit-copyright.h> |
---|
| 11 | #include <moira.h> |
---|
| 12 | #include <moira_site.h> |
---|
| 13 | |
---|
| 14 | #include <stdio.h> |
---|
| 15 | #include <unistd.h> |
---|
| 16 | |
---|
| 17 | #include "util.h" |
---|
| 18 | |
---|
[24319] | 19 | RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/gen/util.c $ $Id: util.c 3956 2010-01-05 20:56:56Z zacheiss $"); |
---|
[23095] | 20 | |
---|
| 21 | /* Julian day of the UNIX epoch (January 1, 1970) */ |
---|
| 22 | #define UNIX_EPOCH 2440588 |
---|
| 23 | |
---|
| 24 | extern void sqlglm(char buf[], int *, int *); |
---|
| 25 | |
---|
| 26 | void fix_file(char *targetfile) |
---|
| 27 | { |
---|
| 28 | char oldfile[64], filename[64]; |
---|
| 29 | |
---|
| 30 | sprintf(oldfile, "%s.old", targetfile); |
---|
| 31 | sprintf(filename, "%s~", targetfile); |
---|
| 32 | if (rename(targetfile, oldfile) == 0) |
---|
| 33 | { |
---|
| 34 | if (rename(filename, targetfile) < 0) |
---|
| 35 | { |
---|
| 36 | rename(oldfile, targetfile); |
---|
| 37 | perror("Unable to install new file (rename failed)\n"); |
---|
| 38 | fprintf(stderr, "Filename = %s\n", targetfile); |
---|
| 39 | exit(MR_CCONFIG); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | else |
---|
| 43 | { |
---|
| 44 | if (rename(filename, targetfile) < 0) |
---|
| 45 | { |
---|
| 46 | perror("Unable to rename old file\n"); |
---|
| 47 | fprintf(stderr, "Filename = %s\n", targetfile); |
---|
| 48 | exit(MR_CCONFIG); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | unlink(oldfile); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | char *dequote(char *s) |
---|
| 56 | { |
---|
| 57 | char *last = s; |
---|
| 58 | |
---|
| 59 | while (*s) |
---|
| 60 | { |
---|
| 61 | if (*s == '"') |
---|
| 62 | *s = '\''; |
---|
| 63 | else if (*s != ' ') |
---|
| 64 | last = s; |
---|
| 65 | s++; |
---|
| 66 | } |
---|
| 67 | if (*last == ' ') |
---|
| 68 | *last = '\0'; |
---|
| 69 | else |
---|
| 70 | *(++last) = '\0'; |
---|
| 71 | return s; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | time_t unixtime(char *timestring) |
---|
| 75 | { |
---|
| 76 | time_t t; |
---|
| 77 | |
---|
| 78 | t = strtol(timestring, ×tring, 10) - UNIX_EPOCH; |
---|
| 79 | t = t * 24 + strtol(timestring, ×tring, 10); |
---|
| 80 | t = t * 60 + strtol(timestring, ×tring, 10); |
---|
| 81 | t = t * 60 + strtol(timestring, ×tring, 10); |
---|
| 82 | |
---|
| 83 | return t; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void db_error(int code) |
---|
| 87 | { |
---|
| 88 | extern char *whoami; |
---|
| 89 | char buf[256]; |
---|
| 90 | int bufsize = 256, len = 0; |
---|
| 91 | |
---|
| 92 | if (code == -1013) |
---|
| 93 | { |
---|
| 94 | com_err(whoami, 0, "build cancelled by user"); |
---|
| 95 | exit(MR_ABORT); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | com_err(whoami, MR_DBMS_ERR, " code %d\n", code); |
---|
| 99 | sqlglm(buf, &bufsize, &len); |
---|
| 100 | buf[len] = 0; |
---|
| 101 | com_err(whoami, 0, "SQL error text = %s", buf); |
---|
[23882] | 102 | critical_alert(whoami, "DCM", "%s build encountered DATABASE ERROR %d\n%s", |
---|
[23095] | 103 | whoami, code, buf); |
---|
| 104 | exit(MR_DBMS_ERR); |
---|
| 105 | } |
---|