Subversion Repositories SvarDOS

Rev

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

Rev 1568 Rev 1571
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
  const char *ptr = s;
12
  const char *ptr = s;
13
  while (*ptr != 0) ptr++;
13
  while (*ptr != 0) ptr++;
14
  return(ptr - s);
14
  return(ptr - s);
15
}
15
}
16
 
16
 
17
void bzero(void *ptr, size_t len) {
17
void bzero(void *ptr, size_t len) {
-
 
18
  char *p = ptr;
18
  while (len > 0) ((char *)ptr)[--len] = 0;
19
  while (len > 0) {
-
 
20
    *p = 0;
-
 
21
    p++;
-
 
22
    len--;
-
 
23
  }
19
}
24
}
20
 
25
 
21
/* TODO this function does not handle overlapping strings well! */
26
/* TODO this function does not handle overlapping strings well! */
22
void fmemmove(void far *dst, const void far *src, size_t len) {
27
void fmemmove(void far *dst, const void far *src, size_t len) {
23
  while (len-- > 0) {
28
  while (len-- > 0) {
24
    *(char far *)dst = *(char far *)src;
29
    *(char far *)dst = *(char far *)src;
25
    dst = (char far *)dst + 1;
30
    dst = (char far *)dst + 1;
26
    src = (char far *)src + 1;
31
    src = (char far *)src + 1;
27
  }
32
  }
28
}
33
}
29
 
34
 
30
unsigned short mdr_dos_fclose(unsigned short handle) {
35
unsigned short mdr_dos_fclose(unsigned short handle) {
31
  unsigned short res = 0;
36
  unsigned short res = 0;
32
  _asm {
37
  _asm {
33
    push bx
38
    push bx
34
 
39
 
35
    mov ah, 0x3e
40
    mov ah, 0x3e
36
    mov bx, handle
41
    mov bx, handle
37
    int 0x21
42
    int 0x21
38
    jnc done
43
    jnc done
39
    mov res, ax
44
    mov res, ax
40
    done:
45
    done:
41
 
46
 
42
    pop bx
47
    pop bx
43
  }
48
  }
44
  return(res);
49
  return(res);
45
}
50
}
46
 
51
 
47
 
52
 
48
unsigned short _dos_freemem(unsigned short segn) {
53
unsigned short _dos_freemem(unsigned short segn) {
49
  unsigned short res = 0;
54
  unsigned short res = 0;
50
  _asm {
55
  _asm {
51
    push es
56
    push es
52
    mov ah, 0x49
57
    mov ah, 0x49
53
    mov es, segn
58
    mov es, segn
54
    int 0x21
59
    int 0x21
55
    pop es
60
    pop es
56
    jnc done
61
    jnc done
57
    mov res, ax
62
    mov res, ax
58
    done:
63
    done:
59
  }
64
  }
60
  return(res);
65
  return(res);
61
}
66
}
62
 
67
 
63
 
68
 
64
unsigned short mdr_dos_allocmem(unsigned short siz) {
69
unsigned short mdr_dos_allocmem(unsigned short siz) {
65
  unsigned short segnum = 0;
70
  unsigned short segnum = 0;
66
 
71
 
67
  _asm {
72
  _asm {
68
    push bx
73
    push bx
69
 
74
 
70
    mov ah, 0x48
75
    mov ah, 0x48
71
    mov bx, siz
76
    mov bx, siz
72
    int 0x21
77
    int 0x21
73
    jc done
78
    jc done
74
    mov segnum, ax
79
    mov segnum, ax
75
 
80
 
76
    done:
81
    done:
77
 
82
 
78
    pop bx
83
    pop bx
79
  }
84
  }
80
 
85
 
81
  return(segnum);
86
  return(segnum);
82
}
87
}
83
 
88
 
84
 
89
 
85
unsigned short mdr_dos_resizeblock(unsigned short siz, unsigned short segn) {
90
unsigned short mdr_dos_resizeblock(unsigned short siz, unsigned short segn) {
86
  unsigned short res = 0;
91
  unsigned short res = 0;
87
 
92
 
88
  _asm {
93
  _asm {
89
    push bx
94
    push bx
90
    push es
95
    push es
91
 
96
 
92
    mov ah, 0x4a
97
    mov ah, 0x4a
93
    mov bx, siz
98
    mov bx, siz
94
    mov es, segn
99
    mov es, segn
95
    int 0x21
100
    int 0x21
96
    jnc done
101
    jnc done
97
    mov res, ax
102
    mov res, ax
98
 
103
 
99
    done:
104
    done:
100
 
105
 
101
    pop es
106
    pop es
102
    pop bx
107
    pop bx
103
  }
108
  }
104
 
109
 
105
  return(res);
110
  return(res);
106
}
111
}
107
 
112
 
108
 
113
 
109
unsigned short mdr_dos_read(unsigned short handle, void far *buf, unsigned short count, unsigned short *bytes) {
114
unsigned short mdr_dos_read(unsigned short handle, void far *buf, unsigned short count, unsigned short *bytes) {
110
  unsigned short res = 0;
115
  unsigned short res = 0;
111
  unsigned short resax = 0;
116
  unsigned short resax = 0;
112
  unsigned short buf_off = FP_OFF(buf);
117
  unsigned short buf_off = FP_OFF(buf);
113
  unsigned short buf_seg = FP_SEG(buf);
118
  unsigned short buf_seg = FP_SEG(buf);
114
 
119
 
115
  _asm {
120
  _asm {
116
    push bx,
121
    push bx,
117
    push cx
122
    push cx
118
    push dx
123
    push dx
119
 
124
 
120
    mov ah, 0x3f
125
    mov ah, 0x3f
121
    mov bx, handle
126
    mov bx, handle
122
    mov cx, count
127
    mov cx, count
123
    mov dx, buf_off
128
    mov dx, buf_off
124
    push ds
129
    push ds
125
    mov ds, buf_seg
130
    mov ds, buf_seg
126
    int 0x21
131
    int 0x21
127
    pop ds
132
    pop ds
128
 
133
 
129
    jnc done
134
    jnc done
130
    mov res, ax
135
    mov res, ax
131
 
136
 
132
    done:
137
    done:
133
    mov resax, ax
138
    mov resax, ax
134
 
139
 
135
    pop dx
140
    pop dx
136
    pop cx
141
    pop cx
137
    pop bx
142
    pop bx
138
  }
143
  }
139
 
144
 
140
  *bytes = resax;
145
  *bytes = resax;
141
  return(res);
146
  return(res);
142
}
147
}
143
 
148
 
144
 
149
 
145
unsigned short mdr_dos_write(unsigned short handle, const void far *buf, unsigned short count, unsigned short *bytes) {
150
unsigned short mdr_dos_write(unsigned short handle, const void far *buf, unsigned short count, unsigned short *bytes) {
146
  unsigned short res = 0;
151
  unsigned short res = 0;
147
  unsigned short resax = 0;
152
  unsigned short resax = 0;
148
  unsigned short buf_seg = FP_SEG(buf);
153
  unsigned short buf_seg = FP_SEG(buf);
149
  unsigned short buf_off = FP_OFF(buf);
154
  unsigned short buf_off = FP_OFF(buf);
150
 
155
 
151
  _asm {
156
  _asm {
152
    push bx
157
    push bx
153
    push cx
158
    push cx
154
    push dx
159
    push dx
155
 
160
 
156
    mov ah, 0x40
161
    mov ah, 0x40
157
    mov bx, handle
162
    mov bx, handle
158
    mov cx, count
163
    mov cx, count
159
    mov dx, buf_off
164
    mov dx, buf_off
160
    push ds
165
    push ds
161
    mov ds, buf_seg
166
    mov ds, buf_seg
162
 
167
 
163
    int 0x21
168
    int 0x21
164
    pop ds
169
    pop ds
165
    jnc done
170
    jnc done
166
    mov res, ax
171
    mov res, ax
167
 
172
 
168
    done:
173
    done:
169
    mov resax, ax
174
    mov resax, ax
170
 
175
 
171
    pop dx
176
    pop dx
172
    pop cx
177
    pop cx
173
    pop bx
178
    pop bx
174
  }
179
  }
175
 
180
 
176
  *bytes = resax;
181
  *bytes = resax;
177
  return(res);
182
  return(res);
178
}
183
}
179
 
184