Clarify that -exec.. commands will be executed before program _exit_ (not _execution_...
[findutils.git] / lib / qmark.c
bloba23a5f804905c447013154f67dc4e0b9afdd5c4d
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 3 of the License, or
10 (at your option) 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, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 # include <stddef.h>
24 # include <stdlib.h>
25 #include <ctype.h>
27 #if HAVE_STRING_H || STDC_HEADERS
28 #include <string.h>
29 #else
30 #include <strings.h>
31 #endif
34 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
35 #if HAVE_WCHAR_H
36 # include <wchar.h>
37 #endif
39 #include "printquoted.h"
42 /*
43 This comment, IN_CTYPE_DOMAIN and ISPRINT were borrowed from
44 coreutils at Sun Jun 5 21:17:40 2005 UTC.
46 Jim Meyering writes:
48 "... Some ctype macros are valid only for character codes that
49 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
50 using /bin/cc or gcc but without giving an ansi option). So, all
51 ctype uses should be through macros like ISPRINT... If
52 STDC_HEADERS is defined, then autoconf has verified that the ctype
53 macros don't need to be guarded with references to isascii. ...
54 Defining isascii to 1 should let any compiler worth its salt
55 eliminate the && through constant folding."
57 Bruno Haible adds:
59 "... Furthermore, isupper(c) etc. have an undefined result if c is
60 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
61 with c being of type `char', but this is wrong if c is an 8-bit
62 character >= 128 which gets sign-extended to a negative value.
63 The macro ISUPPER protects against this as well." */
68 /* ISPRINT is defined in <sys/euc.h> on at least Solaris2.6 systems. */
69 #undef ISPRINT
70 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
72 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
73 # define IN_CTYPE_DOMAIN(c) 1
74 #else
75 # define IN_CTYPE_DOMAIN(c) isascii(c)
76 #endif
82 /* Convert a possibly-signed character to an unsigned character. This is
83 * a bit safer than casting to unsigned char, since it catches some type
84 * errors that the cast doesn't.
86 * This code taken from coreutils' system.h header at
87 * Sun Jun 5 21:05:21 2005 UTC.
89 static inline unsigned char to_uchar (char ch)
91 return ch;
96 static size_t
97 unibyte_qmark_chars(char *buf, size_t len)
99 char *p = buf;
100 char const *plimit = buf + len;
102 while (p < plimit)
104 if (! ISPRINT (to_uchar (*p)))
105 *p = '?';
106 p++;
108 return len;
112 #if HAVE_MBRTOWC
113 static size_t
114 multibyte_qmark_chars(char *buf, size_t len)
116 if (MB_CUR_MAX <= 1)
118 return unibyte_qmark_chars(buf, len);
120 else
122 char const *p = buf;
123 char const *plimit = buf + len;
124 char *q = buf;
126 while (p < plimit)
127 switch (*p)
129 case ' ': case '!': case '"': case '#': case '%':
130 case '&': case '\'': case '(': case ')': case '*':
131 case '+': case ',': case '-': case '.': case '/':
132 case '0': case '1': case '2': case '3': case '4':
133 case '5': case '6': case '7': case '8': case '9':
134 case ':': case ';': case '<': case '=': case '>':
135 case '?':
136 case 'A': case 'B': case 'C': case 'D': case 'E':
137 case 'F': case 'G': case 'H': case 'I': case 'J':
138 case 'K': case 'L': case 'M': case 'N': case 'O':
139 case 'P': case 'Q': case 'R': case 'S': case 'T':
140 case 'U': case 'V': case 'W': case 'X': case 'Y':
141 case 'Z':
142 case '[': case '\\': case ']': case '^': case '_':
143 case 'a': case 'b': case 'c': case 'd': case 'e':
144 case 'f': case 'g': case 'h': case 'i': case 'j':
145 case 'k': case 'l': case 'm': case 'n': case 'o':
146 case 'p': case 'q': case 'r': case 's': case 't':
147 case 'u': case 'v': case 'w': case 'x': case 'y':
148 case 'z': case '{': case '|': case '}': case '~':
149 /* These characters are printable ASCII characters. */
150 *q++ = *p++;
151 break;
152 default:
153 /* If we have a multibyte sequence, copy it until we
154 reach its end, replacing each non-printable multibyte
155 character with a single question mark. */
157 mbstate_t mbstate;
158 memset (&mbstate, 0, sizeof mbstate);
161 wchar_t wc;
162 size_t bytes;
163 int w;
165 bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
167 if (bytes == (size_t) -1)
169 /* An invalid multibyte sequence was
170 encountered. Skip one input byte, and
171 put a question mark. */
172 p++;
173 *q++ = '?';
174 break;
177 if (bytes == (size_t) -2)
179 /* An incomplete multibyte character
180 at the end. Replace it entirely with
181 a question mark. */
182 p = plimit;
183 *q++ = '?';
184 break;
187 if (bytes == 0)
188 /* A null wide character was encountered. */
189 bytes = 1;
191 w = wcwidth (wc);
192 if (w >= 0)
194 /* A printable multibyte character.
195 Keep it. */
196 for (; bytes > 0; --bytes)
197 *q++ = *p++;
199 else
201 /* An unprintable multibyte character.
202 Replace it entirely with a question
203 mark. */
204 p += bytes;
205 *q++ = '?';
208 while (! mbsinit (&mbstate));
210 break;
213 /* The buffer may have shrunk. */
214 len = q - buf;
215 return len;
218 #endif
221 /* Scan BUF, replacing any dangerous-looking characters with question
222 * marks. This code is taken from the ls.c file in coreutils as at
223 * Sun Jun 5 20:51:54 2005 UTC.
225 * This function may shrink the buffer. Either way, the new length
226 * is returned.
228 size_t
229 qmark_chars(char *buf, size_t len)
231 #if HAVE_MBRTOWC
232 return multibyte_qmark_chars(buf, len);
233 #else
234 return unibyte_qmark_chars(buf, len);
235 #endif