Subversion Repositories SvarDOS

Rev

Rev 1556 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1556 Rev 1557
1
/*
1
/*
2
 * replacement for a couple of libc functions
2
 * replacement for a couple of libc functions
3
 */
3
 */
4
 
4
 
5
#include <i86.h>
5
#include <i86.h>
6
#include <stddef.h>
6
#include <stddef.h>
7
 
7
 
8
#include "libc.h"
8
#include "libc.h"
9
 
9
 
10
 
10
 
11
size_t strlen(const char *s) {
11
size_t strlen(const char *s) {
12
  size_t res = 0;
12
  size_t res = 0;
13
  while (s[res] != 0) res++;
13
  while (s[res] != 0) res++;
14
  return(res);
14
  return(res);
15
}
15
}
16
 
16
 
17
void bzero(void *ptr, size_t len) {
17
void bzero(void *ptr, size_t len) {
18
  while (len > 0) ((char *)ptr)[--len] = 0;
18
  while (len > 0) ((char *)ptr)[--len] = 0;
19
}
19
}
20
 
20
 
21
/* TODO this function does not handle overlapping strings well! */
21
/* TODO this function does not handle overlapping strings well! */
22
void far *_fmemmove(void far *dst, const void far *src, size_t len) {
22
void far *_fmemmove(void far *dst, const void far *src, size_t len) {
23
  while (len-- > 0) {
23
  while (len-- > 0) {
24
    ((char far *)dst)[len] = ((char far *)src)[len];
24
    ((char far *)dst)[len] = ((char far *)src)[len];
25
  }
25
  }
26
  return(dst);
26
  return(dst);
27
}
27
}
28
 
28
 
29
unsigned short mdr_dos_fopen(const char *fname, unsigned short *fhandle) {
29
unsigned short mdr_dos_fopen(const char *fname, unsigned short *fhandle) {
30
  unsigned short res = 0;
30
  unsigned short res = 0;
31
  unsigned short handle = 0;
31
  unsigned short handle = 0;
32
  unsigned short fname_seg = FP_SEG(fname);
-
 
33
  unsigned short fname_off = FP_OFF(fname);
-
 
34
  _asm {
32
  _asm {
35
    push cx
33
    push cx
36
    push dx
34
    push dx
37
 
35
 
38
    mov ax, 0x3d00
36
    mov ax, 0x3d00
39
    mov dx, fname_off
37
    mov dx, fname
40
    xor cl, cl
38
    xor cl, cl
41
    push ds
-
 
42
    mov ds, fname_seg
-
 
43
    int 0x21
39
    int 0x21
44
    pop ds
-
 
45
    jc err
40
    jc err
46
    mov handle, ax
41
    mov handle, ax
47
    jmp done
42
    jmp done
48
    err:
43
    err:
49
    mov res, ax
44
    mov res, ax
50
    done:
45
    done:
51
 
46
 
52
    pop dx
47
    pop dx
53
    pop cx
48
    pop cx
54
  }
49
  }
55
  *fhandle = handle;
50
  *fhandle = handle;
56
  return(res);
51
  return(res);
57
}
52
}
58
 
53
 
59
 
54
 
60
unsigned short mdr_dos_fclose(unsigned short handle) {
55
unsigned short mdr_dos_fclose(unsigned short handle) {
61
  unsigned short res = 0;
56
  unsigned short res = 0;
62
  _asm {
57
  _asm {
63
    push bx
58
    push bx
64
 
59
 
65
    mov ah, 0x3e
60
    mov ah, 0x3e
66
    mov bx, handle
61
    mov bx, handle
67
    int 0x21
62
    int 0x21
68
    jnc done
63
    jnc done
69
    mov res, ax
64
    mov res, ax
70
    done:
65
    done:
71
 
66
 
72
    pop bx
67
    pop bx
73
  }
68
  }
74
  return(res);
69
  return(res);
75
}
70
}
76
 
71
 
77
 
72
 
78
unsigned short _dos_freemem(unsigned short segn) {
73
unsigned short _dos_freemem(unsigned short segn) {
79
  unsigned short res = 0;
74
  unsigned short res = 0;
80
  _asm {
75
  _asm {
81
    push es
76
    push es
82
    mov ah, 0x49
77
    mov ah, 0x49
83
    mov es, segn
78
    mov es, segn
84
    int 0x21
79
    int 0x21
85
    pop es
80
    pop es
86
    jnc done
81
    jnc done
87
    mov res, ax
82
    mov res, ax
88
    done:
83
    done:
89
  }
84
  }
90
  return(res);
85
  return(res);
91
}
86
}
92
 
87
 
93
 
88
 
94
unsigned short mdr_dos_allocmem(unsigned short siz) {
89
unsigned short mdr_dos_allocmem(unsigned short siz) {
95
  unsigned short segnum = 0;
90
  unsigned short segnum = 0;
96
 
91
 
97
  _asm {
92
  _asm {
98
    push bx
93
    push bx
99
 
94
 
100
    mov ah, 0x48
95
    mov ah, 0x48
101
    mov bx, siz
96
    mov bx, siz
102
    int 0x21
97
    int 0x21
103
    jc done
98
    jc done
104
    mov segnum, ax
99
    mov segnum, ax
105
 
100
 
106
    done:
101
    done:
107
 
102
 
108
    pop bx
103
    pop bx
109
  }
104
  }
110
 
105
 
111
  return(segnum);
106
  return(segnum);
112
}
107
}
113
 
108
 
114
 
109
 
115
unsigned short mdr_dos_resizeblock(unsigned short siz, unsigned short segn, unsigned short *maxsiz) {
110
unsigned short mdr_dos_resizeblock(unsigned short siz, unsigned short segn) {
116
  unsigned short resbx = 0;
-
 
117
  unsigned short res = 0;
111
  unsigned short res = 0;
118
 
112
 
119
  _asm {
113
  _asm {
120
    push bx
114
    push bx
121
    push es
115
    push es
122
 
116
 
123
    mov ah, 0x4a
117
    mov ah, 0x4a
124
    mov bx, siz
118
    mov bx, siz
125
    mov es, segn
119
    mov es, segn
126
    int 0x21
120
    int 0x21
127
    jnc done
121
    jnc done
128
    mov resbx, bx
-
 
129
    mov res, ax
122
    mov res, ax
130
 
123
 
131
    done:
124
    done:
132
 
125
 
133
    pop es
126
    pop es
134
    pop bx
127
    pop bx
135
  }
128
  }
136
 
129
 
137
  *maxsiz = resbx;
-
 
138
  return(res);
130
  return(res);
139
}
131
}
140
 
132
 
141
 
133
 
142
unsigned short mdr_dos_read(unsigned short handle, void far *buf, unsigned short count, unsigned short *bytes) {
134
unsigned short mdr_dos_read(unsigned short handle, void far *buf, unsigned short count, unsigned short *bytes) {
143
  unsigned short res = 0;
135
  unsigned short res = 0;
144
  unsigned short resax = 0;
136
  unsigned short resax = 0;
145
  unsigned short buf_off = FP_OFF(buf);
137
  unsigned short buf_off = FP_OFF(buf);
146
  unsigned short buf_seg = FP_SEG(buf);
138
  unsigned short buf_seg = FP_SEG(buf);
147
 
139
 
148
  _asm {
140
  _asm {
149
    push bx,
141
    push bx,
150
    push cx
142
    push cx
151
    push dx
143
    push dx
152
 
144
 
153
    mov ah, 0x3f
145
    mov ah, 0x3f
154
    mov bx, handle
146
    mov bx, handle
155
    mov cx, count
147
    mov cx, count
156
    mov dx, buf_off
148
    mov dx, buf_off
157
    push ds
149
    push ds
158
    mov ds, buf_seg
150
    mov ds, buf_seg
159
    int 0x21
151
    int 0x21
160
    pop ds
152
    pop ds
161
 
153
 
162
    jnc done
154
    jnc done
163
    mov res, ax
155
    mov res, ax
164
 
156
 
165
    done:
157
    done:
166
    mov resax, ax
158
    mov resax, ax
167
 
159
 
168
    pop dx
160
    pop dx
169
    pop cx
161
    pop cx
170
    pop bx
162
    pop bx
171
  }
163
  }
172
 
164
 
173
  *bytes = resax;
165
  *bytes = resax;
174
  return(res);
166
  return(res);
175
}
167
}
176
 
168
 
177
 
169
 
178
unsigned short mdr_dos_write(unsigned short handle, const void far *buf, unsigned short count, unsigned short *bytes) {
170
unsigned short mdr_dos_write(unsigned short handle, const void far *buf, unsigned short count, unsigned short *bytes) {
179
  unsigned short res = 0;
171
  unsigned short res = 0;
180
  unsigned short resax = 0;
172
  unsigned short resax = 0;
181
  unsigned short buf_seg = FP_SEG(buf);
173
  unsigned short buf_seg = FP_SEG(buf);
182
  unsigned short buf_off = FP_OFF(buf);
174
  unsigned short buf_off = FP_OFF(buf);
183
 
175
 
184
  _asm {
176
  _asm {
185
    push bx
177
    push bx
186
    push cx
178
    push cx
187
    push dx
179
    push dx
188
 
180
 
189
    mov ah, 0x40
181
    mov ah, 0x40
190
    mov bx, handle
182
    mov bx, handle
191
    mov cx, count
183
    mov cx, count
192
    mov dx, buf_off
184
    mov dx, buf_off
193
    push ds
185
    push ds
194
    mov ds, buf_seg
186
    mov ds, buf_seg
195
 
187
 
196
    int 0x21
188
    int 0x21
197
    pop ds
189
    pop ds
198
    jnc done
190
    jnc done
199
    mov res, ax
191
    mov res, ax
200
 
192
 
201
    done:
193
    done:
202
    mov resax, ax
194
    mov resax, ax
203
 
195
 
204
    pop dx
196
    pop dx
205
    pop cx
197
    pop cx
206
    pop bx
198
    pop bx
207
  }
199
  }
208
 
200
 
209
  *bytes = resax;
201
  *bytes = resax;
210
  return(res);
202
  return(res);
211
}
203
}
212
 
204