kernel - Implement sbrk(), change low-address mmap hinting
[dragonfly.git] / usr.bin / tip / tod.c
blobf585063fc966fb9729f5ca9a1979ee5f45cf9568
1 /*
2 * tod.c -- time of day pseudo-class implementation
4 * Copyright (c) 1995 John H. Poplett
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Absolutely no warranty of function or purpose is made by the author
17 * John H. Poplett.
18 * 4. This work was done expressly for inclusion into FreeBSD. Other use
19 * is allowed if this notation is included.
20 * 5. Modifications may be freely made to this file if the above conditions
21 * are met.
25 #include <sys/types.h>
26 #include <sys/time.h>
28 #include <assert.h>
29 #include <stdio.h>
31 #include "tod.h"
33 #define USP 1000000
35 int tod_cmp (const struct timeval *a, const struct timeval *b)
37 int rc;
38 assert (a->tv_usec <= USP);
39 assert (b->tv_usec <= USP);
40 rc = a->tv_sec - b->tv_sec;
41 if (rc == 0)
42 rc = a->tv_usec - b->tv_usec;
43 return rc;
47 TOD < command
49 int tod_lt (const struct timeval *a, const struct timeval *b)
51 return tod_cmp (a, b) < 0;
54 int tod_gt (const struct timeval *a, const struct timeval *b)
56 return tod_cmp (a, b) > 0;
59 int tod_lte (const struct timeval *a, const struct timeval *b)
61 return tod_cmp (a, b) <= 0;
64 int tod_gte (const struct timeval *a, const struct timeval *b)
66 return tod_cmp (a, b) >= 0;
69 int tod_eq (const struct timeval *a, const struct timeval *b)
71 return tod_cmp (a, b) == 0;
75 TOD += command
77 void tod_addto (struct timeval *a, const struct timeval *b)
79 a->tv_usec += b->tv_usec;
80 a->tv_sec += b->tv_sec + a->tv_usec / USP;
81 a->tv_usec %= USP;
85 TOD -= command
87 void tod_subfrom (struct timeval *a, struct timeval b)
89 assert (a->tv_usec <= USP);
90 assert (b.tv_usec <= USP);
91 if (b.tv_usec > a->tv_usec)
93 a->tv_usec += USP;
94 a->tv_sec -= 1;
96 a->tv_usec -= b.tv_usec;
97 a->tv_sec -= b.tv_sec;
100 void tod_gettime (struct timeval *tp)
102 gettimeofday (tp, NULL);
103 tp->tv_sec += tp->tv_usec / USP;
104 tp->tv_usec %= USP;
107 /* end of tod.c */