Debian Codegraph @

texk/xdvik/string-utils.c

raw
1 /*
2 * miscellaneous string utility functions
3 *
4 * Copyright (c) 2001-2022 the xdvik development team
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /* #define MYDEBUG 1 */
26
27 #include "xdvi-config.h"
28 #include "xdvi.h"
29 #include "string-utils.h"
30 #include "util.h"
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <locale.h>
35 #include <ctype.h>
36
37 /*------------------------------------------------------------
38 * str_is_prefix
39 *
40 * Purpose:
41 * Return True if <str1> is a prefix of <str2>, else False.
42 *
43 * If `case_sensitive' is set to False, it performs matching
44 * in a case-insensitive manner; in that case, str1 should
45 * be lowercase.
46 *------------------------------------------------------------*/
47
48 Boolean
49 str_is_prefix(const char *str1, const char *str2, Boolean case_sensitive)
50 {
51 int i;
52
53 for (i = 0; *str1 != '\0' && *str2 != '\0'; i++) {
54 if ((case_sensitive && *str1 != *str2) ||
55 (!case_sensitive && *str1 != tolower((int)*str2)))
56 return False;
57 str1++;
58 str2++;
59 }
60 return *str1 == '\0';
61 }
62
63
64
65 /*------------------------------------------------------------
66 * str_is_suffix
67 *
68 * Purpose:
69 * Return True if str1 is a suffix of str2, else False.
70 * E.g. returns true if str1 = ".tex", str2 = "test.tex".
71 *
72 * If `case_sensitive' is set to False, it performs matching
73 * in a case-insensitive manner; in that case, str1 should
74 * be lowercase.
75 *------------------------------------------------------------*/
76
77 Boolean
78 str_is_suffix(const char *str1, const char *str2, Boolean case_sensitive)
79 {
80 int len1 = strlen(str1);
81 int len2 = strlen(str2);
82
83 while (len2 > len1) {
84 str2++;
85 len2--;
86 }
87 if (case_sensitive)
88 return strcmp(str1, str2) == 0;
89 else
90 /* also compare terminating 0; second string
91 is assumed to be all-lowercase! */
92 return memicmp(str2, str1, len1 + 1) == 0;
93 }
94
95 /*
96 * Like strstr(), but does case-insensitive matching: Brute-force algorithm
97 * to find the first occurrence of subsrtring `needle' (which should be all-lowercase)
98 * in string `haystack', ignoring case in (i.e. lowercasing) haystack. The terminating
99 * '\0' characters are not compared.
100 * Returns a pointer to the beginning of the substring if found, NULL else.
101 *
102 * This code was derived from public domain code (Stristr.c on www.snippets.org).
103 * Currently unused.
104 */
105 char *
106 my_stristr(const char *haystack, const char *needle)
107 {
108 const char *curr;
109
110 for (curr = haystack; *curr != '\0'; curr++) {
111 const char *ptr1, *ptr2;
112 /* search for first character */
113 for (; ((*curr != '\0') && (tolower((int)*curr) != *needle)); curr++) { ; }
114
115 if (*curr == '\0') /* not found */
116 return NULL;
117
118 /* now compare other characters */
119 ptr1 = needle;
120 ptr2 = curr;
121 while (tolower((int)*ptr2) == *ptr1) {
122 ptr2++;
123 ptr1++;
124 /* success if at end of needle */
125 if (*ptr1 == '\0')
126 return (char *)curr;
127 }
128 }
129 return NULL;
130 }
131
132 /*
133 expand filename to include full path name;
134 returns result in a freshly allocated string.
135 */
136 char *
137 expand_filename(const char *filename, expandPathTypeT type)
138 {
139 char *path_name = NULL;
140
141 if (*filename == '/') /* already an absolute path */
142 return xstrdup(filename);
143
144 if (type == USE_CWD_PATH) {
145 size_t path_name_len = 512;
146 size_t len = strlen(filename) + 1;
147
148 /* append to cwd if it's not already a full path */
149 if (filename[0] != '/') {
150 for (;;) {
151 char *tmp;
152 path_name = xrealloc(path_name, path_name_len);
153 if ((tmp = getcwd(path_name, path_name_len)) == NULL && errno == ERANGE) {
154 path_name_len *= 2;
155 }
156 else {
157 path_name = tmp;
158 break;
159 }
160 }
161 len += strlen(path_name) + 1; /* 1 for '/' */
162 path_name = xrealloc(path_name, len);
163 strcat(path_name, "/");
164 strcat(path_name, filename);
165 }
166
167 TRACE_HTEX((stderr,
168 "Expanding filename |%s| with CWD gives |%s|",
169 filename, path_name == NULL ? "<NULL>" : path_name));
170 return path_name ? path_name : xstrdup(filename);
171 }
172 else {
173 ASSERT(globals.dvi_file.dirname != NULL, "globals.dvi_file.dirname should have been initialized before");
174 path_name = xstrdup(globals.dvi_file.dirname);
175 path_name = xstrcat(path_name, filename);
176 TRACE_HTEX((stderr,
177 "Expanding filename |%s| with globals.dvi_file.dirname |%s| gives |%s|",
178 filename, globals.dvi_file.dirname, path_name));
179 return path_name;
180 }
181 }
182
183 /* expand filename to include `.dvi' extension and full path name;
184 returns malloc()ed string (caller is responsible for free()ing).
185 */
186 char *
187 filename_append_dvi(const char *filename)
188 {
189 char *expanded_filename = NULL;
190 const char *orig_filename = filename;
191 size_t len;
192 char *p;
193
194 /* skip over `file:' prefix if present */
195 if (str_is_prefix("file:", filename, True)) {
196 filename += strlen("file:");
197 if (str_is_prefix("//", filename, True)) { /* is there a hostname following? */
198 char *tmp = strchr(filename+2, '/'); /* skip over host name */
199 if (tmp == NULL) {
200 XDVI_WARNING((stderr, "Malformed hostname part in filename `%s'; not expanding file name",
201 orig_filename));
202 }
203 else {
204 /* deal with multiple `//' after "file://localhost";
205 while the RFC seems to suggest that file://localhost/foo/bar defines a path
206 `foo/bar', most browsers (and actually, also libwww) will treat this path as absolute:
207 `/foo/bar'. So we treat
208 file://localhost//foo/bar
209 and
210 file://localhost/foo/bar
211 as equivalent.
212 */
213 while (*(tmp+1) == '/')
214 tmp++;
215 filename = tmp;
216 }
217 }
218 }
219
220 len = strlen(filename) + 5; /* 5 in case we need to add `.dvi\0' */
221
222 expanded_filename = xmalloc(len);
223
224 strcpy(expanded_filename, filename);
225
226 /* Append ".dvi" extension if no extension is present.
227 Only look at the filename part when trying to find a `.'.
228 */
229 if ((p = strrchr(expanded_filename, '/')) == NULL) {
230 p = expanded_filename;
231 }
232 if ((p = strrchr(p, '.')) == NULL) {
233 TRACE_HTEX((stderr, "appending .dvi extension to filename |%s|", expanded_filename));
234 strcat(expanded_filename, ".dvi");
235 }
236 return expanded_filename;
237 }
238
239 char *
240 expand_filename_append_dvi(const char *filename, expandPathTypeT type, Boolean must_exist)
241 {
242 char canonical_path[MAXPATHLEN + 1];
243 char *normalized_fname = filename_append_dvi(filename);
244 char *expanded_fname = expand_filename(normalized_fname, type);
245 if (must_exist) {
246 char *canonical_name = REALPATH(expanded_fname, canonical_path);
247 free(normalized_fname);
248 free(expanded_fname);
249 return xstrdup(canonical_name);
250 }
251 else {
252 free(normalized_fname);
253 return expanded_fname;
254 }
255 }
256
257 char *
258 format_arg(const char *fmt, const char *arg, int *match)
259 {
260 char *tmp = xmalloc(strlen(fmt) + strlen(arg) + 1);
261 if (strchr(fmt, '%') != NULL) {
262 sprintf(tmp, fmt, arg);
263 *match = 1;
264 }
265 else {
266 strcpy(tmp, fmt);
267 /* NOTE: don't reset *match to 0, leave that to caller */
268 }
269 return tmp;
270 }
271
272 /* escape single `%' characters in arg and return newly allocated string */
273 char *
274 escape_format_arg(const char *arg)
275 {
276 char *ret, *ptr;
277 ASSERT(arg != NULL, "");
278 ret = xmalloc(strlen(arg) * 2 + 1); /* more than enuff */
279
280 ptr = ret;
281 while (*arg != '\0') {
282 /* need to escape? */
283 if (*arg == '%') { /* && (ptr == ret
284 || (ptr > ret && *(arg - 1) != '%'))) { */
285 *ptr++ = '%';
286 }
287 *ptr++ = *arg++;
288 }
289 *ptr = '\0';
290
291 /* trim return buffer */
292 return xrealloc(ret, strlen(ret) + 1);
293 }
294
295 char *
296 unquote_arg(const char *fmt, const char *arg, int *match, int *len)
297 {
298 char *ptr;
299 char c;
300
301 c = fmt[0];
302 fmt++;
303 if ((ptr = strchr(fmt, c)) != NULL) {
304 *ptr++ = '\0';
305 while (*ptr == ' ' || *ptr == '\t') {
306 ptr++;
307 }
308 *len = ptr - fmt;
309 return format_arg(fmt, arg, match);
310 }
311 else {
312 *len = strlen(fmt);
313 XDVI_WARNING((stderr, "Ignoring lonesome quote in string %s", fmt - 1));
314 return format_arg(fmt, arg, match);
315 }
316 }
317
318 /* Chop `source' into chunks separated by `sep', and save these
319 * to newly allocated return list. The end of the list is indicated
320 * by a NULL element (i.e. the returned list will always contain at
321 * least 1 element). The caller is responsible for free()ing the returned
322 * list.
323 *
324 * If `do_unquote' is True, separators inside single or double quotation marks will not be
325 * treated as boundaries. The quotation marks surrounding the chunk will be removed
326 * as well in that case.
327 */
328 char **
329 get_separated_list(const char *source, const char *sep, Boolean do_unquote)
330 {
331 /* allocate at least list of size 1, for NULL termination below */
332 char **chunks = xmalloc(sizeof *chunks);
333 const char *b_ptr, *e_ptr;
334 size_t i = 0;
335
336 b_ptr = source;
337
338 while (*b_ptr != '\0' && strchr(sep, *b_ptr) != NULL)
339 b_ptr++;
340
341 for (i = 0; *b_ptr != '\0'; i++) {
342 char *quote;
343 char quotechar = 0;
344 size_t len, chunk_len, alloc_len = 0;
345
346 if ((len = strcspn(b_ptr, sep)) == 0) /* at end */
347 break;
348
349 /* check for quoted chunk, in which case we don't want to treat
350 spaces as chunk separators */
351 if (do_unquote && (quote = strchr("'\"", *b_ptr)) != NULL) {
352 const char *curr = b_ptr + 1;
353 quotechar = *quote;
354
355 for (;;) { /* find end of quote */
356 char *maybe_end = strchr(curr, quotechar);
357 if (maybe_end != NULL) {
358 if (maybe_end - curr > 0 &&
359 *(maybe_end - 1) == '\\') { /* escaped quote */
360 curr = ++maybe_end;
361 }
362 else { /* found */
363 b_ptr++;
364 len = maybe_end - b_ptr;
365 break;
366 }
367 }
368 else { /* not found end - warn, and forget about this quote */
369 XDVI_WARNING((stderr, "Unmatched quote character in string `%s'", b_ptr));
370 break;
371 }
372 }
373 }
374
375 e_ptr = b_ptr + len;
376 chunk_len = e_ptr - b_ptr;
377 while (i + 1 >= alloc_len) {
378 alloc_len++;
379 }
380 chunks = xrealloc(chunks, alloc_len * sizeof *chunks);
381 chunks[i] = xmalloc(chunk_len + 1);
382 memcpy(chunks[i], b_ptr, chunk_len);
383 chunks[i][chunk_len] = '\0';
384
385 /* skip trailing quotechar and spaces */
386 b_ptr = e_ptr;
387 if (do_unquote && quotechar != 0 && *b_ptr == quotechar)
388 b_ptr++;
389 while (*b_ptr != '\0' && strchr(sep, *b_ptr) != NULL)
390 b_ptr++;
391 }
392 /* terminate list with NULL element */
393 chunks[i] = NULL;
394 return chunks;
395 }
396
397 const char *
398 find_format_str(const char *input, const char *fmt)
399 {
400 const char *ptr = input;
401 while ((ptr = strstr(ptr, fmt)) != NULL) {
402 if (ptr > input && *(ptr - 1) == '\\') {
403 ptr++;
404 continue;
405 }
406 else
407 return ptr;
408 }
409 return NULL;
410 }
411
412 /* return directory component of `path', or NULL if path is a filename only */
413 char *
414 get_dir_component(const char *path)
415 {
416 char *ret = NULL;
417 char *p;
418
419 if ((p = strrchr(path, '/')) != NULL) {
420 /* copy, chop off filename (but not the '/') */
421 ret = xstrdup(path);
422 *(ret + (p - path) + 1) = '\0';
423 TRACE_CLIENT((stderr, "get_dir_component of |%s| is |%s|\n", path, ret));
424 }
425 return ret;
426 }
427
428 /*
429 If `src' is an absolute path, compare it with `target', ignoring `.tex' extension in
430 both strings. Else, compare the concatenation of `src' with `dvi_path' and `target',
431 in the same way.
432 Since efficiency is a concern here, we don't copy any strings; instead, we loop through
433 `src' and `target' from the end of both strings (which makes expanding `../' easier, and
434 will terminate earlier for non-equal files), replacing `src' with `dvi_path' when
435 dropping off the beginning of `src'.
436 */
437 Boolean
438 src_compare(const char *src, int src_len, const char *target, const char *dvi_path, size_t dvi_path_len)
439 {
440 int i, j;
441 Boolean matched = True;
442
443 /* This macro sets the `src' pointer to `dvi_path' after having
444 dropped off the beginning of src, or returns False if dvi_path
445 is NULL (which either means that `src' was an absolute path, or
446 that the dvi_path has been used up already).
447 */
448 #define CHK_USE_DIR(i) \
449 if (i == -1) { \
450 if (dvi_path == NULL) /* already done */ \
451 return False; \
452 src = dvi_path; \
453 dvi_path = NULL; \
454 i = dvi_path_len - 1; \
455 MYTRACE((stderr, "swapped, now using: |%s| of len %d", src, i)); \
456 }
457
458 /* ignore path in both filenames if one of them does not contain a path */
459 {
460 char *src_p = strrchr(src, '/');
461 char *target_p = strrchr(target, '/');
462
463 if (src_p == NULL) {
464 dvi_path = NULL;
465 if (target_p != NULL)
466 target = target_p + 1;
467 }
468
469 if (target_p == NULL) {
470 dvi_path = NULL;
471 if (src_p != NULL)
472 src = src_p + 1;
473 }
474 }
475
476 /* don't prepend dvi_path if src is absolute */
477 if (*src == '/') {
478 dvi_path = NULL;
479 }
480
481 /* skip `.tex' suffix in strings if present */
482 i = src_len;
483 MYTRACE((stderr, "len of |%s| %d", src, i));
484 if (i >= 4 && strcmp(src + (i - 4), ".tex") == 0) {
485 MYTRACE((stderr, "src |%s| has .tex suffix!", src));
486 i -= 4;
487 }
488
489 j = strlen(target);
490 MYTRACE((stderr, "len of |%s| %d", target, j));
491 if (j >= 4 && strcmp(target + (j - 4), ".tex") == 0) {
492 MYTRACE((stderr, "target |%s| has .tex suffix!", target));
493 j -= 4;
494 }
495
496 /* start with index of last char */
497 i--;
498 j--;
499
500 while (i >= 0 && j >= 0) {
501 int skip_dirs = 0;
502 /* fprintf(stderr, "check: %d[%c]\n", i, src[i]); */
503 while (src[i] == '.' && src[i + 1] == '/') { /* normalize `../' and `./' */
504 MYTRACE((stderr, "check2: %d[%c]", i, src[i]));
505
506 if (i >= 2 && src[i - 1] == '.' && src[i - 2] == '/') {
507 MYTRACE((stderr, "case /.."));
508 i -= 3;
509 skip_dirs++;
510 }
511 else if (i == 1) { /* `../' or `/./' at start of src */
512 if (src[0] == '.') { /* `../' */
513 MYTRACE((stderr, "../ at start"));
514 i -= 2;
515 skip_dirs++;
516 }
517 else if (src[0] == '/') { /* `/./' */
518 MYTRACE((stderr, "/./ at start"));
519 i -= 1;
520 }
521 else /* something else */
522 break;
523 }
524 else if (i == 0 || (i > 1 && src[i - 1] == '/')) { /* ./ at start, or /./ somewhere */
525 i -= 1;
526 }
527 else /* might be `abc./' or `abc../' (strange but legal) */
528 break;
529
530 CHK_USE_DIR(i);
531 while (i >= 0 && src[i] == '/') i--;
532 CHK_USE_DIR(i);
533
534 while (src[i] != '.' && skip_dirs-- > 0) { /* unless there are subsequent `../' */
535 /* skip directories backwards */
536 while (i >= 0 && src[i] != '/') {
537 MYTRACE((stderr, "non-slash: %d,%c", i, src[i]));
538 i--;
539 }
540 CHK_USE_DIR(i);
541 while (i >= 0 && src[i] == '/') {
542 MYTRACE((stderr, "slash: %d,%c", i, src[i]));
543 i--;
544 }
545 CHK_USE_DIR(i);
546 MYTRACE((stderr, "skipped backwards: %d,%c", i, src[i]));
547 }
548 }
549
550 /* skip multiple '/'. No need to fall off the beginning of src and use
551 CHK_USE_DIR() here, since with (multiple) '/' at the beginning, src
552 must be an absolute path. */
553 while (i > 0 && src[i] == '/' && src[i - 1] == '/') {
554 i--;
555 }
556
557 MYTRACE((stderr, "comparing: %d[%c] - %d[%c]", i, src[i], j, target[j]));
558 if (src[i] != target[j]) {
559 matched = False;
560 break;
561 }
562 if (i == 0 && j == 0) /* at start of both strings */
563 break;
564 i--;
565 j--;
566 CHK_USE_DIR(i);
567 }
568
569 if (i != 0 || j != 0) /* not at start of both strings, no match */
570 return False;
571 else
572 return matched;
573 #undef CHK_USE_DIR
574 }
575
576
577 /*
578 Contributed by ZLB: Return a canonicalized version of path (with `../'
579 and `./' resolved and `//' reduced to '/') in a freshly malloc()ed
580 buffer, which the caller is responsible for free()ing. Cannot fail.
581 */
582 char *
583 canonicalize_path(const char *path)
584 {
585 char *p, *q, *start;
586 char c;
587 size_t len = strlen(path);
588
589 assert(path != NULL);
590 assert(*path == '/');
591
592 start = q = p = xstrdup(path);
593
594 /* len is the length of string */
595 while (p < start + len) {
596 if ((c = p[1]) == '/') {
597 /* remove multiple '/' in pathname */
598 memmove(p + 1, p + 2, len - (p + 2 - start) + 1);
599 len--;
600 continue;
601 }
602 else if (c == '.') {
603 if ((c = p[2]) == '/') {
604 /* p = '/.' in pathname */
605 memmove(p + 1, p + 3, len - (p + 3 - start) + 1);
606 len -= 2;
607 continue;
608 }
609 else if (c == '.' && ((c = p[3]) == '/' || c == '\0')) {
610 /* p == "/.." */
611 memmove(q, p + 3, len - (p + 3 - start) + 1);
612 len -= (p - q) + 3;
613 p = q;
614 /* check if the new dirname at p is "//" or './' or '../' */
615 if ((c = p[1]) == '/')
616 continue;
617 else if (c == '.') {
618 if ((c = p[2]) == '/')
619 continue;
620 else if (c == '.' && ((c = p[3]) == '/' || c == '\0')) {
621 while (--q >= start && *q != '/');
622 if (q < start)
623 q = start;
624 continue;
625 }
626 }
627 }
628 }
629
630 /* search next '/' */
631 q = p;
632 while (++p <= start + len && *p != '/');
633 }
634
635 return start;
636 }
637
638 /* Escape shell metacharacters in str, hopefully making it safe to pass
639 str to system(), i.e., /bin/sh -c, without further quoting. Return
640 result in a newly allocated string, which the caller is responsible
641 to free() after use.
642 */
643 char *
644 shell_escape_string(const char *str)
645 {
646 size_t len = strlen(str);
647 char *new_str = xmalloc(len * 2 + 1); /* safe amount, since each char will be doubled at most */
648
649 const char *src_ptr = str;
650 char *target_ptr = new_str;
651 while (*src_ptr != '\0') {
652 if (*src_ptr == '\\'
653 || *src_ptr == '`'
654 || *src_ptr == '\''
655 || *src_ptr == '"'
656 || *src_ptr == '(' || *src_ptr == ')'
657 || *src_ptr == '{' || *src_ptr == '}'
658 || *src_ptr == '[' || *src_ptr == ']'
659 || *src_ptr == '<' || *src_ptr == '>'
660 || *src_ptr == '&'
661 || *src_ptr == '|'
662 || *src_ptr == '!'
663 || *src_ptr == '$'
664 || *src_ptr == '*'
665 || *src_ptr == '?'
666 || *src_ptr == ';'
667 || *src_ptr == ' ' || *src_ptr == '\t' || *src_ptr == '\n') {
668 #if 0
669 /* only if not yet escaped? */
670 && (src_ptr == str || (src_ptr > str && *(src_ptr - 1) != '\\'))) {
671 #endif
672 *target_ptr++ = '\\';
673 }
674 *target_ptr++ = *src_ptr++;
675 }
676 *target_ptr = '\0'; /* terminate */
677 return new_str;
678 }
679
680 /* Get a pointer to the extension of the filename 'fname'
681 (ie. into the existing string),
682 or NULL if fname doens't have an extension.
683 */
684 const char *
685 get_extension(const char *fname)
686 {
687 char *sep, *tmp;
688 /* does filename have a directory component?
689 If so, be careful with dots within this component.
690 */
691 if ((sep = strrchr(fname, '/')) != NULL) {
692 tmp = sep;
693 if ((sep = strrchr(tmp, '.')) != NULL) {
694 return sep;
695 }
696 else {
697 return NULL;
698 }
699 }
700 else if ((sep = strrchr(fname, '.')) != NULL) {
701 return sep;
702 }
703 else {
704 return NULL;
705 }
706 }
707
708 void
709 replace_extension(const char *fname, const char *extension,
710 char *buf, size_t buf_len)
711 {
712 char *sep;
713 if ((sep = strrchr(fname, '.')) != NULL) {
714 size_t len = strlen(extension);
715 if (len + (sep - fname) > buf_len)
716 return;
717 memcpy(buf, fname, sep - fname);
718 strcpy(buf + (sep - fname), extension);
719 }
720 return;
721 }
722
723 #if 0
724 /*
725 * Estimate the string length needed for %p conversion. Currently unused,
726 * since we use the more general VSNPRINTF() approach.
727 */
728 #define PTR_CONVERSION_LEN_GUESS sizeof(void *) * CHAR_BIT
729 #endif
730
731
732 /*
733 * Return a formatted string in newly allocated memory.
734 */
735 char *
736 get_string_va(const char *fmt, ...)
737 {
738 char *buf = NULL;
739 XDVI_GET_STRING_ARGP(buf, fmt);
740 return buf;
741 }
742
743 /* Wrapper for atof() for strtod()-like error checking,
744 * with an XDVI_WARNING if the conversion of <str> wasn't complete.
745 */
746 double
747 my_atof(const char *str)
748 {
749 char *ptr;
750 double f;
751
752 f = strtod(str, (char **)&ptr);
753 if (*ptr != '\0') {
754 XDVI_WARNING((stderr, "strtod: incomplete conversion of %s to %f", str, f));
755 }
756 return f;
757 }
758
759 /* return length of a string representation of the integer n */
760 int
761 length_of_int(int n)
762 {
763 int ret = 1;
764
765 if (n < 0) {
766 ret++;
767 n *= -1;
768 }
769 while (n >= 10) {
770 n /= 10;
771 ret++;
772 }
773
774 return ret;
775 }
776
777 Boolean
778 is_spaces_only(const char *ptr)
779 {
780 for (; *ptr; ptr++) {
781 if (!isspace((int)*ptr))
782 return False;
783 }
784 return True;
785 }