X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/db97733d2c8fabbaa8b73daae5669222d3c2b643..247239becbeb8f8f92d86e038de6315437679f71:/tv.c diff --git a/tv.c b/tv.c new file mode 100644 index 0000000..9548a13 --- /dev/null +++ b/tv.c @@ -0,0 +1,110 @@ +/* -*-c-*- + * + * $Id: tv.c,v 1.1 1998/11/25 23:30:01 mdw Exp $ + * + * Manipulation of timeval structures + * + * (c) 1998 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the mLib utilities library. + * + * mLib is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * mLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with mLib; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: tv.c,v $ + * Revision 1.1 1998/11/25 23:30:01 mdw + * New file. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include +#include "tv.h" + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @tv_add@ --- * + * + * Arguments: @struct timeval *dst@ = destination block + * @const struct timeval *a, *b@ = source blocks + * + * Returns: --- + * + * Use: Adds two timevals. + */ + +void tv_add(struct timeval *dst, + const struct timeval *a, const struct timeval *b) +{ + dst->tv_sec = a->tv_sec + b->tv_sec; + dst->tv_usec = a->tv_usec + b->tv_usec; + if (dst->tv_usec >= 1000000) { + dst->tv_usec -= 1000000; + dst->tv_sec++; + } +} + +/* --- @tv_sub@ --- * + * + * Arguments: @struct timeval *dst@ = destination block + * @const struct timeval *a, *b@ = source blocks + * + * Returns: --- + * + * Use: Subtracts two timevals. + */ + +void tv_sub(struct timeval *dst, + const struct timeval *a, const struct timeval *b) +{ + dst->tv_sec = a->tv_sec - b->tv_sec; + if (a->tv_usec >= b->tv_usec) + dst->tv_usec = a->tv_usec - b->tv_usec; + else { + dst->tv_usec = a->tv_usec + 1000000 - b->tv_usec; + dst->tv_sec--; + } +} + +/* --- @tv_cmp@ --- * + * + * Arguments: @const struct timeval *a, *b@ = source blocks + * + * Returns: Less than, equal to, or greater than zero. + * + * Use: Compares two timevals. + */ + +int tv_cmp(const struct timeval *a, const struct timeval *b) +{ + if (a->tv_sec > b->tv_sec) + return (1); + else if (a->tv_sec < b->tv_sec) + return (-1); + else if (a->tv_usec > b->tv_usec) + return (1); + else if (a->tv_usec < b->tv_usec) + return (-1); + else + return (0); +} + +/*----- That's all, folks -------------------------------------------------*/