summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/OSLib/MacSpecs.c
blob: 6d116206f8eeb23a032cd37c77b34631544fe17f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "oslib.h"
#include <errno.h>

extern char STSbuf[256];

typedef struct DirNode {
    char *name;
    unsigned int dirID;
    union {
        struct DirNode *parent;
        struct VolNode *volume;
    } p;
    struct DirNode *list;
    struct DirNode *next;
} DirNode;

typedef struct VolNode {
    UInt16 vRefNum;
    DirNode root;
    struct VolNode *next;
} VolNode;

static unsigned int lastDirID = 3;
static unsigned int lastVolRef = 2;
static char stname[256];
static char stpath[256];
static char stdir[256];
static char stvol[8];
static unsigned int dirMapRows;
static DirNode **dirIDMap[256];
static VolNode *vols;

static int AddDirMapEntry(DirNode *node) {
    unsigned int row;
    unsigned int col;

    row = node->dirID >> 8;
    col = node->dirID & 0xFF;

    while (row >= dirMapRows) {
        if (row >= 256) {
            fprintf(stderr, "Fatal error:  too many directories referenced, out of memory\n");
            return 0;
        }

        dirIDMap[row] = calloc(sizeof(DirNode *), 256);
        if (!dirIDMap[row])
            return 0;

        dirMapRows++;
    }

    dirIDMap[row][col] = node;
    return 1;
}

static VolNode *FindOrAddVolRef(VolNode **list, char *name) {
    int sz;
    VolNode *vn;

    while (*list) {
        if (OS_EqualPath(name, (*list)->root.name))
            return *list;
        list = &(*list)->next;
    }

    if (lastVolRef >= 256)
        return 0;

    vn = malloc(sizeof(VolNode));
    if (!vn)
        return 0;

    vn->next = 0;
    vn->vRefNum = lastVolRef++;

    sz = strlen(name) + 1;
    vn->root.name = malloc(sz);
    if (!vn->root.name)
        return 0;

    strcpy(vn->root.name, name);
    vn->root.dirID = 2;
    vn->root.p.volume = vn;
    vn->root.list = 0;
    vn->root.next = 0;
    *list = vn;
    return vn;
}

static DirNode *FindDirMapEntry(unsigned int dirID) {
    unsigned int row;
    unsigned int col;

    row = dirID >> 8;
    col = dirID & 0xFF;
    OS_ASSERT(166, dirID != 2);

    if (row >= dirMapRows)
        return 0;
    else
        return dirIDMap[row][col];
}

static DirNode *FindOrAddDirRef(DirNode *parent, char *name) {
    int sz;
    DirNode *dn;
    DirNode **list;

    list = &parent->list;
    while (*list) {
        if (OS_EqualPath(name, (*list)->name))
            return *list;
        list = &(*list)->next;
    }

    if (lastDirID >= 0x10000)
        return 0;

    sz = strlen(name) + 1;

    dn = malloc(sizeof(DirNode));
    if (!dn)
        return 0;
    dn->name = malloc(sz);
    if (!dn->name)
        return 0;
    strcpy(dn->name, name);

    dn->list = 0;
    dn->next = 0;
    dn->dirID = lastDirID++;

    if (!AddDirMapEntry(dn))
        return 0;

    dn->p.parent = parent;
    *list = dn;
    return dn;
}

static int FindOrAdd(const OSPathSpec *path, unsigned int *vRefNum, unsigned int *dirID) {
    VolNode *vol;
    DirNode *level;
    char pb[256];
    char voln[8];
    char pathbuf[256];
    char *ptr;
    char *st;
    char ch;

    if (!OS_PathSpecToString(path, pathbuf, 256))
        return 0;
    if (!pathbuf[0])
        return 0;

    ptr = OS_GetDirPtr(pathbuf);
    strncpy(voln, pathbuf, ptr - pathbuf);
    voln[ptr - pathbuf] = 0;
    strcpy(pb, ptr);

    vol = FindOrAddVolRef(&vols, voln);
    if (!vol)
        return 0;

    *vRefNum = vol->vRefNum;
    level = &vol->root;
    ptr = &pb[1];
    OS_ASSERT(267, *pb == OS_PATHSEP);

    while (*ptr) {
        st = ptr;
        while ((ch = *ptr) && *ptr != OS_PATHSEP) {
            ++ptr;
        }

        *ptr = 0;

        level = FindOrAddDirRef(level, st);
        if (!ch)
            break;
        ++ptr;
        if (!level)
            return 0;
    }

    *dirID = level->dirID;
    return 1;
}

static void StepVolDir(unsigned int *vRefNum, unsigned int *dirID, DirNode **node) {
    VolNode *step;

    if (*vRefNum == 1) {
        *node = 0;
        return;
    }

    if (*dirID == 2) {
        step = vols;
        while (step && step->vRefNum != *vRefNum)
            step = step->next;
        if (step) {
            *node = &step->root;
            *vRefNum = 1;
            *dirID = 1;
        } else {
            *node = 0;
        }
    } else {
        *node = FindDirMapEntry(*dirID);
        if (*node)
            *dirID = (*node)->p.parent->dirID;
    }
}

static int ResolvePath(const OSPathSpec *path, unsigned int *vRefNum, unsigned int *dirID) {
    if (FindOrAdd(path, vRefNum, dirID)) {
        *vRefNum = -*vRefNum;
        return 1;
    } else {
        *vRefNum = 0;
        *dirID = 0;
        return 0;
    }
}

static int ResolveVolDir(unsigned int vRefNum, unsigned int dirID, char *vol, char *dir) {
    DirNode *nodes[256];
    DirNode *cur;
    int idx;
    int len;
    char *dptr;

    vRefNum = -vRefNum;
    idx = 0;
    do {
        StepVolDir(&vRefNum, &dirID, &cur);
        if (cur)
            nodes[idx++] = cur;
    } while (cur);

    if (idx) {
        strcpy(vol, nodes[--idx]->name);
    } else {
        vol[0] = 0;
        dir[0] = 0;
        return 0;
    }

    dptr = dir;
    *(dptr++) = OS_PATHSEP;
    while (idx--) {
        len = strlen(nodes[idx]->name);
        if ((dptr - dir) + len + 1 > 256) {
            *dptr = 0;
            return 0;
        } else {
            memcpy(dptr, nodes[idx]->name, len);
            dptr += len;
            *(dptr++) = OS_PATHSEP;
        }
    }

    *dptr = 0;
    return 1;
}

int OS_OSPathSpec_To_VolDir(const OSPathSpec *spec, SInt16 *vRefNum, SInt32 *dirID) {
    unsigned int vol;
    unsigned int dir;

    if (ResolvePath(spec, &vol, &dir)) {
        *vRefNum = vol;
        *dirID = dir;
        return 0;
    } else {
        *vRefNum = 0;
        *dirID = 0;
        return ENOENT;
    }
}

int OS_OSSpec_To_FSSpec(const OSSpec *spec, FSSpec *fss) {
    int err;
    SInt16 vRefNum;
    SInt32 dirID;
    char *s;

    err = OS_OSPathSpec_To_VolDir(&spec->path, &vRefNum, &dirID);
    fss->vRefNum = vRefNum;
    fss->parID = dirID;
    if (err) return err;

    s = OS_NameSpecToString(&spec->name, STSbuf, sizeof(STSbuf));
    if (!s || strlen(s) >= 256)
        return ENAMETOOLONG;

    c2pstrcpy(fss->name, s);
    return 0;
}

int OS_VolDir_To_OSNameSpec(SInt16 vRefNum, SInt32 dirID, OSNameSpec *spec, SInt32 *parID) {
    unsigned int cv;
    unsigned int cd;
    DirNode *node;

    cv = -vRefNum;
    cd = dirID;
    StepVolDir(&cv, &cd, &node);
    if (!node)
        return ENOENT;

    *parID = cd;
    return OS_MakeNameSpec(node->name, spec);
}

int OS_VolDir_To_OSPathSpec(SInt16 vRefNum, SInt32 dirID, OSPathSpec *spec) {
    int err;
    int vlen;
    int dlen;

    if (!vRefNum || !dirID) {
        err = OS_GetCWD(spec);
        if (err)
            return err;
    } else {
        if (!ResolveVolDir(vRefNum, dirID, stvol, stdir))
            return ENOENT;

        vlen = strlen(stvol);
        dlen = strlen(stdir);
        if (vlen + dlen < 256) {
            memcpy(spec->s, stvol, vlen);
            memcpy(spec->s + vlen, stdir, dlen + 1);
        }
    }

    return 0;
}

int OS_FSSpec_To_OSSpec(const FSSpec *fss, OSSpec *spec) {
    int err;

    err = OS_VolDir_To_OSPathSpec(fss->vRefNum, fss->parID, &spec->path);
    if (err)
        return err;

    p2cstrcpy(stname, fss->name);
    err = OS_MakeNameSpec(stname, &spec->name);
    if (err)
        return err;

    return err;
}

int OS_GetRsrcOSSpec(const OSSpec *spec, OSSpec *rspec, Boolean create) {
    char dbuffer[256];
    int err;

    OS_PathSpecToString(&spec->path, dbuffer, sizeof(dbuffer));
    err = OS_MakeSpec2(dbuffer, "resource.frk", rspec);
    if (err)
        return err;

    err = OS_Status(rspec);
    if (err) {
        if (create) {
            err = OS_Mkdir(rspec);
            if (err)
                return err;
            // Windows version has a call to SetFileAttributes here
        } else {
            return err;
        }
        err = OS_MakeSpec2(dbuffer, "resource.frk", rspec);
        if (err)
            return err;
    } else if (OS_IsFile(rspec)) {
        return ENOTDIR;
    }

    err = OS_MakeNameSpec(OS_NameSpecToString(&spec->name, STSbuf, sizeof(STSbuf)), &rspec->name);
    if (err)
        return err;

    return 0;
}