1 /*
2 File: quote.c
3
4 Copyright (C) 2003 Andreas Gruenbacher <andreas.gruenbacher@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2.1 of the License, or (at
9 your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include "misc.h"
26
27 const char *__acl_quote(const char *str, const char *quote_chars)
28 {
29 static char *quoted_str;
30 static size_t quoted_str_len;
31 const unsigned char *s;
32 char *q;
33 size_t nonpr;
34
35 if (!str)
36 return str;
37
38 for (nonpr = 0, s = (unsigned char *)str; *s != '\0'; s++)
39 if (*s == '\\' || strchr(quote_chars, *s))
40 nonpr++;
41 if (nonpr == 0)
42 return str;
43
44 if (__acl_high_water_alloc((void **)"ed_str, "ed_str_len,
45 (s - (unsigned char *)str) + nonpr * 3 + 1))
46 return NULL;
47 for (s = (unsigned char *)str, q = quoted_str; *s != '\0'; s++) {
48 if (strchr(quote_chars, *s)) {
49 *q++ = '\\';
50 *q++ = '0' + ((*s >> 6) );
51 *q++ = '0' + ((*s >> 3) & 7);
52 *q++ = '0' + ((*s ) & 7);
53 } else if (*s == '\\') {
54 *q++ = '\\';
55 *q++ = '\\';
56 } else
57 *q++ = *s;
58 }
59 *q++ = '\0';
60
61 return quoted_str;
62 }