1*4c3eb207Smrg /* Copyright (C) 2002-2020 Free Software Foundation, Inc.
2627f7eb2Smrg Contributed by Paul Brook
3627f7eb2Smrg
4627f7eb2Smrg This file is part of the GNU Fortran runtime library (libgfortran).
5627f7eb2Smrg
6627f7eb2Smrg Libgfortran is free software; you can redistribute it and/or modify
7627f7eb2Smrg it under the terms of the GNU General Public License as published by
8627f7eb2Smrg the Free Software Foundation; either version 3, or (at your option)
9627f7eb2Smrg any later version.
10627f7eb2Smrg
11627f7eb2Smrg Libgfortran is distributed in the hope that it will be useful,
12627f7eb2Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
13627f7eb2Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14627f7eb2Smrg GNU General Public License for more details.
15627f7eb2Smrg
16627f7eb2Smrg Under Section 7 of GPL version 3, you are granted additional
17627f7eb2Smrg permissions described in the GCC Runtime Library Exception, version
18627f7eb2Smrg 3.1, as published by the Free Software Foundation.
19627f7eb2Smrg
20627f7eb2Smrg You should have received a copy of the GNU General Public License and
21627f7eb2Smrg a copy of the GCC Runtime Library Exception along with this program;
22627f7eb2Smrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23627f7eb2Smrg <http://www.gnu.org/licenses/>. */
24627f7eb2Smrg
25627f7eb2Smrg #include "libgfortran.h"
26627f7eb2Smrg #include <string.h>
27627f7eb2Smrg #include <strings.h>
28627f7eb2Smrg
29627f7eb2Smrg
30627f7eb2Smrg /* Given a fortran string, return its length exclusive of the trailing
31627f7eb2Smrg spaces. */
32627f7eb2Smrg
33627f7eb2Smrg gfc_charlen_type
fstrlen(const char * string,gfc_charlen_type len)34627f7eb2Smrg fstrlen (const char *string, gfc_charlen_type len)
35627f7eb2Smrg {
36627f7eb2Smrg for (; len > 0; len--)
37627f7eb2Smrg if (string[len-1] != ' ')
38627f7eb2Smrg break;
39627f7eb2Smrg
40627f7eb2Smrg return len;
41627f7eb2Smrg }
42627f7eb2Smrg
43627f7eb2Smrg
44627f7eb2Smrg /* Copy a Fortran string (not null-terminated, hence length arguments
45627f7eb2Smrg for both source and destination strings. Returns the non-padded
46627f7eb2Smrg length of the destination. */
47627f7eb2Smrg
48627f7eb2Smrg gfc_charlen_type
fstrcpy(char * dest,gfc_charlen_type destlen,const char * src,gfc_charlen_type srclen)49627f7eb2Smrg fstrcpy (char *dest, gfc_charlen_type destlen,
50627f7eb2Smrg const char *src, gfc_charlen_type srclen)
51627f7eb2Smrg {
52627f7eb2Smrg if (srclen >= destlen)
53627f7eb2Smrg {
54627f7eb2Smrg /* This will truncate if too long. */
55627f7eb2Smrg memcpy (dest, src, destlen);
56627f7eb2Smrg return destlen;
57627f7eb2Smrg }
58627f7eb2Smrg else
59627f7eb2Smrg {
60627f7eb2Smrg memcpy (dest, src, srclen);
61627f7eb2Smrg /* Pad with spaces. */
62627f7eb2Smrg memset (&dest[srclen], ' ', destlen - srclen);
63627f7eb2Smrg return srclen;
64627f7eb2Smrg }
65627f7eb2Smrg }
66627f7eb2Smrg
67627f7eb2Smrg
68627f7eb2Smrg /* Copy a null-terminated C string to a non-null-terminated Fortran
69627f7eb2Smrg string. Returns the non-padded length of the destination string. */
70627f7eb2Smrg
71627f7eb2Smrg gfc_charlen_type
cf_strcpy(char * dest,gfc_charlen_type dest_len,const char * src)72627f7eb2Smrg cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src)
73627f7eb2Smrg {
74627f7eb2Smrg size_t src_len;
75627f7eb2Smrg
76627f7eb2Smrg src_len = strlen (src);
77627f7eb2Smrg
78627f7eb2Smrg if (src_len >= (size_t) dest_len)
79627f7eb2Smrg {
80627f7eb2Smrg /* This will truncate if too long. */
81627f7eb2Smrg memcpy (dest, src, dest_len);
82627f7eb2Smrg return dest_len;
83627f7eb2Smrg }
84627f7eb2Smrg else
85627f7eb2Smrg {
86627f7eb2Smrg memcpy (dest, src, src_len);
87627f7eb2Smrg /* Pad with spaces. */
88627f7eb2Smrg memset (&dest[src_len], ' ', dest_len - src_len);
89627f7eb2Smrg return src_len;
90627f7eb2Smrg }
91627f7eb2Smrg }
92627f7eb2Smrg
93627f7eb2Smrg
94627f7eb2Smrg #ifndef HAVE_STRNLEN
95627f7eb2Smrg static size_t
strnlen(const char * s,size_t maxlen)96627f7eb2Smrg strnlen (const char *s, size_t maxlen)
97627f7eb2Smrg {
98627f7eb2Smrg for (size_t ii = 0; ii < maxlen; ii++)
99627f7eb2Smrg {
100627f7eb2Smrg if (s[ii] == '\0')
101627f7eb2Smrg return ii;
102627f7eb2Smrg }
103627f7eb2Smrg return maxlen;
104627f7eb2Smrg }
105627f7eb2Smrg #endif
106627f7eb2Smrg
107627f7eb2Smrg
108627f7eb2Smrg #ifndef HAVE_STRNDUP
109627f7eb2Smrg static char *
strndup(const char * s,size_t n)110627f7eb2Smrg strndup (const char *s, size_t n)
111627f7eb2Smrg {
112627f7eb2Smrg size_t len = strnlen (s, n);
113627f7eb2Smrg char *p = malloc (len + 1);
114627f7eb2Smrg if (!p)
115627f7eb2Smrg return NULL;
116627f7eb2Smrg memcpy (p, s, len);
117627f7eb2Smrg p[len] = '\0';
118627f7eb2Smrg return p;
119627f7eb2Smrg }
120627f7eb2Smrg #endif
121627f7eb2Smrg
122627f7eb2Smrg
123627f7eb2Smrg /* Duplicate a non-null-terminated Fortran string to a malloced
124627f7eb2Smrg null-terminated C string. */
125627f7eb2Smrg
126627f7eb2Smrg char *
fc_strdup(const char * src,gfc_charlen_type src_len)127627f7eb2Smrg fc_strdup (const char *src, gfc_charlen_type src_len)
128627f7eb2Smrg {
129627f7eb2Smrg gfc_charlen_type n = fstrlen (src, src_len);
130627f7eb2Smrg char *p = strndup (src, n);
131627f7eb2Smrg if (!p)
132627f7eb2Smrg os_error ("Memory allocation failed in fc_strdup");
133627f7eb2Smrg return p;
134627f7eb2Smrg }
135627f7eb2Smrg
136627f7eb2Smrg
137627f7eb2Smrg /* Duplicate a non-null-terminated Fortran string to a malloced
138627f7eb2Smrg null-terminated C string, without getting rid of trailing
139627f7eb2Smrg blanks. */
140627f7eb2Smrg
141627f7eb2Smrg char *
fc_strdup_notrim(const char * src,gfc_charlen_type src_len)142627f7eb2Smrg fc_strdup_notrim (const char *src, gfc_charlen_type src_len)
143627f7eb2Smrg {
144627f7eb2Smrg char *p = strndup (src, src_len);
145627f7eb2Smrg if (!p)
146627f7eb2Smrg os_error ("Memory allocation failed in fc_strdup");
147627f7eb2Smrg return p;
148627f7eb2Smrg }
149627f7eb2Smrg
150627f7eb2Smrg
151627f7eb2Smrg /* Given a fortran string and an array of st_option structures, search through
152627f7eb2Smrg the array to find a match. If the option is not found, we generate an error
153627f7eb2Smrg if no default is provided. */
154627f7eb2Smrg
155627f7eb2Smrg int
find_option(st_parameter_common * cmp,const char * s1,gfc_charlen_type s1_len,const st_option * opts,const char * error_message)156627f7eb2Smrg find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
157627f7eb2Smrg const st_option * opts, const char *error_message)
158627f7eb2Smrg {
159627f7eb2Smrg /* Strip trailing blanks from the Fortran string. */
160627f7eb2Smrg size_t len = (size_t) fstrlen (s1, s1_len);
161627f7eb2Smrg
162627f7eb2Smrg for (; opts->name; opts++)
163627f7eb2Smrg if (len == strlen(opts->name) && strncasecmp (s1, opts->name, len) == 0)
164627f7eb2Smrg return opts->value;
165627f7eb2Smrg
166627f7eb2Smrg generate_error (cmp, LIBERROR_BAD_OPTION, error_message);
167627f7eb2Smrg
168627f7eb2Smrg return -1;
169627f7eb2Smrg }
170627f7eb2Smrg
171627f7eb2Smrg
172627f7eb2Smrg /* gfc_itoa()-- Integer to decimal conversion.
173627f7eb2Smrg The itoa function is a widespread non-standard extension to
174627f7eb2Smrg standard C, often declared in <stdlib.h>. Even though the itoa
175627f7eb2Smrg defined here is a static function we take care not to conflict with
176627f7eb2Smrg any prior non-static declaration. Hence the 'gfc_' prefix, which
177627f7eb2Smrg is normally reserved for functions with external linkage. Notably,
178627f7eb2Smrg in contrast to the *printf() family of functions, this ought to be
179627f7eb2Smrg async-signal-safe. */
180627f7eb2Smrg
181627f7eb2Smrg const char *
gfc_itoa(GFC_INTEGER_LARGEST n,char * buffer,size_t len)182627f7eb2Smrg gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len)
183627f7eb2Smrg {
184627f7eb2Smrg int negative;
185627f7eb2Smrg char *p;
186627f7eb2Smrg GFC_UINTEGER_LARGEST t;
187627f7eb2Smrg
188627f7eb2Smrg if (len < GFC_ITOA_BUF_SIZE)
189627f7eb2Smrg sys_abort ();
190627f7eb2Smrg
191627f7eb2Smrg if (n == 0)
192627f7eb2Smrg return "0";
193627f7eb2Smrg
194627f7eb2Smrg negative = 0;
195627f7eb2Smrg t = n;
196627f7eb2Smrg if (n < 0)
197627f7eb2Smrg {
198627f7eb2Smrg negative = 1;
199627f7eb2Smrg t = -n; /*must use unsigned to protect from overflow*/
200627f7eb2Smrg }
201627f7eb2Smrg
202627f7eb2Smrg p = buffer + GFC_ITOA_BUF_SIZE - 1;
203627f7eb2Smrg *p = '\0';
204627f7eb2Smrg
205627f7eb2Smrg while (t != 0)
206627f7eb2Smrg {
207627f7eb2Smrg *--p = '0' + (t % 10);
208627f7eb2Smrg t /= 10;
209627f7eb2Smrg }
210627f7eb2Smrg
211627f7eb2Smrg if (negative)
212627f7eb2Smrg *--p = '-';
213627f7eb2Smrg return p;
214627f7eb2Smrg }
215