summaryrefslogtreecommitdiff
path: root/dmake/msdos/find.c
blob: ac0c6b8ed6706fdb54dd46685cbcc895bc32471f (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
/*
    Directory Access Library

           FIND.C taken from DIRLIB.C by M. J. Weinstein
         Released to public domain 1-Jan-89

    The author may be contacted at: 
    matt@cs.ucla.edu -or- POB 84524, L.A., CA  90073

    Modified by dvadura@watdragon.edu to work with dmake.
    (nuked the DOS version 2 code, since dmake needs version
    3.0 or greater to function).
 */


/*
 * revision history:
 *
 *	VER	MM/DD/YY	COMMENTS
 *	----	--------	--------
 *	0.99	02/24/86	Beta release to INTERNET
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <alloc.h>
#include <dos.h>
#include "dosdta.h"

#ifndef MK_FP
#define MK_FP(seg,ofs)	((void far *) \
               (((unsigned long)(seg) << 16) | (unsigned)(ofs)))
#endif
#ifndef FP_SEG
#define FP_SEG(fp)	((unsigned)((unsigned long)(fp) >> 16))
#endif
#ifndef FP_OFF
#define FP_OFF(fp)	((unsigned)(fp))
#endif

int find_err;

/*
 * get/set dta address
 */

static DTA far *
_getsetdta(newdta)
DTA far *newdta;
{
    DTA far *olddta;
    union REGS r;
    struct SREGS s;

    /* get old dta */     	
    r.h.ah = 0x2f;
    intdos(&r, &r);
    segread(&s);
    olddta = (DTA far *) MK_FP(s.es, r.x.bx);

    /* conditionally set new dta */
    if (newdta) {
        r.h.ah = 0x1a;
        s.ds	= FP_SEG(newdta);
        r.x.dx	= FP_OFF(newdta);	
        intdosx(&r, &r, &s);
    }

    return olddta;
}

/*
 * dos findfirst
 */

DTA *
findfirst(name, dta)
char *name;
DTA  *dta;
{
    union REGS r;  
    struct SREGS s;
    DTA far *dtasave;
    char far *nmp = (char far *)name;

    dtasave = _getsetdta((DTA far *)dta);
    
    /* do directory lookup */
    segread(&s);
    r.h.ah	= 0x4e;
    r.x.cx	= 0x10;
    r.x.dx	= FP_OFF(nmp);
    s.ds	= FP_SEG(nmp);
    intdosx(&r, &r, &s);
    /* restore dta */
    _getsetdta(dtasave);
    find_err = r.x.ax;
    if (r.x.cflag)
        return(NULL);

    return dta;
}

/*
 * dos findnext
 */

DTA *
findnext(dta)
DTA *dta;
{
    union REGS r;  
    DTA far *dtasave;

    dtasave = _getsetdta((DTA far *)dta);

    /* do directory lookup */
    r.h.ah = 0x4f;
    intdos(&r, &r);
    /* restore old dta */
    _getsetdta(dtasave);
    find_err = r.x.ax;
    if (r.x.cflag)
        return(NULL);

    return dta;
}