1 |
|
/* |
2 |
|
* sys_unix.cpp - System dependent routines, Unix implementation |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2003 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2005 Christian Bauer |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
24 |
|
#include <sys/stat.h> |
25 |
|
#include <errno.h> |
26 |
|
|
27 |
+ |
#ifdef HAVE_AVAILABILITYMACROS_H |
28 |
+ |
#include <AvailabilityMacros.h> |
29 |
+ |
#endif |
30 |
+ |
|
31 |
|
#ifdef __linux__ |
32 |
|
#include <sys/mount.h> |
33 |
|
#include <linux/cdrom.h> |
34 |
|
#include <linux/fd.h> |
35 |
|
#include <linux/major.h> |
36 |
|
#include <linux/kdev_t.h> |
33 |
– |
#include <linux/unistd.h> |
37 |
|
#include <dirent.h> |
35 |
– |
|
36 |
– |
#ifdef __NR__llseek |
37 |
– |
_syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo, loff_t *, res, unsigned int, wh); |
38 |
– |
#else |
39 |
– |
static int _llseek(unsigned int fd, unsigned long hi, unsigned long lo, loff_t *res, unsigned int wh) |
40 |
– |
{ |
41 |
– |
if (hi) |
42 |
– |
return -1; |
43 |
– |
*res = lseek(fd, lo, wh); |
44 |
– |
if (*res == -1) |
45 |
– |
return -1; |
46 |
– |
return 0; |
47 |
– |
} |
48 |
– |
#endif |
38 |
|
#endif |
39 |
|
|
40 |
|
#if defined(__FreeBSD__) || defined(__NetBSD__) |
53 |
|
|
54 |
|
// File handles are pointers to these structures |
55 |
|
struct file_handle { |
56 |
< |
char *name; // Copy of device/file name |
56 |
> |
char *name; // Copy of device/file name |
57 |
|
int fd; |
58 |
|
bool is_file; // Flag: plain file or /dev/something? |
59 |
|
bool is_floppy; // Flag: floppy device |
113 |
|
void SysAddFloppyPrefs(void) |
114 |
|
{ |
115 |
|
#if defined(__linux__) |
116 |
< |
if (access("/dev/.devfsd", F_OK) < 0) { |
117 |
< |
PrefsAddString("floppy", "/dev/fd0u1440"); |
118 |
< |
PrefsAddString("floppy", "/dev/fd1u1440"); |
119 |
< |
} else { |
120 |
< |
DIR *fd_dir = opendir("/dev/floppy"); |
121 |
< |
if (fd_dir) { |
122 |
< |
struct dirent *floppy_dev; |
123 |
< |
while ((floppy_dev = readdir(fd_dir)) != NULL) { |
135 |
< |
if (strstr(floppy_dev->d_name, "u1440") != NULL) { |
136 |
< |
char fd_dev[20]; |
137 |
< |
sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name); |
138 |
< |
PrefsAddString("floppy", fd_dev); |
139 |
< |
} |
116 |
> |
DIR *fd_dir = opendir("/dev/floppy"); |
117 |
> |
if (fd_dir) { |
118 |
> |
struct dirent *floppy_dev; |
119 |
> |
while ((floppy_dev = readdir(fd_dir)) != NULL) { |
120 |
> |
if (strstr(floppy_dev->d_name, "u1440") != NULL) { |
121 |
> |
char fd_dev[20]; |
122 |
> |
sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name); |
123 |
> |
PrefsAddString("floppy", fd_dev); |
124 |
|
} |
141 |
– |
closedir(fd_dir); |
125 |
|
} |
126 |
+ |
closedir(fd_dir); |
127 |
+ |
} else { |
128 |
+ |
PrefsAddString("floppy", "/dev/fd0"); |
129 |
+ |
PrefsAddString("floppy", "/dev/fd1"); |
130 |
|
} |
131 |
|
#elif defined(__NetBSD__) |
132 |
|
PrefsAddString("floppy", "/dev/fd0a"); |
133 |
|
PrefsAddString("floppy", "/dev/fd1a"); |
134 |
|
#elif defined(__APPLE__) && defined(__MACH__) |
135 |
< |
// FIXME: We assume an Aqua build causes <AvailabilityMacros.h> to |
136 |
< |
// be included, thusly enabling this part of code that would cause |
137 |
< |
// Basilisk II to hang otherwise. |
138 |
< |
#ifdef MAC_OS_X_VERSION_10_0 |
139 |
< |
PrefsAddString("floppy", "/dev/fd/0"); |
140 |
< |
PrefsAddString("floppy", "/dev/fd/1"); |
141 |
< |
#endif |
135 |
> |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION) |
136 |
> |
extern void DarwinAddFloppyPrefs(void); |
137 |
> |
|
138 |
> |
DarwinAddFloppyPrefs(); |
139 |
> |
#else |
140 |
> |
// Until I can convince the other guys that my Darwin code is useful, |
141 |
> |
// we just add something safe (a non-existant device): |
142 |
> |
PrefsAddString("floppy", "/dev/null"); |
143 |
> |
#endif |
144 |
|
#else |
145 |
|
PrefsAddString("floppy", "/dev/fd0"); |
146 |
|
PrefsAddString("floppy", "/dev/fd1"); |
211 |
|
closedir(cd_dir); |
212 |
|
} |
213 |
|
} |
214 |
< |
#elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_0) |
214 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
215 |
> |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION) |
216 |
|
extern void DarwinAddCDROMPrefs(void); |
217 |
|
|
218 |
|
DarwinAddCDROMPrefs(); |
219 |
+ |
#else |
220 |
+ |
// Until I can convince the other guys that my Darwin code is useful, |
221 |
+ |
// we just do nothing (it is safe to have no cdrom device) |
222 |
+ |
#endif |
223 |
|
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
224 |
|
PrefsAddString("cdrom", "/dev/cd0c"); |
225 |
|
#endif |
247 |
|
PrefsAddString("seriala", "/dev/tty00"); |
248 |
|
PrefsAddString("serialb", "/dev/tty01"); |
249 |
|
#elif defined(__APPLE__) && defined(__MACH__) |
250 |
< |
// FIXME: We assume an Aqua build causes <AvailabilityMacros.h> to |
251 |
< |
// be included, thusly enabling this part of code that would cause |
252 |
< |
// Basilisk II to hang otherwise. |
253 |
< |
#ifdef MAC_OS_X_VERSION_10_0 |
254 |
< |
PrefsAddString("seriala", "/dev/ttys0"); |
255 |
< |
PrefsAddString("serialb", "/dev/ttys1"); |
256 |
< |
#endif |
257 |
< |
// PrefsAddString("seriala", "/dev/cu.modem"); |
258 |
< |
// PrefsAddString("serialb", "/dev/cu.IrDA-IrCOMMch-b"); |
250 |
> |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION) |
251 |
> |
extern void DarwinAddSerialPrefs(void); |
252 |
> |
|
253 |
> |
DarwinAddSerialPrefs(); |
254 |
> |
#else |
255 |
> |
// Until I can convince the other guys that my Darwin code is useful, |
256 |
> |
// we just add something safe (non-existant devices): |
257 |
> |
PrefsAddString("seriala", "/dev/null"); |
258 |
> |
PrefsAddString("serialb", "/dev/null"); |
259 |
> |
#endif |
260 |
|
#endif |
261 |
|
} |
262 |
|
|
308 |
|
#else |
309 |
|
bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0; |
310 |
|
#endif |
311 |
+ |
bool is_floppy = strncmp(name, "/dev/fd", 7) == 0; |
312 |
|
|
313 |
|
#if defined(__APPLE__) && defined(__MACH__) |
314 |
|
// |
361 |
|
read_only = true; |
362 |
|
fd = open(name, O_RDONLY); |
363 |
|
} |
364 |
< |
if (fd >= 0) { |
364 |
> |
if (fd >= 0 || is_floppy) { // Floppy open fails if there's no disk inserted |
365 |
|
file_handle *fh = new file_handle; |
366 |
|
fh->name = strdup(name); |
367 |
|
fh->fd = fd; |
368 |
|
fh->is_file = is_file; |
369 |
|
fh->read_only = read_only; |
370 |
|
fh->start_byte = 0; |
371 |
< |
fh->is_floppy = false; |
372 |
< |
fh->is_cdrom = false; |
371 |
> |
fh->is_floppy = is_floppy; |
372 |
> |
fh->is_cdrom = is_cdrom; |
373 |
|
if (fh->is_file) { |
374 |
|
// Detect disk image file layout |
375 |
|
loff_t size = 0; |
380 |
– |
#if defined(__linux__) |
381 |
– |
_llseek(fh->fd, 0, 0, &size, SEEK_END); |
382 |
– |
#else |
376 |
|
size = lseek(fd, 0, SEEK_END); |
384 |
– |
#endif |
377 |
|
uint8 data[256]; |
378 |
|
lseek(fd, 0, SEEK_SET); |
379 |
|
read(fd, data, 256); |
459 |
|
if (!fh) |
460 |
|
return; |
461 |
|
|
462 |
< |
close(fh->fd); |
462 |
> |
if (fh->fd >= 0) |
463 |
> |
close(fh->fd); |
464 |
|
if (fh->name) |
465 |
|
free(fh->name); |
466 |
|
delete fh; |
479 |
|
return 0; |
480 |
|
|
481 |
|
// Seek to position |
489 |
– |
#if defined(__linux__) |
490 |
– |
loff_t pos = offset + fh->start_byte, res; |
491 |
– |
if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0) |
492 |
– |
return 0; |
493 |
– |
#else |
482 |
|
if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0) |
483 |
|
return 0; |
496 |
– |
#endif |
484 |
|
|
485 |
|
// Read data |
486 |
|
return read(fh->fd, buffer, length); |
499 |
|
return 0; |
500 |
|
|
501 |
|
// Seek to position |
515 |
– |
#if defined(__linux__) |
516 |
– |
loff_t pos = offset + fh->start_byte, res; |
517 |
– |
if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0) |
518 |
– |
return 0; |
519 |
– |
#else |
502 |
|
if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0) |
503 |
|
return 0; |
522 |
– |
#endif |
504 |
|
|
505 |
|
// Write data |
506 |
|
return write(fh->fd, buffer, length); |
545 |
|
|
546 |
|
#if defined(__linux__) |
547 |
|
if (fh->is_floppy) { |
548 |
< |
fsync(fh->fd); |
549 |
< |
ioctl(fh->fd, FDFLUSH); |
550 |
< |
ioctl(fh->fd, FDEJECT); |
551 |
< |
close(fh->fd); // Close and reopen so the driver will see the media change |
548 |
> |
if (fh->fd >= 0) { |
549 |
> |
fsync(fh->fd); |
550 |
> |
ioctl(fh->fd, FDFLUSH); |
551 |
> |
ioctl(fh->fd, FDEJECT); |
552 |
> |
close(fh->fd); // Close and reopen so the driver will see the media change |
553 |
> |
} |
554 |
|
fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR); |
555 |
|
} else if (fh->is_cdrom) { |
556 |
|
ioctl(fh->fd, CDROMEJECT); |
623 |
|
|
624 |
|
#if defined(__linux__) |
625 |
|
if (fh->is_floppy) { |
626 |
< |
struct floppy_drive_struct stat; |
627 |
< |
ioctl(fh->fd, FDGETDRVSTAT, &stat); |
628 |
< |
return !(stat.flags & FD_DISK_WRITABLE); |
626 |
> |
if (fh->fd >= 0) { |
627 |
> |
struct floppy_drive_struct stat; |
628 |
> |
ioctl(fh->fd, FDGETDRVSTAT, &stat); |
629 |
> |
return !(stat.flags & FD_DISK_WRITABLE); |
630 |
> |
} else |
631 |
> |
return true; |
632 |
|
} else |
633 |
|
#endif |
634 |
|
return fh->read_only; |
801 |
|
*toc++ = toc_size >> 8; |
802 |
|
*toc++ = toc_size & 0xff; |
803 |
|
return true; |
804 |
< |
#elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_2) |
804 |
> |
#elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_2) && defined(HAVE_FRAMEWORK_COREFOUNDATION) |
805 |
|
extern bool DarwinCDReadTOC(char *name, uint8 *toc); |
806 |
|
|
807 |
|
return DarwinCDReadTOC(fh->name, toc); |