Fix Savannah bug #20263 in a more portable way.
[findutils.git] / lib / qmark.c
blob83f4d60c7a8c561cdfd970dabd3cf28d46745317
1 /* qmark.c -- quote 'dangerous' filenames
3 Copyright (C) 2005 Free Software Foundation, Inc.
4 Derived from courutils' ls.c:
5 Copyright (C) 85, 88, 90, 91, 1995-2005 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 USA.
23 #include <config.h>
25 # include <stddef.h>
26 # include <stdlib.h>
27 #include <ctype.h>
29 #if HAVE_STRING_H || STDC_HEADERS
30 #include <string.h>
31 #else
32 #include <strings.h>
33 #endif
36 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
37 #if HAVE_WCHAR_H
38 # include <wchar.h>
39 #endif
41 #include "printquoted.h"
44 /*
45 This comment, IN_CTYPE_DOMAIN and ISPRINT were borrowed from
46 coreutils at Sun Jun 5 21:17:40 2005 UTC.
48 Jim Meyering writes:
50 "... Some ctype macros are valid only for character codes that
51 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
52 using /bin/cc or gcc but without giving an ansi option). So, all
53 ctype uses should be through macros like ISPRINT... If
54 STDC_HEADERS is defined, then autoconf has verified that the ctype
55 macros don't need to be guarded with references to isascii. ...
56 Defining isascii to 1 should let any compiler worth its salt
57 eliminate the && through constant folding."
59 Bruno Haible adds:
61 "... Furthermore, isupper(c) etc. have an undefined result if c is
62 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
63 with c being of type `char', but this is wrong if c is an 8-bit
64 character >= 128 which gets sign-extended to a negative value.
65 The macro ISUPPER protects against this as well." */
70 /* ISPRINT is defined in <sys/euc.h> on at least Solaris2.6 systems. */
71 #undef ISPRINT
72 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
74 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
75 # define IN_CTYPE_DOMAIN(c) 1
76 #else
77 # define IN_CTYPE_DOMAIN(c) isascii(c)
78 #endif
84 /* Convert a possibly-signed character to an unsigned character. This is
85 * a bit safer than casting to unsigned char, since it catches some type
86 * errors that the cast doesn't.
88 * This code taken from coreutils' system.h header at
89 * Sun Jun 5 21:05:21 2005 UTC.
91 static inline unsigned char to_uchar (char ch)
93 return ch;
98 static size_t
99 unibyte_qmark_chars(char *buf, size_t len)
101 char *p = buf;
102 char const *plimit = buf + len;
104 while (p < plimit)
106 if (! ISPRINT (to_uchar (*p)))
107 *p = '?';
108 p++;
110 return len;
114 #if HAVE_MBRTOWC
115 static size_t
116 multibyte_qmark_chars(char *buf, size_t len)
118 if (MB_CUR_MAX <= 1)
120 return unibyte_qmark_chars(buf, len);
122 else
124 char const *p = buf;
125 char const *plimit = buf + len;
126 char *q = buf;
128 while (p < plimit)
129 switch (*p)
131 case ' ': case '!': case '"': case '#': case '%':
132 case '&': case '\'': case '(': case ')': case '*':
133 case '+': case ',': case '-': case '.': case '/':
134 case '0': case '1': case '2': case '3': case '4':
135 case '5': case '6': case '7': case '8': case '9':
136 case ':': case ';': case '<': case '=': case '>':
137 case '?':
138 case 'A': case 'B': case 'C': case 'D': case 'E':
139 case 'F': case 'G': case 'H': case 'I': case 'J':
140 case 'K': case 'L': case 'M': case 'N': case 'O':
141 case 'P': case 'Q': case 'R': case 'S': case 'T':
142 case 'U': case 'V': case 'W': case 'X': case 'Y':
143 case 'Z':
144 case '[': case '\\': case ']': case '^': case '_':
145 case 'a': case 'b': case 'c': case 'd': case 'e':
146 case 'f': case 'g': case 'h': case 'i': case 'j':
147 case 'k': case 'l': case 'm': case 'n': case 'o':
148 case 'p': case 'q': case 'r': case 's': case 't':
149 case 'u': case 'v': case 'w': case 'x': case 'y':
150 case 'z': case '{': case '|': case '}': case '~':
151 /* These characters are printable ASCII characters. */
152 *q++ = *p++;
153 break;
154 default:
155 /* If we have a multibyte sequence, copy it until we
156 reach its end, replacing each non-printable multibyte
157 character with a single question mark. */
159 mbstate_t mbstate;
160 memset (&mbstate, 0, sizeof mbstate);
163 wchar_t wc;
164 size_t bytes;
165 int w;
167 bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
169 if (bytes == (size_t) -1)
171 /* An invalid multibyte sequence was
172 encountered. Skip one input byte, and
173 put a question mark. */
174 p++;
175 *q++ = '?';
176 break;
179 if (bytes == (size_t) -2)
181 /* An incomplete multibyte character
182 at the end. Replace it entirely with
183 a question mark. */
184 p = plimit;
185 *q++ = '?';
186 break;
189 if (bytes == 0)
190 /* A null wide character was encountered. */
191 bytes = 1;
193 w = wcwidth (wc);
194 if (w >= 0)
196 /* A printable multibyte character.
197 Keep it. */
198 for (; bytes > 0; --bytes)
199 *q++ = *p++;
201 else
203 /* An unprintable multibyte character.
204 Replace it entirely with a question
205 mark. */
206 p += bytes;
207 *q++ = '?';
210 while (! mbsinit (&mbstate));
212 break;
215 /* The buffer may have shrunk. */
216 len = q - buf;
217 return len;
220 #endif
223 /* Scan BUF, replacing any dangerous-looking characters with question
224 * marks. This code is taken from the ls.c file in coreutils as at
225 * Sun Jun 5 20:51:54 2005 UTC.
227 * This function may shrink the buffer. Either way, the new length
228 * is returned.
230 size_t
231 qmark_chars(char *buf, size_t len)
233 #if HAVE_MBRTOWC
234 return multibyte_qmark_chars(buf, len);
235 #else
236 return unibyte_qmark_chars(buf, len);
237 #endif