]> andersk Git - moira.git/blob - gen/moddiff.dc
detect deadlocks
[moira.git] / gen / moddiff.dc
1 /* $Header$
2  *
3  *  (c) Copyright 1990 by the Massachusetts Institute of Technology.
4  *  For copying and distribution information, please see the file
5  *  <mit-copyright.h>.
6  */
7
8 #include <mit-copyright.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/time.h>
13 #include <moira.h>
14 #include <moira_site.h>
15 EXEC SQL INCLUDE sqlca;
16
17 static char * MONTHNAMES[] = {"january", "february", "march", "april", "may", 
18                               "june", "july", "august", "september",
19                               "october", "november", "december"};
20
21 static int MONTHDAYS[] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
22
23
24 /****************************************************************/
25 /**  Return the difference between the modtime & table modtime **/
26 /**  in flag variable.  Return 0 if success, else errno.       **/
27 /****************************************************************/
28
29 int ModDiff (flag, tbl, ModTime)
30 int *flag;
31 char *tbl;
32 time_t ModTime;
33 {
34     time_t filetimeno;
35     EXEC SQL BEGIN DECLARE SECTION;
36     char filetime[48], *tbl_name;
37     EXEC SQL END DECLARE SECTION;
38
39     *flag = 0;
40     tbl_name = tbl;
41     EXEC SQL SELECT modtime INTO :filetime FROM tblstats 
42       WHERE table_name = :tbl_name;
43     if (sqlca.sqlcode != 0) {
44         if (sqlca.sqlcode == -37000 ||
45             sqlca.sqlcode == 17700)
46           exit(MR_DEADLOCK);
47         fprintf(stderr, "Moddiff Query failed: %d\n", sqlca.sqlcode);
48         return(MR_DATE);
49     }
50 #ifdef DEBUG
51     printf("about to call Date2Sec, flag = 0x%x\n", flag);
52 #endif
53     if (Date2Sec (filetime, &filetimeno)) {
54         fprintf(stderr,"Unable to parse mod. date for file %s\n", tbl);
55         return(MR_DATE);
56     }
57 #ifdef DEBUG
58     printf("got time, flag = 0x%x\n", flag);
59 #endif
60     *flag = (int) (filetimeno - ModTime);
61     return(0);
62 }
63
64
65 /***************************************************/
66 /**  convert Unix-time (# of seconds since 1970)  **/
67 /**  from a date-time string                      **/
68 /**  Return 1 if failure, 0 if success            **/
69 /***************************************************/
70
71 Date2Sec(DS, UTime)
72 char *DS;
73 time_t *UTime;
74 {
75     static int defzone = -1;
76     int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
77     struct timeval tv;
78     struct timezone tz;
79
80     if (defzone == -1) {
81         if (gettimeofday(&tv, &tz))
82           defzone = 0;
83         else
84           defzone = tz.tz_minuteswest;
85     }
86
87 #ifdef DEBUG
88     printf("Date2Sec(\"%s\", %d)\n", DS, *UTime);
89 #endif
90     Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute,&Sec);
91 #ifdef DEBUG
92     printf("date=%d/%d/%d %d:%d:%d, err=%d\n", Day, Month, Year,
93            Hour, Minute, Sec, Err);
94 #endif
95     if (Err) return (Err);
96
97     if (Year < 1970) {
98         for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--) {
99             TotalDays += NumdaysY(Iter);}
100 #ifdef DEBUG
101         printf("Days in years           : %d\n", TotalDays);
102 #endif
103         TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
104 #ifdef DEBUG
105         printf("Days after months & days: %d\n", TotalDays);
106 #endif
107         *UTime = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
108                           Sec) + defzone;
109         return (0);
110     } else {
111         for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++) {
112             TotalDays += NumdaysY(Iter);}
113 #ifdef DEBUG
114         printf("Days in years    : %d\n", TotalDays);
115 #endif
116         TotalDays += NumdaysM(Month, Year);
117 #ifdef DEBUG
118         printf("Days after months: %d\n", TotalDays);
119 #endif
120         TotalDays += (Day - 1);
121 #ifdef DEBUG
122         printf("Days after Days  : %d\n", TotalDays);
123         printf("Calculating seconds...(UTime = 0x%x)\n", *UTime);
124 #endif
125         *UTime = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
126                         Sec + defzone;
127 #ifdef DEBUG
128         printf("Calculated value of %d\n", *UTime);
129 #endif
130         return (0);
131     }
132 }
133
134
135 /*****************************************/
136 /**  Return the # of days in the Year   **/
137 /*****************************************/
138
139 static int NumdaysY(Year)
140 int Year;
141 {
142     return (365 + Leapyear(Year));
143 }
144
145
146 /*****************************************/
147 /**  Return the # of days in the Month  **/
148 /*****************************************/
149
150 static int NumdaysM(Month, Year)
151 int Month, Year;
152 {
153     if ((Month > 2) && (Leapyear (Year)))
154       return (MONTHDAYS[Month] + 1);
155     else
156       return (MONTHDAYS[Month]);
157 }
158
159
160 /*****************************************/
161 /**  Return 1 if a leapyear, else 0     **/
162 /*****************************************/
163
164 static int Leapyear (Year)
165 int Year;
166 {
167     if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
168       return (0);
169     else
170       return (1);
171 }
172
173
174 /************************************************/
175 /**  Compute numeric breakdown of date string  **/
176 /**  Return 0 if success, 1 if failure         **/
177 /************************************************/
178
179 static int ParseDateString (DS, Day, Month, Year, Hour, Minute, Sec)
180 char *DS;
181 int *Day, *Month, *Year, *Hour, *Minute, *Sec;
182 {
183     int Gotten;
184     char *M, *D, *Temp;
185     int Y=0, H=0, Min=0, S=0, DayNum=0, MonthNum=0;
186
187     M = (char *)malloc(strlen(DS) + 2);
188     D = (char *)malloc(strlen(DS) + 2);
189     if (!(M && D)) return (1);
190     Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
191     if (Gotten < 3) 
192       Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
193     if (Gotten < 3)
194       Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
195 #ifdef DEBUG
196     printf ("Month = %s\nDay = %s\nYear = %d\n", M, D, Y);
197     printf ("Hour = %d\nMin= %d\nSec = %d\n", H, Min, S);
198 #endif
199     if ((Gotten < 3) || !(D && M && Y)) {
200         free(M);
201         free(D);
202         return (1);}               /* Couldn't scan in a date */
203     if (atoi(M)) {               /* Month not text, so M/D/Y not D/M/Y */
204         Temp = M;
205         M = D;
206         D = Temp;}
207     DayNum = atoi(D);
208     MonthNum = MonthNo(M);
209     free(M);
210     free(D);
211 #ifdef DEBUG
212     printf ("Month = %d\nDay = %d\nYear = %d\n", MonthNum, DayNum, Y);
213 #endif
214     if ((DayNum < 1) || (DayNum > 31)) 
215       return (1);                /* Bad Day */
216     if (!MonthNum) 
217       return (1);                /* Bad Month */
218     if ((Y < 1) || (Y > 10000)) 
219       return (1);                /* Bad Year */
220     if (Gotten == 4) 
221       return(1);                 /* Bad Time (Hour only) */
222     if ((Gotten > 4) && (H < 0) || (H > 24)) 
223       return(1);                 /* Bad Hour */
224     if ((Gotten > 4) && ((Min < 0) || (Min > 59))) 
225       return(1);                 /* Bad Minute */
226     if ((Gotten > 5) && ((S < 0) || (S > 59))) 
227       return(1);                 /* Bad Second */
228     *Day = DayNum; 
229     *Month = MonthNum;
230     if (Y < 100)                   /* For abreviations like 90 for 1990    */
231       Y += 1900;                   /* (Yes, it will be wrong in 2000) */
232     *Year = Y;
233     if (Gotten > 4) {
234         *Hour = H;
235         *Minute = Min;
236     }
237     else {
238         *Hour = 0;
239         *Minute = 0;}
240     if (Gotten > 5) 
241       *Sec = S;
242     else
243       *Sec = 0;
244     return(0);
245 }
246
247
248 /***********************************************************/
249 /**  Return the Month number of a Month number or string  **/
250 /***********************************************************/
251
252 static int MonthNo (M)
253 char *M;
254 {
255     int Count;
256     if (atoi(M)) {
257         if ((atoi(M) > 0) && (atoi(M) < 13))
258           return (atoi(M));
259         else
260           return (0);
261     }  
262     for (Count = 0; Count < 12; Count++) {
263         if (!strcasecmp (M, MONTHNAMES[Count]) 
264             || !strncasecmp(M, MONTHNAMES[Count], 3)) 
265           return (Count + 1);
266     }
267     return (0);
268 }
This page took 0.060459 seconds and 5 git commands to generate.