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/>.
27 #if HAVE_STRING_H || STDC_HEADERS
34 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
39 #include "printquoted.h"
43 This comment, IN_CTYPE_DOMAIN and ISPRINT were borrowed from
44 coreutils at Sun Jun 5 21:17:40 2005 UTC.
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."
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. */
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
75 # define IN_CTYPE_DOMAIN(c) isascii(c)
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
)
97 unibyte_qmark_chars(char *buf
, size_t len
)
100 char const *plimit
= buf
+ len
;
104 if (! ISPRINT (to_uchar (*p
)))
114 multibyte_qmark_chars(char *buf
, size_t len
)
118 return unibyte_qmark_chars(buf
, len
);
123 char const *plimit
= buf
+ len
;
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 '>':
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':
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. */
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. */
158 memset (&mbstate
, 0, sizeof mbstate
);
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. */
177 if (bytes
== (size_t) -2)
179 /* An incomplete multibyte character
180 at the end. Replace it entirely with
188 /* A null wide character was encountered. */
194 /* A printable multibyte character.
196 for (; bytes
> 0; --bytes
)
201 /* An unprintable multibyte character.
202 Replace it entirely with a question
208 while (! mbsinit (&mbstate
));
213 /* The buffer may have shrunk. */
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
229 qmark_chars(char *buf
, size_t len
)
232 return multibyte_qmark_chars(buf
, len
);
234 return unibyte_qmark_chars(buf
, len
);