ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/sys_unix.cpp (file contents):
Revision 1.2 by cebix, 1999-10-03T17:45:08Z vs.
Revision 1.17 by gbeauche, 2002-03-29T16:24:18Z

# Line 1 | Line 1
1   /*
2   *  sys_unix.cpp - System dependent routines, Unix implementation
3   *
4 < *  Basilisk II (C) 1997-1999 Christian Bauer
4 > *  Basilisk II (C) 1997-2002 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
# Line 25 | Line 25
25   #include <errno.h>
26  
27   #ifdef __linux__
28 + #include <sys/mount.h>
29   #include <linux/cdrom.h>
30   #include <linux/fd.h>
31   #include <linux/major.h>
32   #include <linux/kdev_t.h>
33   #include <linux/unistd.h>
34 + #include <dirent.h>
35  
36   #ifdef __NR__llseek
37 < _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, loff_t *, res, uint, wh);
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(uint fd, ulong hi, ulong lo, loff_t *res, uint wh)
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;
# Line 46 | Line 48 | static int _llseek(uint fd, ulong hi, ul
48   #endif
49   #endif
50  
51 < #ifdef __FreeBSD__
51 > #if defined(__FreeBSD__) || defined(__NetBSD__)
52   #include <sys/cdio.h>
53   #endif
54  
# Line 119 | Line 121 | void SysMountFirstFloppy(void)
121   void SysAddFloppyPrefs(void)
122   {
123   #if defined(__linux__)
124 <        PrefsAddString("floppy", "/dev/fd0H1440");
125 <        PrefsAddString("floppy", "/dev/fd1H1440");
124 >        if (access("/dev/.devfsd", F_OK) < 0) {
125 >                PrefsAddString("floppy", "/dev/fd0u1440");
126 >                PrefsAddString("floppy", "/dev/fd1u1440");
127 >        } else {
128 >                DIR *fd_dir = opendir("/dev/floppy");
129 >                if (fd_dir) {
130 >                        struct dirent *floppy_dev;
131 >                        while ((floppy_dev = readdir(fd_dir)) != NULL) {
132 >                                if (strstr(floppy_dev->d_name, "u1440") != NULL) {
133 >                                        char fd_dev[20];
134 >                                        sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name);
135 >                                        PrefsAddString("floppy", fd_dev);
136 >                                }
137 >                        }
138 >                        closedir(fd_dir);
139 >                }
140 >        }
141 > #elif defined(__NetBSD__)
142 >        PrefsAddString("floppy", "/dev/fd0a");
143 >        PrefsAddString("floppy", "/dev/fd1a");
144   #else
145          PrefsAddString("floppy", "/dev/fd0");
146          PrefsAddString("floppy", "/dev/fd1");
# Line 142 | Line 162 | void SysAddDiskPrefs(void)
162                  while(fgets(line, 255, f)) {
163                          // Read line
164                          int len = strlen(line);
165 <                        if (len == 0)
165 >                        if (len == 0 || line[0] == '#')
166                                  continue;
167                          line[len-1] = 0;
168  
169                          // Parse line
170                          char *dev, *mnt_point, *fstype;
171 <                        if (sscanf(line, "%s %s %s", &dev, &mnt_point, &fstype) == 3) {
171 >                        if (sscanf(line, "%as %as %as", &dev, &mnt_point, &fstype) == 3) {
172                                  if (strcmp(fstype, "hfs") == 0)
173                                          PrefsAddString("disk", dev);
174                          }
175 +                        free(dev); free(mnt_point); free(fstype);
176                  }
177                  fclose(f);
178          }
# Line 171 | Line 192 | void SysAddCDROMPrefs(void)
192                  return;
193  
194   #if defined(__linux__)
195 <        PrefsAddString("cdrom", "/dev/cdrom");
195 >        if (access("/dev/.devfsd", F_OK) < 0)
196 >                PrefsAddString("cdrom", "/dev/cdrom");
197 >        else {
198 >                DIR *cd_dir = opendir("/dev/cdroms");
199 >                if (cd_dir) {
200 >                        struct dirent *cdrom_dev;
201 >                        while ((cdrom_dev = readdir(cd_dir)) != NULL) {
202 >                                if (strcmp(cdrom_dev->d_name, ".") != 0 && strcmp(cdrom_dev->d_name, "..") != 0) {
203 >                                        char cd_dev[20];
204 >                                        sprintf(cd_dev, "/dev/cdroms/%s", cdrom_dev->d_name);
205 >                                        PrefsAddString("cdrom", cd_dev);
206 >                                }
207 >                        }
208 >                        closedir(cd_dir);
209 >                }
210 >        }
211   #elif defined(__FreeBSD__)
212          PrefsAddString("cdrom", "/dev/cd0c");
213 + #elif defined(__NetBSD__)
214 +        PrefsAddString("cdrom", "/dev/cd0d");
215   #endif
216   }
217  
# Line 185 | Line 223 | void SysAddCDROMPrefs(void)
223   void SysAddSerialPrefs(void)
224   {
225   #if defined(__linux__)
226 <        PrefsAddString("seriala", "/dev/ttyS0");
227 <        PrefsAddString("serialb", "/dev/ttyS1");
226 >        if (access("/dev/.devfsd", F_OK) < 0) {
227 >                PrefsAddString("seriala", "/dev/ttyS0");
228 >                PrefsAddString("serialb", "/dev/ttyS1");
229 >        } else {
230 >                PrefsAddString("seriala", "/dev/tts/0");
231 >                PrefsAddString("serialb", "/dev/tts/1");
232 >        }
233   #elif defined(__FreeBSD__)
234          PrefsAddString("seriala", "/dev/cuaa0");
235          PrefsAddString("serialb", "/dev/cuaa1");
236 + #elif defined(__NetBSD__)
237 +        PrefsAddString("seriala", "/dev/tty00");
238 +        PrefsAddString("serialb", "/dev/tty01");
239   #endif
240   }
241  
# Line 214 | Line 260 | static bool is_drive_mounted(const char
260                          // Parse line
261                          if (strncmp(line, dev_name, strlen(dev_name)) == 0) {
262                                  mount_name[0] = 0;
263 <                                char dummy[256];
264 <                                sscanf(line, "%s %s", dummy, mount_name);
263 >                                char *dummy;
264 >                                sscanf(line, "%as %s", &dummy, mount_name);
265 >                                free(dummy);
266                                  fclose(f);
267                                  return true;
268                          }
# Line 234 | Line 281 | static bool is_drive_mounted(const char
281   void *Sys_open(const char *name, bool read_only)
282   {
283          bool is_file = strncmp(name, "/dev/", 5) != 0;
284 < #ifdef __FreeBSD__
284 > #if defined(__FreeBSD__)
285                          // SCSI                             IDE
286          bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0 || strncmp(name, "/dev/acd", 8) == 0;
287   #else
# Line 262 | Line 309 | void *Sys_open(const char *name, bool re
309          }
310  
311          // Open file/device
312 < #ifdef __linux__
312 > #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
313          int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0));
314   #else
315          int fd = open(name, read_only ? O_RDONLY : O_RDWR);
# Line 284 | Line 331 | void *Sys_open(const char *name, bool re
331                  if (fh->is_file) {
332                          // Detect disk image file layout
333                          loff_t size = 0;
334 < #ifdef __linux__
334 > #if defined(__linux__)
335                          _llseek(fh->fd, 0, 0, &size, SEEK_END);
336   #else
337                          size = lseek(fd, 0, SEEK_END);
# Line 319 | Line 366 | void *Sys_open(const char *name, bool re
366   #else
367                                          fh->cdrom_cap = 0;
368   #endif
369 + #elif defined(__NetBSD__)
370 +                                        fh->is_floppy = ((st.st_rdev >> 16) == 2);
371   #endif
372                                  }
373                          }
# Line 362 | Line 411 | size_t Sys_read(void *arg, void *buffer,
411                  return 0;
412  
413          // Seek to position
414 < #ifdef __linux__
414 > #if defined(__linux__)
415          loff_t pos = offset + fh->start_byte, res;
416          if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0)
417                  return 0;
# Line 388 | Line 437 | size_t Sys_write(void *arg, void *buffer
437                  return 0;
438  
439          // Seek to position
440 < #ifdef __linux__
440 > #if defined(__linux__)
441          loff_t pos = offset + fh->start_byte, res;
442          if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0)
443                  return 0;
# Line 415 | Line 464 | loff_t SysGetFileSize(void *arg)
464          if (fh->is_file)
465                  return fh->file_size;
466          else {
467 < #ifdef __linux__
468 <                loff_t pos = 0;
469 <                _llseek(fh->fd, 0, 0, &pos, SEEK_END);
470 <                return pos - fh->start_byte;
467 > #if defined(__linux__)
468 >                long blocks;
469 >                if (ioctl(fh->fd, BLKGETSIZE, &blocks) < 0)
470 >                        return 0;
471 >                D(bug(" BLKGETSIZE returns %d blocks\n", blocks));
472 >                return (loff_t)blocks * 512;
473   #else
474                  return lseek(fh->fd, 0, SEEK_END) - fh->start_byte;
475   #endif
# Line 441 | Line 492 | void SysEject(void *arg)
492                  fsync(fh->fd);
493                  ioctl(fh->fd, FDFLUSH);
494                  ioctl(fh->fd, FDEJECT);
495 +                close(fh->fd);  // Close and reopen so the driver will see the media change
496 +                fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
497          } else if (fh->is_cdrom) {
498                  ioctl(fh->fd, CDROMEJECT);
499                  close(fh->fd);  // Close and reopen so the driver will see the media change
500                  fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
501          }
502 < #elif defined(__FreeBSD__)
502 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
503          if (fh->is_floppy) {
504                  fsync(fh->fd);
452                //ioctl(fh->fd, FDFLUSH);
453                //ioctl(fh->fd, FDEJECT);
505          } else if (fh->is_cdrom) {
506                  ioctl(fh->fd, CDIOCEJECT);
507                  close(fh->fd);  // Close and reopen so the driver will see the media change
# Line 485 | Line 536 | bool SysIsReadOnly(void *arg)
536          if (!fh)
537                  return true;
538  
539 < #ifdef __linux__
539 > #if defined(__linux__)
540          if (fh->is_floppy) {
541                  struct floppy_drive_struct stat;
542                  ioctl(fh->fd, FDGETDRVSTAT, &stat);
# Line 532 | Line 583 | bool SysIsDiskInserted(void *arg)
583          } else if (fh->is_floppy) {
584                  char block[512];
585                  lseek(fh->fd, 0, SEEK_SET);
586 <                return read(fh->fd, block, 512) == 512;
586 >                ssize_t actual = read(fh->fd, block, 512);
587 >                if (actual < 0) {
588 >                        close(fh->fd);  // Close and reopen so the driver will see the media change
589 >                        fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
590 >                        actual = read(fh->fd, block, 512);
591 >                }
592 >                return actual == 512;
593          } else if (fh->is_cdrom) {
594 + #ifdef CDROM_MEDIA_CHANGED
595 +                if (fh->cdrom_cap & CDC_MEDIA_CHANGED) {
596 +                        // If we don't do this, all attempts to read from a disc fail
597 +                        // once the tray has been opened (altough the TOC reads fine).
598 +                        // Can somebody explain this to me?
599 +                        if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) {
600 +                                close(fh->fd);
601 +                                fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
602 +                        }
603 +                }
604 + #endif
605   #ifdef CDROM_DRIVE_STATUS
606                  if (fh->cdrom_cap & CDC_DRIVE_STATUS) {
607                          return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
# Line 541 | Line 609 | bool SysIsDiskInserted(void *arg)
609   #endif
610                  cdrom_tochdr header;
611                  return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0;
612 < #elif defined(__FreeBSD__)
612 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
613          } else if (fh->is_floppy) {
614                  return false;   //!!
615          } else if (fh->is_cdrom) {
# Line 691 | Line 759 | bool SysCDReadTOC(void *arg, uint8 *toc)
759                  *toc++ = toc_size >> 8;
760                  *toc++ = toc_size & 0xff;
761                  return true;
762 + #elif defined(__NetBSD__)
763 +                uint8 *p = toc + 2;
764 +
765 +                // Header
766 +                struct ioc_toc_header header;
767 +                if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
768 +                        return false;
769 +                *p++ = header.starting_track;
770 +                *p++ = header.ending_track;
771 +
772 +                // Tracks (this is nice... :-)
773 +                struct ioc_read_toc_entry entries;
774 +                entries.address_format = CD_MSF_FORMAT;
775 +                entries.starting_track = 1;
776 +                entries.data_len = 800;
777 +                entries.data = (cd_toc_entry *)p;
778 +                if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0)
779 +                        return false;
780 +
781 +                // TOC size
782 +                int toc_size = p - toc;
783 +                *toc++ = toc_size >> 8;
784 +                *toc++ = toc_size & 0xff;
785 +                return true;
786 + #else
787 +                return false;
788   #endif
789          } else
790                  return false;
# Line 730 | Line 824 | bool SysCDGetPosition(void *arg, uint8 *
824                  *pos++ = chan.cdsc_reladdr.msf.second;
825                  *pos++ = chan.cdsc_reladdr.msf.frame;
826                  return true;
827 < #elif defined(__FreeBSD__)
827 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
828                  struct ioc_read_subchannel chan;
829                  chan.data_format = CD_MSF_FORMAT;
830                  chan.address_format = CD_MSF_FORMAT;
# Line 754 | Line 848 | bool SysCDGetPosition(void *arg, uint8 *
848                  *pos++ = chan.data->what.position.reladdr.msf.second;
849                  *pos++ = chan.data->what.position.reladdr.msf.frame;
850                  return true;
851 + #else
852 +                return false;
853   #endif
854          } else
855                  return false;
# Line 780 | Line 876 | bool SysCDPlay(void *arg, uint8 start_m,
876                  play.cdmsf_sec1 = end_s;
877                  play.cdmsf_frame1 = end_f;
878                  return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0;
879 < #elif defined(__FreeBSD__)
879 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
880                  struct ioc_play_msf play;
881                  play.start_m = start_m;
882                  play.start_s = start_s;
# Line 789 | Line 885 | bool SysCDPlay(void *arg, uint8 start_m,
885                  play.end_s = end_s;
886                  play.end_f = end_f;
887                  return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0;
888 + #else
889 +                return false;
890   #endif
891          } else
892                  return false;
# Line 808 | Line 906 | bool SysCDPause(void *arg)
906          if (fh->is_cdrom) {
907   #if defined(__linux__)
908                  return ioctl(fh->fd, CDROMPAUSE) == 0;
909 < #elif defined(__FreeBSD__)
909 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
910                  return ioctl(fh->fd, CDIOCPAUSE) == 0;
911 + #else
912 +                return false;
913   #endif
914          } else
915                  return false;
# Line 829 | Line 929 | bool SysCDResume(void *arg)
929          if (fh->is_cdrom) {
930   #if defined(__linux__)
931                  return ioctl(fh->fd, CDROMRESUME) == 0;
932 < #elif defined(__FreeBSD__)
932 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
933                  return ioctl(fh->fd, CDIOCRESUME) == 0;
934 + #else
935 +                return false;
936   #endif
937          } else
938                  return false;
# Line 850 | Line 952 | bool SysCDStop(void *arg, uint8 lead_out
952          if (fh->is_cdrom) {
953   #if defined(__linux__)
954                  return ioctl(fh->fd, CDROMSTOP) == 0;
955 < #elif defined(__FreeBSD__)
955 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
956                  return ioctl(fh->fd, CDIOCSTOP) == 0;
957 + #else
958 +                return false;
959   #endif
960          } else
961                  return false;
# Line 889 | Line 993 | void SysCDSetVolume(void *arg, uint8 lef
993                  vol.channel0 = vol.channel2 = left;
994                  vol.channel1 = vol.channel3 = right;
995                  ioctl(fh->fd, CDROMVOLCTRL, &vol);
996 < #elif defined(__FreeBSD__)
996 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
997                  struct ioc_vol vol;
998                  vol.vol[0] = vol.vol[2] = left;
999                  vol.vol[1] = vol.vol[3] = right;
# Line 916 | Line 1020 | void SysCDGetVolume(void *arg, uint8 &le
1020                  ioctl(fh->fd, CDROMVOLREAD, &vol);
1021                  left = vol.channel0;
1022                  right = vol.channel1;
1023 < #elif defined(__FreeBSD__)
1023 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
1024                  struct ioc_vol vol;
1025                  ioctl(fh->fd, CDIOCGETVOL, &vol);
1026                  left = vol.vol[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines