xref: /netbsd-src/external/gpl2/xcvs/dist/lib/time_r.c (revision 5a6c14c844c4c665da5632061aebde7bb2cb5766)
1 /* Reentrant time functions like localtime_r.
2 
3    Copyright (C) 2003 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18 #include <sys/cdefs.h>
19 __RCSID("$NetBSD: time_r.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
20 
21 
22 /* Written by Paul Eggert.  */
23 
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 
28 #include "time_r.h"
29 
30 #include <string.h>
31 
32 static char *
copy_string_result(char * dest,char const * src)33 copy_string_result (char *dest, char const *src)
34 {
35   if (! src)
36     return 0;
37   return strcpy (dest, src);
38 }
39 
40 static struct tm *
copy_tm_result(struct tm * dest,struct tm const * src)41 copy_tm_result (struct tm *dest, struct tm const *src)
42 {
43   if (! src)
44     return 0;
45   *dest = *src;
46   return dest;
47 }
48 
49 
50 char *
asctime_r(struct tm const * restrict tm,char * restrict buf)51 asctime_r (struct tm const * restrict tm, char * restrict buf)
52 {
53   return copy_string_result (buf, asctime (tm));
54 }
55 
56 char *
ctime_r(time_t const * t,char * buf)57 ctime_r (time_t const *t, char *buf)
58 {
59   return copy_string_result (buf, ctime (t));
60 }
61 
62 struct tm *
gmtime_r(time_t const * restrict t,struct tm * restrict tp)63 gmtime_r (time_t const * restrict t, struct tm * restrict tp)
64 {
65   return copy_tm_result (tp, gmtime (t));
66 }
67 
68 struct tm *
localtime_r(time_t const * restrict t,struct tm * restrict tp)69 localtime_r (time_t const * restrict t, struct tm * restrict tp)
70 {
71   return copy_tm_result (tp, localtime (t));
72 }
73