Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4d7728

Browse files
committed
Updates for THINK C 6.0. Moved the necessary UNIX emulation routines here.
1 parent e89bc75 commit d4d7728

16 files changed

Lines changed: 618 additions & 51 deletions

File tree

Mac/Compat/chdir.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Chdir for the Macintosh.
2+
Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
3+
Pathnames must be Macintosh paths, with colons as separators. */
4+
5+
#include "macdefs.h"
6+
7+
/* Change current directory. */
8+
9+
int
10+
chdir(path)
11+
char *path;
12+
{
13+
WDPBRec pb;
14+
char name[MAXPATH];
15+
16+
strncpy(name, path, sizeof name);
17+
name[MAXPATH-1]= EOS;
18+
pb.ioNamePtr= (StringPtr) c2pstr(name);
19+
pb.ioVRefNum= 0;
20+
pb.ioWDDirID= 0;
21+
if (PBHSetVol(&pb, FALSE) != noErr) {
22+
errno= ENOENT;
23+
return -1;
24+
}
25+
return 0;
26+
}

Mac/Compat/dirent.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* "Dir.h" for the Macintosh.
3+
* Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
4+
*/
5+
6+
#define MAXNAMLEN 31
7+
#define MAXPATH 256
8+
9+
#define DIR struct _dir
10+
11+
struct _dir {
12+
long dirid;
13+
int nextfile;
14+
};
15+
16+
struct dirent {
17+
char d_name[MAXPATH];
18+
};
19+
20+
extern DIR *opendir(char *);
21+
extern struct dirent *readdir(DIR *);
22+
extern void closedir(DIR *);

Mac/Compat/getbootvol.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Return the name of the boot volume (not the current directory).
2+
Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
3+
*/
4+
5+
#include "macdefs.h"
6+
7+
char *
8+
getbootvol()
9+
{
10+
short vrefnum;
11+
static unsigned char name[32];
12+
13+
(void) GetVol(name, &vrefnum);
14+
p2cstr(name);
15+
/* Shouldn't fail; return ":" if it does */
16+
strcat((char *)name, ":");
17+
return (char *)name;
18+
}

Mac/Compat/getwd.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* Get full pathname of current working directory. The pathname is
2+
copied to the parameter array 'cwd', and a pointer to this array
3+
is also returned as function result. If an error occurred, however,
4+
the return value is NULL but 'cwd' is filled with an error message.
5+
6+
BUG: expect spectacular crashes when called from a directory whose
7+
path would be over MAXPATH bytes long (files in such directories are
8+
not reachable by full pathname).
9+
10+
Starting with the dir ID returned by PBHGetVol, we do successive
11+
PBGetCatInfo's to get a component of the path until we reach the
12+
root (recognized by a dir ID of 2). We move up along the path
13+
using the dir ID of the parent directory returned by PBGetCatInfo.
14+
15+
Then we catenate the components found in reverse order with the volume
16+
name (already gotten from PBHGetVol), with intervening and trailing
17+
colons
18+
19+
The code works correctly on MFS disks (where it always returns the
20+
volume name) by simply skipping the PBGetCatinfo calls in that case.
21+
There is a 'bug' in PBGetCatInfo when called for an MFS disk (with
22+
HFS running): it then seems to call PBHGetVInfo, which returns a
23+
larger parameter block. But we won't run into this problem because
24+
we never call PBGetCatInfo for the root (assuming that PBHGetVol
25+
still sets the root ID in this case).
26+
27+
Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
28+
*/
29+
30+
#ifdef MPW
31+
#include <Strings.h>
32+
#endif
33+
#include "macdefs.h"
34+
35+
#define ROOTID 2 /* Root directory ID */
36+
37+
char *
38+
getwd(cwd)
39+
char *cwd;
40+
{
41+
/* Universal parameter block. */
42+
union {
43+
#ifdef THINK_C
44+
HFileInfo f;
45+
DirInfo d;
46+
WDPBRec w;
47+
#else /* MPW */
48+
struct HFileInfo f;
49+
struct DirInfo d;
50+
struct WDPBRec w;
51+
#endif
52+
} pb;
53+
char buf[MAXPATH]; /* Buffer to store the name components */
54+
char *ecwd, *ebuf; /* Pointers to end of used part of cwd and buf */
55+
int err; /* Error code of last I/O call */
56+
57+
/* First, get the default volume name and working directory ID. */
58+
59+
pb.w.ioNamePtr= (unsigned char *)cwd;
60+
err= PBHGetVol(&pb.w, FALSE);
61+
if (err != noErr) {
62+
sprintf(cwd, "I/O error %d in PBHGetVol", err);
63+
return NULL;
64+
}
65+
ecwd= strchr(p2cstr((unsigned char*)cwd), EOS);
66+
ebuf= buf;
67+
*ebuf = EOS;
68+
69+
/* Next, if at least we're running HFS, walk up the path. */
70+
71+
if (hfsrunning()) {
72+
long dirid= pb.w.ioWDDirID;
73+
pb.d.ioVRefNum= pb.w.ioWDVRefNum;
74+
while (dirid != ROOTID) {
75+
pb.d.ioNamePtr= (unsigned char *) ++ebuf;
76+
pb.d.ioFDirIndex= -1;
77+
pb.d.ioDrDirID= dirid;
78+
err= PBGetCatInfo((CInfoPBPtr)&pb.d, FALSE);
79+
if (err != noErr) {
80+
sprintf(cwd, "I/O error %d in PBGetCatInfo", err);
81+
return NULL;
82+
}
83+
dirid= pb.d.ioDrParID;
84+
ebuf += strlen(p2cstr((unsigned char *)ebuf));
85+
/* Should check for buf overflow */
86+
}
87+
}
88+
89+
/* Finally, reverse the list of components and append it to cwd.
90+
Ebuf points at the EOS after last component,
91+
and there is an EOS before the first component.
92+
If there are no components, ebuf equals buf (but there
93+
is still an EOS where it points).
94+
Ecwd points at the EOS after the path built up so far,
95+
initially the volume name.
96+
We break out of the loop in the middle, thus
97+
appending a colon at the end in all cases. */
98+
99+
for (;;) {
100+
*ecwd++ = ':';
101+
if (ebuf == buf)
102+
break;
103+
do { } while (*--ebuf != EOS); /* Find component start */
104+
strcpy(ecwd, ebuf+1);
105+
ecwd= strchr(ecwd, EOS);
106+
}
107+
*ecwd= EOS;
108+
return cwd;
109+
}

Mac/Compat/mkdir.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Mkdir for the Macintosh.
2+
Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
3+
Pathnames must be Macintosh paths, with colons as separators. */
4+
5+
#include "macdefs.h"
6+
7+
/* Create a directory. */
8+
9+
int
10+
mkdir(path, mode)
11+
char *path;
12+
int mode; /* Ignored */
13+
{
14+
HFileParam pb;
15+
char name[MAXPATH];
16+
17+
if (!hfsrunning()) {
18+
errno= ENODEV;
19+
return -1;
20+
}
21+
strncpy(name, path, sizeof name);
22+
pb.ioNamePtr= (StringPtr) c2pstr(name);
23+
pb.ioVRefNum= 0;
24+
pb.ioDirID= 0;
25+
if (PBDirCreate((HParmBlkPtr)&pb, FALSE) != noErr) {
26+
errno= EACCES;
27+
return -1;
28+
}
29+
return 0;
30+
}

Mac/Compat/opendir.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Macintosh version of UNIX directory access package
3+
* (opendir, readdir, closedir).
4+
* Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
5+
*/
6+
7+
#include "dirent.h"
8+
#include "macdefs.h"
9+
10+
static DIR opened;
11+
12+
/*
13+
* Open a directory. This means calling PBOpenWD.
14+
* The value returned is always the address of opened, or NULL.
15+
* (I have as yet no use for multiple open directories; this could
16+
* be implemented by allocating memory dynamically.)
17+
*/
18+
19+
DIR *
20+
opendir(path)
21+
char *path;
22+
{
23+
union {
24+
WDPBRec d;
25+
VolumeParam v;
26+
} pb;
27+
char ppath[MAXPATH];
28+
short err;
29+
30+
if (opened.nextfile != 0) {
31+
errno = EBUSY;
32+
return NULL; /* A directory is already open. */
33+
}
34+
strncpy(ppath+1, path, ppath[0]= strlen(path));
35+
pb.d.ioNamePtr= (unsigned char *)ppath;
36+
pb.d.ioVRefNum= 0;
37+
if (hfsrunning()) {
38+
pb.d.ioWDProcID= 0;
39+
pb.d.ioWDDirID= 0;
40+
err= PBOpenWD((WDPBPtr)&pb, FALSE);
41+
}
42+
else {
43+
pb.v.ioVolIndex= 0;
44+
err= PBGetVInfo((ParmBlkPtr)&pb, FALSE);
45+
}
46+
if (err != noErr) {
47+
errno = ENOENT;
48+
return NULL;
49+
}
50+
opened.dirid= pb.d.ioVRefNum;
51+
opened.nextfile= 1;
52+
return &opened;
53+
}
54+
55+
/*
56+
* Close a directory.
57+
*/
58+
59+
void
60+
closedir(dirp)
61+
DIR *dirp;
62+
{
63+
if (hfsrunning()) {
64+
WDPBRec pb;
65+
66+
pb.ioVRefNum= dirp->dirid;
67+
(void) PBCloseWD(&pb, FALSE);
68+
}
69+
dirp->dirid= 0;
70+
dirp->nextfile= 0;
71+
}
72+
73+
/*
74+
* Read the next directory entry.
75+
*/
76+
77+
struct dirent *
78+
readdir(dp)
79+
DIR *dp;
80+
{
81+
union {
82+
DirInfo d;
83+
FileParam f;
84+
HFileInfo hf;
85+
} pb;
86+
short err;
87+
static struct dirent dir;
88+
89+
dir.d_name[0]= 0;
90+
pb.d.ioNamePtr= (unsigned char *)dir.d_name;
91+
pb.d.ioVRefNum= dp->dirid;
92+
pb.d.ioFDirIndex= dp->nextfile++;
93+
pb.d.ioDrDirID= 0;
94+
if (hfsrunning())
95+
err= PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
96+
else
97+
err= PBGetFInfo((ParmBlkPtr)&pb, FALSE);
98+
if (err != noErr) {
99+
errno = EIO;
100+
return NULL;
101+
}
102+
(void) p2cstr((unsigned char *)dir.d_name);
103+
return &dir;
104+
}

Mac/Compat/rmdir.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Rmdir for the Macintosh.
2+
Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
3+
Pathnames must be Macintosh paths, with colons as separators. */
4+
5+
#include "macdefs.h"
6+
7+
int
8+
rmdir(path)
9+
char *path;
10+
{
11+
IOParam pb;
12+
char name[MAXPATH];
13+
14+
strncpy(name, path, sizeof name);
15+
pb.ioNamePtr= (StringPtr) c2pstr(name);
16+
pb.ioVRefNum= 0;
17+
if (PBDelete((ParmBlkPtr)&pb, FALSE) != noErr) {
18+
errno= EACCES;
19+
return -1;
20+
}
21+
return 0;
22+
}

Mac/Compat/sync.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* The equivalent of the Unix 'sync' system call: FlushVol.
2+
Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
3+
For now, we only flush the default volume
4+
(since that's the only volume written to by MacB). */
5+
6+
#include "macdefs.h"
7+
8+
int
9+
sync()
10+
{
11+
if (FlushVol((StringPtr)0, 0) == noErr)
12+
return 0;
13+
else {
14+
errno= ENODEV;
15+
return -1;
16+
}
17+
}

0 commit comments

Comments
 (0)