Subversion Repositories SvarDOS

Rev

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

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