~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to trampoline/norm.c

  • Committer: Matt Giuca
  • Date: 2009-03-24 02:57:37 UTC
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: matt.giuca@gmail.com-20090324025737-l4cdwuj6009pib5s
Removed all documentation.
(To be replaced with Sphinx).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <assert.h>
2
 
#include <string.h>
3
 
#include <stdlib.h>
4
 
#include <stdio.h>
5
 
#include <unistd.h>
6
 
#include "norm.h"
7
 
 
8
 
#define SKP()               \
9
 
    do {                    \
10
 
        s++;                \
11
 
    } while (0)
12
 
 
13
 
#define CPY()               \
14
 
    do {                    \
15
 
        *d++ = *s++;        \
16
 
    } while (0)
17
 
 
18
 
#define MRK()               \
19
 
    do {                    \
20
 
        PUSH();             \
21
 
    } while (0)
22
 
 
23
 
#define BKP()               \
24
 
    do {                    \
25
 
        POP();              \
26
 
    } while (0)
27
 
 
28
 
#define BKP2()              \
29
 
    do {                    \
30
 
        POP();              \
31
 
        POP();              \
32
 
    } while (0)
33
 
 
34
 
#define PUSH()              \
35
 
    do {                    \
36
 
        stk[top++] = d;     \
37
 
    } while (0)
38
 
 
39
 
#define POP()               \
40
 
    do {                    \
41
 
        if (top == 0)       \
42
 
        {                   \
43
 
            dest[0] = '\0'; \
44
 
            return -1;      \
45
 
        }                   \
46
 
        d = stk[--top];     \
47
 
    } while (0)
48
 
 
49
 
/*
50
 
 * Normalize the unix pathname in src eliminating .. sequences
51
 
 * to yield an absolute path. Returns 0 on success, and -1 on
52
 
 * error.
53
 
 */
54
 
int norm(char* dest, int len, const char* src)
55
 
{
56
 
    const char* s = src;
57
 
    char* d = dest;
58
 
    char* stk[256];
59
 
    int top = 0;
60
 
    enum { L0, L1, L2, L3, L4 } state = L0;
61
 
    assert(strlen(src) <= len);
62
 
 
63
 
    while (*s)
64
 
    {
65
 
        char c = *s;
66
 
        enum { slash, dot, other } cls;
67
 
        switch (c)
68
 
        {
69
 
            case '/':
70
 
            {
71
 
                cls = slash;
72
 
                break;
73
 
            }
74
 
            case '.':
75
 
            {
76
 
                cls = dot;
77
 
                break;
78
 
            }
79
 
            default:
80
 
            {
81
 
                cls = other;
82
 
            }
83
 
        }
84
 
        switch (state)
85
 
        {
86
 
            case L0:
87
 
            {
88
 
                switch (cls)
89
 
                {
90
 
                    case slash:
91
 
                    {
92
 
                        CPY();
93
 
                        state = L1;
94
 
                        break;
95
 
                    }
96
 
                    case dot:
97
 
                    case other:
98
 
                    {
99
 
                        dest[0] = '\0';
100
 
                        return -1;
101
 
                    }
102
 
                }
103
 
                break;
104
 
            }
105
 
            case L1:
106
 
            {
107
 
                switch (cls)
108
 
                {
109
 
                    case slash:
110
 
                    {
111
 
                        SKP();
112
 
                        break;
113
 
                    }
114
 
                    case dot:
115
 
                    {
116
 
                        MRK();
117
 
                        CPY();
118
 
                        state = L2;
119
 
                        break;
120
 
                    }
121
 
                    case other:
122
 
                    {
123
 
                        MRK();
124
 
                        CPY();
125
 
                        state = L4;
126
 
                        break;
127
 
                    }
128
 
                }
129
 
                break;
130
 
            }
131
 
            case L2:
132
 
            {
133
 
                switch (cls)
134
 
                {
135
 
                    case slash:
136
 
                    {
137
 
                        BKP();
138
 
                        state = L1;
139
 
                        break;
140
 
                    }
141
 
                    case dot:
142
 
                    {
143
 
                        CPY();
144
 
                        state = L3;
145
 
                        break;
146
 
                    }
147
 
                    case other:
148
 
                    {
149
 
                        CPY();
150
 
                        state = L4;
151
 
                        break;
152
 
                    }
153
 
                }
154
 
                break;
155
 
            }
156
 
            case L3:
157
 
            {
158
 
                switch (cls)
159
 
                {
160
 
                    case slash:
161
 
                    {
162
 
                        BKP2();
163
 
                        state = L1;
164
 
                        break;
165
 
                    }
166
 
                    case dot:
167
 
                    case other:
168
 
                    {
169
 
                        CPY();
170
 
                        state = L4;
171
 
                        break;
172
 
                    }
173
 
                }
174
 
                break;
175
 
            }
176
 
            case L4:
177
 
            {
178
 
                switch (cls)
179
 
                {
180
 
                    case slash:
181
 
                    {
182
 
                        CPY();
183
 
                        state = L1;
184
 
                        break;
185
 
                    }
186
 
                    case dot:
187
 
                    case other:
188
 
                    {
189
 
                        CPY();
190
 
                        break;
191
 
                    }
192
 
                }
193
 
                break;
194
 
            }
195
 
        }
196
 
    }
197
 
    *d = '\0';
198
 
    return 0;
199
 
}