MCPcopy Create free account
hub / github.com/coreutils/coreutils / scan_arg

Function scan_arg

src/seq.c:156–236  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

154 Return if the string is correct else signal error. */
155
156static operand
157scan_arg (char const *arg)
158{
159 operand ret;
160
161 if (! xstrtold (arg, NULL, &ret.value, cl_strtold))
162 {
163 error (0, 0, _("invalid floating point argument: %s"), quote (arg));
164 usage (EXIT_FAILURE);
165 }
166
167 if (isnan (ret.value))
168 {
169 error (0, 0, _("invalid %s argument: %s"), quote_n (0, "not-a-number"),
170 quote_n (1, arg));
171 usage (EXIT_FAILURE);
172 }
173
174 /* We don't output spaces or '+' so don't include in width */
175 while (isspace (to_uchar (*arg)) || *arg == '+')
176 arg++;
177
178 /* Default to auto width and precision. */
179 ret.width = 0;
180 ret.precision = INT_MAX;
181
182 /* Use no precision (and possibly fast generation) for integers. */
183 char const *decimal_point = strchr (arg, '.');
184 if (! decimal_point && ! strchr (arg, 'p') /* not a hex float */)
185 ret.precision = 0;
186
187 /* auto set width and precision for decimal inputs. */
188 if (! arg[strcspn (arg, "xX")] && isfinite (ret.value))
189 {
190 size_t fraction_len = 0;
191 ret.width = strlen (arg);
192
193 if (decimal_point)
194 {
195 fraction_len = strcspn (decimal_point + 1, "eE");
196 if (fraction_len <= INT_MAX)
197 ret.precision = fraction_len;
198 ret.width += (fraction_len == 0 /* #. -> # */
199 ? -1
200 : (decimal_point == arg /* .# -> 0.# */
201 || !c_isdigit (decimal_point[-1]))); /* -.# -> 0.# */
202 }
203 char const *e = strchr (arg, 'e');
204 if (! e)
205 e = strchr (arg, 'E');
206 if (e)
207 {
208 long exponent = MAX (strtol (e + 1, NULL, 10), -LONG_MAX);
209 ret.precision += exponent < 0 ? -exponent
210 : - MIN (ret.precision, exponent);
211 /* Don't account for e.... in the width since this is not output. */
212 ret.width -= strlen (arg) - (e - arg);
213 /* Adjust the width as per the exponent. */

Callers 1

mainFunction · 0.85

Calls 2

to_ucharFunction · 0.85
usageFunction · 0.70

Tested by

no test coverage detected