mpw/mpw/mpw_access.cpp

185 lines
3.4 KiB
C++
Raw Normal View History

2013-02-16 20:37:37 +00:00
#include "mpw.h"
#include "mpw_internal.h"
#include "mpw_errno.h"
#include <algorithm>
#include <memory>
#include <string>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <fcntl.h>
2013-03-27 02:52:37 +00:00
#include <stdlib.h>
2013-02-16 20:37:37 +00:00
#include <sys/paths.h>
#include <cpu/defs.h>
#include <cpu/fmem.h>
#include <cpu/cpuModule.h>
#include <toolbox/toolbox.h>
#include <toolbox/os.h>
2013-02-26 01:16:55 +00:00
#include <toolbox/os_internal.h>
2013-02-16 20:37:37 +00:00
2013-02-17 17:25:20 +00:00
/*
* access return errors are |= 0x40000000. Not entirely sure why...
* may return an errno or an oserr, too, apparently.
*/
2013-02-16 20:37:37 +00:00
namespace MPW
{
2013-03-27 02:52:37 +00:00
uint32_t ftrap_delete(uint32_t name)
{
std::string sname;
int rv;
sname = ToolBox::ReadCString(name, true);
Log(" delete(%s)\n", sname.c_str());
rv = ::unlink(sname.c_str());
if (rv < 0) return 0x40000000 | errno_to_errno(errno);
return 0;
}
2013-02-16 20:37:37 +00:00
uint32_t ftrap_open(uint32_t name, uint32_t parm)
{
uint32_t d0;
int fd;
std::string sname;
MPWFile f;
int nativeFlags;
std::memset(&f, 0, sizeof(f));
f.flags = memoryReadWord(parm);
nativeFlags = 0;
switch (f.flags & 0x03)
{
case 0x01:
nativeFlags = O_RDONLY;
break;
case 0x02:
nativeFlags = O_WRONLY;
break;
case 0x00: // ????
case 0x03:
nativeFlags = O_RDWR;
break;
}
if (f.flags & kO_APPEND) nativeFlags |= O_APPEND;
if (f.flags & kO_CREAT) nativeFlags |= O_CREAT;
if (f.flags & kO_TRUNC) nativeFlags |= O_TRUNC;
if (f.flags & kO_EXCL) nativeFlags |= O_EXCL;
sname = ToolBox::ReadCString(name, true);
2013-02-26 01:40:57 +00:00
std::string xname = sname;
2013-02-16 20:37:37 +00:00
2013-02-16 23:32:37 +00:00
Log(" open(%s, %04x)\n", sname.c_str(), f.flags);
2013-02-16 20:37:37 +00:00
// TODO -- can you create a resource file like this?
if (f.flags & kO_RSRC)
sname.append(_PATH_RSRCFORKSPEC);
if (f.flags & kO_CREAT)
fd = ::open(sname.c_str(), nativeFlags, 0666);
else
fd = ::open(sname.c_str(), nativeFlags);
if (fd < 0)
{
2013-02-17 17:25:20 +00:00
// return an errno.
2013-02-16 20:37:37 +00:00
d0 = 0x40000000 | errno_to_errno(errno);
f.error = -36; // ioErr ... whatever.
f.cookie = 0;
}
else
{
d0 = 0;
f.error = 0;
f.cookie = fd;
// adjust the binary flags...
// some apps are good about this but
// dumpobj, makelib, linkiigs don't set O_BINARY (but should)
// MPW Assembler sets O_BINARY (but shouldn't)
2013-02-16 20:37:37 +00:00
if (OS::IsTextFile(sname)) f.flags &= ~kO_BINARY;
if (OS::IsBinaryFile(sname)) f.flags |= kO_BINARY;
if (f.flags & kO_RSRC) f.flags |= kO_BINARY;
2013-02-26 01:40:57 +00:00
auto &e = OS::Internal::FDEntry::allocate(fd, std::move(xname));
2013-02-26 01:16:55 +00:00
e.text = !(f.flags & kO_BINARY);
e.resource = f.flags & kO_RSRC;
2013-02-16 20:37:37 +00:00
}
memoryWriteWord(f.flags, parm + 0);
memoryWriteWord(f.error, parm + 2);
memoryWriteLong(f.cookie, parm + 8);
return d0;
}
2013-03-27 02:52:37 +00:00
2013-02-16 20:37:37 +00:00
void ftrap_access(uint16_t trap)
{
// open a file, rename a file, or delete a file.
std::string sname;
uint32_t d0;
uint32_t sp = cpuGetAReg(7);
// hmmm not sure if 3 or 4 parameters.
uint32_t name = memoryReadLong(sp + 4);
uint32_t op = memoryReadLong(sp + 8);
uint32_t parm = memoryReadLong(sp + 12);
2013-02-16 23:32:37 +00:00
Log("%04x Access(%08x, %04x, %08x)\n", trap, name, op, parm);
2013-02-16 20:37:37 +00:00
switch (op)
{
case kF_OPEN:
d0 = ftrap_open(name, parm);
break;
2013-03-27 02:52:37 +00:00
case kF_DELETE:
d0 = ftrap_delete(name);
break;
2013-03-07 02:51:12 +00:00
case kF_GTABINFO:
d0 = 0x40000000 | kEINVAL;
break;
2013-05-23 23:10:12 +00:00
case kF_STABINFO:
d0 = 0x40000000 | kEINVAL;
break;
2013-03-07 02:51:12 +00:00
2013-02-16 20:37:37 +00:00
default:
d0 = 0x40000000 | kEINVAL;
2013-03-27 02:52:37 +00:00
fprintf(stderr, "faccess - unsupported op %04x\n", op);
exit(1);
2013-02-16 20:37:37 +00:00
}
cpuSetDReg(0, d0);
}
}