Subversion Repositories SvarDOS

Rev

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

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