bugfix renameat2 wrong implement.
[uclibc-ng.git] / utils / StringEscape.c
blobbe5ec55c35c31d7a1db61e0718317d9b80615265
1 #include <stddef.h>
3 //FIXME out gets silently truncated if outsize is too small
5 size_t escape(char* in, char* out, size_t outsize) {
6 size_t l = 0;
7 while(*in && l + 3 < outsize) {
8 switch(*in) {
9 case '\n':
10 *out++ = '\\';
11 l++;
12 *out = 'n';
13 break;
14 case '\r':
15 *out++ = '\\';
16 l++;
17 *out = 'r';
18 break;
19 case '\t':
20 *out++ = '\\';
21 l++;
22 *out = 't';
23 break;
24 case '\\':
25 *out++ = '\\';
26 l++;
27 *out = '\\';
28 break;
29 case '"':
30 *out++ = '\\';
31 l++;
32 *out = '"';
33 break;
34 case '\v':
35 *out++ = '\\';
36 l++;
37 *out = '\v';
38 break;
39 case '\?':
40 *out++ = '\\';
41 l++;
42 *out = '\?';
43 break;
44 case '\f':
45 *out++ = '\\';
46 l++;
47 *out = '\f';
48 break;
49 default:
50 *out = *in;
52 in++;
53 out++;
54 l++;
56 *out = 0;
57 return l;
59 #include <assert.h>
60 #include <stdlib.h>
61 size_t unescape(char* in, char *out, size_t outsize) {
62 size_t l = 0;
63 while(*in && l + 2 < outsize) {
64 switch (*in) {
65 case '\\':
66 ++in;
67 assert(*in);
68 switch(*in) {
69 case 'n':
70 *out='\n';
71 break;
72 case 'r':
73 *out='\r';
74 break;
75 case 't':
76 *out='\t';
77 break;
78 case '\\':
79 *out='\\';
80 break;
81 case '"':
82 *out='"';
83 break;
84 case 'v':
85 *out='\v';
86 break;
87 case '\?':
88 *out = '\?';
89 break;
90 case 'f':
91 *out = '\f';
92 break;
93 case '\'':
94 *out = '\'';
95 break;
96 case 'b':
97 *out = '\b';
98 break;
99 // FIXME add handling of hex and octal
100 default:
101 abort();
103 break;
104 default:
105 *out=*in;
107 in++;
108 out++;
109 l++;
111 *out = 0;
112 return l;