| 1 |
/* |
| 2 |
* Copyright (C) 2005, 2007, 2009-2025 Free Software Foundation, Inc. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 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 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Jim Meyering and Bruno Haible. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <sys/time.h> |
| 22 |
|
| 23 |
#include "signature.h" |
| 24 |
SIGNATURE_CHECK (gettimeofday, int, |
| 25 |
(struct timeval *, GETTIMEOFDAY_TIMEZONE *)); |
| 26 |
|
| 27 |
#include <time.h> |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "macros.h" |
| 34 |
|
| 35 |
static void |
| 36 |
test_clobber () |
| 37 |
{ |
| 38 |
time_t t = 0; |
| 39 |
struct tm *lt; |
| 40 |
struct tm saved_lt; |
| 41 |
struct timeval tv; |
| 42 |
lt = localtime (&t); |
| 43 |
saved_lt = *lt; |
| 44 |
gettimeofday (&tv, NULL); |
| 45 |
if (memcmp (lt, &saved_lt, sizeof (struct tm)) != 0) |
| 46 |
{ |
| 47 |
fprintf (stderr, "gettimeofday still clobbers the localtime buffer!\n"); |
| 48 |
exit (1); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
test_consistency () |
| 54 |
{ |
| 55 |
struct timeval tv1; |
| 56 |
time_t tt2; |
| 57 |
struct timeval tv3; |
| 58 |
time_t tt4; |
| 59 |
|
| 60 |
ASSERT (gettimeofday (&tv1, NULL) == 0); |
| 61 |
tt2 = time (NULL); |
| 62 |
ASSERT (gettimeofday (&tv3, NULL) == 0); |
| 63 |
tt4 = time (NULL); |
| 64 |
|
| 65 |
/* Verify monotonicity of gettimeofday(). */ |
| 66 |
ASSERT (tv1.tv_sec < tv3.tv_sec |
| 67 |
|| (tv1.tv_sec == tv3.tv_sec && tv1.tv_usec <= tv3.tv_usec)); |
| 68 |
|
| 69 |
/* Verify monotonicity of time(). */ |
| 70 |
ASSERT (tt2 <= tt4); |
| 71 |
|
| 72 |
/* Verify that the tv_sec field of the result is the same as time(NULL). */ |
| 73 |
/* Note: It's here that the dependency to the 'time' module is needed. |
| 74 |
Without it, this assertion would sometimes fail on glibc systems, see |
| 75 |
https://sourceware.org/bugzilla/show_bug.cgi?id=30200 */ |
| 76 |
ASSERT (tv1.tv_sec <= tt2); |
| 77 |
ASSERT (tt2 <= tv3.tv_sec); |
| 78 |
ASSERT (tv3.tv_sec <= tt4); |
| 79 |
} |
| 80 |
|
| 81 |
int |
| 82 |
main (void) |
| 83 |
{ |
| 84 |
test_clobber (); |
| 85 |
test_consistency (); |
| 86 |
|
| 87 |
return test_exit_status; |
| 88 |
} |