1 | /* printquoted.c -- print a specified string with any necessary quoting.
|
---|
2 |
|
---|
3 | Copyright (C) 1990, 1991, 1992, 1993, 1994, 2000,
|
---|
4 | 2003, 2004, 2005 Free Software Foundation, Inc.
|
---|
5 |
|
---|
6 | This program 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 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | This program 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.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
---|
19 | USA.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifdef HAVE_CONFIG_H
|
---|
23 | #include <config.h>
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #include <stddef.h>
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <stdio.h>
|
---|
29 |
|
---|
30 | /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
|
---|
31 | #if HAVE_WCHAR_H
|
---|
32 | # include <wchar.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | #include "xalloc.h"
|
---|
38 | #include "printquoted.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*
|
---|
42 | * Print S according to the format FORMAT, but if the destination is a tty,
|
---|
43 | * convert any potentially-dangerous characters. The logic in this function
|
---|
44 | * was taken from ls.c in coreutils (at Sun Jun 5 20:42:51 2005 UTC).
|
---|
45 | */
|
---|
46 | void
|
---|
47 | print_quoted (FILE *fp,
|
---|
48 | const struct quoting_options *qopts,
|
---|
49 | bool dest_is_tty,
|
---|
50 | const char *format,
|
---|
51 | const char *s)
|
---|
52 | {
|
---|
53 | if (dest_is_tty)
|
---|
54 | {
|
---|
55 | char smallbuf[BUFSIZ];
|
---|
56 | size_t len = quotearg_buffer (smallbuf, sizeof smallbuf, s, -1, qopts);
|
---|
57 | char *buf;
|
---|
58 | if (len < sizeof smallbuf)
|
---|
59 | buf = smallbuf;
|
---|
60 | else
|
---|
61 | {
|
---|
62 | /* The original coreutils code uses alloca(), but I don't
|
---|
63 | * want to take on the anguish of introducing alloca() to
|
---|
64 | * 'find'.
|
---|
65 | */
|
---|
66 | buf = xmalloc (len + 1);
|
---|
67 | quotearg_buffer (buf, len + 1, s, -1, qopts);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* Replace any remaining funny characters with '?'. */
|
---|
71 | len = qmark_chars(buf, len);
|
---|
72 |
|
---|
73 | fprintf(fp, format, buf); /* Print the quoted version */
|
---|
74 | if (buf != smallbuf)
|
---|
75 | {
|
---|
76 | free(buf);
|
---|
77 | buf = NULL;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | /* no need to quote things. */
|
---|
83 | fprintf(fp, format, s);
|
---|
84 | }
|
---|
85 |
|
---|
86 | }
|
---|
87 |
|
---|