This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: [PATCH] time: Always set the passed variable if any
- From: Jeff Johnston <jjohnstn at redhat dot com>
- To: BenoÃt ThÃbaudeau <benoit at wsystem dot com>
- Cc: newlib at sourceware dot org
- Date: Mon, 2 May 2016 16:13:08 -0400 (EDT)
- Subject: Re: [PATCH] time: Always set the passed variable if any
- Authentication-results: sourceware.org; auth=none
- References: <1461937496-31935-1-git-send-email-benoit at wsystem dot com>
Patch applied.
-- Jeff J.
----- Original Message -----
> If the passed t pointer is not a null pointer, always assign the return
> value to the object it points to, regardless of whether the return value
> is an error.
>
> This is what the GNU C Library does, and this is also the expected
> behavior according to the latest draft of the C programming language
> standard (C11 ISO/IEC 9899:201x WG14 N1570, dated 2011-04-12):
>
> Synopsis
> #include <time.h>
> time_t time(time_t *timer);
>
> Description
> The time function determines the current calendar time. The encoding
> of the value is unspecified.
>
> Returns
> The time function returns the implementationâs best approximation to
> the current calendar time. The value (time_t)(-1) is returned if the
> calendar time is not available. If timer is not a null pointer, the
> return value is also assigned to the object it points to.
>
> Signed-off-by: BenoÃt ThÃbaudeau <benoit@wsystem.com>
> ---
> newlib/libc/time/time.c | 13 ++++++-------
> 1 file changed, 6 insertions(+), 7 deletions(-)
>
> diff --git a/newlib/libc/time/time.c b/newlib/libc/time/time.c
> index 2506388..9de71d4 100644
> --- a/newlib/libc/time/time.c
> +++ b/newlib/libc/time/time.c
> @@ -43,11 +43,10 @@ _DEFUN (time, (t),
> {
> struct timeval now;
>
> - if (_gettimeofday_r (_REENT, &now, NULL) >= 0)
> - {
> - if (t)
> - *t = now.tv_sec;
> - return now.tv_sec;
> - }
> - return -1;
> + if (_gettimeofday_r (_REENT, &now, NULL) < 0)
> + now.tv_sec = (time_t) -1;
> +
> + if (t)
> + *t = now.tv_sec;
> + return now.tv_sec;
> }
> --
> 2.5.0
>
>