Skip to content

Scottcjn/posix9

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

License Mac OS 9 POSIX

POSIX9 - POSIX Compatibility Layer for Classic Mac OS

A shim library providing POSIX-compatible APIs on top of Classic Mac OS Toolbox calls, enabling modern Unix software to compile and run on vintage Macintosh computers.

Supported Platforms

Platform CPU OS Version Toolchain Status
68K Mac 68020+ System 7.0 - 8.1 Retro68 (m68k) Supported
PowerPC Mac G3/G4/G5 Mac OS 7.5.2 - 9.2.2 Retro68 (ppc) Primary

Features

  • File I/O: open, read, write, close, lseek, stat, fstat, unlink
  • Directories: opendir, readdir, closedir, mkdir, rmdir, chdir, getcwd
  • Path Translation: Automatic POSIX ↔ Mac path conversion
  • Sockets: BSD socket API via Open Transport (Mac OS 8.6+)
  • Threads: POSIX threads via Thread Manager
  • Signals: Emulated signal handling via Deferred Tasks
  • Time: time, localtime, strftime, gettimeofday
  • Environment: getenv, setenv, unsetenv

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  POSIX Application (SSH, etc.)      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  libposix9.a                        β”‚
β”‚  β”œβ”€β”€ posix9_file.c    (file I/O)   β”‚
β”‚  β”œβ”€β”€ posix9_dir.c     (directories)β”‚
β”‚  β”œβ”€β”€ posix9_path.c    (path xlat)  β”‚
β”‚  β”œβ”€β”€ posix9_thread.c  (pthreads)   β”‚
β”‚  β”œβ”€β”€ posix9_signal.c  (signals)    β”‚
β”‚  β”œβ”€β”€ posix9_socket.c  (networking) β”‚
β”‚  └── posix9_misc.c    (utilities)  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Mac OS Toolbox                     β”‚
β”‚  β”œβ”€β”€ File Manager (FSSpec/HFS)     β”‚
β”‚  β”œβ”€β”€ Open Transport (TCP/IP)       β”‚
β”‚  β”œβ”€β”€ Thread Manager                β”‚
β”‚  └── Time Manager                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Path Translation

POSIX9 automatically converts between POSIX and Mac paths:

POSIX Path Mac Path
/ Volume root (default volume)
/Volumes/Macintosh HD/Users/scott Macintosh HD:Users:scott
./foo/bar :foo:bar
../parent ::parent

Building with Retro68

Prerequisites

  1. Retro68 Cross-Compiler: https://github.com/autc04/Retro68
# Clone Retro68
git clone https://github.com/autc04/Retro68.git
cd Retro68

# Build for both 68K and PowerPC
mkdir build && cd build
../build-toolchain.bash --ppc-only   # PowerPC only (faster)
# OR
../build-toolchain.bash               # Full build (68K + PPC)

Building POSIX9

PowerPC Build (Mac OS 7.5.2 - 9.2.2)

cd posix9
./build-ppc.sh

This creates build-ppc/libposix9.a (~50KB) with the following modules:

  • posix9_dir.o - Directory operations
  • posix9_file.o - File I/O
  • posix9_path.o - Path translation
  • posix9_signal.o - Signal emulation
  • posix9_thread.o - POSIX threads

68K Build (System 7.0 - 8.1)

cd posix9
./build-68k.sh

Building Your Application

# Set environment variables
export RETRO68_PREFIX="$HOME/Retro68/build/toolchain"
export POSIX9_LIB="/path/to/posix9/build-ppc/libposix9.a"
export POSIX9_INC="/path/to/posix9/include"

# Compile (PowerPC)
$RETRO68_PREFIX/bin/powerpc-apple-macos-gcc \
    -O2 \
    -I"$POSIX9_INC" \
    -I"$POSIX9_INC/mac_stubs" \
    -c myapp.c -o myapp.o

# Link
$RETRO68_PREFIX/bin/powerpc-apple-macos-gcc \
    myapp.o "$POSIX9_LIB" -o myapp.xcoff

# Convert to PEF (Mac executable)
$RETRO68_PREFIX/bin/MakePEF myapp.xcoff -o myapp.pef

API Reference

File I/O

#include "posix9.h"

int fd = open("/path/to/file", O_RDONLY);
char buf[1024];
ssize_t n = read(fd, buf, sizeof(buf));
close(fd);

// Create file
fd = open("/new/file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
write(fd, "Hello Mac OS 9!
", 16);
close(fd);

Directories

DIR *dir = opendir("/");
struct dirent *ent;
while ((ent = readdir(dir)) != NULL) {
    printf("%s
", ent->d_name);
}
closedir(dir);

mkdir("/new/directory", 0755);
chdir("/some/path");
getcwd(buf, sizeof(buf));

Path Translation

char mac_path[256];
posix9_path_to_mac("/Volumes/Macintosh HD/file.txt", mac_path, sizeof(mac_path));
// Result: "Macintosh HD:file.txt"

char posix_path[256];
posix9_path_from_mac("Macintosh HD:Users:scott", posix_path, sizeof(posix_path));
// Result: "/Volumes/Macintosh HD/Users/scott"

Sockets (Mac OS 8.6+ with Open Transport)

int sock = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(22);
addr.sin_addr.s_addr = inet_addr("192.168.1.1");

connect(sock, (struct sockaddr*)&addr, sizeof(addr));
send(sock, "Hello
", 6, 0);
close(sock);

Threads

#include "posix9.h"

void *thread_func(void *arg) {
    // Thread code
    return NULL;
}

pthread_t tid;
pthread_create(&tid, NULL, thread_func, NULL);
pthread_join(tid, NULL);

Limitations

No Process Model

Mac OS 9 has no true process model. These functions are stubbed:

  • fork() - Returns -1 with ENOSYS
  • exec*() - Launches separate app (doesn't replace current process)
  • getpid() - Always returns 1

Networking

  • Sockets require Open Transport (Mac OS 8.6+)
  • System 7 has no TCP/IP stack by default
  • MacTCP is not supported

Signals

Signals are emulated via polling. Some behaviors differ from Unix:

  • No async signal delivery (polled in event loop)
  • SIGKILL/SIGSTOP not implemented

File Systems

  • No symbolic links (HFS/HFS+ limitation)
  • 31-character filename limit (HFS)
  • No file permissions (mode is ignored)

Directory Structure

posix9/
β”œβ”€β”€ include/
β”‚   β”œβ”€β”€ posix9.h              # Main header (include this)
β”‚   β”œβ”€β”€ posix9/
β”‚   β”‚   β”œβ”€β”€ types.h           # POSIX types
β”‚   β”‚   β”œβ”€β”€ errno.h           # Error codes
β”‚   β”‚   β”œβ”€β”€ socket.h          # Socket definitions
β”‚   β”‚   β”œβ”€β”€ pthread.h         # Thread definitions
β”‚   β”‚   β”œβ”€β”€ signal.h          # Signal definitions
β”‚   β”‚   β”œβ”€β”€ time.h            # Time definitions
β”‚   β”‚   └── unistd.h          # Misc definitions
β”‚   └── mac_stubs/
β”‚       β”œβ”€β”€ Multiverse.h      # Retro68 Mac API
β”‚       β”œβ”€β”€ MacCompat.h       # Missing Mac definitions
β”‚       β”œβ”€β”€ Timer.h           # Time Manager stubs
β”‚       β”œβ”€β”€ Threads.h         # Thread Manager stubs
β”‚       └── OpenTransport.h   # OT stubs
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ posix9_file.c         # File operations
β”‚   β”œβ”€β”€ posix9_dir.c          # Directory operations
β”‚   β”œβ”€β”€ posix9_path.c         # Path translation
β”‚   β”œβ”€β”€ posix9_thread.c       # POSIX threads
β”‚   β”œβ”€β”€ posix9_signal.c       # Signal emulation
β”‚   β”œβ”€β”€ posix9_socket.c       # BSD sockets
β”‚   └── posix9_misc.c         # Misc utilities
β”œβ”€β”€ test/
β”‚   β”œβ”€β”€ posix9_test.c         # Test program
β”‚   └── build-test.sh         # Test build script
β”œβ”€β”€ build-ppc.sh              # PowerPC build script
β”œβ”€β”€ build-68k.sh              # 68K build script
└── README.md                 # This file

Testing on Real Hardware

  1. Build the test program:
cd test
./build-test.sh
  1. Transfer POSIX9 Test to your Mac via:

    • Floppy disk
    • Network (AppleTalk/AFP)
    • Serial connection (Zterm)
    • Compact Flash adapter
  2. Run on Mac OS 9 - creates POSIX9 Test Log file with results

Related Projects

  • GUSI - Grand Unified Socket Interface by Matthias Neeracher
  • MachTen - Full BSD on Mac OS (commercial, discontinued)
  • A/UX - Apple's Unix for 68K Macs
  • MPW POSIX - Partial POSIX in Macintosh Programmer's Workshop
  • Retro68 - Cross-compiler for Classic Mac OS

Target Applications

POSIX9 was created to port:

  • Dropbear SSH - Lightweight SSH server
  • curl/wget - HTTP clients
  • Python 2.x - Scripting language
  • Various Unix utilities

License

MIT License

Contributing

Contributions welcome! Areas needing work:

  • Complete posix9_socket.c Open Transport implementation
  • Fix posix9_misc.c type conflicts with newlib
  • Add 68K-specific optimizations
  • Test on real System 7 hardware
  • Port more Unix software

Status

Module PPC 68K Notes
posix9_file.c OK OK File operations
posix9_dir.c OK OK Directory operations
posix9_path.c OK OK Path translation
posix9_signal.c OK OK Signal emulation
posix9_thread.c OK OK POSIX threads
posix9_socket.c WIP WIP Needs OT constants
posix9_misc.c WIP WIP Type conflicts

Current library size: ~50KB (5 modules)

About

POSIX compatibility layer for Mac OS 9 - enables SSH and Unix tools on classic Mac

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages