Annotation of embedaddon/mtr/packet/timeval.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:     mtr  --  a network diagnostic tool
                      3:     Copyright (C) 2016  Matt Kimball
                      4: 
                      5:     This program is free software; you can redistribute it and/or modify
                      6:     it under the terms of the GNU General Public License version 2 as
                      7:     published by the Free Software Foundation.
                      8: 
                      9:     This program is distributed in the hope that it will be useful,
                     10:     but WITHOUT ANY WARRANTY; without even the implied warranty of
                     11:     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     12:     GNU General Public License for more details.
                     13: 
1.1.1.2 ! misho      14:     You should have received a copy of the GNU General Public License along
        !            15:     with this program; if not, write to the Free Software Foundation, Inc.,
        !            16:     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1.1       misho      17: */
                     18: 
                     19: #include "timeval.h"
                     20: 
                     21: /*
                     22:     Ensure that a timevalue has a microsecond value in the range
                     23:     [0.0, 1.0e6) microseconds by converting microseconds to full seconds.
                     24: */
                     25: void normalize_timeval(
                     26:     struct timeval *timeval)
                     27: {
                     28:     int full_sec;
                     29: 
                     30:     /*
                     31:        If tv_usec has overflowed a full second, convert the overflow
                     32:        to tv_sec.
                     33:      */
                     34:     full_sec = timeval->tv_usec / 1000000;
                     35:     timeval->tv_sec += full_sec;
                     36:     timeval->tv_usec -= 1000000 * full_sec;
                     37: 
                     38:     /*  If tv_usec is negative, make it positive by rolling tv_sec back  */
                     39:     if (timeval->tv_usec < 0) {
                     40:         timeval->tv_sec--;
                     41:         timeval->tv_usec += 1000000;
                     42:     }
                     43: 
                     44:     /*  If the entire time value is negative, clamp to zero  */
                     45:     if (timeval->tv_sec < 0) {
                     46:         timeval->tv_sec = 0;
                     47:         timeval->tv_usec = 0;
                     48:     }
                     49: }
                     50: 
                     51: /*
                     52:     Compare two time values.  Return:
                     53: 
                     54:         -1 if a < b
                     55:          0 if a == b
                     56:          1 if a > b
                     57: */
                     58: int compare_timeval(
                     59:     struct timeval a,
                     60:     struct timeval b)
                     61: {
                     62:     if (a.tv_sec > b.tv_sec) {
                     63:         return 1;
                     64:     }
                     65:     if (a.tv_sec < b.tv_sec) {
                     66:         return -1;
                     67:     }
                     68: 
                     69:     if (a.tv_usec > b.tv_usec) {
                     70:         return 1;
                     71:     }
                     72:     if (a.tv_usec < b.tv_usec) {
                     73:         return -1;
                     74:     }
                     75: 
                     76:     return 0;
                     77: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>