core: utils/tmrec - use safer localtime_r() instead of localtime()
authorDaniel-Constantin Mierla <miconda@gmail.com>
Sun, 3 May 2020 09:05:45 +0000 (11:05 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Sun, 3 May 2020 09:38:59 +0000 (11:38 +0200)
- coherent whitespacing and indentation

src/core/utils/tmrec.c

index 7195a63..609aa78 100644 (file)
@@ -62,17 +62,17 @@ static inline char* tr_trim(char* _s)
        int len;
        char* end;
 
-            /* Null pointer, there is nothing to do */
+       /* Null pointer, there is nothing to do */
        if (!_s) return _s;
 
-            /* Remove spaces and tabs from the beginning of string */
+       /* Remove spaces and tabs from the beginning of string */
        while ((*_s == ' ') || (*_s == '\t')) _s++;
 
        len = strlen(_s);
 
-        end = _s + len - 1;
+       end = _s + len - 1;
 
-            /* Remove trailing spaces and tabs */
+       /* Remove trailing spaces and tabs */
        while ((*end == ' ') || (*end == '\t')) end--;
        if (end != (_s + len - 1)) {
                *(end+1) = '\0';
@@ -117,7 +117,7 @@ ac_tm_t *ac_tm_new(void)
        if(!_atp)
                return NULL;
        memset(_atp, 0, sizeof(ac_tm_t));
-       
+
        return _atp;
 }
 
@@ -134,7 +134,7 @@ int ac_tm_fill(ac_tm_t *_atp, struct tm* _tm)
        _atp->t.tm_wday = _tm->tm_wday;     /* day of the week */
        _atp->t.tm_yday = _tm->tm_yday;     /* day in the year */
        _atp->t.tm_isdst = _tm->tm_isdst;   /* daylight saving time */
-       
+
        _atp->mweek = ac_get_mweek(_tm);
        _atp->yweek = ac_get_yweek(_tm);
        _atp->ywday = ac_get_wday_yr(_tm);
@@ -144,10 +144,12 @@ int ac_tm_fill(ac_tm_t *_atp, struct tm* _tm)
 
 int ac_tm_set_time(ac_tm_t *_atp, time_t _t)
 {
+       struct tm tnow;
        if(!_atp)
                return -1;
        _atp->time = _t;
-       return ac_tm_fill(_atp, localtime(&_t));
+       localtime_r(&_t, &tnow);
+       return ac_tm_fill(_atp, &tnow);
 }
 
 int ac_get_mweek(struct tm* _tm)
@@ -167,10 +169,10 @@ int ac_get_yweek(struct tm* _tm)
 #ifdef USE_YWEEK_V
        int days;
 #endif
-       
+
        if(!_tm)
                return -1;
-       
+
 #ifdef USE_YWEEK_U
        week = SUN_WEEK(_tm);
 #else
@@ -180,10 +182,10 @@ int ac_get_yweek(struct tm* _tm)
 #ifdef USE_YWEEK_V
        days = ((_tm->tm_yday + 7 - (_tm->tm_wday ? _tm->tm_wday-1 : 6)) % 7);
 
-       if(days >= 4) 
+       if(days >= 4)
                week++;
-       else 
-               if(week == 0) 
+       else
+               if(week == 0)
                        week = 53;
 #endif
        return week;
@@ -236,7 +238,7 @@ ac_maxval_t *ac_get_maxval(ac_tm_t *_atp)
        _amp = (ac_maxval_t*)pkg_malloc(sizeof(ac_maxval_t));
        if(!_amp)
                return NULL;
-       
+
        /* the number of the days in the year */
        _amp->yday = 365 + tr_is_leap_year(_atp->t.tm_year+1900);
 
@@ -255,7 +257,7 @@ ac_maxval_t *ac_get_maxval(ac_tm_t *_atp)
                default:
                        _amp->mday = 31;
        }
-       
+
        /* maximum occurrences of a week day in the year */
        memset(&_tm, 0, sizeof(struct tm));
        _tm.tm_year = _atp->t.tm_year;
@@ -268,13 +270,13 @@ ac_maxval_t *ac_get_maxval(ac_tm_t *_atp)
        else
                _v = _tm.tm_wday - _atp->t.tm_wday;
        _amp->ywday = (int)((_tm.tm_yday-_v)/7) + 1;
-       
+
        /* maximum number of weeks in the year */
        _amp->yweek = ac_get_yweek(&_tm) + 1;
-       
+
        /* maximum number of the week day in the month */
        _amp->mwday=(int)((_amp->mday-1-(_amp->mday-_atp->t.tm_mday)%7)/7)+1;
-       
+
        /* maximum number of weeks in the month */
        _v = (_atp->t.tm_wday + (_amp->mday - _atp->t.tm_mday)%7)%7;
 #ifdef USE_YWEEK_U
@@ -289,13 +291,13 @@ ac_maxval_t *ac_get_maxval(ac_tm_t *_atp)
 
 int ac_print(ac_tm_t *_atp)
 {
-       static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; 
+       static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
        if(!_atp)
        {
                printf("\n(null)\n");
                return -1;
        }
-       
+
        printf("\nSys time: %d\nTime: %02d:%02d:%02d\n", (int)_atp->time,
                                _atp->t.tm_hour, _atp->t.tm_min, _atp->t.tm_sec);
        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)
                _bxp->xxx = NULL;
                return -1;
        }
-       
+
        memset(_bxp->xxx, 0, _nr*sizeof(int));
        memset(_bxp->req, 0, _nr*sizeof(int));
-       
+
        return 0;
 }
 
@@ -381,7 +383,7 @@ int tmrec_free(tmrec_t *_trp)
 {
        if(!_trp)
                return -1;
-       
+
        tr_byxxx_free(_trp->byday);
        tr_byxxx_free(_trp->bymday);
        tr_byxxx_free(_trp->byyday);
@@ -396,7 +398,7 @@ int tmrec_destroy(tmrec_t *_trp)
 {
        if(!_trp)
                return -1;
-       
+
        tr_byxxx_free(_trp->byday);
        tr_byxxx_free(_trp->bymday);
        tr_byxxx_free(_trp->byyday);
@@ -481,7 +483,7 @@ int tr_parse_byday(tmrec_t *_trp, char *_in)
 {
        if(!_trp || !_in)
                return -1;
-       _trp->byday = ic_parse_byday(_in); 
+       _trp->byday = ic_parse_byday(_in);
        return 0;
 }
 
@@ -489,7 +491,7 @@ int tr_parse_bymday(tmrec_t *_trp, char *_in)
 {
        if(!_trp || !_in)
                return -1;
-       _trp->bymday = ic_parse_byxxx(_in); 
+       _trp->bymday = ic_parse_byxxx(_in);
        return 0;
 }
 
@@ -497,7 +499,7 @@ int tr_parse_byyday(tmrec_t *_trp, char *_in)
 {
        if(!_trp || !_in)
                return -1;
-       _trp->byyday = ic_parse_byxxx(_in); 
+       _trp->byyday = ic_parse_byxxx(_in);
        return 0;
 }
 
@@ -505,7 +507,7 @@ int tr_parse_bymonth(tmrec_t *_trp, char *_in)
 {
        if(!_trp || !_in)
                return -1;
-       _trp->bymonth = ic_parse_byxxx(_in); 
+       _trp->bymonth = ic_parse_byxxx(_in);
        return 0;
 }
 
@@ -513,7 +515,7 @@ int tr_parse_byweekno(tmrec_t *_trp, char *_in)
 {
        if(!_trp || !_in)
                return -1;
-       _trp->byweekno = ic_parse_byxxx(_in); 
+       _trp->byweekno = ic_parse_byxxx(_in);
        return 0;
 }
 
@@ -527,9 +529,9 @@ int tr_parse_wkst(tmrec_t *_trp, char *_in)
 
 int tr_print(tmrec_t *_trp)
 {
-       static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; 
+       static char *_wdays[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
        int i;
-       
+
        if(!_trp)
        {
                printf("\n(null)\n");
@@ -537,7 +539,7 @@ int tr_print(tmrec_t *_trp)
        }
        printf("Recurrence definition\n-- start time ---\n");
        printf("Sys time: %d\n", (int)_trp->dtstart);
-       printf("Time: %02d:%02d:%02d\n", _trp->ts.tm_hour, 
+       printf("Time: %02d:%02d:%02d\n", _trp->ts.tm_hour,
                                _trp->ts.tm_min, _trp->ts.tm_sec);
        printf("Date: %s, %04d-%02d-%02d\n", _wdays[_trp->ts.tm_wday],
                                _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)
 {
        if(!_in || !_tm || strlen(_in)!=15)
                return 0;
-       
+
        memset(_tm, 0, sizeof(struct tm));
-       _tm->tm_year = _D(_in[0])*1000 + _D(_in[1])*100 
+       _tm->tm_year = _D(_in[0])*1000 + _D(_in[1])*100
                        + _D(_in[2])*10 + _D(_in[3]) - 1900;
        _tm->tm_mon = _D(_in[4])*10 + _D(_in[5]) - 1;
        _tm->tm_mday = _D(_in[6])*10 + _D(_in[7]);
@@ -608,10 +610,10 @@ time_t ic_parse_duration(char *_in)
        time_t _t, _ft;
        char *_p;
        int _fl;
-       
+
        if(!_in || (*_in!='+' && *_in!='-' && *_in!='P' && *_in!='p'))
                return 0;
-       
+
        if(*_in == 'P' || *_in=='p')
                _p = _in+1;
        else
@@ -620,10 +622,10 @@ time_t ic_parse_duration(char *_in)
                        return 0;
                _p = _in+2;
        }
-       
+
        _t = _ft = 0;
        _fl = 1;
-       
+
        while(*_p)
        {
                switch(*_p)
@@ -634,7 +636,7 @@ time_t ic_parse_duration(char *_in)
                        case '9':
                                _t = _t*10 + *_p - '0';
                        break;
-                       
+
                        case 'w':
                        case 'W':
                                if(!_fl)
@@ -722,7 +724,7 @@ tr_byxxx_t *ic_parse_byday(char *_in)
                        case '9':
                                _v = _v*10 + *_p - '0';
                        break;
-                       
+
                        case 's':
                        case 'S':
                                _p++;
@@ -855,7 +857,7 @@ tr_byxxx_t *ic_parse_byxxx(char *_in)
                        case '9':
                                _v = _v*10 + *_p - '0';
                        break;
-                       
+
                        case '-':
                                _s = -1;
                        break;
@@ -891,7 +893,7 @@ int ic_parse_wkst(char *_in)
 {
        if(!_in || strlen(_in)!=2)
                goto error;
-       
+
        switch(_in[0])
        {
                case 's':
@@ -939,7 +941,7 @@ int ic_parse_wkst(char *_in)
                default:
                        goto error;
        }
-       
+
 error:
 #ifdef USE_YWEEK_U
        return WDAY_SU;
@@ -979,11 +981,11 @@ int tr_check_recurrence(tmrec_t *_trp, ac_tm_t *_atp, tr_res_t *_tsw)
        /* it is before start date */
        if(_atp->time < _trp->dtstart)
                return REC_NOMATCH;
-       
+
        /* compute the duration of the recurrence interval */
        if(!_IS_SET(_trp->duration))
                _trp->duration = _trp->dtend - _trp->dtstart;
-       
+
        if(_atp->time <= _trp->dtstart+_trp->duration)
        {
                if(_tsw)
@@ -1001,11 +1003,11 @@ int tr_check_recurrence(tmrec_t *_trp, ac_tm_t *_atp, tr_res_t *_tsw)
                }
                return REC_MATCH;
        }
-       
+
        /* after the bound of recurrence */
        if(_IS_SET(_trp->until) && _atp->time >= _trp->until + _trp->duration)
                return REC_NOMATCH;
-       
+
        /* check if the instance of recurrence matches the 'interval' */
        if(check_freq_interval(_trp, _atp)!=REC_MATCH)
                return REC_NOMATCH;
@@ -1026,13 +1028,13 @@ int check_freq_interval(tmrec_t *_trp, ac_tm_t *_atp)
        struct tm _tm;
        if(!_trp || !_atp)
                return REC_ERR;
-       
+
        if(!_IS_SET(_trp->freq))
                return REC_NOMATCH;
-       
+
        if(!_IS_SET(_trp->interval) || _trp->interval==1)
                return REC_MATCH;
-       
+
        switch(_trp->freq)
        {
                case FREQ_DAILY:
@@ -1067,7 +1069,7 @@ int check_freq_interval(tmrec_t *_trp, ac_tm_t *_atp)
                        return ((_atp->t.tm_year-_trp->ts.tm_year)%_trp->interval==0)?
                                        REC_MATCH:REC_NOMATCH;
        }
-       
+
        return REC_NOMATCH;
 }
 
@@ -1075,16 +1077,16 @@ int get_min_interval(tmrec_t *_trp)
 {
        if(!_trp)
                return FREQ_NOFREQ;
-       
+
        if(_trp->freq == FREQ_DAILY || _trp->byday || _trp->bymday || _trp->byyday)
                return FREQ_DAILY;
-       if(_trp->freq == FREQ_WEEKLY || _trp->byweekno) 
+       if(_trp->freq == FREQ_WEEKLY || _trp->byweekno)
                return FREQ_WEEKLY;
        if(_trp->freq == FREQ_MONTHLY || _trp->bymonth)
                return FREQ_MONTHLY;
        if(_trp->freq == FREQ_YEARLY)
                return FREQ_YEARLY;
-       
+
        return FREQ_NOFREQ;
 }
 
@@ -1106,7 +1108,7 @@ int check_min_unit(tmrec_t *_trp, ac_tm_t *_atp, tr_res_t *_tsw)
                                return REC_NOMATCH;
                break;
                case FREQ_YEARLY:
-                       if(_trp->ts.tm_mon != _atp->t.tm_mon 
+                       if(_trp->ts.tm_mon != _atp->t.tm_mon
                                        || _trp->ts.tm_mday != _atp->t.tm_mday)
                                return REC_NOMATCH;
                break;
@@ -1132,7 +1134,7 @@ int check_min_unit(tmrec_t *_trp, ac_tm_t *_atp, tr_res_t *_tsw)
                }
                return REC_MATCH;
        }
-       
+
        return REC_NOMATCH;
 }
 
@@ -1142,19 +1144,19 @@ int check_byxxx(tmrec_t *_trp, ac_tm_t *_atp)
        ac_maxval_t *_amp = NULL;
        if(!_trp || !_atp)
                return REC_ERR;
-       if(!_trp->byday && !_trp->bymday && !_trp->byyday && !_trp->bymonth 
+       if(!_trp->byday && !_trp->bymday && !_trp->byyday && !_trp->bymonth
                        && !_trp->byweekno)
                return REC_MATCH;
-       
+
        _amp = ac_get_maxval(_atp);
        if(!_amp)
                return REC_NOMATCH;
-       
+
        if(_trp->bymonth)
        {
                for(i=0; i<_trp->bymonth->nr; i++)
                {
-                       if(_atp->t.tm_mon == 
+                       if(_atp->t.tm_mon ==
                                        (_trp->bymonth->xxx[i]*_trp->bymonth->req[i]+12)%12)
                                break;
                }
@@ -1207,7 +1209,7 @@ int check_byxxx(tmrec_t *_trp, ac_tm_t *_atp)
                        {
 #ifdef EXTRA_DEBUG
                                DBG("Req:byday:y: %d==%d && %d==%d\n", _atp->t.tm_wday,
-                                       _trp->byday->xxx[i], _atp->ywday+1, 
+                                       _trp->byday->xxx[i], _atp->ywday+1,
                                        (_trp->byday->req[i]+_amp->ywday)%_amp->ywday);
 #endif
                                if(_atp->t.tm_wday == _trp->byday->xxx[i] &&
@@ -1221,7 +1223,7 @@ int check_byxxx(tmrec_t *_trp, ac_tm_t *_atp)
                                {
 #ifdef EXTRA_DEBUG
                                        DBG("Req:byday:m: %d==%d && %d==%d\n", _atp->t.tm_wday,
-                                               _trp->byday->xxx[i], _atp->mwday+1, 
+                                               _trp->byday->xxx[i], _atp->mwday+1,
                                                (_trp->byday->req[i]+_amp->mwday)%_amp->mwday);
 #endif
                                        if(_atp->t.tm_wday == _trp->byday->xxx[i] &&