Annotation of embedaddon/sudo/compat/clock_gettime.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2014 Todd C. Miller <Todd.Miller@courtesan.com>
        !             3:  *
        !             4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
        !             7:  *
        !             8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            15:  */
        !            16: 
        !            17: #include <config.h>
        !            18: 
        !            19: #if !defined(HAVE_CLOCK_GETTIME)
        !            20: 
        !            21: #include <sys/types.h>
        !            22: #include <sys/time.h>
        !            23: 
        !            24: #include <errno.h>
        !            25: #include <stdio.h>
        !            26: #ifdef TIME_WITH_SYS_TIME
        !            27: # include <time.h>
        !            28: #endif
        !            29: #ifndef HAVE_STRUCT_TIMESPEC
        !            30: # include "compat/timespec.h"
        !            31: #endif
        !            32: 
        !            33: #include "missing.h"
        !            34: 
        !            35: #ifdef __MACH__
        !            36: # include <mach/mach.h>
        !            37: # include <mach/mach_time.h>
        !            38: # include <mach/clock.h>
        !            39: #endif
        !            40: 
        !            41: /*
        !            42:  * Trivial clock_gettime() that supports CLOCK_REALTIME
        !            43:  * (and CLOCK_MONOTONIC on Mach).
        !            44:  */
        !            45: int
        !            46: clock_gettime(clockid_t clock_id, struct timespec *ts)
        !            47: {
        !            48: 
        !            49:     switch (clock_id) {
        !            50: #ifdef __MACH__
        !            51:     case CLOCK_MONOTONIC:
        !            52:        {
        !            53:            uint64_t abstime, nsec;
        !            54:            static mach_timebase_info_data_t timebase_info;
        !            55: 
        !            56:            if (timebase_info.denom == 0)
        !            57:                (void) mach_timebase_info(&timebase_info);
        !            58:            abstime = mach_absolute_time();
        !            59:            nsec = abstime * timebase_info.numer / timebase_info.denom;
        !            60:            ts->tv_sec = nsec / 1000000000;
        !            61:            ts->tv_nsec = nsec % 1000000000;
        !            62:            return 0;
        !            63:        }
        !            64: #endif
        !            65:     case CLOCK_REALTIME:
        !            66:        {
        !            67:            struct timeval tv;
        !            68: 
        !            69:            gettimeofday(&tv, NULL);
        !            70:            ts->tv_sec = tv.tv_sec;
        !            71:            ts->tv_nsec = tv.tv_usec * 1000;
        !            72:            return 0;
        !            73:        }
        !            74:     default:
        !            75:        errno = EINVAL;
        !            76:        return -1;
        !            77:     }
        !            78: }
        !            79: 
        !            80: #endif /* !HAVE_CLOCK_GETTIME */

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