Mention that error messages involving filenames will quote unusual characters.
[findutils.git] / lib / printquoted.c
blob4f2d2ce7c4f5e10223119d15e23651e4b75abb59
1 /* printquoted.c -- print a specified string with any necessary quoting.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 2000,
4 2003, 2004, 2005 Free Software Foundation, Inc.
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.
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.
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.
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <stdio.h>
30 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
31 #if HAVE_WCHAR_H
32 # include <wchar.h>
33 #endif
37 #include "xalloc.h"
38 #include "printquoted.h"
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).
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)
53 if (dest_is_tty)
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
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 * XXX: newsflash: we already have alloca().
67 buf = xmalloc (len + 1);
68 quotearg_buffer (buf, len + 1, s, -1, qopts);
71 /* Replace any remaining funny characters with '?'. */
72 len = qmark_chars(buf, len);
74 fprintf(fp, format, buf); /* Print the quoted version */
75 if (buf != smallbuf)
77 free(buf);
78 buf = NULL;
81 else
83 /* no need to quote things. */
84 fprintf(fp, format, s);