1 |
|
/* |
2 |
|
* sys_unix.cpp - System dependent routines, Unix implementation |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-1999 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> |
37 |
< |
#include <linux/unistd.h> |
33 |
< |
|
34 |
< |
#ifdef __NR__llseek |
35 |
< |
_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, loff_t *, res, uint, wh); |
36 |
< |
#else |
37 |
< |
static int _llseek(uint fd, ulong hi, ulong lo, loff_t *res, uint wh) |
38 |
< |
{ |
39 |
< |
if (hi) |
40 |
< |
return -1; |
41 |
< |
*res = lseek(fd, lo, wh); |
42 |
< |
if (*res == -1) |
43 |
< |
return -1; |
44 |
< |
return 0; |
45 |
< |
} |
46 |
< |
#endif |
37 |
> |
#include <dirent.h> |
38 |
|
#endif |
39 |
|
|
40 |
< |
#ifdef __FreeBSD__ |
40 |
> |
#if defined(__FreeBSD__) || defined(__NetBSD__) |
41 |
|
#include <sys/cdio.h> |
42 |
|
#endif |
43 |
|
|
44 |
+ |
#if defined __APPLE__ && defined __MACH__ |
45 |
+ |
#include <sys/disk.h> |
46 |
+ |
#if (defined AQUA || defined HAVE_FRAMEWORK_COREFOUNDATION) |
47 |
+ |
#ifndef __MACOSX__ |
48 |
+ |
#define __MACOSX__ MAC_OS_X_VERSION_MIN_REQUIRED |
49 |
+ |
#endif |
50 |
+ |
#endif |
51 |
+ |
#endif |
52 |
+ |
|
53 |
|
#include "main.h" |
54 |
|
#include "macos_util.h" |
55 |
|
#include "prefs.h" |
62 |
|
|
63 |
|
// File handles are pointers to these structures |
64 |
|
struct file_handle { |
65 |
< |
char *name; // Copy of device/file name |
65 |
> |
char *name; // Copy of device/file name |
66 |
|
int fd; |
67 |
|
bool is_file; // Flag: plain file or /dev/something? |
68 |
|
bool is_floppy; // Flag: floppy device |
70 |
|
bool read_only; // Copy of Sys_open() flag |
71 |
|
loff_t start_byte; // Size of file header (if any) |
72 |
|
loff_t file_size; // Size of file data (only valid if is_file is true) |
73 |
+ |
bool is_media_present; // Flag: media is inserted and available |
74 |
|
|
75 |
|
#if defined(__linux__) |
76 |
|
int cdrom_cap; // CD-ROM capability flags (only valid if is_cdrom is true) |
77 |
|
#elif defined(__FreeBSD__) |
78 |
|
struct ioc_capability cdrom_cap; |
79 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
80 |
+ |
char *ioctl_name; // For CDs on OS X - a device for special ioctls |
81 |
+ |
int ioctl_fd; |
82 |
|
#endif |
83 |
|
}; |
84 |
|
|
85 |
+ |
// Open file handles |
86 |
+ |
struct open_file_handle { |
87 |
+ |
file_handle *fh; |
88 |
+ |
open_file_handle *next; |
89 |
+ |
}; |
90 |
+ |
static open_file_handle *open_file_handles = NULL; |
91 |
+ |
|
92 |
|
// File handle of first floppy drive (for SysMountFirstFloppy()) |
93 |
|
static file_handle *first_floppy = NULL; |
94 |
|
|
95 |
+ |
// Prototypes |
96 |
+ |
static void cdrom_close(file_handle *fh); |
97 |
+ |
static bool cdrom_open(file_handle *fh, const char *path = NULL); |
98 |
+ |
|
99 |
|
|
100 |
|
/* |
101 |
|
* Initialization |
103 |
|
|
104 |
|
void SysInit(void) |
105 |
|
{ |
106 |
+ |
#if defined __MACOSX__ |
107 |
+ |
extern void DarwinSysInit(void); |
108 |
+ |
DarwinSysInit(); |
109 |
+ |
#endif |
110 |
|
} |
111 |
|
|
112 |
|
|
116 |
|
|
117 |
|
void SysExit(void) |
118 |
|
{ |
119 |
+ |
#if defined __MACOSX__ |
120 |
+ |
extern void DarwinSysExit(void); |
121 |
+ |
DarwinSysExit(); |
122 |
+ |
#endif |
123 |
+ |
} |
124 |
+ |
|
125 |
+ |
|
126 |
+ |
/* |
127 |
+ |
* Manage open file handles |
128 |
+ |
*/ |
129 |
+ |
|
130 |
+ |
static void sys_add_file_handle(file_handle *fh) |
131 |
+ |
{ |
132 |
+ |
open_file_handle *p = new open_file_handle; |
133 |
+ |
p->fh = fh; |
134 |
+ |
p->next = open_file_handles; |
135 |
+ |
open_file_handles = p; |
136 |
+ |
} |
137 |
+ |
|
138 |
+ |
static void sys_remove_file_handle(file_handle *fh) |
139 |
+ |
{ |
140 |
+ |
open_file_handle *p = open_file_handles; |
141 |
+ |
open_file_handle *q = NULL; |
142 |
+ |
|
143 |
+ |
while (p) { |
144 |
+ |
if (p->fh == fh) { |
145 |
+ |
if (q) |
146 |
+ |
q->next = p->next; |
147 |
+ |
else |
148 |
+ |
open_file_handles = p->next; |
149 |
+ |
delete p; |
150 |
+ |
break; |
151 |
+ |
} |
152 |
+ |
q = p; |
153 |
+ |
p = p->next; |
154 |
+ |
} |
155 |
+ |
} |
156 |
+ |
|
157 |
+ |
|
158 |
+ |
/* |
159 |
+ |
* Account for media that has just arrived |
160 |
+ |
*/ |
161 |
+ |
|
162 |
+ |
void SysMediaArrived(const char *path, int type) |
163 |
+ |
{ |
164 |
+ |
// Replace the "cdrom" entry (we are polling, it's unique) |
165 |
+ |
if (type == MEDIA_CD && !PrefsFindBool("nocdrom")) |
166 |
+ |
PrefsReplaceString("cdrom", path); |
167 |
+ |
|
168 |
+ |
// Wait for media to be available for reading |
169 |
+ |
if (open_file_handles) { |
170 |
+ |
const int MAX_WAIT = 5; |
171 |
+ |
for (int i = 0; i < MAX_WAIT; i++) { |
172 |
+ |
if (access(path, R_OK) == 0) |
173 |
+ |
break; |
174 |
+ |
switch (errno) { |
175 |
+ |
case ENOENT: // Unlikely |
176 |
+ |
case EACCES: // MacOS X is mounting the media |
177 |
+ |
sleep(1); |
178 |
+ |
continue; |
179 |
+ |
} |
180 |
+ |
printf("WARNING: Cannot access %s (%s)\n", path, strerror(errno)); |
181 |
+ |
return; |
182 |
+ |
} |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
for (open_file_handle *p = open_file_handles; p != NULL; p = p->next) { |
186 |
+ |
file_handle * const fh = p->fh; |
187 |
+ |
|
188 |
+ |
// Re-open CD-ROM device |
189 |
+ |
if (fh->is_cdrom && type == MEDIA_CD) { |
190 |
+ |
cdrom_close(fh); |
191 |
+ |
if (cdrom_open(fh, path)) { |
192 |
+ |
fh->is_media_present = true; |
193 |
+ |
MountVolume(fh); |
194 |
+ |
} |
195 |
+ |
} |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
|
199 |
+ |
|
200 |
+ |
/* |
201 |
+ |
* Account for media that has just been removed |
202 |
+ |
*/ |
203 |
+ |
|
204 |
+ |
void SysMediaRemoved(const char *path, int type) |
205 |
+ |
{ |
206 |
+ |
if ((type & MEDIA_REMOVABLE) != MEDIA_CD) |
207 |
+ |
return; |
208 |
+ |
|
209 |
+ |
for (open_file_handle *p = open_file_handles; p != NULL; p = p->next) { |
210 |
+ |
file_handle * const fh = p->fh; |
211 |
+ |
|
212 |
+ |
// Mark media as not available |
213 |
+ |
if (!fh->is_cdrom || !fh->is_media_present) |
214 |
+ |
continue; |
215 |
+ |
if (fh->name && strcmp(fh->name, path) == 0) { |
216 |
+ |
fh->is_media_present = false; |
217 |
+ |
break; |
218 |
+ |
} |
219 |
+ |
#if defined __MACOSX__ |
220 |
+ |
if (fh->ioctl_name && strcmp(fh->ioctl_name, path) == 0) { |
221 |
+ |
fh->is_media_present = false; |
222 |
+ |
break; |
223 |
+ |
} |
224 |
+ |
#endif |
225 |
+ |
} |
226 |
|
} |
227 |
|
|
228 |
|
|
245 |
|
void SysAddFloppyPrefs(void) |
246 |
|
{ |
247 |
|
#if defined(__linux__) |
248 |
< |
PrefsAddString("floppy", "/dev/fd0H1440"); |
249 |
< |
PrefsAddString("floppy", "/dev/fd1H1440"); |
248 |
> |
DIR *fd_dir = opendir("/dev/floppy"); |
249 |
> |
if (fd_dir) { |
250 |
> |
struct dirent *floppy_dev; |
251 |
> |
while ((floppy_dev = readdir(fd_dir)) != NULL) { |
252 |
> |
if (strstr(floppy_dev->d_name, "u1440") != NULL) { |
253 |
> |
char fd_dev[20]; |
254 |
> |
sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name); |
255 |
> |
PrefsAddString("floppy", fd_dev); |
256 |
> |
} |
257 |
> |
} |
258 |
> |
closedir(fd_dir); |
259 |
> |
} else { |
260 |
> |
PrefsAddString("floppy", "/dev/fd0"); |
261 |
> |
PrefsAddString("floppy", "/dev/fd1"); |
262 |
> |
} |
263 |
> |
#elif defined(__NetBSD__) |
264 |
> |
PrefsAddString("floppy", "/dev/fd0a"); |
265 |
> |
PrefsAddString("floppy", "/dev/fd1a"); |
266 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
267 |
> |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION) |
268 |
> |
extern void DarwinAddFloppyPrefs(void); |
269 |
> |
|
270 |
> |
DarwinAddFloppyPrefs(); |
271 |
> |
#else |
272 |
> |
// Until I can convince the other guys that my Darwin code is useful, |
273 |
> |
// we just add something safe (a non-existant device): |
274 |
> |
PrefsAddString("floppy", "/dev/null"); |
275 |
> |
#endif |
276 |
|
#else |
277 |
|
PrefsAddString("floppy", "/dev/fd0"); |
278 |
|
PrefsAddString("floppy", "/dev/fd1"); |
283 |
|
/* |
284 |
|
* This gets called when no "disk" prefs items are found |
285 |
|
* It scans for available HFS volumes and adds appropriate prefs items |
286 |
+ |
* On OS X, we could do the same, but on an OS X machine I think it is |
287 |
+ |
* very unlikely that any mounted volumes would contain a system which |
288 |
+ |
* is old enough to boot a 68k Mac, so we just do nothing here for now. |
289 |
|
*/ |
290 |
|
|
291 |
|
void SysAddDiskPrefs(void) |
297 |
|
while(fgets(line, 255, f)) { |
298 |
|
// Read line |
299 |
|
int len = strlen(line); |
300 |
< |
if (len == 0) |
300 |
> |
if (len == 0 || line[0] == '#') |
301 |
|
continue; |
302 |
|
line[len-1] = 0; |
303 |
|
|
304 |
|
// Parse line |
305 |
|
char *dev, *mnt_point, *fstype; |
306 |
< |
if (sscanf(line, "%s %s %s", &dev, &mnt_point, &fstype) == 3) { |
306 |
> |
if (sscanf(line, "%as %as %as", &dev, &mnt_point, &fstype) == 3) { |
307 |
|
if (strcmp(fstype, "hfs") == 0) |
308 |
|
PrefsAddString("disk", dev); |
309 |
|
} |
310 |
+ |
free(dev); free(mnt_point); free(fstype); |
311 |
|
} |
312 |
|
fclose(f); |
313 |
|
} |
327 |
|
return; |
328 |
|
|
329 |
|
#if defined(__linux__) |
330 |
< |
PrefsAddString("cdrom", "/dev/cdrom"); |
331 |
< |
#elif defined(__FreeBSD__) |
330 |
> |
if (access("/dev/.devfsd", F_OK) < 0) |
331 |
> |
PrefsAddString("cdrom", "/dev/cdrom"); |
332 |
> |
else { |
333 |
> |
DIR *cd_dir = opendir("/dev/cdroms"); |
334 |
> |
if (cd_dir) { |
335 |
> |
struct dirent *cdrom_dev; |
336 |
> |
while ((cdrom_dev = readdir(cd_dir)) != NULL) { |
337 |
> |
if (strcmp(cdrom_dev->d_name, ".") != 0 && strcmp(cdrom_dev->d_name, "..") != 0) { |
338 |
> |
char cd_dev[20]; |
339 |
> |
sprintf(cd_dev, "/dev/cdroms/%s", cdrom_dev->d_name); |
340 |
> |
PrefsAddString("cdrom", cd_dev); |
341 |
> |
} |
342 |
> |
} |
343 |
> |
closedir(cd_dir); |
344 |
> |
} |
345 |
> |
} |
346 |
> |
#elif defined __MACOSX__ |
347 |
> |
// There is no predefined path for CD-ROMs on MacOS X. Rather, we |
348 |
> |
// define a single fake CD-ROM entry for the emulated MacOS. |
349 |
> |
// XXX this means we handle only CD-ROM drive at a time, wherever |
350 |
> |
// the disk is, the latest one is used. |
351 |
> |
PrefsAddString("cdrom", "/dev/poll/cdrom"); |
352 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
353 |
|
PrefsAddString("cdrom", "/dev/cd0c"); |
354 |
|
#endif |
355 |
|
} |
362 |
|
void SysAddSerialPrefs(void) |
363 |
|
{ |
364 |
|
#if defined(__linux__) |
365 |
< |
PrefsAddString("seriala", "/dev/ttyS0"); |
366 |
< |
PrefsAddString("serialb", "/dev/ttyS1"); |
365 |
> |
if (access("/dev/.devfsd", F_OK) < 0) { |
366 |
> |
PrefsAddString("seriala", "/dev/ttyS0"); |
367 |
> |
PrefsAddString("serialb", "/dev/ttyS1"); |
368 |
> |
} else { |
369 |
> |
PrefsAddString("seriala", "/dev/tts/0"); |
370 |
> |
PrefsAddString("serialb", "/dev/tts/1"); |
371 |
> |
} |
372 |
|
#elif defined(__FreeBSD__) |
373 |
|
PrefsAddString("seriala", "/dev/cuaa0"); |
374 |
|
PrefsAddString("serialb", "/dev/cuaa1"); |
375 |
+ |
#elif defined(__NetBSD__) |
376 |
+ |
PrefsAddString("seriala", "/dev/tty00"); |
377 |
+ |
PrefsAddString("serialb", "/dev/tty01"); |
378 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
379 |
+ |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION) |
380 |
+ |
extern void DarwinAddSerialPrefs(void); |
381 |
+ |
|
382 |
+ |
DarwinAddSerialPrefs(); |
383 |
+ |
#else |
384 |
+ |
// Until I can convince the other guys that my Darwin code is useful, |
385 |
+ |
// we just add something safe (non-existant devices): |
386 |
+ |
PrefsAddString("seriala", "/dev/null"); |
387 |
+ |
PrefsAddString("serialb", "/dev/null"); |
388 |
+ |
#endif |
389 |
+ |
#endif |
390 |
+ |
} |
391 |
+ |
|
392 |
+ |
|
393 |
+ |
/* |
394 |
+ |
* Open CD-ROM device and initialize internal data |
395 |
+ |
*/ |
396 |
+ |
|
397 |
+ |
static bool cdrom_open_1(file_handle *fh) |
398 |
+ |
{ |
399 |
+ |
#if defined __MACOSX__ |
400 |
+ |
// In OS X, the device name is OK for sending ioctls to, |
401 |
+ |
// but not for reading raw CDROM data from. |
402 |
+ |
// (it seems to have extra data padded in) |
403 |
+ |
// |
404 |
+ |
// So, we keep the already opened file handle, |
405 |
+ |
// and open a slightly different file for CDROM data |
406 |
+ |
// |
407 |
+ |
fh->ioctl_fd = fh->fd; |
408 |
+ |
fh->ioctl_name = fh->name; |
409 |
+ |
fh->fd = -1; |
410 |
+ |
fh->name = (char *)malloc(strlen(fh->ioctl_name) + 3); |
411 |
+ |
if (fh->name) { |
412 |
+ |
strcpy(fh->name, fh->ioctl_name); |
413 |
+ |
strcat(fh->name, "s1"); |
414 |
+ |
fh->fd = open(fh->name, O_RDONLY, O_NONBLOCK); |
415 |
+ |
} |
416 |
+ |
if (fh->ioctl_fd < 0) |
417 |
+ |
return false; |
418 |
+ |
#endif |
419 |
+ |
return true; |
420 |
+ |
} |
421 |
+ |
|
422 |
+ |
bool cdrom_open(file_handle *fh, const char *path) |
423 |
+ |
{ |
424 |
+ |
if (path) |
425 |
+ |
fh->name = strdup(path); |
426 |
+ |
fh->fd = open(fh->name, O_RDONLY, O_NONBLOCK); |
427 |
+ |
fh->start_byte = 0; |
428 |
+ |
if (!cdrom_open_1(fh)) |
429 |
+ |
return false; |
430 |
+ |
return fh->fd >= 0; |
431 |
+ |
} |
432 |
+ |
|
433 |
+ |
|
434 |
+ |
/* |
435 |
+ |
* Close a CD-ROM device |
436 |
+ |
*/ |
437 |
+ |
|
438 |
+ |
void cdrom_close(file_handle *fh) |
439 |
+ |
{ |
440 |
+ |
if (fh->fd >= 0) { |
441 |
+ |
close(fh->fd); |
442 |
+ |
fh->fd = -1; |
443 |
+ |
} |
444 |
+ |
if (fh->name) { |
445 |
+ |
free(fh->name); |
446 |
+ |
fh->name = NULL; |
447 |
+ |
} |
448 |
+ |
#if defined __MACOSX__ |
449 |
+ |
if (fh->ioctl_fd >= 0) { |
450 |
+ |
close(fh->ioctl_fd); |
451 |
+ |
fh->ioctl_fd = -1; |
452 |
+ |
} |
453 |
+ |
if (fh->ioctl_name) { |
454 |
+ |
free(fh->ioctl_name); |
455 |
+ |
fh->ioctl_name = NULL; |
456 |
+ |
} |
457 |
|
#endif |
458 |
|
} |
459 |
|
|
478 |
|
// Parse line |
479 |
|
if (strncmp(line, dev_name, strlen(dev_name)) == 0) { |
480 |
|
mount_name[0] = 0; |
481 |
< |
char dummy[256]; |
482 |
< |
sscanf(line, "%s %s", dummy, mount_name); |
481 |
> |
char *dummy; |
482 |
> |
sscanf(line, "%as %s", &dummy, mount_name); |
483 |
> |
free(dummy); |
484 |
|
fclose(f); |
485 |
|
return true; |
486 |
|
} |
499 |
|
void *Sys_open(const char *name, bool read_only) |
500 |
|
{ |
501 |
|
bool is_file = strncmp(name, "/dev/", 5) != 0; |
502 |
< |
#ifdef __FreeBSD__ |
502 |
> |
#if defined(__FreeBSD__) |
503 |
|
// SCSI IDE |
504 |
|
bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0 || strncmp(name, "/dev/acd", 8) == 0; |
505 |
|
#else |
506 |
|
bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0; |
507 |
|
#endif |
508 |
+ |
bool is_floppy = strncmp(name, "/dev/fd", 7) == 0; |
509 |
+ |
|
510 |
+ |
bool is_polled_media = strncmp(name, "/dev/poll/", 10) == 0; |
511 |
+ |
if (is_floppy) // Floppy open fails if there's no disk inserted |
512 |
+ |
is_polled_media = true; |
513 |
+ |
|
514 |
+ |
#if defined __MACOSX__ |
515 |
+ |
// There is no set filename in /dev which is the cdrom, |
516 |
+ |
// so we have to see if it is any of the devices that we found earlier |
517 |
+ |
{ |
518 |
+ |
int index = 0; |
519 |
+ |
const char *str; |
520 |
+ |
while ((str = PrefsFindString("cdrom", index++)) != NULL) { |
521 |
+ |
if (is_polled_media || strcmp(str, name) == 0) { |
522 |
+ |
is_cdrom = true; |
523 |
+ |
read_only = true; |
524 |
+ |
break; |
525 |
+ |
} |
526 |
+ |
} |
527 |
+ |
} |
528 |
+ |
#endif |
529 |
|
|
530 |
|
D(bug("Sys_open(%s, %s)\n", name, read_only ? "read-only" : "read/write")); |
531 |
|
|
548 |
|
} |
549 |
|
|
550 |
|
// Open file/device |
551 |
< |
#ifdef __linux__ |
551 |
> |
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__MACOSX__) |
552 |
|
int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0)); |
553 |
|
#else |
554 |
|
int fd = open(name, read_only ? O_RDONLY : O_RDWR); |
558 |
|
read_only = true; |
559 |
|
fd = open(name, O_RDONLY); |
560 |
|
} |
561 |
< |
if (fd >= 0) { |
561 |
> |
if (fd >= 0 || is_polled_media) { |
562 |
|
file_handle *fh = new file_handle; |
563 |
|
fh->name = strdup(name); |
564 |
|
fh->fd = fd; |
565 |
|
fh->is_file = is_file; |
566 |
|
fh->read_only = read_only; |
567 |
|
fh->start_byte = 0; |
568 |
< |
fh->is_floppy = false; |
569 |
< |
fh->is_cdrom = false; |
568 |
> |
fh->is_floppy = is_floppy; |
569 |
> |
fh->is_cdrom = is_cdrom; |
570 |
> |
fh->is_media_present = false; |
571 |
> |
#if defined __MACOSX__ |
572 |
> |
fh->ioctl_fd = -1; |
573 |
> |
fh->ioctl_name = NULL; |
574 |
> |
#endif |
575 |
|
if (fh->is_file) { |
576 |
+ |
fh->is_media_present = true; |
577 |
|
// Detect disk image file layout |
578 |
|
loff_t size = 0; |
287 |
– |
#ifdef __linux__ |
288 |
– |
_llseek(fh->fd, 0, 0, &size, SEEK_END); |
289 |
– |
#else |
579 |
|
size = lseek(fd, 0, SEEK_END); |
291 |
– |
#endif |
580 |
|
uint8 data[256]; |
581 |
|
lseek(fd, 0, SEEK_SET); |
582 |
|
read(fd, data, 256); |
584 |
|
} else { |
585 |
|
struct stat st; |
586 |
|
if (fstat(fd, &st) == 0) { |
587 |
+ |
fh->is_media_present = true; |
588 |
|
if (S_ISBLK(st.st_mode)) { |
589 |
|
fh->is_cdrom = is_cdrom; |
590 |
|
#if defined(__linux__) |
608 |
|
#else |
609 |
|
fh->cdrom_cap = 0; |
610 |
|
#endif |
611 |
+ |
#elif defined(__NetBSD__) |
612 |
+ |
fh->is_floppy = ((st.st_rdev >> 16) == 2); |
613 |
|
#endif |
614 |
|
} |
615 |
+ |
#if defined __MACOSX__ |
616 |
+ |
if (is_cdrom) { |
617 |
+ |
fh->is_cdrom = true; |
618 |
+ |
fh->is_floppy = false; |
619 |
+ |
if (cdrom_open_1(fh)) |
620 |
+ |
fh->is_media_present = true; |
621 |
+ |
} |
622 |
+ |
#endif |
623 |
|
} |
624 |
|
} |
625 |
|
if (fh->is_floppy && first_floppy == NULL) |
626 |
|
first_floppy = fh; |
627 |
+ |
sys_add_file_handle(fh); |
628 |
|
return fh; |
629 |
|
} else { |
630 |
|
printf("WARNING: Cannot open %s (%s)\n", name, strerror(errno)); |
643 |
|
if (!fh) |
644 |
|
return; |
645 |
|
|
646 |
< |
close(fh->fd); |
646 |
> |
sys_remove_file_handle(fh); |
647 |
> |
|
648 |
> |
if (fh->is_cdrom) |
649 |
> |
cdrom_close(fh); |
650 |
> |
if (fh->fd >= 0) |
651 |
> |
close(fh->fd); |
652 |
|
if (fh->name) |
653 |
|
free(fh->name); |
654 |
|
delete fh; |
667 |
|
return 0; |
668 |
|
|
669 |
|
// Seek to position |
365 |
– |
#ifdef __linux__ |
366 |
– |
loff_t pos = offset + fh->start_byte, res; |
367 |
– |
if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0) |
368 |
– |
return 0; |
369 |
– |
#else |
670 |
|
if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0) |
671 |
|
return 0; |
372 |
– |
#endif |
672 |
|
|
673 |
|
// Read data |
674 |
|
return read(fh->fd, buffer, length); |
687 |
|
return 0; |
688 |
|
|
689 |
|
// Seek to position |
391 |
– |
#ifdef __linux__ |
392 |
– |
loff_t pos = offset + fh->start_byte, res; |
393 |
– |
if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0) |
394 |
– |
return 0; |
395 |
– |
#else |
690 |
|
if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0) |
691 |
|
return 0; |
398 |
– |
#endif |
692 |
|
|
693 |
|
// Write data |
694 |
|
return write(fh->fd, buffer, length); |
708 |
|
if (fh->is_file) |
709 |
|
return fh->file_size; |
710 |
|
else { |
711 |
< |
#ifdef __linux__ |
712 |
< |
loff_t pos = 0; |
713 |
< |
_llseek(fh->fd, 0, 0, &pos, SEEK_END); |
714 |
< |
return pos - fh->start_byte; |
711 |
> |
#if defined(__linux__) |
712 |
> |
long blocks; |
713 |
> |
if (ioctl(fh->fd, BLKGETSIZE, &blocks) < 0) |
714 |
> |
return 0; |
715 |
> |
D(bug(" BLKGETSIZE returns %d blocks\n", blocks)); |
716 |
> |
return (loff_t)blocks * 512; |
717 |
> |
#elif defined __MACOSX__ |
718 |
> |
uint32 block_size; |
719 |
> |
if (ioctl(fh->ioctl_fd, DKIOCGETBLOCKSIZE, &block_size) < 0) |
720 |
> |
return 0; |
721 |
> |
D(bug(" DKIOCGETBLOCKSIZE returns %lu bytes\n", (unsigned long)block_size)); |
722 |
> |
uint64 block_count; |
723 |
> |
if (ioctl(fh->ioctl_fd, DKIOCGETBLOCKCOUNT, &block_count) < 0) |
724 |
> |
return 0; |
725 |
> |
D(bug(" DKIOCGETBLOCKCOUNT returns %llu blocks\n", (unsigned long long)block_count)); |
726 |
> |
return block_count * block_size; |
727 |
|
#else |
728 |
|
return lseek(fh->fd, 0, SEEK_END) - fh->start_byte; |
729 |
|
#endif |
743 |
|
|
744 |
|
#if defined(__linux__) |
745 |
|
if (fh->is_floppy) { |
746 |
< |
fsync(fh->fd); |
747 |
< |
ioctl(fh->fd, FDFLUSH); |
748 |
< |
ioctl(fh->fd, FDEJECT); |
746 |
> |
if (fh->fd >= 0) { |
747 |
> |
fsync(fh->fd); |
748 |
> |
ioctl(fh->fd, FDFLUSH); |
749 |
> |
ioctl(fh->fd, FDEJECT); |
750 |
> |
close(fh->fd); // Close and reopen so the driver will see the media change |
751 |
> |
} |
752 |
> |
fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR); |
753 |
|
} else if (fh->is_cdrom) { |
754 |
|
ioctl(fh->fd, CDROMEJECT); |
755 |
|
close(fh->fd); // Close and reopen so the driver will see the media change |
756 |
|
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
757 |
|
} |
758 |
< |
#elif defined(__FreeBSD__) |
758 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
759 |
|
if (fh->is_floppy) { |
760 |
|
fsync(fh->fd); |
452 |
– |
//ioctl(fh->fd, FDFLUSH); |
453 |
– |
//ioctl(fh->fd, FDEJECT); |
761 |
|
} else if (fh->is_cdrom) { |
762 |
|
ioctl(fh->fd, CDIOCEJECT); |
763 |
|
close(fh->fd); // Close and reopen so the driver will see the media change |
764 |
|
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
765 |
|
} |
766 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
767 |
+ |
if (fh->is_cdrom && fh->is_media_present) { |
768 |
+ |
close(fh->fd); |
769 |
+ |
fh->fd = -1; |
770 |
+ |
if (ioctl(fh->ioctl_fd, DKIOCEJECT) < 0) { |
771 |
+ |
D(bug(" DKIOCEJECT failed on file %s: %s\n", |
772 |
+ |
fh->ioctl_name, strerror(errno))); |
773 |
+ |
|
774 |
+ |
// If we are running MacOS X, the device may be in busy |
775 |
+ |
// state because the Finder has mounted the disk |
776 |
+ |
close(fh->ioctl_fd); |
777 |
+ |
fh->ioctl_fd = -1; |
778 |
+ |
|
779 |
+ |
// Try to use "diskutil eject" but it can take up to 5 |
780 |
+ |
// seconds to complete |
781 |
+ |
static const char eject_cmd[] = "/usr/sbin/diskutil eject %s 2>&1 >/dev/null"; |
782 |
+ |
char *cmd = (char *)alloca(strlen(eject_cmd) + strlen(fh->ioctl_name) + 1); |
783 |
+ |
sprintf(cmd, eject_cmd, fh->ioctl_name); |
784 |
+ |
system(cmd); |
785 |
+ |
} |
786 |
+ |
fh->is_media_present = false; |
787 |
+ |
} |
788 |
|
#endif |
789 |
|
} |
790 |
|
|
814 |
|
if (!fh) |
815 |
|
return true; |
816 |
|
|
817 |
< |
#ifdef __linux__ |
817 |
> |
#if defined(__linux__) |
818 |
|
if (fh->is_floppy) { |
819 |
< |
struct floppy_drive_struct stat; |
820 |
< |
ioctl(fh->fd, FDGETDRVSTAT, &stat); |
821 |
< |
return !(stat.flags & FD_DISK_WRITABLE); |
819 |
> |
if (fh->fd >= 0) { |
820 |
> |
struct floppy_drive_struct stat; |
821 |
> |
ioctl(fh->fd, FDGETDRVSTAT, &stat); |
822 |
> |
return !(stat.flags & FD_DISK_WRITABLE); |
823 |
> |
} else |
824 |
> |
return true; |
825 |
|
} else |
826 |
|
#endif |
827 |
|
return fh->read_only; |
864 |
|
} else if (fh->is_floppy) { |
865 |
|
char block[512]; |
866 |
|
lseek(fh->fd, 0, SEEK_SET); |
867 |
< |
return read(fh->fd, block, 512) == 512; |
867 |
> |
ssize_t actual = read(fh->fd, block, 512); |
868 |
> |
if (actual < 0) { |
869 |
> |
close(fh->fd); // Close and reopen so the driver will see the media change |
870 |
> |
fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR); |
871 |
> |
actual = read(fh->fd, block, 512); |
872 |
> |
} |
873 |
> |
return actual == 512; |
874 |
|
} else if (fh->is_cdrom) { |
875 |
+ |
#ifdef CDROM_MEDIA_CHANGED |
876 |
+ |
if (fh->cdrom_cap & CDC_MEDIA_CHANGED) { |
877 |
+ |
// If we don't do this, all attempts to read from a disc fail |
878 |
+ |
// once the tray has been opened (altough the TOC reads fine). |
879 |
+ |
// Can somebody explain this to me? |
880 |
+ |
if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) { |
881 |
+ |
close(fh->fd); |
882 |
+ |
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
883 |
+ |
} |
884 |
+ |
} |
885 |
+ |
#endif |
886 |
|
#ifdef CDROM_DRIVE_STATUS |
887 |
|
if (fh->cdrom_cap & CDC_DRIVE_STATUS) { |
888 |
|
return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK; |
890 |
|
#endif |
891 |
|
cdrom_tochdr header; |
892 |
|
return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0; |
893 |
< |
#elif defined(__FreeBSD__) |
893 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
894 |
|
} else if (fh->is_floppy) { |
895 |
|
return false; //!! |
896 |
|
} else if (fh->is_cdrom) { |
897 |
|
struct ioc_toc_header header; |
898 |
|
return ioctl(fh->fd, CDIOREADTOCHEADER, &header) == 0; |
899 |
+ |
#elif defined __MACOSX__ |
900 |
+ |
} else if (fh->is_cdrom || fh->is_floppy) { |
901 |
+ |
return fh->is_media_present; |
902 |
|
#endif |
903 |
|
|
904 |
|
} else |
933 |
|
if (!fh) |
934 |
|
return; |
935 |
|
|
936 |
< |
#ifdef defined(__linux__) && defined(CDROM_LOCKDOOR) |
936 |
> |
#if defined(__linux__) && defined(CDROM_LOCKDOOR) |
937 |
|
if (fh->is_cdrom) |
938 |
|
ioctl(fh->fd, CDROM_LOCKDOOR, 0); |
939 |
|
#endif |
997 |
|
*toc++ = toc_size >> 8; |
998 |
|
*toc++ = toc_size & 0xff; |
999 |
|
return true; |
1000 |
+ |
#elif defined __MACOSX__ && defined MAC_OS_X_VERSION_10_2 |
1001 |
+ |
if (fh->is_media_present) { |
1002 |
+ |
extern bool DarwinCDReadTOC(char *name, uint8 *toc); |
1003 |
+ |
return DarwinCDReadTOC(fh->name, toc); |
1004 |
+ |
} |
1005 |
+ |
return false; |
1006 |
|
#elif defined(__FreeBSD__) |
1007 |
|
uint8 *p = toc + 2; |
1008 |
|
|
1049 |
|
*toc++ = toc_size >> 8; |
1050 |
|
*toc++ = toc_size & 0xff; |
1051 |
|
return true; |
1052 |
+ |
#elif defined(__NetBSD__) |
1053 |
+ |
uint8 *p = toc + 2; |
1054 |
+ |
|
1055 |
+ |
// Header |
1056 |
+ |
struct ioc_toc_header header; |
1057 |
+ |
if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0) |
1058 |
+ |
return false; |
1059 |
+ |
*p++ = header.starting_track; |
1060 |
+ |
*p++ = header.ending_track; |
1061 |
+ |
|
1062 |
+ |
// Tracks (this is nice... :-) |
1063 |
+ |
struct ioc_read_toc_entry entries; |
1064 |
+ |
entries.address_format = CD_MSF_FORMAT; |
1065 |
+ |
entries.starting_track = 1; |
1066 |
+ |
entries.data_len = 800; |
1067 |
+ |
entries.data = (cd_toc_entry *)p; |
1068 |
+ |
if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0) |
1069 |
+ |
return false; |
1070 |
+ |
|
1071 |
+ |
// TOC size |
1072 |
+ |
int toc_size = p - toc; |
1073 |
+ |
*toc++ = toc_size >> 8; |
1074 |
+ |
*toc++ = toc_size & 0xff; |
1075 |
+ |
return true; |
1076 |
+ |
#else |
1077 |
+ |
return false; |
1078 |
|
#endif |
1079 |
|
} else |
1080 |
|
return false; |
1114 |
|
*pos++ = chan.cdsc_reladdr.msf.second; |
1115 |
|
*pos++ = chan.cdsc_reladdr.msf.frame; |
1116 |
|
return true; |
1117 |
< |
#elif defined(__FreeBSD__) |
1117 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1118 |
|
struct ioc_read_subchannel chan; |
1119 |
|
chan.data_format = CD_MSF_FORMAT; |
1120 |
|
chan.address_format = CD_MSF_FORMAT; |
1138 |
|
*pos++ = chan.data->what.position.reladdr.msf.second; |
1139 |
|
*pos++ = chan.data->what.position.reladdr.msf.frame; |
1140 |
|
return true; |
1141 |
+ |
#else |
1142 |
+ |
return false; |
1143 |
|
#endif |
1144 |
|
} else |
1145 |
|
return false; |
1166 |
|
play.cdmsf_sec1 = end_s; |
1167 |
|
play.cdmsf_frame1 = end_f; |
1168 |
|
return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0; |
1169 |
< |
#elif defined(__FreeBSD__) |
1169 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1170 |
|
struct ioc_play_msf play; |
1171 |
|
play.start_m = start_m; |
1172 |
|
play.start_s = start_s; |
1175 |
|
play.end_s = end_s; |
1176 |
|
play.end_f = end_f; |
1177 |
|
return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0; |
1178 |
+ |
#else |
1179 |
+ |
return false; |
1180 |
|
#endif |
1181 |
|
} else |
1182 |
|
return false; |
1196 |
|
if (fh->is_cdrom) { |
1197 |
|
#if defined(__linux__) |
1198 |
|
return ioctl(fh->fd, CDROMPAUSE) == 0; |
1199 |
< |
#elif defined(__FreeBSD__) |
1199 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1200 |
|
return ioctl(fh->fd, CDIOCPAUSE) == 0; |
1201 |
+ |
#else |
1202 |
+ |
return false; |
1203 |
|
#endif |
1204 |
|
} else |
1205 |
|
return false; |
1219 |
|
if (fh->is_cdrom) { |
1220 |
|
#if defined(__linux__) |
1221 |
|
return ioctl(fh->fd, CDROMRESUME) == 0; |
1222 |
< |
#elif defined(__FreeBSD__) |
1222 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1223 |
|
return ioctl(fh->fd, CDIOCRESUME) == 0; |
1224 |
+ |
#else |
1225 |
+ |
return false; |
1226 |
|
#endif |
1227 |
|
} else |
1228 |
|
return false; |
1242 |
|
if (fh->is_cdrom) { |
1243 |
|
#if defined(__linux__) |
1244 |
|
return ioctl(fh->fd, CDROMSTOP) == 0; |
1245 |
< |
#elif defined(__FreeBSD__) |
1245 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1246 |
|
return ioctl(fh->fd, CDIOCSTOP) == 0; |
1247 |
+ |
#else |
1248 |
+ |
return false; |
1249 |
|
#endif |
1250 |
|
} else |
1251 |
|
return false; |
1283 |
|
vol.channel0 = vol.channel2 = left; |
1284 |
|
vol.channel1 = vol.channel3 = right; |
1285 |
|
ioctl(fh->fd, CDROMVOLCTRL, &vol); |
1286 |
< |
#elif defined(__FreeBSD__) |
1286 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1287 |
|
struct ioc_vol vol; |
1288 |
|
vol.vol[0] = vol.vol[2] = left; |
1289 |
|
vol.vol[1] = vol.vol[3] = right; |
1310 |
|
ioctl(fh->fd, CDROMVOLREAD, &vol); |
1311 |
|
left = vol.channel0; |
1312 |
|
right = vol.channel1; |
1313 |
< |
#elif defined(__FreeBSD__) |
1313 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
1314 |
|
struct ioc_vol vol; |
1315 |
|
ioctl(fh->fd, CDIOCGETVOL, &vol); |
1316 |
|
left = vol.vol[0]; |