1 /* Implementation of the CTIME and FDATE g77 intrinsics.
2    Copyright (C) 2005-2013 Free Software Foundation, Inc.
3    Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
4 
5 This file is part of the GNU Fortran runtime library (libgfortran).
6 
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
11 
12 Libgfortran 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 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20 
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25 
26 #include "libgfortran.h"
27 
28 #include "time_1.h"
29 
30 #include <stdlib.h>
31 #include <string.h>
32 
33 
34 /* strftime-like function that fills a C string with %c format which
35    is identical to ctime in the default locale. As ctime and ctime_r
36    are poorly specified and their usage not recommended, the
37    implementation instead uses strftime.  */
38 
39 static size_t
strctime(char * s,size_t max,const time_t * timep)40 strctime (char *s, size_t max, const time_t *timep)
41 {
42   struct tm ltm;
43   int failed;
44   /* Some targets provide a localtime_r based on a draft of the POSIX
45      standard where the return type is int rather than the
46      standardized struct tm*.  */
47   __builtin_choose_expr (__builtin_classify_type (localtime_r (timep, &ltm))
48 			 == 5,
49 			 failed = localtime_r (timep, &ltm) == NULL,
50 			 failed = localtime_r (timep, &ltm) != 0);
51   if (failed)
52     return 0;
53   return strftime (s, max, "%c", &ltm);
54 }
55 
56 /* In the default locale, the date and time representation fits in 26
57    bytes. However, other locales might need more space.  */
58 #define CSZ 100
59 
60 extern void fdate (char **, gfc_charlen_type *);
61 export_proto(fdate);
62 
63 void
fdate(char ** date,gfc_charlen_type * date_len)64 fdate (char ** date, gfc_charlen_type * date_len)
65 {
66   time_t now = time(NULL);
67   *date = xmalloc (CSZ);
68   *date_len = strctime (*date, CSZ, &now);
69 }
70 
71 
72 extern void fdate_sub (char *, gfc_charlen_type);
73 export_proto(fdate_sub);
74 
75 void
fdate_sub(char * date,gfc_charlen_type date_len)76 fdate_sub (char * date, gfc_charlen_type date_len)
77 {
78   time_t now = time(NULL);
79   char *s = xmalloc (date_len + 1);
80   size_t n = strctime (s, date_len + 1, &now);
81   fstrcpy (date, date_len, s, n);
82   free (s);
83 }
84 
85 
86 
87 extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
88 export_proto_np(PREFIX(ctime));
89 
90 void
PREFIX(ctime)91 PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
92 {
93   time_t now = t;
94   *date = xmalloc (CSZ);
95   *date_len = strctime (*date, CSZ, &now);
96 }
97 
98 
99 extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
100 export_proto(ctime_sub);
101 
102 void
ctime_sub(GFC_INTEGER_8 * t,char * date,gfc_charlen_type date_len)103 ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
104 {
105   time_t now = *t;
106   char *s = xmalloc (date_len + 1);
107   size_t n = strctime (s, date_len + 1, &now);
108   fstrcpy (date, date_len, s, n);
109   free (s);
110 }
111