Import of tac_plus.v8.tar.gz: 173206 bytes, md5:
[tac_plus.git] / time_limit.c
1 /*              
2         These functions writen by Devrim SERAL <devrim@gazi.edu.tr>
3
4 Applicable format is  : <day str><time str> [,|] <day str><time str> [,|] and so on  
5
6 The accept parameter for day str is:
7 SU = Sunday
8 MO = Monday
9 TU = Tuesday
10 WE = Wendsday
11 TH = Thursday
12 FR = Friday
13 SA = Saturday
14 WK = For week days
15 WD = For Week and
16 AL = For All days
17
18 And time str must be:
19 Hourminute-Hourminute  
20 For example it's to be -> 0000-1200 or 1600-1700 or 1600-0800
21
22 License: This code is free software; you can redistribute it and/or modify it 
23 under the terms of the GNU General Public License as published by the Free 
24 Software Foundation; either version 2, or (at your option) any later version.
25                 
26 */      
27
28 #include"time_limit.h"
29 #include "tac_plus.h"
30
31 int problem=0;
32
33 int 
34 time_limit_process(str) 
35 char *str;
36 {
37 int ret=0;
38 char *tmp_str;
39
40 tmp_str=(char *)strtok(str,",|");
41 while ( tmp_str != NULL) {
42         ret|=str_token_proc(tmp_str);
43         tmp_str=(char *)strtok(NULL,",");
44         }
45 return (ret); 
46 }
47
48 int 
49 str_token_proc(str)
50 char *str;
51 {
52 int inv=0,ret;
53
54 /* Pass space characters */ 
55 while (isspace(*str)) str++;
56
57 if (*str=='!') { 
58                 inv=1;str++; 
59 }
60
61 ret=process(str);
62
63 if (problem) {
64         if ( debug & DEBUG_AUTHEN_FLAG )
65                report(LOG_DEBUG,"Timestamp format incorrect");
66         problem=0;
67         return(0);
68
69
70 if (inv) 
71         ret=!ret;
72 return(ret);    
73 }
74
75
76 int
77 process(str)
78 char *str;
79 {
80 int count=0,ret=0,i,j,localtm;
81 char *head,*buf,*gec;
82 long sec;
83 struct tm *tms;
84
85 /* Pass space characters  */
86 while (isspace(*str)) str++;
87
88 head=str;
89
90 /* Count alphanumeric char */
91 while (isalpha(*str)) { 
92         count++;
93         str++;
94 }
95
96 if ( count==0 || count%2 ) { 
97         problem++;
98         return 0;
99 }
100
101 buf=(char *)malloc(count+1);
102 strncpy(buf,head,count);
103 gec=buf;
104 str_up(buf);
105
106 for(i=1;i<=(count/2);i++) {
107         for (j=0;j<NUM;j++) {
108                 if(!strncmp(gec,week_days[j],2)) {
109                         ret=ret^week_day_val[j];
110                 }
111         }
112         gec+=2;
113 }
114
115 /* We finished to use buffer so free it */
116 free(buf);
117
118 sec=time(0);
119 tms=localtime(&sec);
120 localtm=(tms->tm_hour)*60+tms->tm_min;
121 ret=( week_day_val[tms->tm_wday] & ret ) && time_calc(str,localtm);
122
123 if (ret>0) 
124         return (1); 
125 else 
126         return(0); 
127 }
128
129 str_up(str)
130 char *str;
131 {
132   while(*str) {
133         if(islower(*str)) *str=toupper(*str);
134         str++;
135   }
136 }
137
138 int 
139 time_calc(str,lct)
140 char *str;
141 int lct;
142 {
143 char *t1,*t2,*head;
144 int say1,say2,count=0;
145
146 head=str;
147
148  while (isdigit(*head) || *head=='-') {
149         count++;
150         head++; 
151  }
152
153 if (*str=='\0' || count!= TPL ) {
154         problem++;      
155         return (0);
156 }
157
158   t1=(char *) malloc(count);
159   strncpy(t1,str,count);        /*Put str value to t1*/
160
161   t2=(char *) strstr(t1,"-"); /* Find next time part */
162
163 if (t2==NULL) {
164    free(t1);
165    problem++;
166    return(0);
167 }
168         
169 *t2='\0';t2++;
170         
171 if ( strlen(t1)<4 || strlen(t2)<4 ) {
172         free(t1);
173         problem++;
174         return(0);
175 }
176         say1=antoi(t1,2)*60+antoi(t1+2,2);
177         say2=antoi(t2,2)*60+antoi(t2+2,2);
178
179 free(t1);
180
181 if (say1<=say2) { 
182         if( (lct>=say1) && (lct<=say2) ) return(1); 
183 }
184 else {
185         if( (lct>=say1) || (lct<=say2) ) return(1); 
186 }
187 return(0);
188
189 }
190
191 int 
192 antoi(str,n)
193 char *str;int n;
194 {
195 char *buf;
196 int ret;
197
198   buf=(char *) malloc(n);
199   strncpy(buf,str,n);
200   ret=atoi(buf);
201   free(buf);
202
203 return(ret);
204 }