1 /* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
2 /* Balsa E-Mail Client
3  *
4  * Copyright (C) 1997-2013 Stuart Parmenter and others,
5  *                         See the file AUTHORS for a list.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20  * 02111-1307, USA.
21  */
22 
23 #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
24 # include "config.h"
25 #endif                          /* HAVE_CONFIG_H */
26 
27 #if !defined(HAVE_CTIME_R)     || \
28     !defined(HAVE_LOCALTIME_R) || \
29     !defined(HAVE_GMTIME_R)
30 
31 #include "missing.h"
32 
33 #include <string.h>
34 
35 #if BALSA_USE_THREADS
36 #include <pthread.h>
37 static pthread_mutex_t time_lock = PTHREAD_MUTEX_INITIALIZER;
38 #define LOCK(mutex)   pthread_mutex_lock(&mutex)
39 #define UNLOCK(mutex) pthread_mutex_unlock(&mutex)
40 #else
41 #define LOCK(mutex)
42 #define UNLOCK(mutex)
43 #endif /* BALSA_USE_THREADS */
44 
45 #ifndef HAVE_CTIME_R
46 char *
ctime_r(const time_t * clock,char * buf)47 ctime_r(const time_t *clock, char *buf)
48 {
49     LOCK(time_lock);
50     strcpy(buf, ctime(clock));
51     UNLOCK(time_lock);
52     return buf;
53 }
54 #endif
55 
56 
57 #ifndef HAVE_LOCALTIME_R
58 struct tm *
localtime_r(const time_t * clock,struct tm * result)59 localtime_r(const time_t *clock, struct tm *result)
60 {
61     LOCK(time_lock);
62     memcpy(result, localtime(clock), sizeof(struct tm));
63     UNLOCK(time_lock);
64     return result;
65 }
66 #endif
67 
68 
69 #ifndef HAVE_GMTIME_R
70 struct tm *
gmtime_r(const time_t * clock,struct tm * result)71 gmtime_r(const time_t *clock, struct tm *result)
72 {
73     LOCK(time_lock);
74     memcpy(result, gmtime(clock), sizeof(struct tm));
75     UNLOCK(time_lock);
76     return result;
77 }
78 #endif
79 #endif
80