Imported Upstream version 2.4.46
[platform/upstream/attr.git] / libmisc / unquote.c
1 /*
2   File: unquote.c
3
4   Copyright (C) 2003 Andreas Gruenbacher <a.gruenbacher@bestbits.at>
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 <stdio.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include "misc.h"
24
25 char *unquote(char *str)
26 {
27         unsigned char *s, *t;
28
29         if (!str)
30                 return str;
31
32         for (s = (unsigned char *)str; *s != '\0'; s++)
33                 if (*s == '\\')
34                         break;
35         if (*s == '\0')
36                 return str;
37
38 #define isoctal(c) \
39         ((c) >= '0' && (c) <= '7')
40
41         t = s;
42         do {
43                 if (*s == '\\' &&
44                     isoctal(*(s+1)) && isoctal(*(s+2)) && isoctal(*(s+3))) {
45                         *t++ = ((*(s+1) - '0') << 6) +
46                                ((*(s+2) - '0') << 3) +
47                                ((*(s+3) - '0')     );
48                         s += 3;
49                 } else
50                         *t++ = *s;
51         } while (*s++ != '\0');
52
53         return str;
54 }