1 /* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 Contributed by Paul Brook
4 This file is part of the GNU Fortran runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
31 /* Given a fortran string, return its length exclusive of the trailing
35 fstrlen (const char *string
, gfc_charlen_type len
)
37 for (; len
> 0; len
--)
38 if (string
[len
-1] != ' ')
45 /* Copy a Fortran string (not null-terminated, hence length arguments
46 for both source and destination strings. Returns the non-padded
47 length of the destination. */
50 fstrcpy (char *dest
, gfc_charlen_type destlen
,
51 const char *src
, gfc_charlen_type srclen
)
53 if (srclen
>= destlen
)
55 /* This will truncate if too long. */
56 memcpy (dest
, src
, destlen
);
61 memcpy (dest
, src
, srclen
);
62 /* Pad with spaces. */
63 memset (&dest
[srclen
], ' ', destlen
- srclen
);
69 /* Copy a null-terminated C string to a non-null-terminated Fortran
70 string. Returns the non-padded length of the destination string. */
73 cf_strcpy (char *dest
, gfc_charlen_type dest_len
, const char *src
)
77 src_len
= strlen (src
);
79 if (src_len
>= (size_t) dest_len
)
81 /* This will truncate if too long. */
82 memcpy (dest
, src
, dest_len
);
87 memcpy (dest
, src
, src_len
);
88 /* Pad with spaces. */
89 memset (&dest
[src_len
], ' ', dest_len
- src_len
);
97 strnlen (const char *s
, size_t maxlen
)
99 for (size_t ii
= 0; ii
< maxlen
; ii
++)
111 strndup (const char *s
, size_t n
)
113 size_t len
= strnlen (s
, n
);
114 char *p
= malloc (len
+ 1);
124 /* Duplicate a non-null-terminated Fortran string to a malloced
125 null-terminated C string. */
128 fc_strdup (const char *src
, gfc_charlen_type src_len
)
130 gfc_charlen_type n
= fstrlen (src
, src_len
);
131 char *p
= strndup (src
, n
);
133 os_error ("Memory allocation failed in fc_strdup");
138 /* Duplicate a non-null-terminated Fortran string to a malloced
139 null-terminated C string, without getting rid of trailing
143 fc_strdup_notrim (const char *src
, gfc_charlen_type src_len
)
145 char *p
= strndup (src
, src_len
);
147 os_error ("Memory allocation failed in fc_strdup");
152 /* Given a fortran string and an array of st_option structures, search through
153 the array to find a match. If the option is not found, we generate an error
154 if no default is provided. */
157 find_option (st_parameter_common
*cmp
, const char *s1
, gfc_charlen_type s1_len
,
158 const st_option
* opts
, const char *error_message
)
160 /* Strip trailing blanks from the Fortran string. */
161 size_t len
= (size_t) fstrlen (s1
, s1_len
);
163 for (; opts
->name
; opts
++)
164 if (len
== strlen(opts
->name
) && strncasecmp (s1
, opts
->name
, len
) == 0)
167 generate_error (cmp
, LIBERROR_BAD_OPTION
, error_message
);
173 /* gfc_itoa()-- Integer to decimal conversion.
174 The itoa function is a widespread non-standard extension to
175 standard C, often declared in <stdlib.h>. Even though the itoa
176 defined here is a static function we take care not to conflict with
177 any prior non-static declaration. Hence the 'gfc_' prefix, which
178 is normally reserved for functions with external linkage. Notably,
179 in contrast to the *printf() family of functions, this ought to be
180 async-signal-safe. */
183 gfc_itoa (GFC_INTEGER_LARGEST n
, char *buffer
, size_t len
)
187 GFC_UINTEGER_LARGEST t
;
189 if (len
< GFC_ITOA_BUF_SIZE
)
200 t
= -n
; /*must use unsigned to protect from overflow*/
203 p
= buffer
+ GFC_ITOA_BUF_SIZE
- 1;
208 *--p
= '0' + (t
% 10);