Subversion Repositories SvarDOS

Rev

Details | Last modification | View Log | RSS feed

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