set_timer.c
int conv_str2timeval(char *str, struct timeval *result)
{
    struct timeval tv;
    time_t t;
    char *cp;
    int i;

    //timespecclear(&tv);
    tv.tv_sec        = 0;
    tv.tv_usec       = 0;

    /* Handle whole seconds. */
    for (cp = str; *cp != '\0' && *cp != '.'; cp++) {
        if (!isdigit((unsigned char)*cp)) {
            warnx("seconds is invalid: %s", str);
            return -1;
        }
        t = (tv.tv_sec * 10) + (*cp - '0');
        if (t / 10 != tv.tv_sec) {   /* overflow */
            warnx("seconds is too large: %s", str);
            return -1;
        }

        tv.tv_sec = t;
    }

    /*
     * Handle fractions of a second.  The multiplier divides to zero
     * after nine digits so anything more precise than a microsecond is
     * validated but not used.
     */
    if (*cp == '.') {
        i = 100000;
        for (cp++; *cp != '\0'; cp++) {
            if (!isdigit((unsigned char)*cp)) {
                warnx("seconds is invalid: %s", str);
                return -1;
            }
            tv.tv_usec += (*cp - '0') * i;
            i /= 10;
        }
    }

    result->tv_sec  = tv.tv_sec;
    result->tv_usec = tv.tv_usec;

    return 0;
}
/* 
 * conv_str2timeval(char * str, struct timeval *result)
 * Convert decimal string to struct timeval
 * Result will be filled in struct timeval result
 * On error, return -1.
 * valid string example: 1.23, .23
 * invalid string example: a.12, 1.2a
 */
conv_str2timeval(char * str, struct timeval *result)
Convert decimal string to struct timeval
Result will be filled in struct timeval result
On error, return -1.
conv_str2timeval ( char * str struct timeval * result ) Convert decimal string to struct timeval Result will be filled in struct timeval result On error return - 1 .
int conv_str2timeval(char *str, struct timeval *result)
{
    struct timeval tv;
    time_t t;
    char *cp;
    int i;
    tv.tv_sec        = 0;
    tv.tv_usec       = 0;
    for (cp = str; *cp != '\0' && *cp != '.'; cp++) {
        if (!isdigit((unsigned char)*cp)) {
            warnx("seconds is invalid: %s", str);
            return -1;
        }
        t = (tv.tv_sec * 10) + (*cp - '0');
        if (t / 10 != tv.tv_sec) {   
            warnx("seconds is too large: %s", str);
            return -1;
        }
        tv.tv_sec = t;
    }
    if (*cp == '.') {
        i = 100000;
        for (cp++; *cp != '\0'; cp++) {
            if (!isdigit((unsigned char)*cp)) {
                warnx("seconds is invalid: %s", str);
                return -1;
            }
            tv.tv_usec += (*cp - '0') * i;
            i /= 10;
        }
    }
    result->tv_sec  = tv.tv_sec;
    result->tv_usec = tv.tv_usec;
    return 0;
}
int conv_str2timeval ( char * str , struct timeval * result ) { struct timeval tv ; time_t t ; char * cp ; int i ; tv . tv_sec = 0 ; tv . tv_usec = 0 ; for ( cp = str ; * cp != ' \0 ' && * cp != ' ' ; cp ++ ) { if ( ! isdigit ( ( unsigned char ) * cp ) ) { warnx ( " " , str ) ; return -1 ; } t = ( tv . tv_sec * 10 ) + ( * cp - ' ' ) ; if ( t / 10 != tv . tv_sec ) { warnx ( " " , str ) ; return -1 ; } tv . tv_sec = t ; } if ( * cp == ' ' ) { i = 100000 ; for ( cp ++ ; * cp != ' \0 ' ; cp ++ ) { if ( ! isdigit ( ( unsigned char ) * cp ) ) { warnx ( " " , str ) ; return -1 ; } tv . tv_usec += ( * cp - ' ' ) * i ; i /= 10 ; } } result -> tv_sec = tv . tv_sec ; result -> tv_usec = tv . tv_usec ; return 0 ; }
conv_str2timeval(char * str, struct timeval *result)
 Convert decimal string to struct timeval
 Result will be filled in struct timeval result
 On error, return -1.
 valid string example: 1.23, .23
 invalid string example: a.12, 1.2a
conv_str2timeval ( char * str struct timeval * result ) Convert decimal string to struct timeval Result will be filled in struct timeval result On error return - 1 . valid string example : 1 . 23 . 23 invalid string example : a . 12 1 . 2a
//timespecclear(&tv); /* Handle whole seconds. */ /* overflow */ /*
     * Handle fractions of a second.  The multiplier divides to zero
     * after nine digits so anything more precise than a microsecond is
     * validated but not used.
     */