Subversion Repositories SvarDOS

Rev

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

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