Ignore the usually-ignored files in git
[findutils.git] / lib / printquoted.c
blob0f1155fb880ada35602d4e752b06b4e167abe089
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 #include <config.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <stdio.h>
29 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
30 #if HAVE_WCHAR_H
31 # include <wchar.h>
32 #endif
36 #include "xalloc.h"
37 #include "printquoted.h"
41 * Print S according to the format FORMAT, but if the destination is a tty,
42 * convert any potentially-dangerous characters. The logic in this function
43 * was taken from ls.c in coreutils (at Sun Jun 5 20:42:51 2005 UTC).
45 int
46 print_quoted (FILE *fp,
47 const struct quoting_options *qopts,
48 bool dest_is_tty,
49 const char *format,
50 const char *s)
52 int rv;
54 if (dest_is_tty)
56 char smallbuf[BUFSIZ];
57 size_t len = quotearg_buffer (smallbuf, sizeof smallbuf, s, -1, qopts);
58 char *buf;
59 if (len < sizeof smallbuf)
60 buf = smallbuf;
61 else
63 /* The original coreutils code uses alloca(), but I don't
64 * want to take on the anguish of introducing alloca() to
65 * 'find'.
66 * XXX: newsflash: we already have alloca().
68 buf = xmalloc (len + 1);
69 quotearg_buffer (buf, len + 1, s, -1, qopts);
72 /* Replace any remaining funny characters with '?'. */
73 len = qmark_chars(buf, len);
75 rv = fprintf(fp, format, buf); /* Print the quoted version */
76 if (buf != smallbuf)
78 free(buf);
79 buf = NULL;
82 else
84 /* no need to quote things. */
85 rv = fprintf(fp, format, s);
87 return rv;