1 /*
2 * (c) Copyright 1992 by Panagiotis Tsirigotis
3 * (c) Sections Copyright 1998-2001 by Rob Braun
4 * All rights reserved. The file named COPYRIGHT specifies the terms
5 * and conditions for redistribution.
6 */
7
8
9 #include "config.h"
10 #include <sys/types.h>
11 #include <syslog.h>
12 #include <ctype.h>
13 #include <time.h>
14
15 #include "sio.h"
16 #include "timex.h"
17 #include "msg.h"
18 #include "util.h"
19
20
21 #define IN_RANGE( val, low, high ) ( (low) <= (val) && (val) <= (high) )
22
23 struct time_interval
24 {
25 int16_t min_start ;
26 int16_t min_end ;
27 } ;
28
29 #define TIP( p ) ( (struct time_interval *) (p) )
30 #define NEW_TI() NEW( struct time_interval )
31 #define FREE_TI( tip ) FREE( tip )
32
33
34 /*
35 * Returns TRUE if the current time is within at least one of the intervals
36 */
ti_current_time_check(const pset_h intervals)37 bool_int ti_current_time_check( const pset_h intervals )
38 {
39 time_t current_time ;
40 unsigned u ;
41 int16_t min_current ;
42 struct tm *tmp ;
43
44 (void) time( ¤t_time ) ;
45 tmp = localtime( ¤t_time ) ;
46 min_current = tmp->tm_hour * 60 + tmp->tm_min ;
47
48 for ( u = 0 ; u < pset_count( intervals ) ; u++ )
49 {
50 struct time_interval *tip ;
51
52 tip = TIP( pset_pointer( intervals, u ) ) ;
53 if ( IN_RANGE( min_current, tip->min_start, tip->min_end ) )
54 return( TRUE ) ;
55 }
56 return( FALSE ) ;
57 }
58
59
get_num(int * nump,int min_val,int max_val,const char * s,char stop_char)60 static int get_num( int *nump,
61 int min_val,
62 int max_val,
63 const char *s,
64 char stop_char )
65 {
66 const char *func = "get_num" ;
67 int i = 0;
68
69 for ( *nump = 0 ; isdigit( s[i] ) ; i++ )
70 {
71 *nump *= 10 ;
72 *nump += s[i] - '0' ;
73 }
74
75 if ( s[i] != stop_char )
76 {
77 parsemsg( LOG_ERR, func, "incorrect time interval" ) ;
78 return( -1 );
79 }
80
81 if ( ! IN_RANGE( *nump, min_val, max_val ) )
82 {
83 parsemsg( LOG_ERR, func, "invalid time interval" ) ;
84 return( -1 ) ;
85 }
86 return( i ) ;
87 }
88
89
90 /*
91 * Each interval should have the form:
92 * hour:min-hour:min
93 * Example: 2:30-4:15
94 */
ti_add(pset_h iset,const char * interval_str)95 status_e ti_add( pset_h iset, const char *interval_str )
96 {
97 struct time_interval *tip ;
98 int hours ;
99 int minutes ;
100 int min_start ;
101 int min_end ;
102 int p, r = 0 ;
103 const char *func = "add_interval" ;
104
105 while (interval_str[r] == ' ')
106 r++; /* Eat white space */
107 if ( ( p = get_num( &hours, 0, 23, interval_str+r, ':' ) ) == -1 )
108 return( FAILED ) ;
109 r += p;
110 r++; /* Get past : */
111 if ( ( p = get_num( &minutes, 0, 59, interval_str+r, '-' ) ) == -1 )
112 return( FAILED ) ;
113 min_start = hours * 60 + minutes ;
114
115 r += p;
116 r++; /* Get past - */
117 if ( ( p = get_num( &hours, 0, 23, interval_str+r, ':' ) ) == -1 )
118 return( FAILED ) ;
119 r += p;
120 r++; /* Get past : */
121 if ( get_num( &minutes, 0, 59, interval_str+r, NUL ) == -1 )
122 return( FAILED ) ;
123 min_end = hours * 60 + minutes ;
124 if ( min_start >= min_end )
125 {
126 parsemsg( LOG_ERR, func, "invalid time interval: %s", interval_str ) ;
127 return( FAILED ) ;
128 }
129
130 tip = NEW_TI() ;
131 if ( tip == NULL )
132 {
133 out_of_memory( func ) ;
134 return( FAILED ) ;
135 }
136 tip->min_start = min_start ;
137 tip->min_end = min_end ;
138 if ( pset_add( iset, tip ) == NULL )
139 {
140 FREE_TI( tip ) ;
141 out_of_memory( func ) ;
142 return( FAILED ) ;
143 }
144 return( OK ) ;
145 }
146
147
ti_dump(pset_h iset,int fd)148 void ti_dump( pset_h iset, int fd )
149 {
150 unsigned u ;
151
152 for ( u = 0 ; u < pset_count( iset ) ; u++ )
153 {
154 struct time_interval *tip = TIP( pset_pointer( iset, u ) ) ;
155
156 Sprint( fd, " %02d:%02d-%02d:%02d",
157 tip->min_start / 60, tip->min_start % 60,
158 tip->min_end / 60, tip->min_end % 60 ) ;
159 }
160 }
161
162
ti_free(pset_h iset)163 void ti_free( pset_h iset )
164 {
165 pset_apply( iset, free, NULL ) ;
166 }
167
168