/
dosfile.c
1396 lines (1165 loc) · 28 KB
/
dosfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
*
* This file has been modified as part of the FreeMiNT project. See
* the file Changes.MH for details and dates.
*
*
* Copyright 1990,1991,1992 Eric R. Smith.
* Copyright 1992,1993,1994 Atari Corporation.
* All rights reserved.
*
*/
/* DOS file handling routines */
# include "dosfile.h"
# include "global.h"
# include "libkern/libkern.h"
# include "mint/asm.h"
# include "mint/filedesc.h"
# include "mint/ioctl.h"
# include "mint/iov.h"
# include "mint/net.h"
# include "biosfs.h"
# include "filesys.h"
# include "info.h"
# include "ipc_socketdev.h"
# include "k_fds.h"
# include "k_prot.h"
# include "kmemory.h"
# include "memory.h"
# include "pipefs.h"
# include "proc.h"
# include "procfs.h"
# include "time.h"
# include "timeout.h"
# include "tty.h"
# include "util.h"
/* wait condition for selecting processes which got collisions */
short select_coll;
long _cdecl
sys_f_open (const char *name, short mode)
{
struct proc *p = get_curproc();
FILEPTR *fp = NULL;
short fd = MIN_OPEN - 1;
long ret;
TRACE (("Fopen(%s, %x)", name, mode));
if (mode & O_GLOBAL) {
ALERT("O_GLOBAL is obsolete, please update your driver (%s)",name);
return EINVAL;
}
/* make sure the mode is legal */
mode &= O_USER;
/* note: file mode 3 is reserved for the kernel;
* for users, transmogrify it into O_RDWR (mode 2)
*/
if ((mode & O_RWMODE) == O_EXEC)
mode = (mode & ~O_RWMODE) | O_RDWR;
assert (p->p_fd && p->p_cwd);
ret = FD_ALLOC (p, &fd, MIN_OPEN);
if (ret) goto error;
ret = FP_ALLOC (p, &fp);
if (ret) goto error;
ret = do_open (&fp, name, mode, 0, NULL);
if (ret) goto error;
/* activate the fp, default is to close non-standard files on exec */
FP_DONE (p, fp, fd, FD_CLOEXEC);
TRACE (("Fopen: returning %d", fd));
return fd;
error:
if (fd >= MIN_OPEN) FD_REMOVE (p, fd);
if (fp) { fp->links--; FP_FREE (fp); }
return ret;
}
long _cdecl
sys_f_create (const char *name, short attrib)
{
struct proc *p = get_curproc();
FILEPTR *fp = NULL;
short fd = MIN_OPEN - 1;
long ret;
TRACE (("Fcreate(%s, %x)", name, attrib));
assert (p->p_fd && p->p_cwd);
ret = FD_ALLOC (p, &fd, MIN_OPEN);
if (ret) goto error;
ret = FP_ALLOC (p, &fp);
if (ret) goto error;
if (attrib == FA_LABEL)
{
char temp1[PATH_MAX];
fcookie dir;
/* just in case the caller tries to do something with this handle,
* make it point to u:\dev\null
*/
ret = do_open (&fp, "u:\\dev\\null", O_RDWR|O_CREAT|O_TRUNC, 0, NULL);
if (ret) goto error;
ret = path2cookie (p, name, temp1, &dir);
if (ret) goto error;
ret = xfs_writelabel (dir.fs, &dir, temp1);
release_cookie (&dir);
if (ret) goto error;
}
else if (attrib & (FA_LABEL|FA_DIR))
{
DEBUG (("Fcreate(%s,%x): illegal attributes", name, attrib));
ret = EACCES;
goto error;
}
else
{
ret = do_open (&fp, name, O_RDWR|O_CREAT|O_TRUNC, attrib, NULL);
if (ret)
{
DEBUG (("Fcreate(%s) failed, error %d", name, ret));
goto error;
}
}
/* activate the fp, default is to close non-standard files on exec */
FP_DONE (p, fp, fd, FD_CLOEXEC);
TRACE (("Fcreate: returning %d", fd));
return fd;
error:
if (fd >= MIN_OPEN) FD_REMOVE (p, fd);
if (fp) { fp->links--; FP_FREE (fp); }
return ret;
}
long _cdecl
sys_f_close (short fd)
{
struct proc *p = get_curproc();
FILEPTR *f;
DIR **where, **_where;
long r;
DEBUG (("Fclose: %d", fd));
# ifdef WITH_SINGLE_TASK_SUPPORT
/* this is for pure-debugger:
* some progs call Fclose(-1) when they exit which would
* cause pd to lose keyboard
*/
if( fd < 0 && (p->modeflags & M_SINGLE_TASK) )
{
DEBUG(("Fclose:return 0 for negative fd in singletask-mode."));
return 0;
}
# endif
r = GETFILEPTR (&p, &fd, &f);
if (r) return r;
/* close dirent's for the same FD */
where = &p->p_fd->searches;
while (*where) {
_where = &((*where)->next);
if (fd >= MIN_OPEN && (*where)->fd == fd) {
r = xfs_closedir ((*where)->fc.fs, (*where));
release_cookie (&(*where)->fc);
kfree(*where);
/* unlink the directory from the chain */
*where = *_where;
}
where = _where;
}
r = do_close (p, f);
/* XXX do this before do_close? */
FD_REMOVE (p, fd);
# if 0
/* standard handles should be restored to default values
* in TOS domain!
*
* XXX: why?
*/
if (p->domain == DOM_TOS)
{
f = NULL;
if (fd == 0 || fd == 1)
f = p->p_fd->ofiles[-1];
else if (fd == 2 || fd == 3)
f = p->p_fd->ofiles[-fd];
if (f)
{
FP_DONE (p, f, fd, 0);
f->links++;
}
}
# endif
return r;
}
long _cdecl
sys_f_read (short fd, long count, char *buf)
{
struct proc *p = get_curproc();
FILEPTR *f;
long r;
r = GETFILEPTR (&p, &fd, &f);
if (r) return r;
if ((f->flags & O_RWMODE) == O_WRONLY)
{
DEBUG (("Fread: read on a write-only handle"));
return EACCES;
}
if (f->flags & O_DIRECTORY)
{
DEBUG (("Fread(%i): read on a directory", fd));
return EISDIR;
}
if (is_terminal (f))
return tty_read (f, buf, count);
TRACELOW (("Fread: %ld bytes from handle %d to %lx", count, fd, buf));
return xdd_read (f, buf, count);
}
long _cdecl
sys_f_write (short fd, long count, const char *buf)
{
struct proc *p = get_curproc();
FILEPTR *f;
long r;
r = GETFILEPTR (&p, &fd, &f);
if (r) return r;
if ((f->flags & O_RWMODE) == O_RDONLY)
{
DEBUG (("Fwrite: write on a read-only handle"));
return EACCES;
}
if (is_terminal (f))
return tty_write (f, buf, count);
/* Prevent broken device drivers from wiping the disk.
* We return a zero rather than a negative error code
* to help programs those don't handle GEMDOS errors
* returned by Fwrite()
*/
if (count <= 0)
{
DEBUG (("Fwrite: invalid count: %d", count));
return 0;
}
/* it would be faster to do this in the device driver, but this
* way the drivers are easier to write
*/
if (f->flags & O_APPEND)
{
r = xdd_lseek (f, 0L, SEEK_END);
/* ignore errors from unseekable files (e.g. pipes) */
if (r == EACCES)
r = 0;
} else
r = 0;
if (r >= 0)
{
TRACELOW (("Fwrite: %ld bytes to handle %d", count, fd));
r = xdd_write (f, buf, count);
}
if (r < 0)
DEBUG (("Fwrite: error %ld", r));
return r;
}
long _cdecl
sys_f_seek (long place, short fd, short how)
{
struct proc *p = get_curproc();
FILEPTR *f;
long r;
TRACE (("Fseek(%ld, %d) on handle %d", place, how, fd));
r = GETFILEPTR (&p, &fd, &f);
if (r) return r;
if (is_terminal (f))
return ESPIPE;
return xdd_lseek (f, place, how);
}
long _cdecl
sys_f_dup (short fd)
{
long r;
r = do_dup (fd, 0, 0);
TRACE (("Fdup(%d) -> %ld", fd, r));
return r;
}
long _cdecl
sys_f_force (short newfd, short oldfd)
{
struct proc *p = get_curproc();
FILEPTR *fp;
long ret;
TRACE (("Fforce(%d, %d)", newfd, oldfd));
ret = GETFILEPTR (&p, &oldfd, &fp);
if (ret) return ret;
if (newfd < MIN_HANDLE || newfd >= p->p_fd->nfiles)
{
DEBUG (("Fforce: new handle out of range"));
return EBADF;
}
do_close (p, p->p_fd->ofiles[newfd]);
p->p_fd->ofiles[newfd] = fp;
p->p_fd->ofileflags[newfd] = 0;
fp->links++;
/* special: for a tty, if this is becoming a control terminal and the
* tty doesn't have a pgrp yet, make it have the pgrp of the process
* doing the Fforce
*/
if (is_terminal (fp) && newfd == -1 && !(fp->flags & O_HEAD))
{
struct tty *tty = (struct tty *) fp->devinfo;
if (!tty->pgrp)
{
tty->pgrp = p->pgrp;
DEBUG (("f_force: assigned tty->pgrp = %i", tty->pgrp));
if (!(fp->flags & O_NDELAY) && (tty->state & TS_BLIND))
(*fp->dev->ioctl)(fp, TIOCWONLINE, 0);
}
}
return E_OK;
}
long _cdecl
sys_f_datime (ushort *timeptr, short fd, short wflag)
{
struct proc *p = get_curproc();
FILEPTR *f;
long r;
TRACE (("%s(%i)", __FUNCTION__, fd));
r = GETFILEPTR (&p, &fd, &f);
if (r) return r;
/* some programs use Fdatime to test for TTY devices */
if (is_terminal (f))
return EACCES;
if (f->fc.fs && f->fc.fs->fsflags & FS_EXT_3)
{
unsigned long ut = 0;
unsigned short dt;
if (wflag)
ut = unixtime(timeptr[0], timeptr[1]) + timezone;
dt = ut >> 16;
r = xdd_datime(f, &dt, wflag);
if (!r && !wflag) {
ut = (ut & 0x0000ffffUL) | ((unsigned long)dt << 16);
ut = dostime(ut - timezone);
timeptr[1] = (unsigned short)ut;
ut >>= 16;
timeptr[0] = (unsigned short)ut;
}
return r;
}
return xdd_datime (f, timeptr, wflag);
}
long _cdecl
sys_f_lock (short fd, short mode, long start, long length)
{
struct proc *p = get_curproc();
FILEPTR *f;
long r;
struct flock lock;
TRACE (("Flock(%i, %i, %li, %li)", fd, mode, start, length));
r = GETFILEPTR (&p, &fd, &f);
if (r) return r;
lock.l_whence = SEEK_SET;
lock.l_start = start;
lock.l_len = length;
if (mode == 0)
/* create a lock */
lock.l_type = F_WRLCK;
else if (mode == 1)
/* unlock region */
lock.l_type = F_UNLCK;
else
return EINVAL;
return xdd_ioctl (f, F_SETLK, &lock);
}
/*
* extensions to GEMDOS:
*/
static long
sys__ffstat_1_12 (struct file *f, XATTR *xattr)
{
long ret;
# ifdef OLDSOCKDEVEMU
if (f->dev == &sockdev || f->dev == &sockdevemu)
# else
if (f->dev == &sockdev)
# endif
return so_fstat_old (f, xattr);
if (!f->fc.fs)
{
DEBUG (("sys__ffstat_1_12: no xfs!"));
return ENOSYS;
}
ret = xfs_getxattr (f->fc.fs, &f->fc, xattr);
if ((ret == E_OK) && (f->fc.fs->fsflags & FS_EXT_3))
{
xtime_to_local_dos(xattr,m);
xtime_to_local_dos(xattr,a);
xtime_to_local_dos(xattr,c);
}
return ret;
}
static long
sys__ffstat_1_16 (struct file *f, struct stat *st)
{
# ifdef OLDSOCKDEVEMU
if (f->dev == &sockdev || f->dev == &sockdevemu)
# else
if (f->dev == &sockdev)
# endif
return so_fstat (f, st);
if (!f->fc.fs)
{
DEBUG (("sys__ffstat_1_16: no xfs"));
return ENOSYS;
}
return xfs_stat64 (f->fc.fs, &f->fc, st);
}
long _cdecl
sys_ffstat (short fd, struct stat *st)
{
struct proc *p = get_curproc();
FILEPTR *f;
long ret;
ret = GETFILEPTR (&p, &fd, &f);
if (ret) return ret;
return sys__ffstat_1_16 (f, st);
}
/*
* f_cntl: a combination "ioctl" and "fcntl". Some functions are
* handled here, if they apply to the file descriptors directly
* (e.g. F_DUPFD) or if they're easily translated into file system
* functions (e.g. FSTAT). Others are passed on to the device driver
* via dev->ioctl.
*/
long _cdecl
sys_f_cntl (short fd, long arg, short cmd)
{
struct proc *p = get_curproc();
FILEPTR *f;
long r;
TRACE (("Fcntl(%i, cmd=0x%x)", fd, cmd));
if (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC) {
return do_dup (fd, arg, cmd == F_DUPFD_CLOEXEC ? 1: 0);
}
TRACE(("Fcntl getfileptr"));
r = GETFILEPTR (&p, &fd, &f);
TRACE(("Fcntl r = %lx", r));
if (r) return r;
switch (cmd)
{
case F_GETFD:
{
TRACE (("Fcntl F_GETFD"));
return p->p_fd->ofileflags[fd];
}
case F_SETFD:
{
TRACE (("Fcntl F_SETFD"));
p->p_fd->ofileflags[fd] = arg;
return E_OK;
}
case F_GETFL:
{
TRACE (("Fcntl F_GETFL"));
return (f->flags & O_USER);
}
case F_SETFL:
{
TRACE (("Fcntl F_SETFL"));
/* make sure only user bits set */
arg &= O_USER;
/* make sure the file access and sharing modes are not changed */
arg &= ~(O_RWMODE | O_SHMODE);
arg |= f->flags & (O_RWMODE | O_SHMODE);
/* set user bits to arg */
f->flags &= ~O_USER;
f->flags |= arg;
return E_OK;
}
case FSTAT:
{
TRACE (("Fcntl FSTAT (%i, %lx) on \"%s\" -> %li", fd, arg, xfs_name (&(f->fc)), r));
return sys__ffstat_1_12 (f, (XATTR *) arg);
}
case FSTAT64:
{
TRACE (("Fcntl FSTAT64"));
TRACE (("Fcntl FSTAT64 (%i, %lx) on \"%s\" -> %li", fd, arg, xfs_name(&(f->fc)), r));
return sys__ffstat_1_16 (f, (struct stat *) arg);
}
case FUTIME:
{
TRACE (("Fcntl FUTIME"));
if (f->fc.fs && (f->fc.fs->fsflags & FS_EXT_3) && arg)
{
MUTIMBUF *buf = (MUTIMBUF *) arg;
ulong t [2];
t [0] = unixtime (buf->actime, buf->acdate) + timezone;
t [1] = unixtime (buf->modtime, buf->moddate) + timezone;
return xdd_ioctl (f, cmd, (void *) t);
}
break;
}
}
/* fall through to device ioctl */
TRACE (("Fcntl mode %x: calling ioctl", cmd));
if (is_terminal (f))
{
/* tty in the middle of a hangup? */
while (((struct tty *) f->devinfo)->hup_ospeed)
sleep (IO_Q, (long) &((struct tty *) f->devinfo)->state);
if (cmd == FIONREAD
|| cmd == FIONWRITE
|| cmd == TIOCSTART
|| cmd == TIOCSTOP
|| cmd == TIOCSBRK
|| cmd == TIOCFLUSH)
{
r = tty_ioctl (f, cmd, (void *) arg);
}
else
{
r = (*f->dev->ioctl)(f, cmd, (void *) arg);
if (r == ENOSYS)
r = tty_ioctl (f, cmd, (void *) arg);
}
}
else
r = xdd_ioctl (f, cmd, (void *) arg);
return r;
}
/*
* Fselect(timeout, rfd, wfd, xfd)
* timeout is an (unsigned) 16 bit integer giving the maximum number
* of milliseconds to wait; rfd, wfd, and xfd are pointers to 32 bit
* integers containing bitmasks that describe which file descriptors
* we're interested in. These masks are changed to represent which
* file descriptors actually have data waiting (rfd), are ready to
* output (wfd), or have exceptional conditions (xfd). If timeout is 0,
* fselect blocks until some file descriptor is ready; otherwise, it
* waits only "timeout" milliseconds. Return value: number of file
* descriptors that are available for reading/writing; or a negative
* error number.
*/
/* helper function for time outs */
static void _cdecl
unselectme (struct proc *p, long arg)
{
wakeselect (p);
}
long _cdecl
sys_f_select (ushort timeout, long *rfdp, long *wfdp, long *xfdp)
{
long rfd, wfd, xfd, col_rfd, col_wfd, col_xfd;
long mask;
long count;
FILEPTR *f;
struct proc *p;
TIMEOUT *t;
int i, rsel;
long wait_cond;
short sr;
#if 0
long oldgemtimer = 0, gemtimer = 0;
#endif
if (rfdp) col_rfd = rfd = *rfdp;
else col_rfd = rfd = 0;
if (wfdp) col_wfd = wfd = *wfdp;
else col_wfd = wfd = 0;
if (xfdp) col_xfd = xfd = *xfdp;
else col_xfd = xfd = 0;
/* watch out for aliasing */
if (rfdp) *rfdp = 0;
if (wfdp) *wfdp = 0;
if (xfdp) *xfdp = 0;
t = 0;
TRACELOW(("Fselect(%u, %lx, %lx, %lx)", timeout, rfd, wfd, xfd));
p = get_curproc(); /* help the optimizer out */
assert (p->p_fd && p->p_cwd);
/* first, validate the masks */
mask = 1L;
for (i = 0; i < p->p_fd->nfiles; i++)
{
if (((rfd & mask) || (wfd & mask) || (xfd & mask)) && !(p->p_fd->ofiles[i]))
{
DEBUG(("Fselect: invalid handle: %d", i));
return EBADF;
}
mask = mask << 1L;
}
/* now, loop through the file descriptors, setting up the select process */
/* NOTE: wakeselect will set p->wait_cond to 0 if data arrives during the
* selection
* Also note: because of the validation above, we may assume that the
* file handles are valid here. However, this assumption may no longer
* be true after we've gone to sleep, since a signal handler may have
* closed one of the handles.
*/
p->wait_cond = (long)wakeselect; /* flag */
retry_after_collision:
mask = 1L;
wait_cond = (long)wakeselect;
count = 0;
for (i = 0; i < p->p_fd->nfiles; i++)
{
if (col_rfd & mask)
{
f = p->p_fd->ofiles[i];
if (is_terminal(f))
rsel = (int) tty_select(f, (long)p, O_RDONLY);
else
rsel = (int) (*f->dev->select)(f, (long)p, O_RDONLY);
switch (rsel)
{
case 0:
col_rfd &= ~mask;
break;
case 1:
count++;
*rfdp |= mask;
break;
case 2:
wait_cond = (long)&select_coll;
break;
}
}
if (col_wfd & mask)
{
f = p->p_fd->ofiles[i];
if (is_terminal(f))
rsel = (int) tty_select(f, (long)p, O_WRONLY);
else
rsel = (int) (*f->dev->select)(f, (long)p, O_WRONLY);
switch (rsel)
{
case 0:
col_wfd &= ~mask;
break;
case 1:
count++;
*wfdp |= mask;
break;
case 2:
wait_cond = (long)&select_coll;
break;
}
}
if (col_xfd & mask)
{
f = p->p_fd->ofiles[i];
/* tesche: anybody worried about using O_RDWR for exceptional data? ;) */
rsel = (int) (*f->dev->select)(f, (long)p, O_RDWR);
/* tesche: for old device drivers, which don't understand this
* call, this will never be true and therefore won't disturb us here.
*/
switch (rsel)
{
case 0:
col_xfd &= ~mask;
break;
case 1:
count++;
*xfdp |= mask;
break;
case 2:
wait_cond = (long)&select_coll;
break;
}
}
mask = mask << 1L;
}
if (count == 0)
{
/* no data is ready yet */
if (timeout && !t)
{
t = addtimeout (p, (long)timeout, unselectme);
timeout = 0;
}
/* curproc->wait_cond changes when data arrives or the timeout happens */
sr = spl7 ();
while (p->wait_cond == (long)wakeselect)
{
p->wait_cond = wait_cond;
spl (sr);
/*
* The 0x100 tells sleep() to return without sleeping
* when curproc->wait_cond changes. This way we don't
* need spl7 (avoiding endless serial overruns).
* Also fixes a deadlock with checkkeys/checkbttys.
* They are called from sleep and may wakeselect()
* curproc. But sleep used to reset curproc->wait_cond
* to wakeselect causing curproc to sleep forever.
*/
if (sleep (SELECT_Q|0x100, wait_cond)) {
/* signal happened, abort */
if (t) canceltimeout(t);
count = EINTR;
goto cancel;
}
sr = spl7 ();
}
if (p->wait_cond == (long)&select_coll)
{
p->wait_cond = (long)wakeselect;
spl (sr);
goto retry_after_collision;
}
spl (sr);
/* we can cancel the time out now (if it hasn't already happened) */
if (t) canceltimeout(t);
/* OK, let's see what data arrived (if any) */
mask = 1L;
for (i = 0; i < p->p_fd->nfiles; i++)
{
if (rfd & mask)
{
f = p->p_fd->ofiles[i];
if (f)
{
if (is_terminal(f))
rsel = (int) tty_select(f, (long)p, O_RDONLY);
else
rsel = (int) (*f->dev->select)(f, (long)p, O_RDONLY);
if (rsel == 1)
{
*rfdp |= mask;
count++;
}
}
}
if (wfd & mask)
{
f = p->p_fd->ofiles[i];
if (f)
{
if (is_terminal(f))
rsel = (int) tty_select(f, (long)p, O_WRONLY);
else
rsel = (int) (*f->dev->select)(f, (long)p, O_WRONLY);
if (rsel == 1)
{
*wfdp |= mask;
count++;
}
}
}
if (xfd & mask)
{
f = p->p_fd->ofiles[i];
if (f)
{
/* tesche: since old device drivers won't understand this call,
* we set up `no exceptional condition' as default.
*/
rsel = (int) (*f->dev->select)(f, (long)p, O_RDWR);
if (rsel == 1)
{
*xfdp |= mask;
count++;
}
}
}
mask = mask << 1L;
}
}
else if (t)
{
/* in case data arrived after a collsion, there
* could be a timeout pending even if count > 0
*/
canceltimeout(t);
}
cancel:
/* at this point, we either have data or a time out */
/* cancel all the selects */
mask = 1L;
for (i = 0; i < p->p_fd->nfiles; i++)
{
if (rfd & mask)
{
f = p->p_fd->ofiles[i];
if (f)
(*f->dev->unselect)(f, (long)p, O_RDONLY);
}
if (wfd & mask)
{
f = p->p_fd->ofiles[i];
if (f)
(*f->dev->unselect)(f, (long)p, O_WRONLY);
}
if (xfd & mask)
{
f = p->p_fd->ofiles[i];
if (f)
(*f->dev->unselect)(f, (long)p, O_RDWR);
}
mask = mask << 1L;
}
#if 0
/* GEM kludgery continued...
*
* if the counter was already in use and is somewhere at 3 minutes now
* then just restore the old value
*/
if (oldgemtimer) {
long *bar = (long *)gemtimer;
short sr = spl7();
if ((gemtimer = *bar) <= 0x2600 && gemtimer > 0x2000 &&
gemtimer > oldgemtimer) {
*bar = oldgemtimer;
spl(sr);
DEBUG(("select: restoring gem timer (%lx: %lx -> %lx)", bar, gemtimer, oldgemtimer));
}
spl(sr);
}
#endif
/* wake other processes which got a collision */
if (rfd || wfd || xfd)
wake(SELECT_Q, (long)&select_coll);
TRACELOW(("Fselect: returning %d", count));
return count;
}
/*
* GEMDOS extension: Fmidipipe
* Fmidipipe(pid, in, out) manipulates the MIDI file handles (handles -4 and -5)
* of process "pid" so that they now point to the files with handles "in" and
* "out" in the calling process
*/
long _cdecl
sys_f_midipipe (short pid, short in, short out)
{
FILEPTR *fin, *fout;
struct proc *p;
/* first, find the process */
if (pid == 0)
p = get_curproc();
else
{
p = pid2proc(pid);
if (!p)
return ENOENT;
}
assert (p->p_fd && p->p_cwd);
/* next, validate the input and output file handles */
if (in < MIN_HANDLE || in >= p->p_fd->nfiles || (0==(fin = p->p_fd->ofiles[in])))
return EBADF;
if ((fin->flags & O_RWMODE) == O_WRONLY)
{
DEBUG(("Fmidipipe: input side is write only"));
return EACCES;
}
if (out < MIN_HANDLE || out >= p->p_fd->nfiles || (0==(fout = p->p_fd->ofiles[out])))
return EBADF;
if ((fout->flags & O_RWMODE) == O_RDONLY)
{
DEBUG(("Fmidipipe: output side is read only"));
return EACCES;