Browse code

core: utils/tmrec - use safer localtime_r() instead of localtime()

- coherent whitespacing and indentation

(cherry picked from commit a7496069c60479e41d92830172bd5b9315001df5)

Daniel-Constantin Mierla authored on 03/05/2020 09:05:45
Showing 1 changed files
... ...
@@ -62,17 +62,17 @@ static inline char* tr_trim(char* _s)
62 62
 	int len;
63 63
 	char* end;
64 64
 
65
-	     /* Null pointer, there is nothing to do */
65
+	/* Null pointer, there is nothing to do */
66 66
 	if (!_s) return _s;
67 67
 
68
-	     /* Remove spaces and tabs from the beginning of string */
68
+	/* Remove spaces and tabs from the beginning of string */
69 69
 	while ((*_s == ' ') || (*_s == '\t')) _s++;
70 70
 
71 71
 	len = strlen(_s);
72 72
 
73
-        end = _s + len - 1;
73
+	end = _s + len - 1;
74 74
 
75
-	     /* Remove trailing spaces and tabs */
75
+	/* Remove trailing spaces and tabs */
76 76
 	while ((*end == ' ') || (*end == '\t')) end--;
77 77
 	if (end != (_s + len - 1)) {
78 78
 		*(end+1) = '\0';
... ...
@@ -117,7 +117,7 @@ ac_tm_t *ac_tm_new(void)
117 117
 	if(!_atp)
118 118
 		return NULL;
119 119
 	memset(_atp, 0, sizeof(ac_tm_t));
120
-	
120
+
121 121
 	return _atp;
122 122
 }
123 123
 
... ...
@@ -134,7 +134,7 @@ int ac_tm_fill(ac_tm_t *_atp, struct tm* _tm)
134 134
 	_atp->t.tm_wday = _tm->tm_wday;     /* day of the week */
135 135
 	_atp->t.tm_yday = _tm->tm_yday;     /* day in the year */
136 136
 	_atp->t.tm_isdst = _tm->tm_isdst;   /* daylight saving time */
137
-	
137
+
138 138
 	_atp->mweek = ac_get_mweek(_tm);
139 139
 	_atp->yweek = ac_get_yweek(_tm);
140 140
 	_atp->ywday = ac_get_wday_yr(_tm);
... ...
@@ -144,10 +144,12 @@ int ac_tm_fill(ac_tm_t *_atp, struct tm* _tm)
144 144
 
145 145
 int ac_tm_set_time(ac_tm_t *_atp, time_t _t)
146 146
 {
147
+	struct tm tnow;
147 148
 	if(!_atp)
148 149
 		return -1;
149 150
 	_atp->time = _t;
150
-	return ac_tm_fill(_atp, localtime(&_t));
151
+	localtime_r(&_t, &tnow);
152
+	return ac_tm_fill(_atp, &tnow);
151 153
 }
152 154
 
153 155
 int ac_get_mweek(struct tm* _tm)
... ...
@@ -167,10 +169,10 @@ int ac_get_yweek(struct tm* _tm)
167 169
 #ifdef USE_YWEEK_V
168 170
 	int days;
169 171
 #endif
170
-	
172
+
171 173
 	if(!_tm)
172 174
 		return -1;
173
-	
175
+
174 176
 #ifdef USE_YWEEK_U
175 177
 	week = SUN_WEEK(_tm);
176 178
 #else
... ...
@@ -180,10 +182,10 @@ int ac_get_yweek(struct tm* _tm)
180 182
 #ifdef USE_YWEEK_V
181 183
 	days = ((_tm->tm_yday + 7 - (_tm->tm_wday ? _tm->tm_wday-1 : 6)) % 7);
182 184
 
183
-	if(days >= 4) 
185
+	if(days >= 4)
184 186
 		week++;
185
-	else 
186
-		if(week == 0) 
187
+	else
188
+		if(week == 0)
187 189
 			week = 53;
188 190
 #endif
189 191
 	return week;
... ...
@@ -236,7 +238,7 @@ ac_maxval_t *ac_get_maxval(ac_tm_t *_atp)
236 238
 	_amp = (ac_maxval_t*)pkg_malloc(sizeof(ac_maxval_t));
237 239
 	if(!_amp)
238 240
 		return NULL;
239
-	
241
+
240 242
 	/* the number of the days in the year */
241 243
 	_amp->yday = 365 + tr_is_leap_year(_atp->t.tm_year+1900);
242 244
 
... ...
@@ -255,7 +257,7 @@ ac_maxval_t *ac_get_maxval(ac_tm_t *_atp)
255 257
 		default:
256 258
 			_amp->mday = 31;
257 259
 	}
258
-	
260
+
259 261
 	/* maximum occurrences of a week day in the year */
260 262
 	memset(&_tm, 0, sizeof(struct tm));
261 263
 	_tm.tm_year = _atp->t.tm_year;
... ...
@@ -268,13 +270,13 @@ ac_maxval_t *ac_get_maxval(ac_tm_t *_atp)
268 270
 	else
269 271
 		_v = _tm.tm_wday - _atp->t.tm_wday;
270 272
 	_amp->ywday = (int)((_tm.tm_yday-_v)/7) + 1;
271
-	
273
+
272 274
 	/* maximum number of weeks in the year */
273 275
 	_amp->yweek = ac_get_yweek(&_tm) + 1;
274
-	
276
+
275 277
 	/* maximum number of the week day in the month */
276 278
 	_amp->mwday=(int)((_amp->mday-1-(_amp->mday-_atp->t.tm_mday)%7)/7)+1;
277
-	
279
+
278 280
 	/* maximum number of weeks in the month */
279 281
 	_v = (_atp->t.tm_wday + (_amp->mday - _atp->t.tm_mday)%7)%7;
280 282
 #ifdef USE_YWEEK_U
... ...
@@ -289,13 +291,13 @@ ac_maxval_t *ac_get_maxval(ac_tm_t *_atp)
289 291
 
290 292
 int ac_print(ac_tm_t *_atp)
291 293
 {
292
-	static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; 
294
+	static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
293 295
 	if(!_atp)
294 296
 	{
295 297
 		printf("\n(null)\n");
296 298
 		return -1;
297 299
 	}
298
-	
300
+
299 301
 	printf("\nSys time: %d\nTime: %02d:%02d:%02d\n", (int)_atp->time,
300 302
 				_atp->t.tm_hour, _atp->t.tm_min, _atp->t.tm_sec);
301 303
 	printf("Date: %s, %04d-%02d-%02d\n", _wdays[_atp->t.tm_wday],
... ...
@@ -346,10 +348,10 @@ int tr_byxxx_init(tr_byxxx_t *_bxp, int _nr)
346 348
 		_bxp->xxx = NULL;
347 349
 		return -1;
348 350
 	}
349
-	
351
+
350 352
 	memset(_bxp->xxx, 0, _nr*sizeof(int));
351 353
 	memset(_bxp->req, 0, _nr*sizeof(int));
352
-	
354
+
353 355
 	return 0;
354 356
 }
355 357
 
... ...
@@ -381,7 +383,7 @@ int tmrec_free(tmrec_t *_trp)
381 383
 {
382 384
 	if(!_trp)
383 385
 		return -1;
384
-	
386
+
385 387
 	tr_byxxx_free(_trp->byday);
386 388
 	tr_byxxx_free(_trp->bymday);
387 389
 	tr_byxxx_free(_trp->byyday);
... ...
@@ -396,7 +398,7 @@ int tmrec_destroy(tmrec_t *_trp)
396 398
 {
397 399
 	if(!_trp)
398 400
 		return -1;
399
-	
401
+
400 402
 	tr_byxxx_free(_trp->byday);
401 403
 	tr_byxxx_free(_trp->bymday);
402 404
 	tr_byxxx_free(_trp->byyday);
... ...
@@ -481,7 +483,7 @@ int tr_parse_byday(tmrec_t *_trp, char *_in)
481 483
 {
482 484
 	if(!_trp || !_in)
483 485
 		return -1;
484
-	_trp->byday = ic_parse_byday(_in); 
486
+	_trp->byday = ic_parse_byday(_in);
485 487
 	return 0;
486 488
 }
487 489
 
... ...
@@ -489,7 +491,7 @@ int tr_parse_bymday(tmrec_t *_trp, char *_in)
489 491
 {
490 492
 	if(!_trp || !_in)
491 493
 		return -1;
492
-	_trp->bymday = ic_parse_byxxx(_in); 
494
+	_trp->bymday = ic_parse_byxxx(_in);
493 495
 	return 0;
494 496
 }
495 497
 
... ...
@@ -497,7 +499,7 @@ int tr_parse_byyday(tmrec_t *_trp, char *_in)
497 499
 {
498 500
 	if(!_trp || !_in)
499 501
 		return -1;
500
-	_trp->byyday = ic_parse_byxxx(_in); 
502
+	_trp->byyday = ic_parse_byxxx(_in);
501 503
 	return 0;
502 504
 }
503 505
 
... ...
@@ -505,7 +507,7 @@ int tr_parse_bymonth(tmrec_t *_trp, char *_in)
505 507
 {
506 508
 	if(!_trp || !_in)
507 509
 		return -1;
508
-	_trp->bymonth = ic_parse_byxxx(_in); 
510
+	_trp->bymonth = ic_parse_byxxx(_in);
509 511
 	return 0;
510 512
 }
511 513
 
... ...
@@ -513,7 +515,7 @@ int tr_parse_byweekno(tmrec_t *_trp, char *_in)
513 515
 {
514 516
 	if(!_trp || !_in)
515 517
 		return -1;
516
-	_trp->byweekno = ic_parse_byxxx(_in); 
518
+	_trp->byweekno = ic_parse_byxxx(_in);
517 519
 	return 0;
518 520
 }
519 521
 
... ...
@@ -527,9 +529,9 @@ int tr_parse_wkst(tmrec_t *_trp, char *_in)
527 529
 
528 530
 int tr_print(tmrec_t *_trp)
529 531
 {
530
-	static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; 
532
+	static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
531 533
 	int i;
532
-	
534
+
533 535
 	if(!_trp)
534 536
 	{
535 537
 		printf("\n(null)\n");
... ...
@@ -537,7 +539,7 @@ int tr_print(tmrec_t *_trp)
537 539
 	}
538 540
 	printf("Recurrence definition\n-- start time ---\n");
539 541
 	printf("Sys time: %d\n", (int)_trp->dtstart);
540
-	printf("Time: %02d:%02d:%02d\n", _trp->ts.tm_hour, 
542
+	printf("Time: %02d:%02d:%02d\n", _trp->ts.tm_hour,
541 543
 				_trp->ts.tm_min, _trp->ts.tm_sec);
542 544
 	printf("Date: %s, %04d-%02d-%02d\n", _wdays[_trp->ts.tm_wday],
543 545
 				_trp->ts.tm_year+1900, _trp->ts.tm_mon+1, _trp->ts.tm_mday);
... ...
@@ -590,9 +592,9 @@ time_t ic_parse_datetime(char *_in, struct tm *_tm)
590 592
 {
591 593
 	if(!_in || !_tm || strlen(_in)!=15)
592 594
 		return 0;
593
-	
595
+
594 596
 	memset(_tm, 0, sizeof(struct tm));
595
-	_tm->tm_year = _D(_in[0])*1000 + _D(_in[1])*100 
597
+	_tm->tm_year = _D(_in[0])*1000 + _D(_in[1])*100
596 598
 			+ _D(_in[2])*10 + _D(_in[3]) - 1900;
597 599
 	_tm->tm_mon = _D(_in[4])*10 + _D(_in[5]) - 1;
598 600
 	_tm->tm_mday = _D(_in[6])*10 + _D(_in[7]);
... ...
@@ -608,10 +610,10 @@ time_t ic_parse_duration(char *_in)
608 610
 	time_t _t, _ft;
609 611
 	char *_p;
610 612
 	int _fl;
611
-	
613
+
612 614
 	if(!_in || (*_in!='+' && *_in!='-' && *_in!='P' && *_in!='p'))
613 615
 		return 0;
614
-	
616
+
615 617
 	if(*_in == 'P' || *_in=='p')
616 618
 		_p = _in+1;
617 619
 	else
... ...
@@ -620,10 +622,10 @@ time_t ic_parse_duration(char *_in)
620 622
 			return 0;
621 623
 		_p = _in+2;
622 624
 	}
623
-	
625
+
624 626
 	_t = _ft = 0;
625 627
 	_fl = 1;
626
-	
628
+
627 629
 	while(*_p)
628 630
 	{
629 631
 		switch(*_p)
... ...
@@ -634,7 +636,7 @@ time_t ic_parse_duration(char *_in)
634 636
 			case '9':
635 637
 				_t = _t*10 + *_p - '0';
636 638
 			break;
637
-			
639
+
638 640
 			case 'w':
639 641
 			case 'W':
640 642
 				if(!_fl)
... ...
@@ -722,7 +724,7 @@ tr_byxxx_t *ic_parse_byday(char *_in)
722 724
 			case '9':
723 725
 				_v = _v*10 + *_p - '0';
724 726
 			break;
725
-			
727
+
726 728
 			case 's':
727 729
 			case 'S':
728 730
 				_p++;
... ...
@@ -855,7 +857,7 @@ tr_byxxx_t *ic_parse_byxxx(char *_in)
855 857
 			case '9':
856 858
 				_v = _v*10 + *_p - '0';
857 859
 			break;
858
-			
860
+
859 861
 			case '-':
860 862
 				_s = -1;
861 863
 			break;
... ...
@@ -891,7 +893,7 @@ int ic_parse_wkst(char *_in)
891 893
 {
892 894
 	if(!_in || strlen(_in)!=2)
893 895
 		goto error;
894
-	
896
+
895 897
 	switch(_in[0])
896 898
 	{
897 899
 		case 's':
... ...
@@ -939,7 +941,7 @@ int ic_parse_wkst(char *_in)
939 941
 		default:
940 942
 			goto error;
941 943
 	}
942
-	
944
+
943 945
 error:
944 946
 #ifdef USE_YWEEK_U
945 947
 	return WDAY_SU;
... ...
@@ -979,11 +981,11 @@ int tr_check_recurrence(tmrec_t *_trp, ac_tm_t *_atp, tr_res_t *_tsw)
979 981
 	/* it is before start date */
980 982
 	if(_atp->time < _trp->dtstart)
981 983
 		return REC_NOMATCH;
982
-	
984
+
983 985
 	/* compute the duration of the recurrence interval */
984 986
 	if(!_IS_SET(_trp->duration))
985 987
 		_trp->duration = _trp->dtend - _trp->dtstart;
986
-	
988
+
987 989
 	if(_atp->time <= _trp->dtstart+_trp->duration)
988 990
 	{
989 991
 		if(_tsw)
... ...
@@ -1001,11 +1003,11 @@ int tr_check_recurrence(tmrec_t *_trp, ac_tm_t *_atp, tr_res_t *_tsw)
1001 1003
 		}
1002 1004
 		return REC_MATCH;
1003 1005
 	}
1004
-	
1006
+
1005 1007
 	/* after the bound of recurrence */
1006 1008
 	if(_IS_SET(_trp->until) && _atp->time >= _trp->until + _trp->duration)
1007 1009
 		return REC_NOMATCH;
1008
-	
1010
+
1009 1011
 	/* check if the instance of recurrence matches the 'interval' */
1010 1012
 	if(check_freq_interval(_trp, _atp)!=REC_MATCH)
1011 1013
 		return REC_NOMATCH;
... ...
@@ -1026,13 +1028,13 @@ int check_freq_interval(tmrec_t *_trp, ac_tm_t *_atp)
1026 1028
 	struct tm _tm;
1027 1029
 	if(!_trp || !_atp)
1028 1030
 		return REC_ERR;
1029
-	
1031
+
1030 1032
 	if(!_IS_SET(_trp->freq))
1031 1033
 		return REC_NOMATCH;
1032
-	
1034
+
1033 1035
 	if(!_IS_SET(_trp->interval) || _trp->interval==1)
1034 1036
 		return REC_MATCH;
1035
-	
1037
+
1036 1038
 	switch(_trp->freq)
1037 1039
 	{
1038 1040
 		case FREQ_DAILY:
... ...
@@ -1067,7 +1069,7 @@ int check_freq_interval(tmrec_t *_trp, ac_tm_t *_atp)
1067 1069
 			return ((_atp->t.tm_year-_trp->ts.tm_year)%_trp->interval==0)?
1068 1070
 					REC_MATCH:REC_NOMATCH;
1069 1071
 	}
1070
-	
1072
+
1071 1073
 	return REC_NOMATCH;
1072 1074
 }
1073 1075
 
... ...
@@ -1075,16 +1077,16 @@ int get_min_interval(tmrec_t *_trp)
1075 1077
 {
1076 1078
 	if(!_trp)
1077 1079
 		return FREQ_NOFREQ;
1078
-	
1080
+
1079 1081
 	if(_trp->freq == FREQ_DAILY || _trp->byday || _trp->bymday || _trp->byyday)
1080 1082
 		return FREQ_DAILY;
1081
-	if(_trp->freq == FREQ_WEEKLY || _trp->byweekno) 
1083
+	if(_trp->freq == FREQ_WEEKLY || _trp->byweekno)
1082 1084
 		return FREQ_WEEKLY;
1083 1085
 	if(_trp->freq == FREQ_MONTHLY || _trp->bymonth)
1084 1086
 		return FREQ_MONTHLY;
1085 1087
 	if(_trp->freq == FREQ_YEARLY)
1086 1088
 		return FREQ_YEARLY;
1087
-	
1089
+
1088 1090
 	return FREQ_NOFREQ;
1089 1091
 }
1090 1092
 
... ...
@@ -1106,7 +1108,7 @@ int check_min_unit(tmrec_t *_trp, ac_tm_t *_atp, tr_res_t *_tsw)
1106 1108
 				return REC_NOMATCH;
1107 1109
 		break;
1108 1110
 		case FREQ_YEARLY:
1109
-			if(_trp->ts.tm_mon != _atp->t.tm_mon 
1111
+			if(_trp->ts.tm_mon != _atp->t.tm_mon
1110 1112
 					|| _trp->ts.tm_mday != _atp->t.tm_mday)
1111 1113
 				return REC_NOMATCH;
1112 1114
 		break;
... ...
@@ -1132,7 +1134,7 @@ int check_min_unit(tmrec_t *_trp, ac_tm_t *_atp, tr_res_t *_tsw)
1132 1134
 		}
1133 1135
 		return REC_MATCH;
1134 1136
 	}
1135
-	
1137
+
1136 1138
 	return REC_NOMATCH;
1137 1139
 }
1138 1140
 
... ...
@@ -1142,19 +1144,19 @@ int check_byxxx(tmrec_t *_trp, ac_tm_t *_atp)
1142 1144
 	ac_maxval_t *_amp = NULL;
1143 1145
 	if(!_trp || !_atp)
1144 1146
 		return REC_ERR;
1145
-	if(!_trp->byday && !_trp->bymday && !_trp->byyday && !_trp->bymonth 
1147
+	if(!_trp->byday && !_trp->bymday && !_trp->byyday && !_trp->bymonth
1146 1148
 			&& !_trp->byweekno)
1147 1149
 		return REC_MATCH;
1148
-	
1150
+
1149 1151
 	_amp = ac_get_maxval(_atp);
1150 1152
 	if(!_amp)
1151 1153
 		return REC_NOMATCH;
1152
-	
1154
+
1153 1155
 	if(_trp->bymonth)
1154 1156
 	{
1155 1157
 		for(i=0; i<_trp->bymonth->nr; i++)
1156 1158
 		{
1157
-			if(_atp->t.tm_mon == 
1159
+			if(_atp->t.tm_mon ==
1158 1160
 					(_trp->bymonth->xxx[i]*_trp->bymonth->req[i]+12)%12)
1159 1161
 				break;
1160 1162
 		}
... ...
@@ -1207,7 +1209,7 @@ int check_byxxx(tmrec_t *_trp, ac_tm_t *_atp)
1207 1209
 			{
1208 1210
 #ifdef EXTRA_DEBUG
1209 1211
 				DBG("Req:byday:y: %d==%d && %d==%d\n", _atp->t.tm_wday,
1210
-					_trp->byday->xxx[i], _atp->ywday+1, 
1212
+					_trp->byday->xxx[i], _atp->ywday+1,
1211 1213
 					(_trp->byday->req[i]+_amp->ywday)%_amp->ywday);
1212 1214
 #endif
1213 1215
 				if(_atp->t.tm_wday == _trp->byday->xxx[i] &&
... ...
@@ -1221,7 +1223,7 @@ int check_byxxx(tmrec_t *_trp, ac_tm_t *_atp)
1221 1223
 				{
1222 1224
 #ifdef EXTRA_DEBUG
1223 1225
 					DBG("Req:byday:m: %d==%d && %d==%d\n", _atp->t.tm_wday,
1224
-						_trp->byday->xxx[i], _atp->mwday+1, 
1226
+						_trp->byday->xxx[i], _atp->mwday+1,
1225 1227
 						(_trp->byday->req[i]+_amp->mwday)%_amp->mwday);
1226 1228
 #endif
1227 1229
 					if(_atp->t.tm_wday == _trp->byday->xxx[i] &&