mpw/toolbox/os.cpp

477 lines
8.6 KiB
C++
Raw Normal View History

2013-02-12 22:32:31 +00:00
#include "os.h"
2013-02-13 03:35:15 +00:00
#include "toolbox.h"
2013-02-14 03:16:26 +00:00
#include "mpw_time.h"
2013-02-12 22:32:31 +00:00
#include <cpu/defs.h>
#include <cpu/CpuModule.h>
#include <cpu/fmem.h>
#include <string>
2013-02-13 03:35:15 +00:00
#include <cerrno>
2013-02-16 18:32:47 +00:00
#include <cctype>
2013-02-12 22:32:31 +00:00
#include <sys/xattr.h>
#include <sys/stat.h>
#include <sys/paths.h>
#include <stdlib.h>
2013-02-14 03:16:26 +00:00
#include <unistd.h>
#include <fcntl.h>
2013-02-12 22:32:31 +00:00
2013-02-16 18:32:47 +00:00
#include <strings.h>
2013-02-16 23:51:28 +00:00
using ToolBox::Log;
2013-02-13 03:35:15 +00:00
namespace {
using namespace OS;
2013-02-16 18:32:47 +00:00
// should make this public since it's needed by mpw/*
2013-02-13 03:35:15 +00:00
uint16_t errno_to_oserr(int xerrno)
{
switch (xerrno)
{
2013-02-14 03:38:34 +00:00
case 0: return 0;
2013-02-13 03:35:15 +00:00
case EBADF: return rfNumErr;
case EIO: return ioErr;
case EACCES: return permErr;
case ENOENT: return fnfErr;
case ENOTDIR: return dirNFErr;
case EISDIR: return notAFileErr;
case ENOTSUP: return extFSErr;
case EROFS: return wPrErr;
2013-02-14 03:16:26 +00:00
case EEXIST: return dupFNErr;
2013-02-14 03:38:34 +00:00
case EBUSY: return fBsyErr;
2013-02-13 03:35:15 +00:00
case EDQUOT: return dskFulErr;
case ENOSPC: return dskFulErr;
default:
return ioErr;
}
}
2013-02-16 18:32:47 +00:00
std::string extension(const std::string &s)
{
std::string tmp;
int pos;
pos = s.find_last_of("./:");
if (pos == s.npos) return tmp;
if (s[pos++] != '.') return tmp;
if (pos >= s.length()) return tmp;
tmp = s.substr(pos);
std::transform(tmp.begin(), tmp.end(), tmp.begin(),
[](char c) { return tolower(c); }
);
return tmp;
}
2013-02-13 03:35:15 +00:00
}
2013-02-12 22:32:31 +00:00
namespace OS
{
2013-02-16 18:32:47 +00:00
// known text file extensions
bool IsTextFile(const std::string &s)
{
std::string ext = extension(s);
if (ext.empty()) return false;
char c = ext[0];
switch(c)
{
case 'a':
2013-02-17 06:17:14 +00:00
if (ext == "aii") // assembler
2013-02-16 18:32:47 +00:00
return true;
break;
case 'c':
if (ext == "c")
return true;
break;
2013-02-17 06:17:14 +00:00
case 'l':
if (ext == "lst") // asm iigs listing
return true;
break;
2013-02-17 00:50:40 +00:00
case 'm':
if (ext == "macros")
return true;
break;
2013-02-16 18:32:47 +00:00
case 'p':
2013-02-17 06:17:14 +00:00
if (ext == "pii") // pascal
2013-02-16 18:32:47 +00:00
return true;
break;
case 'r':
2013-02-17 06:17:14 +00:00
if (ext == "rii") // rez
return true;
break;
case 's':
if (ext == "src") // asm equates
2013-02-16 18:32:47 +00:00
return true;
break;
}
return false;
}
// known binary file extensions
bool IsBinaryFile(const std::string &s)
{
std::string ext = extension(s);
if (ext.empty()) return false;
char c = ext[0];
switch(c)
{
case 'o':
if (ext == "obj")
return true;
break;
}
return false;
}
2013-02-14 03:16:26 +00:00
uint16_t Create(uint16_t trap)
{
uint32_t d0;
uint32_t parm = cpuGetAReg(0);
2013-02-16 23:51:28 +00:00
Log("%04x Create(%08x)\n", trap, parm);
2013-02-14 03:16:26 +00:00
2013-02-16 23:51:28 +00:00
//uint32_t ioCompletion = memoryReadLong(parm + 12);
2013-02-14 03:16:26 +00:00
uint32_t namePtr = memoryReadLong(parm + 18);
2013-02-16 23:51:28 +00:00
//uint16_t ioVRefNum = memoryReadWord(parm + 22);
//uint8_t ioFVersNum = memoryReadByte(parm + 26);
2013-02-14 03:16:26 +00:00
std::string sname = ToolBox::ReadPString(namePtr, true);
2013-02-14 03:16:26 +00:00
if (!sname.length())
{
memoryWriteWord(bdNamErr, parm + 16);
return bdNamErr;
}
2013-02-16 23:51:28 +00:00
Log(" Create(%s)\n", sname.c_str());
2013-02-14 03:16:26 +00:00
int fd;
fd = ::open(sname.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0)
{
d0 = errno_to_oserr(errno);
}
else
{
::close(fd);
d0 = 0;
}
memoryWriteWord(d0, parm + 16);
return d0;
2013-02-14 03:38:34 +00:00
}
uint16_t Delete(uint16_t trap)
{
uint32_t d0;
uint32_t parm = cpuGetAReg(0);
2013-02-16 23:51:28 +00:00
Log("%04x Delete(%08x)\n", trap, parm);
2013-02-14 03:38:34 +00:00
2013-02-16 23:51:28 +00:00
//uint32_t ioCompletion = memoryReadLong(parm + 12);
2013-02-14 03:38:34 +00:00
uint32_t namePtr = memoryReadLong(parm + 18);
2013-02-16 23:51:28 +00:00
//uint16_t ioVRefNum = memoryReadWord(parm + 22);
//uint8_t ioFVersNum = memoryReadByte(parm + 26);
2013-02-14 03:16:26 +00:00
std::string sname = ToolBox::ReadPString(namePtr, true);
2013-02-14 03:38:34 +00:00
if (!sname.length())
{
memoryWriteWord(bdNamErr, parm + 16);
return bdNamErr;
}
2013-02-16 23:51:28 +00:00
Log(" Delete(%s)\n", sname.c_str());
2013-02-14 03:38:34 +00:00
int ok = ::unlink(sname.c_str());
if (ok < 0)
d0 = errno_to_oserr(errno);
else
d0 = 0;
memoryWriteWord(d0, parm + 16);
return d0;
2013-02-14 03:16:26 +00:00
}
2013-02-16 21:11:10 +00:00
uint16_t GetEOF(uint16_t trap)
{
uint32_t d0;
size_t size;
uint32_t parm = cpuGetAReg(0);
2013-02-16 23:51:28 +00:00
Log("%04x GetEOF(%08x)\n", trap, parm);
2013-02-16 21:11:10 +00:00
2013-02-16 23:51:28 +00:00
//uint32_t ioCompletion = memoryReadLong(parm + 12);
2013-02-16 21:11:10 +00:00
uint16_t ioRefNum = memoryReadWord(parm + 24);
struct stat st;
if (::fstat(ioRefNum, &st) < 0)
{
d0 = errno_to_oserr(errno);
size = 0;
}
else
{
d0 = 0;
size = st.st_size;
}
memoryWriteWord(d0, parm + 16);
memoryWriteLong(size, parm + 28);
return d0;
}
2013-02-14 03:16:26 +00:00
// return the name of the default volume.
// this does not translate well.
uint16_t GetVol(uint16_t trap)
{
uint32_t parm = cpuGetAReg(0);
2013-02-16 23:51:28 +00:00
Log("%04x GetVol(%08x)\n", trap, parm);
2013-02-14 03:16:26 +00:00
2013-02-16 23:51:28 +00:00
//uint32_t ioCompletion = memoryReadLong(parm + 12);
2013-02-14 03:16:26 +00:00
uint32_t namePtr = memoryReadLong(parm + 18);
// ioResult
memoryWriteWord(0, parm + 16);
// ioVRefNum
memoryWriteWord(0, parm + 22);
std::string tmp = "MacOS";
ToolBox::WritePString(namePtr, tmp);
return 0;
}
2013-02-12 22:32:31 +00:00
uint16_t GetFileInfo(uint16_t trap)
{
2013-02-13 03:35:15 +00:00
uint16_t d0;
uint32_t parm = cpuGetAReg(0);
2013-02-12 22:32:31 +00:00
2013-02-16 23:51:28 +00:00
Log("%04x GetFileInfo(%08x)\n", trap, parm);
2013-02-12 22:32:31 +00:00
2013-02-16 23:51:28 +00:00
//uint32_t ioCompletion = memoryReadLong(parm + 12);
2013-02-12 22:32:31 +00:00
uint32_t ioNamePtr = memoryReadLong(parm + 18);
2013-02-16 23:51:28 +00:00
//uint16_t ioVRefNum = memoryReadWord(parm + 22);
//uint8_t ioFVersNum = memoryReadByte(parm + 26);
2013-02-12 22:32:31 +00:00
int16_t ioFDirIndex = memoryReadWord(parm + 28);
if (ioFDirIndex <= 0)
{
// based name
std::string sname;
if (!ioNamePtr)
{
memoryWriteWord(bdNamErr, parm + 16);
return bdNamErr;
}
sname = ToolBox::ReadPString(ioNamePtr, true);
2013-02-12 22:32:31 +00:00
2013-02-16 23:51:28 +00:00
Log(" GetFileInfo(%s)\n", sname.c_str());
2013-02-14 03:16:26 +00:00
2013-02-12 22:32:31 +00:00
// todo -- how are absolute, relative, etc paths handled...
struct stat st;
if (::stat(sname.c_str(), &st) < 0)
{
2013-02-13 03:35:15 +00:00
d0 = errno_to_oserr(errno);
memoryWriteWord(d0, parm + 16);
return d0;
2013-02-12 22:32:31 +00:00
}
// finder info
{
uint8_t buffer[32];
std::memset(buffer, 0, sizeof(buffer));
int rv;
int xerrno;
2013-02-12 22:32:31 +00:00
rv = ::getxattr(sname.c_str(), XATTR_FINDERINFO_NAME, buffer, 32, 0, 0);
xerrno = errno;
2013-02-12 22:32:31 +00:00
2013-02-16 18:32:47 +00:00
// override for source files.
if (IsTextFile(sname))
{
std::memcpy(buffer, "TEXTMPS ", 8);
}
2013-02-12 22:32:31 +00:00
// only 16 bytes copied.
std::memcpy(memoryPointer(parm + 32), buffer, 16);
}
// file reference number
memoryWriteWord(0, parm + 24);
// file attributes
memoryWriteByte(0, parm + 30);
2013-02-12 22:32:31 +00:00
// version (unused)
memoryWriteByte(0, parm + 31);
2013-02-12 22:32:31 +00:00
// file id
memoryWriteLong(0, parm + 48);
// file size
memoryWriteWord(0, parm + 52);
memoryWriteLong(st.st_size, parm + 54);
memoryWriteLong(st.st_size, parm + 58);
// create date.
2013-02-14 03:16:26 +00:00
memoryWriteLong(Time::UnixToMac(st.st_birthtime), parm + 72);
memoryWriteLong(Time::UnixToMac(st.st_mtime), parm + 76);
2013-02-12 22:32:31 +00:00
// res fork...
// do this last since it adjusts the name and the stat.
sname.append(_PATH_RSRCFORKSPEC);
if (::stat(sname.c_str(), &st) >= 0)
{
memoryWriteWord(0, parm + 62);
memoryWriteLong(st.st_size, parm + 64);
memoryWriteLong(st.st_size, parm + 68);
}
else
{
memoryWriteWord(0, parm + 62);
memoryWriteLong(0, parm + 64);
memoryWriteLong(0, parm + 68);
}
// no error.
memoryWriteWord(0, parm + 16);
}
else
{
fprintf(stderr, "GetFileInfo -- ioFDirIndex not yet supported\n");
exit(1);
}
// if iocompletion handler && asyn call....
return 0;
}
2013-02-13 03:35:15 +00:00
uint16_t SetFileInfo(uint16_t trap)
{
std::string sname;
uint16_t d0;
uint32_t parm = cpuGetAReg(0);
2013-02-16 23:51:28 +00:00
Log("%04x SetFileInfo(%08x)\n", trap, parm);
2013-02-13 03:35:15 +00:00
2013-02-16 23:51:28 +00:00
//uint32_t ioCompletion = memoryReadLong(parm + 12);
2013-02-13 03:35:15 +00:00
uint32_t ioNamePtr = memoryReadLong(parm + 18);
2013-02-16 23:51:28 +00:00
//uint16_t ioVRefNum = memoryReadWord(parm + 22);
//uint8_t ioFVersNum = memoryReadByte(parm + 26);
2013-02-13 03:35:15 +00:00
//int16_t ioFDirIndex = memoryReadWord(parm + 28);
// + 32 = finder data - 16 bytes.
2013-02-16 23:51:28 +00:00
//uint32_t ioFlCrDat = memoryReadLong(parm + 72);
//uint32_t ioFlMdDat = memoryReadLong(parm + 76);
2013-02-13 03:35:15 +00:00
// currently, only sets finder info.
if (!ioNamePtr)
{
memoryWriteWord(bdNamErr, parm + 16);
return bdNamErr;
}
sname = ToolBox::ReadPString(ioNamePtr, true);
2013-02-16 23:51:28 +00:00
Log(" SetFileInfo(%s)\n", sname.c_str());
2013-02-13 03:35:15 +00:00
// check if the file actually exists
{
struct stat st;
int ok;
ok = ::stat(sname.c_str(), &st);
if (ok < 0)
{
d0 = errno_to_oserr(errno);
memoryWriteWord(d0, parm + 16);
return d0;
}
}
// finder info is actually 32 bytes, so read and update the first 16.
{
uint8_t buffer[32];
int ok;
std::memset(buffer, 0, sizeof(buffer));
ok = ::getxattr(sname.c_str(), XATTR_FINDERINFO_NAME, buffer, 32, 0, 0);
// only 16 bytes copied.
std::memcpy(buffer, memoryPointer(parm + 32), 16);
ok = ::setxattr(sname.c_str(), XATTR_FINDERINFO_NAME, buffer, 32, 0, 0);
if (ok < 0)
{
d0 = errno_to_oserr(errno);
memoryWriteWord(d0, parm + 16);
return d0;
}
}
d0 = 0;
memoryWriteWord(d0, parm + 16);
return d0;
}
2013-02-12 22:32:31 +00:00
}