| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Manipulation of timeval structures |
| 4 | * |
| 5 | * (c) 1998 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 12 | * mLib is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * mLib is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with mLib; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include <sys/time.h> |
| 31 | #include "tv.h" |
| 32 | |
| 33 | /*----- Main code ---------------------------------------------------------*/ |
| 34 | |
| 35 | /* --- @tv_add@ --- * |
| 36 | * |
| 37 | * Arguments: @struct timeval *dst@ = destination block |
| 38 | * @const struct timeval *a, *b@ = source blocks |
| 39 | * |
| 40 | * Returns: --- |
| 41 | * |
| 42 | * Use: Adds two timevals. |
| 43 | */ |
| 44 | |
| 45 | void tv_add(struct timeval *dst, |
| 46 | const struct timeval *a, const struct timeval *b) |
| 47 | { |
| 48 | TV_ADD(dst, a, b); |
| 49 | } |
| 50 | |
| 51 | /* --- @tv_addl@ --- * |
| 52 | * |
| 53 | * Arguments: @struct timeval *dst@ = destination block |
| 54 | * @const struct timeval *a@ = source blocks |
| 55 | * @time_t sec@, @unsigned long usec@ = time to add |
| 56 | * |
| 57 | * Returns: --- |
| 58 | * |
| 59 | * Use: Adds a literal time in seconds and microseconds. |
| 60 | */ |
| 61 | |
| 62 | void tv_addl(struct timeval *dst, const struct timeval *a, |
| 63 | time_t sec, unsigned long usec) |
| 64 | { |
| 65 | TV_ADDL(dst, a, sec, usec); |
| 66 | } |
| 67 | |
| 68 | /* --- @tv_sub@ --- * |
| 69 | * |
| 70 | * Arguments: @struct timeval *dst@ = destination block |
| 71 | * @const struct timeval *a, *b@ = source blocks |
| 72 | * |
| 73 | * Returns: --- |
| 74 | * |
| 75 | * Use: Subtracts two timevals. |
| 76 | */ |
| 77 | |
| 78 | void tv_sub(struct timeval *dst, |
| 79 | const struct timeval *a, const struct timeval *b) |
| 80 | { |
| 81 | TV_SUB(dst, a, b); |
| 82 | } |
| 83 | |
| 84 | /* --- @tv_subl@ --- * |
| 85 | * |
| 86 | * Arguments: @struct timeval *dst@ = destination block |
| 87 | * @const struct timeval *a@ = source blocks |
| 88 | * @time_t sec@, @unsigned long usec@ = time to subtract |
| 89 | * |
| 90 | * Returns: --- |
| 91 | * |
| 92 | * Use: Subtracts a literal time in seconds and microseconds. |
| 93 | */ |
| 94 | |
| 95 | void tv_subl(struct timeval *dst, const struct timeval *a, |
| 96 | time_t sec, unsigned long usec) |
| 97 | { |
| 98 | TV_SUBL(dst, a, sec, usec); |
| 99 | } |
| 100 | |
| 101 | /* --- @tv_cmp@ --- * |
| 102 | * |
| 103 | * Arguments: @const struct timeval *a, *b@ = source blocks |
| 104 | * |
| 105 | * Returns: Less than, equal to, or greater than zero. |
| 106 | * |
| 107 | * Use: Compares two timevals. |
| 108 | */ |
| 109 | |
| 110 | int tv_cmp(const struct timeval *a, const struct timeval *b) |
| 111 | { |
| 112 | /* --- This is more awkward than the case the macro deals with --- */ |
| 113 | |
| 114 | if (a->tv_sec > b->tv_sec) |
| 115 | return (1); |
| 116 | else if (a->tv_sec < b->tv_sec) |
| 117 | return (-1); |
| 118 | else if (a->tv_usec > b->tv_usec) |
| 119 | return (1); |
| 120 | else if (a->tv_usec < b->tv_usec) |
| 121 | return (-1); |
| 122 | else |
| 123 | return (0); |
| 124 | } |
| 125 | |
| 126 | /*----- That's all, folks -------------------------------------------------*/ |