OSDN Git Service

missed hunk from last commit
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / ctime.c
1 /* Implementation of the CTIME and FDATE g77 intrinsics.
2    Copyright (C) 2005, 2007, 2009, 2011 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 <string.h>
31
32
33 /* strftime-like function that fills a C string with %c format which
34    is identical to ctime in the default locale. As ctime and ctime_r
35    are poorly specified and their usage not recommended, the
36    implementation instead uses strftime.  */
37
38 static size_t
39 strctime (char *s, size_t max, const time_t *timep)
40 {
41 #ifdef HAVE_STRFTIME
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 #else
55   return 0;
56 #endif
57 }
58
59 /* In the default locale, the date and time representation fits in 26
60    bytes. However, other locales might need more space.  */
61 #define CSZ 100
62
63 extern void fdate (char **, gfc_charlen_type *);
64 export_proto(fdate);
65
66 void
67 fdate (char ** date, gfc_charlen_type * date_len)
68 {
69 #if defined(HAVE_TIME)
70   time_t now = time(NULL);
71   *date = get_mem (CSZ);
72   *date_len = strctime (*date, CSZ, &now);
73 #else
74
75   *date = NULL;
76   *date_len = 0;
77 #endif
78 }
79
80
81 extern void fdate_sub (char *, gfc_charlen_type);
82 export_proto(fdate_sub);
83
84 void
85 fdate_sub (char * date, gfc_charlen_type date_len)
86 {
87 #if defined(HAVE_TIME)
88   time_t now = time(NULL);
89   char *s = get_mem (date_len + 1);
90   size_t n = strctime (s, date_len + 1, &now);
91   fstrcpy (date, date_len, s, n);
92   free (s);
93 #else
94   memset (date, ' ', date_len);
95 #endif
96 }
97
98
99
100 extern void PREFIX(ctime) (char **, gfc_charlen_type *, GFC_INTEGER_8);
101 export_proto_np(PREFIX(ctime));
102
103 void
104 PREFIX(ctime) (char ** date, gfc_charlen_type * date_len, GFC_INTEGER_8 t)
105 {
106 #if defined(HAVE_TIME)
107   time_t now = t;
108   *date = get_mem (CSZ);
109   *date_len = strctime (*date, CSZ, &now);
110 #else
111
112   *date = NULL;
113   *date_len = 0;
114 #endif
115 }
116
117
118 extern void ctime_sub (GFC_INTEGER_8 *, char *, gfc_charlen_type);
119 export_proto(ctime_sub);
120
121 void
122 ctime_sub (GFC_INTEGER_8 * t, char * date, gfc_charlen_type date_len)
123 {
124   time_t now = *t;
125   char *s = get_mem (date_len + 1);
126   size_t n = strctime (s, date_len + 1, &now);
127   fstrcpy (date, date_len, s, n);
128   free (s);
129 }