summaryrefslogtreecommitdiffstats
path: root/usr.bin/doscmd/intff.c
blob: a32c64698c8063c1f266668e5c4aa7e01429b32a (plain)
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
/*
 * Copyright (c) 1992, 1993, 1996
 *	Berkeley Software Design, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Berkeley Software
 *	Design, Inc.
 *
 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	BSDI intff.c,v 2.2 1996/04/08 19:32:56 bostic Exp
 *
 * $Id: intff.c,v 1.4 1998/07/02 05:12:53 imp Exp $
 */

#include "doscmd.h"
#include <sys/param.h>
#include <ctype.h>

#include "dispatch.h"

static LOL	*lol = 0;		/* DOS list-of-lists */
static SDA	*sda = 0;		/* DOS swappable data area */

/******************************************************************************
** redirector functions
**
**
** These are set up on entry to the redirector each time, and are referenced
** by the functions here.
*/
static int	r_drive,n_drive = 0;
static CDS	*r_cds;
static SFT	*r_sft;


/*
** 2f:11:0
**
** Installation check
*/
static int
int2f11_00(regcontext_t *REGS)
{
    R_AL = 0xff;
    R_AH = 'U';		/* and why not? 8) */
    return(0);
}

/*
** 2f:11:1 2f:11:2 2f:11:3 2f:11:4 2f:11:5 2f:11:11 2f:11:13
**
** Directory functions
*/
static int
int2f11_dirfn(regcontext_t *REGS)
{
    char	fname[PATH_MAX], tname[PATH_MAX];
    int		error;

    error = translate_filename(sda->filename1, fname, &r_drive);
    if (error)
	return(error);
    
    if (dos_readonly(r_drive) && (R_AL != 0x05))
	return(WRITE_PROT_DISK);

    switch(R_AL) {
    case 0x01:				/* rmdir */
    case 0x02:
	debug(D_REDIR,"rmdir(%s)\n",fname);
	error = rmdir(fname);
	break;
    case 0x03:				/* mkdir */
    case 0x04:
	debug(D_REDIR,"mkdir(%s)\n",fname);
	error = mkdir(fname,0777);
	break;
    case 0x05:				/* chdir */
	debug(D_REDIR,"chdir(%s)\n",fname);
	/* Note returns DOS error directly */
	return(dos_setcwd(sda->filename1));
	
    case 0x11:				/* rename */
	error = translate_filename(sda->filename2, tname, &r_drive);
	if (!error) {
	    debug(D_REDIR,"rename(%s,%s)\n",fname,tname);
	    error = rename(fname, tname);
	}
	break;

    case 0x13:				/* unlink */
	debug(D_REDIR,"unlink(%s)\n",fname);
	error = unlink(fname);
	break;
	
    default:
	fatal("called int2f11_dirfn on unknown function %x\n",R_AL);
    }
    
    if (error < 0) {
	switch(errno) {
	case ENOTDIR:
	case ENOENT:
	    return(PATH_NOT_FOUND);
	case EXDEV:
	    return(NOT_SAME_DEV);
	default:
	    return(ACCESS_DENIED);
	}
    }
    return(0);
}

/*
** 2f:11:6
**
** Close
*/
static int
int2f11_close(regcontext_t *REGS)
{
    int		fd;

    fd = r_sft->fd;
    debug(D_REDIR, "close(%d)\n", fd);
    
    r_sft->nfiles--;
    if (r_sft->nfiles) {
	debug(D_REDIR, "not last close\n");
	return(0);
    }
    if (close(fd) < 0)
	return(HANDLE_INVALID);
    return(0);
}

/*
** 2f:11:8 2f:11:9
**
** read/write
*/
static int
int2f11_rdwr(regcontext_t *REGS)
{
    int		fd;
    char	*addr;
    int		nbytes;
    int		n;

    fd = r_sft->fd;
    if (lseek(fd, r_sft->offset, SEEK_SET) < 0)
	return(SEEK_ERROR);

    addr = (char *)MAKEPTR(sda->dta_seg, sda->dta_off);
    nbytes = R_CX;
    
    switch(R_AL) {
    case 0x08:				/* read */
	debug(D_REDIR, "read(%d, %d)\n", fd, nbytes);
	n = read(fd, addr, nbytes);
	if (n < 0)
	    return(READ_FAULT);
	break;
    case 0x09:
	debug(D_REDIR, "write(%d, %d)\n", fd, nbytes);
	n = write(fd, addr, nbytes);
	if (n < 0)
	    return(WRITE_FAULT);
	break;
    default:
	fatal("called int2f11_rdwr on unknown function %x\n",R_AL);
    }
    
    R_CX = n;				/* report count */
    r_sft->offset += n;
    if (r_sft->offset > r_sft->size)
	r_sft->size = r_sft->offset;
    debug(D_REDIR, "offset now %d\n", r_sft->offset);
    return(0);
}

/*
** 2f:11:c
**
** Get free space (like 21:36)
*/
static int
int2f11_free(regcontext_t *REGS)
{
    fsstat_t	fs;
    int		error;
    
    error = get_space(r_drive, &fs);
    if (error)
	return (error);
    R_AX = fs.sectors_cluster;
    R_BX = fs.total_clusters;
    R_CX = fs.bytes_sector;
    R_DX = fs.avail_clusters;
    return(0);
}

/*
** 2f:11:f
**
** get size and mode
*/
static int
int2f11_stat(regcontext_t *REGS)
{
    char	fname[PATH_MAX];
    struct stat	sb;
    int		error;
    
    error = translate_filename(sda->filename1, fname, &r_drive);
    if (error)
	return(error);
    
    if (stat(fname, &sb) < 0)
	return(FILE_NOT_FOUND);
    
    R_AX = to_dos_attr(sb.st_mode);
    R_BX = sb.st_size >> 16;
    R_DI = sb.st_size & 0xffff;
    return(0);
}

/*
** 2f:11:16 2f:11:17 2f:11:18 2f:11:2e
**
** Open/create a file, closely resembles int21_open.
*/
static int
int2f11_open(regcontext_t *REGS)
{
    char	fname[PATH_MAX];
    struct stat sb;
    int		error;
    int		mode;			/* open mode */
    int		attr;			/* attributes of created file */
    int		action;			/* what to do about file */
    u_char	*p, *e;
    int		i;
    int		omode;			/* mode to say we opened in */
    int		status;
    int		fd;
    
    error = translate_filename(sda->filename1, fname, &r_drive);
    if (error)
	return(error);

    /* 
    ** get attributes/access mode off stack : low byte is attribute, high
    ** byte is (sometimes) used in conjunction with 'action'
    */
    attr = *(u_short *)N_GETPTR(R_SS, R_SP) & 0xff;

    /* which style? */
    switch(R_AL) {
    case 0x16:				/* open */
	action = 0x01;			/* fail if does not exist */
	switch (sda->open_mode & 3) {
	case 0:
	    mode = O_RDONLY;
	    break;
	case 1:
	    mode = O_WRONLY;
	    break;
	case 2:
	    mode = O_RDWR;
	    break;
	default:
	    return (FUNC_NUM_IVALID);
	}
	omode = sda->open_mode & 0x7f;
	debug(D_REDIR,"open");
	break;

    case 0x17:				/* creat/creat new */
    case 0x18:				/* creat/creat new (no CDS, but we don't care)*/
	mode = O_RDWR;
	omode = 3;
	if (attr & 0x100) {		/* creat new */
	    action = 0x10;		/* create if not exist, fail if exists */
	    debug(D_REDIR, "creat_new");
	} else {			/* creat */
	    action = 0x12;		/* create and destroy */
	    debug(D_REDIR, "creat");
	}
	break;

    case 0x2e:				/* multipurpose */
	attr = sda->ext_attr;
	action = sda->ext_action;
	switch (sda->ext_mode & 3) {
	case 0:
	    mode = O_RDONLY;
	    break;
	case 1:
	    mode = O_WRONLY;
	    break;
	case 2:
	    mode = O_RDWR;
	    break;
	default:
	    return (FUNC_NUM_IVALID);
	}
	omode = sda->ext_mode & 0x7f;
	debug(D_REDIR,"mopen");
	break;

    default:
	fatal("called int2f11_open for unknown function %x\n",R_AL);
    }
    if (action & 0x02)	/* replace/open mode */
	mode |= O_TRUNC;
    debug(D_REDIR, "(%s) action 0x%x  mode 0x%x  attr 0x%x omode 0x%x \n", 
	  fname, action, mode, attr, omode);
	
    if (ustat(fname, &sb) < 0) {			/* file does not exist */
	if ((action & 0x10) || (attr & 0x100)) {	/* create it */
	    sb.st_ino = 0;
	    mode |= O_CREAT;				/* have to create as we go */
	    status = 0x02;				/* file created */
	} else {
	    return(FILE_NOT_FOUND);			/* fail */
	}
    } else {
	if (S_ISDIR(sb.st_mode))
	    return(ACCESS_DENIED);
	if ((action & 0x03) && !(attr & 0x100)) {	/* exists, work with it */
	    if (action & 0x02) {
		if (!S_ISREG(sb.st_mode)) {		/* only allowed for files */
		    debug(D_FILE_OPS,"attempt to truncate non-regular file\n");
		    return(ACCESS_DENIED);
		}
		status = 0x03;				/* we're going to truncate it */
	    } else {
		status = 0x01;				/* just open it */
	    }
	} else {
	    return(FILE_ALREADY_EXISTS);		/* exists, fail */
	}
    }

    if ((fd = open(fname, mode, from_dos_attr(attr))) < 0) {
	debug(D_FILE_OPS,"failed to open %s : %s\n",fname,strerror(errno));
	return (ACCESS_DENIED);
    }

    if (R_AL == 0x2e)			/* extended wants status returned */
	R_CX = status;

    /* black magic to populate the SFT */

    e = p = sda->filename1 + 2;		/* parse name */
    while (*p) {
	if (*p++ == '\\')		/* look for beginning of filename */
	    e = p;
    }

    for (i = 0; i < 8; ++i) {		/* copy name and pad with spaces */
	if (*e && *e != '.')
	    r_sft->name[i] = *e++;
	else
	    r_sft->name[i] = ' ';
    }

    if (*e == '.')			/* skip period on short names */
	++e;
    
    for (i = 0; i < 3; ++i) {		/* copy extension and pad with spaces */
	if (*e)
	    r_sft->ext[i] = *e++;
	else
	    r_sft->ext[i] = ' ';
    }

    if (ustat(fname, &sb) < 0)		/* re-stat to be accurate */
	return(WRITE_FAULT);		/* any better ideas?! */

    r_sft->open_mode = omode;		/* file open mode */
    *(u_long *)r_sft->ddr_dpb = 0;	/* no parameter block */
    r_sft->size = sb.st_size;		/* current size */
    r_sft->fd = fd;			/* our fd for it (hidden in starting cluster number) */
    r_sft->offset = 0;			/* current offset is 0 */
    *(u_short *)r_sft->dir_sector = 0;	/* not local file, ignored */
    r_sft->dir_entry = 0;		/* not local file, ignored */
    r_sft->attribute = attr & 0xff;	/* file attributes as requested */
    r_sft->info = r_drive + 0x8040;	/* hide drive number here for later reference */
    encode_dos_file_time(sb.st_mtime, &r_sft->date, &r_sft->time);
    debug(D_REDIR,"success, fd %d  status %x\n", fd, status);
    return(0);
}

/*
** 2f:11:19 2f:11:1b
**
** find first
*/
static int
int2f11_findfirst(regcontext_t *REGS)
{
    return(find_first(sda->filename1,sda->attrmask,
		      (dosdir_t *)sda->foundentry,
		      (find_block_t *)sda->findfirst));
}

/*
** 2f:11:1c
**
** find next
*/
static int
int2f11_findnext(regcontext_t *REGS)
{
    return(find_next((dosdir_t *)sda->foundentry,
		     (find_block_t *)sda->findfirst));
}

/*
** 2f:11:21
**
** lseek
*/
static int
int2f11_lseek(regcontext_t *REGS)
{
    int		fd;
    off_t	offset;

    fd = r_sft->fd;
    offset = (off_t) ((int) ((R_CX << 16) + R_DX));
    
    debug(D_REDIR,"lseek(%d, 0x%qx, SEEK_END)\n", fd, offset);
    
    if ((offset = lseek(fd, offset, SEEK_END)) < 0) {
	if (errno == EBADF)
	    return(HANDLE_INVALID);
	else
	    return(SEEK_ERROR);
    }
    r_sft->offset = offset;		/* update offset in SFT */
    R_DX = offset >> 16;
    R_AX = offset;
    return(0);
}

/*
** 2f:11:23
**
** qualify filename
*/
static int
int2f11_fnqual(regcontext_t *REGS)
{
    char	*fname,*tname;
    int		savedrive;
    int		error;

    return(PATH_NOT_FOUND);
    
    savedrive = diskdrive;		/* to get CWD for network drive */
    diskdrive = n_drive;
    fname = (char *)N_GETPTR(R_DS, R_SI);	/* path pointers */
    tname = (char *)N_GETPTR(R_ES, R_DI);
    
    error = dos_makepath(fname, tname);
    if (error)
	tname = "(failed)";

    diskdrive = savedrive;		/* restore correct drive */
    
    debug(D_REDIR, "qualify '%s' -> '%s'\n", fname, tname);
    return(error);
}

/*
** 2f:11:??
**
** Null function - we know about it but do nothing
*/
static int
int2f11_NULLFUNC(regcontext_t *REGS)
{
    return(0);
}

/*
** 2f:11:??
**
** no function - not handled here (error)
*/
static int
int2f11_NOFUNC(regcontext_t *REGS)
{
    return(-1);
}

struct intfunc_table int2f11_table[] = {
    { 0x00,	IFT_NOSUBFUNC,	int2f11_00,		"installation check"},
    { 0x01,	IFT_NOSUBFUNC,	int2f11_dirfn,		"rmdir"},
    { 0x02,	IFT_NOSUBFUNC,	int2f11_dirfn,		"rmdir"},
    { 0x03,	IFT_NOSUBFUNC,	int2f11_dirfn,		"mkdir"},
    { 0x04,	IFT_NOSUBFUNC,	int2f11_dirfn,		"mkdir"},
    { 0x05,	IFT_NOSUBFUNC,	int2f11_dirfn,		"chdir"},
    { 0x06,	IFT_NOSUBFUNC,	int2f11_close,		"close"},
    { 0x07,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"commit file"},
    { 0x08,	IFT_NOSUBFUNC,	int2f11_rdwr,		"read"},
    { 0x09,	IFT_NOSUBFUNC,	int2f11_rdwr,		"write"},
    { 0x0a,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"lock region"},
    { 0x0b,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"unlock region"},
    { 0x0c,	IFT_NOSUBFUNC,	int2f11_free,		"free space"},
    { 0x0e,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"chmod"},
    { 0x0f,	IFT_NOSUBFUNC,	int2f11_stat,		"stat"},
    { 0x11,	IFT_NOSUBFUNC,	int2f11_dirfn,		"rename"},
    { 0x13,	IFT_NOSUBFUNC,	int2f11_dirfn,		"unlink"},
    { 0x16,	IFT_NOSUBFUNC,	int2f11_open,		"open"},
    { 0x17,	IFT_NOSUBFUNC,	int2f11_open,		"creat"},
    { 0x18,	IFT_NOSUBFUNC,	int2f11_open,		"creat"},
    { 0x19,	IFT_NOSUBFUNC,	int2f11_findfirst,	"find first"},
    { 0x1b,	IFT_NOSUBFUNC,	int2f11_findfirst,	"find first"},
    { 0x1c,	IFT_NOSUBFUNC,	int2f11_findnext,	"find next"},
    { 0x1d,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"close all (abort)"},
    { 0x1e,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"do redirection"},
    { 0x1f,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"printer setup"},
    { 0x20,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"flush all buffers"},
    { 0x21,	IFT_NOSUBFUNC,	int2f11_lseek,		"lseek"},
    { 0x22,	IFT_NOSUBFUNC,	int2f11_NULLFUNC,	"process terminated"},
    { 0x23,	IFT_NOSUBFUNC,	int2f11_fnqual,		"qualify filename"},
    { 0x24,	IFT_NOSUBFUNC,	int2f11_NOFUNC,		"turn off printer"},
    { 0x25,	IFT_NOSUBFUNC,	int2f11_NOFUNC,		"printer mode"},
    { 0x2d,	IFT_NOSUBFUNC,	int2f11_NOFUNC,		"extended attributes"},
    { 0x2e,	IFT_NOSUBFUNC,	int2f11_open,		"extended open/create"},
    { -1,	0,		NULL,			NULL}
};

static int int2f11_fastlookup[256];

/******************************************************************************
** 2f:11
**
** The DOS redirector interface.
*/

/*
** Verify that the drive being referenced is one we are handling, and
** establish some state for upcoming functions.
** 
** Returns 1 if we should handle this request.
**
** XXX this is rather inefficient, but much easier to read than the previous
** incarnation 8(
*/
static int
int2f11_validate(regcontext_t *REGS)
{
    int		func = R_AL;
    char	*path = NULL;
    int		doit = 0;

    /* defaults may help trap problems */
    r_cds = NULL;
    r_sft = NULL;
    r_drive = -1;
    
    /* some functions we accept regardless */
    switch (func) {
    case 0x00:		/* installation check */
    case 0x23:		/* qualify path */
    case 0x1c:		/* XXX really only valid if a search already started... */
	return(1);
    }
    
    /* Where's the CDS? */
    switch(func) {
    case 0x01:		/* referenced by the SDA */
    case 0x02:
    case 0x03:
    case 0x04:
    case 0x05:
    case 0x0e:
    case 0x0f:
    case 0x11:
    case 0x13:
    case 0x17:
    case 0x1b:
	r_cds = (CDS *)MAKEPTR(sda->cds_seg, sda->cds_off);
	break;

    case 0x0c:		/* in es:di */
    case 0x1c:
	r_cds = (CDS *)N_GETPTR(R_ES, R_DI);
	break;
    }

    /* Where's the SFT? */
    switch(func) {
    case 0x06:		/* in es:di */
    case 0x07:
    case 0x08:
    case 0x09:
    case 0x0a:
    case 0x0b:
    case 0x16:
    case 0x17:
    case 0x18:
    case 0x21:
    case 0x2d:
    case 0x2e:
	r_sft = (SFT *)N_GETPTR(R_ES, R_DI);
	break;
    }
    
    /* What drive? */
    switch(func) {
    case 0x01:		/* get drive from fully-qualified path in SDA */
    case 0x02:
    case 0x03:
    case 0x04:
    case 0x05:
    case 0x0c:
    case 0x0e:
    case 0x0f:
    case 0x11:
    case 0x13:
    case 0x16:
    case 0x17:
    case 0x18:
    case 0x19:
    case 0x1b:
    case 0x2e:
	path = sda->filename1;
	break;
	
    case 0x06:		/* get drive from SFT (we put it here when file was opened) */
    case 0x07:
    case 0x08:
    case 0x09:
    case 0x0a:
    case 0x0b:
    case 0x21:
    case 0x2d:
	r_drive = r_sft->info & 0x1f;
	break;
    }

    if (path) {		/* we have a path and need to determine the drive it refers to */

	if (path[1] != ':') {	/* must be fully qualified; we cannot handle this */
	    debug(D_REDIR,"attempt to work non-absolute path %s\n",path);
	    return(0);
	}

	/* translate letter to drive number */
	r_drive = drlton(path[0]);
    } else {
	path = "(no path)";
    }
        
    /* do we handle this drive? */
    if (dos_getcwd(r_drive)) {
	n_drive = r_drive;		/* XXX GROSTIC HACK ALERT */
	doit = 1;
    }
    
    debug(D_REDIR,"%s -> drive %c func %x (%sus)\n",
	  path, drntol(r_drive), func, doit?"":"not ");
    
    /* so do we deal with this one? */
    return(doit);
}

    
int 
int2f_11(regcontext_t *REGS)
{
    int		index;
    int		error;
    char	*fname;
	
    
    if (!sda) {		/* not initialised yet */
	error = FUNC_NUM_IVALID;
    } else {

	index = intfunc_find(int2f11_table, int2f11_fastlookup, R_AL, 0);
	if (index == -1) {
	    debug(D_ALWAYS,"no handler for int2f:11:%x\n", R_AL);
	    return(0);
	}
	reset_poll();
	

	if (!int2f11_validate(REGS)) {		/* determine whether we handle this request */
	    error = -1;				/* not handled by us */
	} else {
	    debug(D_REDIR, "REDIR: %02x (%s)\n", 
		      int2f11_table[index].func, int2f11_table[index].desc);
	    /* call the handler */
	    error = int2f11_table[index].handler(REGS);
	    if (error != -1)
		debug(D_REDIR, "REDIR: returns %d (%s)\n", 
		      error, ((error >= 0) && (error <= dos_ret_size)) ? dos_return[error] : "unknown");
	}
    }

    if (error == -1)
	return (0);
    if (error) {
	R_AX = error;
	R_FLAGS |= PSL_C;
    } else
	R_FLAGS &= ~PSL_C;
    return (1);
}

/******************************************************************************
** intff handler.
**
** intff is the (secret, proprietary, internal, evil) call to 
** initialise the redirector.
*/
static void
install_drive(int drive, u_char *path)
{
    CDS *cds;

    /* check that DOS considers this a valid drive */
    if (drive < 0 || drive >= lol->lastdrive) {
	debug(D_REDIR, "Drive %c beyond limit of %c)\n",
	      drntol(drive), drntol(lol->lastdrive - 1));
	return;
    }

    /* get the CDS for this drive */
    cds = (CDS *)MAKEPTR(lol->cds_seg, lol->cds_offset);
    cds += drive;

#if 0	/* XXX looks OK to me - mjs */
    if (cds->flag & (CDS_remote | CDS_ready)) {
	debug(D_REDIR, "Drive %c already installed\n", drntol(drive));
	return;
    }
#endif

    debug(D_REDIR, "Installing %c: as %s\n", drntol(drive), path);

    cds->flag |= CDS_remote | CDS_ready | CDS_notnet;
    cds->path[0] = drntol(drive);
    cds->path[1] = ':';
    cds->path[2] = '\\';
    cds->path[3] = '\0';
    cds->offset = 2;	/* offset of \ in current path field */
}

static void
init_drives(void)
{
    int drive;
    u_char *path;

    /* for all possible drives */
    for (drive = 0; drive < 26; ++drive) {
	if (path = dos_getpath(drive))		/* assigned to a path? */
	    install_drive(drive, path);		/* make it visible to DOS */
    }	
}


void
intff(regcontext_t *REGS)
{

    if (lol && sda) {				/* already been called? */
	debug(D_REDIR, "redirector duplicate install ignored\n");
	return;
    }
    lol = (LOL *)N_GETPTR(R_BX, R_DX);	/* where DOS keeps its goodies */
    sda = (SDA *)N_GETPTR(R_DI, R_SI);
    init_drives();
    
    /* initialise dispatcher */
    intfunc_init(int2f11_table, int2f11_fastlookup);
}
OpenPOWER on IntegriCloud