Subversion Repositories SvarDOS

Rev

Rev 1557 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1557 Rev 1559
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) {
-
 
30
  unsigned short res = 0;
-
 
31
  unsigned short handle = 0;
-
 
32
  _asm {
-
 
33
    push cx
-
 
34
    push dx
-
 
35
 
-
 
36
    mov ax, 0x3d00
-
 
37
    mov dx, fname
-
 
38
    xor cl, cl
-
 
39
    int 0x21
-
 
40
    jc err
-
 
41
    mov handle, ax
-
 
42
    jmp done
-
 
43
    err:
-
 
44
    mov res, ax
-
 
45
    done:
-
 
46
 
-
 
47
    pop dx
-
 
48
    pop cx
-
 
49
  }
-
 
50
  *fhandle = handle;
-
 
51
  return(res);
-
 
52
}
-
 
53
 
-
 
54
 
-
 
55
unsigned short mdr_dos_fclose(unsigned short handle) {
29
unsigned short mdr_dos_fclose(unsigned short handle) {
56
  unsigned short res = 0;
30
  unsigned short res = 0;
57
  _asm {
31
  _asm {
58
    push bx
32
    push bx
59
 
33
 
60
    mov ah, 0x3e
34
    mov ah, 0x3e
61
    mov bx, handle
35
    mov bx, handle
62
    int 0x21
36
    int 0x21
63
    jnc done
37
    jnc done
64
    mov res, ax
38
    mov res, ax
65
    done:
39
    done:
66
 
40
 
67
    pop bx
41
    pop bx
68
  }
42
  }
69
  return(res);
43
  return(res);
70
}
44
}
71
 
45
 
72
 
46
 
73
unsigned short _dos_freemem(unsigned short segn) {
47
unsigned short _dos_freemem(unsigned short segn) {
74
  unsigned short res = 0;
48
  unsigned short res = 0;
75
  _asm {
49
  _asm {
76
    push es
50
    push es
77
    mov ah, 0x49
51
    mov ah, 0x49
78
    mov es, segn
52
    mov es, segn
79
    int 0x21
53
    int 0x21
80
    pop es
54
    pop es
81
    jnc done
55
    jnc done
82
    mov res, ax
56
    mov res, ax
83
    done:
57
    done:
84
  }
58
  }
85
  return(res);
59
  return(res);
86
}
60
}
87
 
61
 
88
 
62
 
89
unsigned short mdr_dos_allocmem(unsigned short siz) {
63
unsigned short mdr_dos_allocmem(unsigned short siz) {
90
  unsigned short segnum = 0;
64
  unsigned short segnum = 0;
91
 
65
 
92
  _asm {
66
  _asm {
93
    push bx
67
    push bx
94
 
68
 
95
    mov ah, 0x48
69
    mov ah, 0x48
96
    mov bx, siz
70
    mov bx, siz
97
    int 0x21
71
    int 0x21
98
    jc done
72
    jc done
99
    mov segnum, ax
73
    mov segnum, ax
100
 
74
 
101
    done:
75
    done:
102
 
76
 
103
    pop bx
77
    pop bx
104
  }
78
  }
105
 
79
 
106
  return(segnum);
80
  return(segnum);
107
}
81
}
108
 
82
 
109
 
83
 
110
unsigned short mdr_dos_resizeblock(unsigned short siz, unsigned short segn) {
84
unsigned short mdr_dos_resizeblock(unsigned short siz, unsigned short segn) {
111
  unsigned short res = 0;
85
  unsigned short res = 0;
112
 
86
 
113
  _asm {
87
  _asm {
114
    push bx
88
    push bx
115
    push es
89
    push es
116
 
90
 
117
    mov ah, 0x4a
91
    mov ah, 0x4a
118
    mov bx, siz
92
    mov bx, siz
119
    mov es, segn
93
    mov es, segn
120
    int 0x21
94
    int 0x21
121
    jnc done
95
    jnc done
122
    mov res, ax
96
    mov res, ax
123
 
97
 
124
    done:
98
    done:
125
 
99
 
126
    pop es
100
    pop es
127
    pop bx
101
    pop bx
128
  }
102
  }
129
 
103
 
130
  return(res);
104
  return(res);
131
}
105
}
132
 
106
 
133
 
107
 
134
unsigned short mdr_dos_read(unsigned short handle, void far *buf, unsigned short count, unsigned short *bytes) {
108
unsigned short mdr_dos_read(unsigned short handle, void far *buf, unsigned short count, unsigned short *bytes) {
135
  unsigned short res = 0;
109
  unsigned short res = 0;
136
  unsigned short resax = 0;
110
  unsigned short resax = 0;
137
  unsigned short buf_off = FP_OFF(buf);
111
  unsigned short buf_off = FP_OFF(buf);
138
  unsigned short buf_seg = FP_SEG(buf);
112
  unsigned short buf_seg = FP_SEG(buf);
139
 
113
 
140
  _asm {
114
  _asm {
141
    push bx,
115
    push bx,
142
    push cx
116
    push cx
143
    push dx
117
    push dx
144
 
118
 
145
    mov ah, 0x3f
119
    mov ah, 0x3f
146
    mov bx, handle
120
    mov bx, handle
147
    mov cx, count
121
    mov cx, count
148
    mov dx, buf_off
122
    mov dx, buf_off
149
    push ds
123
    push ds
150
    mov ds, buf_seg
124
    mov ds, buf_seg
151
    int 0x21
125
    int 0x21
152
    pop ds
126
    pop ds
153
 
127
 
154
    jnc done
128
    jnc done
155
    mov res, ax
129
    mov res, ax
156
 
130
 
157
    done:
131
    done:
158
    mov resax, ax
132
    mov resax, ax
159
 
133
 
160
    pop dx
134
    pop dx
161
    pop cx
135
    pop cx
162
    pop bx
136
    pop bx
163
  }
137
  }
164
 
138
 
165
  *bytes = resax;
139
  *bytes = resax;
166
  return(res);
140
  return(res);
167
}
141
}
168
 
142
 
169
 
143
 
170
unsigned short mdr_dos_write(unsigned short handle, const void far *buf, unsigned short count, unsigned short *bytes) {
144
unsigned short mdr_dos_write(unsigned short handle, const void far *buf, unsigned short count, unsigned short *bytes) {
171
  unsigned short res = 0;
145
  unsigned short res = 0;
172
  unsigned short resax = 0;
146
  unsigned short resax = 0;
173
  unsigned short buf_seg = FP_SEG(buf);
147
  unsigned short buf_seg = FP_SEG(buf);
174
  unsigned short buf_off = FP_OFF(buf);
148
  unsigned short buf_off = FP_OFF(buf);
175
 
149
 
176
  _asm {
150
  _asm {
177
    push bx
151
    push bx
178
    push cx
152
    push cx
179
    push dx
153
    push dx
180
 
154
 
181
    mov ah, 0x40
155
    mov ah, 0x40
182
    mov bx, handle
156
    mov bx, handle
183
    mov cx, count
157
    mov cx, count
184
    mov dx, buf_off
158
    mov dx, buf_off
185
    push ds
159
    push ds
186
    mov ds, buf_seg
160
    mov ds, buf_seg
187
 
161
 
188
    int 0x21
162
    int 0x21
189
    pop ds
163
    pop ds
190
    jnc done
164
    jnc done
191
    mov res, ax
165
    mov res, ax
192
 
166
 
193
    done:
167
    done:
194
    mov resax, ax
168
    mov resax, ax
195
 
169
 
196
    pop dx
170
    pop dx
197
    pop cx
171
    pop cx
198
    pop bx
172
    pop bx
199
  }
173
  }
200
 
174
 
201
  *bytes = resax;
175
  *bytes = resax;
202
  return(res);
176
  return(res);
203
}
177
}
204
 
178