-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac.c
273 lines (230 loc) · 4.89 KB
/
mac.c
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
/*
* $Id: mac.c 443 2006-05-30 04:37:13Z darren $
*
* Copyright (c) 2001, Maarten L. Hekkelman
*
* Author: Maarten L. Hekkelman <maarten@hekkelman.com>
* http://www.hekkelman.com
*
* This source code is released for free distribution under the terms of the
* GNU General Public License. It is provided on an as-is basis and no
* responsibility is accepted for its failure to perform as expected.
*
* This module contains support functions for Exuberant Ctags on Macintosh.
*/
/*
* INCLUDE FILES
*/
#include "general.h"
#include <Files.h>
#include <TextUtils.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/*
* FUNCTION DEFINITIONS
*/
static int get_path(const char* in_unix_path, unsigned char* out_mac_path)
{
int l = strlen(in_unix_path);
int result = 0;
if (l > 254)
result = -1;
else
{
const char* s = in_unix_path;
char *d = (char*)out_mac_path + 1;
if (*s != '/')
*d++ = ':';
else
++s;
while (*s)
{
if (s[0] == '.' && s[1] == '.' && s[2] == '/')
{
s += 3;
*d++ = ':';
}
else if (s[0] == '.' && s[1] == '/')
s += 2;
else if (s[0] == '/')
{
*d++ = ':';
++s;
while (*s == '/')
++s;
}
else
*d++ = *s++;
}
out_mac_path[0] = (d - (char*)out_mac_path) - 1;
}
return result;
}
DIR *opendir(const char *dirname)
{
DIR* dirp = (DIR*)calloc(1, sizeof(DIR));
if (dirp != NULL)
{
OSErr err;
Str255 s;
CInfoPBRec pb = { 0 };
if (strcmp(dirname, "."))
{
get_path(dirname, s);
pb.hFileInfo.ioNamePtr = s;
}
else
pb.hFileInfo.ioNamePtr = NULL;
err = PBGetCatInfoSync(&pb);
if (err != noErr || (pb.hFileInfo.ioFlAttrib & ioDirMask) == 0)
{
free(dirp);
dirp = NULL;
}
else
{
dirp->file.vRefNum = pb.hFileInfo.ioVRefNum;
dirp->file.parID = pb.hFileInfo.ioDirID;
dirp->file.name[0] = '\0';
dirp->index = 1;
}
}
return dirp;
}
struct dirent *readdir(DIR *dirp)
{
if (dirp)
{
CInfoPBRec pb = { 0 };
pb.hFileInfo.ioVRefNum = dirp->file.vRefNum;
pb.hFileInfo.ioDirID = dirp->file.parID;
pb.hFileInfo.ioFDirIndex = dirp->index++;
pb.hFileInfo.ioNamePtr = dirp->file.name;
if (PBGetCatInfoSync(&pb) != noErr)
return NULL;
memcpy(dirp->ent.d_name, dirp->file.name + 1, dirp->file.name[0]);
dirp->ent.d_name[dirp->file.name[0]] = 0;
return &dirp->ent;
}
return NULL;
}
int closedir(DIR *dirp)
{
if (dirp)
free(dirp);
return 0;
}
void rewinddir(DIR *dirp)
{
if (dirp)
dirp->index = 1;
}
int mstat(const char* file, struct stat* st)
{
CInfoPBRec pb;
unsigned char path[256];
int result = 0;
memset(&pb, 0, sizeof(CInfoPBRec));
if (strcmp(file, ".") == 0)
{
memset(st, 0, sizeof(struct stat));
st->st_mode = S_IFDIR;
st->st_ino = -1;
}
else
{
result = get_path(file, path);
if (result == 0)
{
pb.hFileInfo.ioNamePtr = path;
if (PBGetCatInfoSync(&pb) != noErr)
result = -1;
else
{
memset(st, 0, sizeof(struct stat));
if (pb.hFileInfo.ioFlAttrib & ioDirMask)
st->st_mode = S_IFDIR;
else
st->st_mode = S_IFREG;
st->st_ino = pb.hFileInfo.ioFlStBlk;
st->st_dev = pb.hFileInfo.ioVRefNum;
st->st_nlink = 1;
st->st_size = pb.hFileInfo.ioFlLgLen;
st->st_atime = pb.hFileInfo.ioFlMdDat;
st->st_mtime = pb.hFileInfo.ioFlMdDat;
st->st_ctime = pb.hFileInfo.ioFlCrDat;
}
}
}
return result;
}
#undef fopen
FILE* mfopen(const char* file, const char* mode)
{
unsigned char path[256];
if (get_path(file, path) == 0)
{
int l = path[0];
memmove(path, path + 1, l);
path[l] = 0;
return fopen((char*)path, mode);
}
else
return NULL;
}
char* getcwd(char* out_path, int out_path_len)
{
OSErr err = noErr;
CInfoPBRec pb;
FSSpec cwd;
if (out_path == NULL)
{
if (out_path_len < PATH_MAX)
out_path_len = PATH_MAX;
out_path = (char*)malloc(out_path_len);
}
err = FSMakeFSSpec(0, 0, "\p:", &cwd);
if (cwd.parID == fsRtParID)
{
*out_path = '/';
memcpy(out_path + 1, cwd.name + 1, cwd.name[0]);
out_path[1 + cwd.name[0]] = 0;
}
else
{
/* The object isn't a volume */
/* Is the object a file or a directory? */
char t[PATH_MAX];
char* s;
s = t + PATH_MAX - cwd.name[0] - 1;
memcpy(s, cwd.name + 1, cwd.name[0]);
s[cwd.name[0]] = 0;
/* Get the ancestor directory names */
pb.dirInfo.ioNamePtr = cwd.name;
pb.dirInfo.ioVRefNum = cwd.vRefNum;
pb.dirInfo.ioDrParID = cwd.parID;
do /* loop until we have an error or find the root directory */
{
pb.dirInfo.ioFDirIndex = -1;
pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
err = PBGetCatInfoSync(&pb);
if ( err == noErr )
{
*--s = '/';
s -= cwd.name[0];
memcpy(s, cwd.name + 1, cwd.name[0]);
}
}
while (err == noErr && pb.dirInfo.ioDrDirID != fsRtDirID && s > t + 1);
if (s > t + 1)
{
*--s = '/';
strcpy(out_path, s);
}
else
strcpy(out_path, ".");
}
return out_path;
}
/* vi:set tabstop=4 shiftwidth=4: */