-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathia16dwc.c
More file actions
349 lines (277 loc) · 6.77 KB
/
Copy pathia16dwc.c
File metadata and controls
349 lines (277 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* IBM PC-DOS CRC.EXE-compatible CRC calculator - ia16dwc.c
* Copyright (c) 2026 Jeffrey H. Johnson <johnsonjh.dev@gmail.com>
* SPDX-License-Identifier: MIT-0
* scspell-id: 5a1630f6-7edd-11f1-8995-80ee73e9b8e7
*/
/******************************************************************************/
/* Expands wildcards and modifies argc and argv for IA16-GCC for MS-DOS. */
/******************************************************************************/
/*
* If you are NOT using an ANSI C89 compiler (e.g., K&R, "C86"),
* COMMENT OUT "#define ANSI_COMPILER" below (or define NOANSI).
*/
#define ANSI_COMPILER
/******************************************************************************/
#ifdef NOANSI
# ifdef ANSI_COMPILER
# undef ANSI_COMPILER
# endif
#endif
/******************************************************************************/
#include <stddef.h>
#include <string.h>
/******************************************************************************/
#define MAX_ARGS 128
#define MAX_PATH 128
/******************************************************************************/
#define ATTR_MASK 0x21
/******************************************************************************/
struct dta
{
unsigned char reserved [21];
unsigned char attr;
unsigned short time;
unsigned short date;
unsigned long size;
char name [13];
} __attribute__((packed));
/******************************************************************************/
static struct dta dtabuf;
/******************************************************************************/
static char argbuf [MAX_ARGS] [MAX_PATH];
static char * newargv [MAX_ARGS];
static int newargc;
/******************************************************************************/
static void
#ifdef ANSI_COMPILER
set_dta (
struct const dta * buf)
#else
set_dta (buf)
struct const dta * buf;
#endif
{
unsigned short off = (unsigned short)buf;
__asm__ __volatile__ (
"mov $0x1A, %%ah\n"
"mov %0, %%dx\n"
"int $0x21\n"
:
: "r"(off)
: "ax", "dx"
);
}
/******************************************************************************/
static int
#ifdef ANSI_COMPILER
find_first (
const char * pattern,
unsigned short attr)
#else
find_first (pattern, attr)
const char * pattern;
unsigned short attr;
#endif
{
unsigned short ax;
unsigned short off = (unsigned short)pattern;
__asm__ __volatile__ (
"mov $0x4E, %%ah\n"
"mov %2, %%cx\n"
"mov %1, %%dx\n"
"int $0x21\n"
"mov %%ax, %0\n"
: "=r"(ax)
: "r"(off), "r"(attr)
: "ax", "cx", "dx"
);
return (int)ax;
}
/******************************************************************************/
static int
#ifdef ANSI_COMPILER
find_next (
void)
#else
find_next ()
#endif
{
unsigned short ax;
__asm__ __volatile__ (
"mov $0x4F, %%ah\n"
"int $0x21\n"
"mov %%ax, %0\n"
: "=r"(ax)
:
: "ax"
);
return (int)ax;
}
/******************************************************************************/
static int
#ifdef ANSI_COMPILER
add_arg (
const char * s)
#else
add_arg (s)
const char * s;
#endif
{
unsigned long len;
if (MAX_ARGS <= newargc)
return -1;
len = (unsigned long)strlen (s);
if (MAX_PATH <= len)
len = MAX_PATH - 1;
(void)memcpy (argbuf [newargc], s, len);
argbuf [newargc] [len] = '\0';
newargv [newargc] = argbuf [newargc];
newargc++;
return 0;
}
/******************************************************************************/
static void
#ifdef ANSI_COMPILER
split_prefix (
const char * pattern,
char * prefix,
const char * * mask)
#else
split_prefix (pattern, prefix, mask)
const char * pattern;
char * prefix;
const char * * mask;
#endif
{
const char * last_sep = NULL;
const char * scan = pattern;
while ('\0' != * scan)
{
if ('\\' == * scan || '/' == * scan || ':' == * scan)
last_sep = scan;
scan++;
}
if (NULL != last_sep)
{
long plen = 0L;
const char * p = pattern;
while (p <= last_sep)
{
plen++;
p++;
}
if (MAX_PATH <= plen)
plen = MAX_PATH - 1;
(void)memcpy (prefix, pattern, plen);
prefix [plen] = '\0';
* mask = last_sep + 1;
}
else
{
prefix [0] = '\0';
* mask = pattern;
}
}
/******************************************************************************/
static void
#ifdef ANSI_COMPILER
expand_pattern (
const char * pattern)
#else
expand_pattern (pattern)
const char * pattern;
#endif
{
char prefix [MAX_PATH];
const char * mask;
int rc;
split_prefix (pattern, prefix, & mask);
set_dta (& dtabuf);
rc = find_first (pattern, (unsigned short)ATTR_MASK);
if (0 == rc)
do
{
char full [MAX_PATH];
unsigned long plen = (unsigned long)strlen (prefix);
unsigned long nlen = (unsigned long)strlen (dtabuf.name);
unsigned long total = plen + nlen;
if (MAX_PATH <= total)
{
if (MAX_PATH <= plen)
{
rc = find_next ();
continue;
}
nlen = MAX_PATH - 1 - plen;
total = plen + nlen;
}
(void)memcpy (full, prefix, plen);
(void)memcpy (full + plen, dtabuf.name, nlen);
full [total] = '\0';
if (0 != add_arg (full))
break;
rc = find_next ();
}
while (0 == rc);
else
(void)add_arg (pattern);
}
/******************************************************************************/
void
#ifdef ANSI_COMPILER
ia16_expand_wildcards ( /*cppcheck-suppress unusedFunction*/
int * argc,
char * * * argv)
#else
ia16_expand_wildcards (argc, argv) /*cppcheck-suppress unusedFunction*/
int * argc;
char * * * argv;
#endif
{
int i;
newargc = 0;
if (0 < * argc)
{
(void)add_arg ((* argv)[0]);
i = 1;
}
else
i = 0;
while (i < * argc)
{
const char * p = (* argv)[i];
int has_wc = 0;
while ('\0' != * p)
{
if ('*' == * p || '?' == * p)
{
has_wc = 1;
break;
}
p++;
}
if (1 == has_wc)
expand_pattern ((* argv)[i]);
else
(void)add_arg ((* argv)[i]);
i++;
}
* argc = newargc;
* argv = newargv;
}
/******************************************************************************/
/*
* Local Variables:
* mode: c
* indent-tabs-mode: nil
* tab-width: 2
* c-basic-offset: 2
* fill-column: 80
* eval: (setq-local display-fill-column-indicator-column 80)
* eval: (display-fill-column-indicator-mode 1)
* End:
*/
/******************************************************************************/
/* vim: set ft=c ts=2 sw=2 tw=0 ai expandtab cc=80 : */
/******************************************************************************/