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.18 by cebix, 2002-04-28T14:06:17Z

# 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");
196 < #elif defined(__FreeBSD__)
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__) || defined(__NetBSD__)
212          PrefsAddString("cdrom", "/dev/cd0c");
213   #endif
214   }
# Line 185 | Line 221 | void SysAddCDROMPrefs(void)
221   void SysAddSerialPrefs(void)
222   {
223   #if defined(__linux__)
224 <        PrefsAddString("seriala", "/dev/ttyS0");
225 <        PrefsAddString("serialb", "/dev/ttyS1");
224 >        if (access("/dev/.devfsd", F_OK) < 0) {
225 >                PrefsAddString("seriala", "/dev/ttyS0");
226 >                PrefsAddString("serialb", "/dev/ttyS1");
227 >        } else {
228 >                PrefsAddString("seriala", "/dev/tts/0");
229 >                PrefsAddString("serialb", "/dev/tts/1");
230 >        }
231   #elif defined(__FreeBSD__)
232          PrefsAddString("seriala", "/dev/cuaa0");
233          PrefsAddString("serialb", "/dev/cuaa1");
234 + #elif defined(__NetBSD__)
235 +        PrefsAddString("seriala", "/dev/tty00");
236 +        PrefsAddString("serialb", "/dev/tty01");
237   #endif
238   }
239  
# Line 214 | Line 258 | static bool is_drive_mounted(const char
258                          // Parse line
259                          if (strncmp(line, dev_name, strlen(dev_name)) == 0) {
260                                  mount_name[0] = 0;
261 <                                char dummy[256];
262 <                                sscanf(line, "%s %s", dummy, mount_name);
261 >                                char *dummy;
262 >                                sscanf(line, "%as %s", &dummy, mount_name);
263 >                                free(dummy);
264                                  fclose(f);
265                                  return true;
266                          }
# Line 234 | Line 279 | static bool is_drive_mounted(const char
279   void *Sys_open(const char *name, bool read_only)
280   {
281          bool is_file = strncmp(name, "/dev/", 5) != 0;
282 < #ifdef __FreeBSD__
282 > #if defined(__FreeBSD__)
283                          // SCSI                             IDE
284          bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0 || strncmp(name, "/dev/acd", 8) == 0;
285   #else
# Line 262 | Line 307 | void *Sys_open(const char *name, bool re
307          }
308  
309          // Open file/device
310 < #ifdef __linux__
310 > #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
311          int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0));
312   #else
313          int fd = open(name, read_only ? O_RDONLY : O_RDWR);
# Line 284 | Line 329 | void *Sys_open(const char *name, bool re
329                  if (fh->is_file) {
330                          // Detect disk image file layout
331                          loff_t size = 0;
332 < #ifdef __linux__
332 > #if defined(__linux__)
333                          _llseek(fh->fd, 0, 0, &size, SEEK_END);
334   #else
335                          size = lseek(fd, 0, SEEK_END);
# Line 319 | Line 364 | void *Sys_open(const char *name, bool re
364   #else
365                                          fh->cdrom_cap = 0;
366   #endif
367 + #elif defined(__NetBSD__)
368 +                                        fh->is_floppy = ((st.st_rdev >> 16) == 2);
369   #endif
370                                  }
371                          }
# Line 362 | Line 409 | size_t Sys_read(void *arg, void *buffer,
409                  return 0;
410  
411          // Seek to position
412 < #ifdef __linux__
412 > #if defined(__linux__)
413          loff_t pos = offset + fh->start_byte, res;
414          if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0)
415                  return 0;
# Line 388 | Line 435 | size_t Sys_write(void *arg, void *buffer
435                  return 0;
436  
437          // Seek to position
438 < #ifdef __linux__
438 > #if defined(__linux__)
439          loff_t pos = offset + fh->start_byte, res;
440          if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0)
441                  return 0;
# Line 415 | Line 462 | loff_t SysGetFileSize(void *arg)
462          if (fh->is_file)
463                  return fh->file_size;
464          else {
465 < #ifdef __linux__
466 <                loff_t pos = 0;
467 <                _llseek(fh->fd, 0, 0, &pos, SEEK_END);
468 <                return pos - fh->start_byte;
465 > #if defined(__linux__)
466 >                long blocks;
467 >                if (ioctl(fh->fd, BLKGETSIZE, &blocks) < 0)
468 >                        return 0;
469 >                D(bug(" BLKGETSIZE returns %d blocks\n", blocks));
470 >                return (loff_t)blocks * 512;
471   #else
472                  return lseek(fh->fd, 0, SEEK_END) - fh->start_byte;
473   #endif
# Line 441 | Line 490 | void SysEject(void *arg)
490                  fsync(fh->fd);
491                  ioctl(fh->fd, FDFLUSH);
492                  ioctl(fh->fd, FDEJECT);
493 +                close(fh->fd);  // Close and reopen so the driver will see the media change
494 +                fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
495          } else if (fh->is_cdrom) {
496                  ioctl(fh->fd, CDROMEJECT);
497                  close(fh->fd);  // Close and reopen so the driver will see the media change
498                  fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
499          }
500 < #elif defined(__FreeBSD__)
500 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
501          if (fh->is_floppy) {
502                  fsync(fh->fd);
452                //ioctl(fh->fd, FDFLUSH);
453                //ioctl(fh->fd, FDEJECT);
503          } else if (fh->is_cdrom) {
504                  ioctl(fh->fd, CDIOCEJECT);
505                  close(fh->fd);  // Close and reopen so the driver will see the media change
# Line 485 | Line 534 | bool SysIsReadOnly(void *arg)
534          if (!fh)
535                  return true;
536  
537 < #ifdef __linux__
537 > #if defined(__linux__)
538          if (fh->is_floppy) {
539                  struct floppy_drive_struct stat;
540                  ioctl(fh->fd, FDGETDRVSTAT, &stat);
# Line 532 | Line 581 | bool SysIsDiskInserted(void *arg)
581          } else if (fh->is_floppy) {
582                  char block[512];
583                  lseek(fh->fd, 0, SEEK_SET);
584 <                return read(fh->fd, block, 512) == 512;
584 >                ssize_t actual = read(fh->fd, block, 512);
585 >                if (actual < 0) {
586 >                        close(fh->fd);  // Close and reopen so the driver will see the media change
587 >                        fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
588 >                        actual = read(fh->fd, block, 512);
589 >                }
590 >                return actual == 512;
591          } else if (fh->is_cdrom) {
592 + #ifdef CDROM_MEDIA_CHANGED
593 +                if (fh->cdrom_cap & CDC_MEDIA_CHANGED) {
594 +                        // If we don't do this, all attempts to read from a disc fail
595 +                        // once the tray has been opened (altough the TOC reads fine).
596 +                        // Can somebody explain this to me?
597 +                        if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) {
598 +                                close(fh->fd);
599 +                                fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
600 +                        }
601 +                }
602 + #endif
603   #ifdef CDROM_DRIVE_STATUS
604                  if (fh->cdrom_cap & CDC_DRIVE_STATUS) {
605                          return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
# Line 541 | Line 607 | bool SysIsDiskInserted(void *arg)
607   #endif
608                  cdrom_tochdr header;
609                  return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0;
610 < #elif defined(__FreeBSD__)
610 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
611          } else if (fh->is_floppy) {
612                  return false;   //!!
613          } else if (fh->is_cdrom) {
# Line 691 | Line 757 | bool SysCDReadTOC(void *arg, uint8 *toc)
757                  *toc++ = toc_size >> 8;
758                  *toc++ = toc_size & 0xff;
759                  return true;
760 + #elif defined(__NetBSD__)
761 +                uint8 *p = toc + 2;
762 +
763 +                // Header
764 +                struct ioc_toc_header header;
765 +                if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
766 +                        return false;
767 +                *p++ = header.starting_track;
768 +                *p++ = header.ending_track;
769 +
770 +                // Tracks (this is nice... :-)
771 +                struct ioc_read_toc_entry entries;
772 +                entries.address_format = CD_MSF_FORMAT;
773 +                entries.starting_track = 1;
774 +                entries.data_len = 800;
775 +                entries.data = (cd_toc_entry *)p;
776 +                if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0)
777 +                        return false;
778 +
779 +                // TOC size
780 +                int toc_size = p - toc;
781 +                *toc++ = toc_size >> 8;
782 +                *toc++ = toc_size & 0xff;
783 +                return true;
784 + #else
785 +                return false;
786   #endif
787          } else
788                  return false;
# Line 730 | Line 822 | bool SysCDGetPosition(void *arg, uint8 *
822                  *pos++ = chan.cdsc_reladdr.msf.second;
823                  *pos++ = chan.cdsc_reladdr.msf.frame;
824                  return true;
825 < #elif defined(__FreeBSD__)
825 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
826                  struct ioc_read_subchannel chan;
827                  chan.data_format = CD_MSF_FORMAT;
828                  chan.address_format = CD_MSF_FORMAT;
# Line 754 | Line 846 | bool SysCDGetPosition(void *arg, uint8 *
846                  *pos++ = chan.data->what.position.reladdr.msf.second;
847                  *pos++ = chan.data->what.position.reladdr.msf.frame;
848                  return true;
849 + #else
850 +                return false;
851   #endif
852          } else
853                  return false;
# Line 780 | Line 874 | bool SysCDPlay(void *arg, uint8 start_m,
874                  play.cdmsf_sec1 = end_s;
875                  play.cdmsf_frame1 = end_f;
876                  return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0;
877 < #elif defined(__FreeBSD__)
877 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
878                  struct ioc_play_msf play;
879                  play.start_m = start_m;
880                  play.start_s = start_s;
# Line 789 | Line 883 | bool SysCDPlay(void *arg, uint8 start_m,
883                  play.end_s = end_s;
884                  play.end_f = end_f;
885                  return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0;
886 + #else
887 +                return false;
888   #endif
889          } else
890                  return false;
# Line 808 | Line 904 | bool SysCDPause(void *arg)
904          if (fh->is_cdrom) {
905   #if defined(__linux__)
906                  return ioctl(fh->fd, CDROMPAUSE) == 0;
907 < #elif defined(__FreeBSD__)
907 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
908                  return ioctl(fh->fd, CDIOCPAUSE) == 0;
909 + #else
910 +                return false;
911   #endif
912          } else
913                  return false;
# Line 829 | Line 927 | bool SysCDResume(void *arg)
927          if (fh->is_cdrom) {
928   #if defined(__linux__)
929                  return ioctl(fh->fd, CDROMRESUME) == 0;
930 < #elif defined(__FreeBSD__)
930 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
931                  return ioctl(fh->fd, CDIOCRESUME) == 0;
932 + #else
933 +                return false;
934   #endif
935          } else
936                  return false;
# Line 850 | Line 950 | bool SysCDStop(void *arg, uint8 lead_out
950          if (fh->is_cdrom) {
951   #if defined(__linux__)
952                  return ioctl(fh->fd, CDROMSTOP) == 0;
953 < #elif defined(__FreeBSD__)
953 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
954                  return ioctl(fh->fd, CDIOCSTOP) == 0;
955 + #else
956 +                return false;
957   #endif
958          } else
959                  return false;
# Line 889 | Line 991 | void SysCDSetVolume(void *arg, uint8 lef
991                  vol.channel0 = vol.channel2 = left;
992                  vol.channel1 = vol.channel3 = right;
993                  ioctl(fh->fd, CDROMVOLCTRL, &vol);
994 < #elif defined(__FreeBSD__)
994 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
995                  struct ioc_vol vol;
996                  vol.vol[0] = vol.vol[2] = left;
997                  vol.vol[1] = vol.vol[3] = right;
# Line 916 | Line 1018 | void SysCDGetVolume(void *arg, uint8 &le
1018                  ioctl(fh->fd, CDROMVOLREAD, &vol);
1019                  left = vol.channel0;
1020                  right = vol.channel1;
1021 < #elif defined(__FreeBSD__)
1021 > #elif defined(__FreeBSD__) || defined(__NetBSD__)
1022                  struct ioc_vol vol;
1023                  ioctl(fh->fd, CDIOCGETVOL, &vol);
1024                  left = vol.vol[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines