421 |
mateuszvis |
1 |
/* This file is part of the SvarCOM project and is published under the terms
|
|
|
2 |
* of the MIT license.
|
|
|
3 |
*
|
1716 |
mateusz.vi |
4 |
* Copyright (C) 2021-2024 Mateusz Viste
|
421 |
mateuszvis |
5 |
*
|
|
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
7 |
* copy of this software and associated documentation files (the "Software"),
|
|
|
8 |
* to deal in the Software without restriction, including without limitation
|
|
|
9 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
10 |
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
11 |
* Software is furnished to do so, subject to the following conditions:
|
|
|
12 |
*
|
|
|
13 |
* The above copyright notice and this permission notice shall be included in
|
|
|
14 |
* all copies or substantial portions of the Software.
|
|
|
15 |
*
|
|
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
22 |
* DEALINGS IN THE SOFTWARE.
|
|
|
23 |
*/
|
|
|
24 |
|
368 |
mateuszvis |
25 |
/*
|
|
|
26 |
* dir
|
|
|
27 |
*
|
|
|
28 |
* Displays a list of files and subdirectories in a directory.
|
|
|
29 |
*
|
|
|
30 |
* DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]
|
|
|
31 |
*
|
|
|
32 |
* /P Pauses after each screenful of information.
|
|
|
33 |
* /W Uses wide list format.
|
|
|
34 |
*
|
|
|
35 |
* /A Displays file with specified attributes:
|
|
|
36 |
* D Directories R Read-only files H Hidden files
|
|
|
37 |
* A Ready for archiving S System files - prefix meaning "not"
|
|
|
38 |
*
|
|
|
39 |
* /O List files in sorted order:
|
|
|
40 |
* N by name S by size E by extension
|
|
|
41 |
* D by date G group dirs first - prefix to reverse order
|
|
|
42 |
*
|
|
|
43 |
* /S Displays files in specified directory and all subdirectories.
|
|
|
44 |
* /B Uses bare format (no heading information or summary)
|
|
|
45 |
* /L Uses lowercases
|
2189 |
mateusz.vi |
46 |
*
|
|
|
47 |
* about /S - recursive DIR on specified (or current) path and subdirectories:
|
|
|
48 |
* prerequisite: some sort of mechanism that works as a stack pile of DTAs
|
|
|
49 |
*
|
|
|
50 |
* /S logic:
|
|
|
51 |
* 1. do a FindFirst on current directory
|
|
|
52 |
* 2. do FindNext calls in a loop, if a DIR entry is encountered, remember its
|
|
|
53 |
* name and put a copy of the current DTA on stack, then continue the
|
|
|
54 |
* listing without further interruption
|
|
|
55 |
* 3. if a new DIR was discovered, do a FindFirst on it and jmp to 2.
|
|
|
56 |
* if no DIR found, then go to 4.
|
|
|
57 |
* 4. look on the stack for a DTA.
|
|
|
58 |
* if any found, pop it and jmp to 2.
|
|
|
59 |
* otherwise job is done, exit.
|
368 |
mateuszvis |
60 |
*/
|
|
|
61 |
|
396 |
mateuszvis |
62 |
/* NOTE: /A attributes are matched in an exclusive way, ie. only files with
|
|
|
63 |
* the specified attributes are matched. This is different from how DOS
|
|
|
64 |
* itself matches attributes hence DIR cannot rely on the attributes
|
|
|
65 |
* filter within FindFirst.
|
|
|
66 |
*
|
|
|
67 |
* NOTE: Multiple /A are not supported - only the last one is significant.
|
|
|
68 |
*/
|
|
|
69 |
|
420 |
mateuszvis |
70 |
|
1991 |
mateusz.vi |
71 |
/* width of a column in wide mode output: 15 chars is the MINIMUM because
|
|
|
72 |
* directories are enclosed in [BRACKETS] and they may have an extension, too.
|
|
|
73 |
* Hence "[12345678.123]" is the longest we can get. Plus a delimiter space. */
|
|
|
74 |
#define WCOLWIDTH 15
|
424 |
mateuszvis |
75 |
|
1991 |
mateusz.vi |
76 |
|
1716 |
mateusz.vi |
77 |
/* a "tiny" DTA is a DTA that is stripped from bytes that are not needed for
|
|
|
78 |
* DIR operations */
|
|
|
79 |
_Packed struct TINYDTA {
|
|
|
80 |
/* char reserved[21];
|
|
|
81 |
unsigned char attr; */
|
|
|
82 |
unsigned short time_sec2:5;
|
|
|
83 |
unsigned short time_min:6;
|
|
|
84 |
unsigned short time_hour:5;
|
|
|
85 |
unsigned short date_dy:5;
|
|
|
86 |
unsigned short date_mo:4;
|
|
|
87 |
unsigned short date_yr:7;
|
|
|
88 |
unsigned long size;
|
|
|
89 |
/* char fname[13]; */
|
|
|
90 |
char fname[12];
|
|
|
91 |
};
|
|
|
92 |
|
|
|
93 |
|
424 |
mateuszvis |
94 |
/* fills freebytes with free bytes for drv (A=0, B=1, etc)
|
|
|
95 |
* returns DOS ERR code on failure */
|
|
|
96 |
static unsigned short cmd_dir_df(unsigned long *freebytes, unsigned char drv) {
|
|
|
97 |
unsigned short res = 0;
|
|
|
98 |
unsigned short sects_per_clust = 0, avail_clusts = 0, bytes_per_sect = 0;
|
|
|
99 |
|
|
|
100 |
_asm {
|
|
|
101 |
push ax
|
|
|
102 |
push bx
|
|
|
103 |
push cx
|
|
|
104 |
push dx
|
|
|
105 |
|
|
|
106 |
mov ah, 0x36 /* DOS 2+ -- Get Disk Free Space */
|
|
|
107 |
mov dl, [drv] /* A=1, B=2, etc (0 = DEFAULT DRIVE) */
|
|
|
108 |
inc dl
|
|
|
109 |
int 0x21 /* AX=sects_per_clust, BX=avail_clusts, CX=bytes_per_sect, DX=tot_clusters */
|
|
|
110 |
cmp ax, 0xffff /* AX=0xffff on error (invalid drive) */
|
|
|
111 |
jne COMPUTEDF
|
|
|
112 |
mov [res], 0x0f /* fill res with DOS error code 15 ("invalid drive") */
|
|
|
113 |
jmp DONE
|
|
|
114 |
|
|
|
115 |
COMPUTEDF:
|
|
|
116 |
/* freebytes = AX * BX * CX */
|
|
|
117 |
mov [sects_per_clust], ax
|
|
|
118 |
mov [avail_clusts], bx
|
|
|
119 |
mov [bytes_per_sect], cx
|
|
|
120 |
|
|
|
121 |
DONE:
|
|
|
122 |
pop dx
|
|
|
123 |
pop cx
|
|
|
124 |
pop bx
|
|
|
125 |
pop ax
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/* multiple steps to avoid uint16 overflow */
|
|
|
129 |
*freebytes = sects_per_clust;
|
|
|
130 |
*freebytes *= avail_clusts;
|
|
|
131 |
*freebytes *= bytes_per_sect;
|
|
|
132 |
|
|
|
133 |
return(res);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
|
528 |
mateuszvis |
137 |
static void dir_pagination(unsigned short *availrows) {
|
|
|
138 |
*availrows -= 1;
|
|
|
139 |
if (*availrows == 0) {
|
|
|
140 |
press_any_key();
|
|
|
141 |
*availrows = screen_getheight() - 1;
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
|
2206 |
mateusz.vi |
146 |
static void dir_print_dirprefix(const char *p) {
|
|
|
147 |
unsigned char t, lastbkslash;
|
|
|
148 |
char buff[2] = {0, 0};
|
|
|
149 |
|
|
|
150 |
/* find the last backslash of path */
|
|
|
151 |
lastbkslash = 0;
|
|
|
152 |
for (t = 0; p[t] != 0; t++) {
|
|
|
153 |
if (p[t] == '\\') lastbkslash = t;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/* print path until last bkslash */
|
|
|
157 |
do {
|
|
|
158 |
*buff = *p;
|
|
|
159 |
output(buff);
|
|
|
160 |
p++;
|
|
|
161 |
} while (lastbkslash-- != 0);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
|
2204 |
mateusz.vi |
165 |
/* print the "Directory of C:\ABC\.... string using a buffer with possible
|
|
|
166 |
* file pattern garbage trailing */
|
|
|
167 |
static void dir_print_dirof(const char *p, unsigned short *availrows, unsigned char pagination) {
|
|
|
168 |
char buff[2] = {0, 0};
|
|
|
169 |
const char *dirof = svarlang_str(37,20); /* Directory of % */
|
|
|
170 |
|
2205 |
mateusz.vi |
171 |
outputnl("");
|
|
|
172 |
if (pagination) dir_pagination(availrows);
|
|
|
173 |
|
2204 |
mateusz.vi |
174 |
/* print string until % */
|
|
|
175 |
while ((*dirof != 0) && (*dirof != '%')) {
|
|
|
176 |
*buff = *dirof;
|
|
|
177 |
output(buff);
|
|
|
178 |
dirof++;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
if (*dirof != '%') return;
|
|
|
182 |
dirof++;
|
|
|
183 |
|
|
|
184 |
/* print path until last bkslash */
|
2206 |
mateusz.vi |
185 |
dir_print_dirprefix(p);
|
2204 |
mateusz.vi |
186 |
|
|
|
187 |
/* print the rest of the dirof string */
|
|
|
188 |
while (*dirof != 0) {
|
|
|
189 |
*buff = *dirof;
|
|
|
190 |
output(buff);
|
|
|
191 |
dirof++;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
outputnl("");
|
|
|
195 |
if (pagination) dir_pagination(availrows);
|
|
|
196 |
outputnl("");
|
|
|
197 |
if (pagination) dir_pagination(availrows);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
|
2193 |
mateusz.vi |
201 |
/* add a new dirname to path, C:\XXX\*.EXE + YYY -> C:\XXX\YYY\*.EXE */
|
|
|
202 |
static void path_add(char *path, const char *dirname) {
|
2196 |
mateusz.vi |
203 |
short i, ostatni = -1;
|
2198 |
mateusz.vi |
204 |
//printf("path_add(%s,%s) -> ", path, dirname);
|
2193 |
mateusz.vi |
205 |
/* find the last backslash */
|
|
|
206 |
for (i = 0; path[i] != 0; i++) {
|
|
|
207 |
if (path[i] == '\\') ostatni = i;
|
|
|
208 |
}
|
|
|
209 |
/* abort on error */
|
|
|
210 |
if (ostatni == -1) return;
|
|
|
211 |
/* do the trick */
|
2196 |
mateusz.vi |
212 |
/* move ending to the right */
|
|
|
213 |
memcpy_rtl(path + ostatni + strlen(dirname) + 1, path + ostatni, strlen(path + ostatni) + 1);
|
|
|
214 |
/* fill in the space with dirname */
|
|
|
215 |
memcpy_ltr(path + ostatni + 1, dirname, strlen(dirname));
|
2198 |
mateusz.vi |
216 |
//printf("'%s'\n", path);
|
2193 |
mateusz.vi |
217 |
}
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
/* take back last dir from path, C:\XXX\YYY\*.EXE -> C:\XXX\*.EXE */
|
|
|
221 |
static void path_back(char *path) {
|
|
|
222 |
short i, ostatni = -1, przedostatni = -1;
|
2198 |
mateusz.vi |
223 |
//printf("path_back(%s) -> ", path);
|
2193 |
mateusz.vi |
224 |
/* find the two last backslashes */
|
|
|
225 |
for (i = 0; path[i] != 0; i++) {
|
|
|
226 |
if (path[i] == '\\') {
|
|
|
227 |
przedostatni = ostatni;
|
|
|
228 |
ostatni = i;
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
/* abort on error */
|
|
|
232 |
if (przedostatni == -1) return;
|
|
|
233 |
/* do the trick */
|
2196 |
mateusz.vi |
234 |
memcpy_ltr(path + przedostatni, path + ostatni, 1 + i - ostatni);
|
2198 |
mateusz.vi |
235 |
//printf("'%s'\n", path);
|
2193 |
mateusz.vi |
236 |
}
|
|
|
237 |
|
|
|
238 |
|
542 |
mateuszvis |
239 |
/* parse an attr list like "Ar-hS" and fill bitfield into attrfilter_may and attrfilter_must.
|
|
|
240 |
* /AHS -> adds S and H to mandatory attribs ("must")
|
|
|
241 |
* /A-S -> removes S from allowed attribs ("may")
|
|
|
242 |
* returns non-zero on error. */
|
|
|
243 |
static int dir_parse_attr_list(const char *arg, unsigned char *attrfilter_may, unsigned char *attrfilter_must) {
|
|
|
244 |
for (; *arg != 0; arg++) {
|
|
|
245 |
unsigned char curattr;
|
|
|
246 |
char not;
|
|
|
247 |
if (*arg == '-') {
|
|
|
248 |
not = 1;
|
|
|
249 |
arg++;
|
|
|
250 |
} else {
|
|
|
251 |
not = 0;
|
|
|
252 |
}
|
|
|
253 |
switch (*arg) {
|
|
|
254 |
case 'd':
|
|
|
255 |
case 'D':
|
|
|
256 |
curattr = DOS_ATTR_DIR;
|
|
|
257 |
break;
|
|
|
258 |
case 'r':
|
|
|
259 |
case 'R':
|
|
|
260 |
curattr = DOS_ATTR_RO;
|
|
|
261 |
break;
|
|
|
262 |
case 'a':
|
|
|
263 |
case 'A':
|
|
|
264 |
curattr = DOS_ATTR_ARC;
|
|
|
265 |
break;
|
|
|
266 |
case 'h':
|
|
|
267 |
case 'H':
|
|
|
268 |
curattr = DOS_ATTR_HID;
|
|
|
269 |
break;
|
|
|
270 |
case 's':
|
|
|
271 |
case 'S':
|
|
|
272 |
curattr = DOS_ATTR_SYS;
|
|
|
273 |
break;
|
|
|
274 |
default:
|
|
|
275 |
return(-1);
|
|
|
276 |
}
|
|
|
277 |
/* update res bitfield */
|
|
|
278 |
if (not) {
|
|
|
279 |
*attrfilter_may &= ~curattr;
|
|
|
280 |
} else {
|
|
|
281 |
*attrfilter_must |= curattr;
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
return(0);
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
|
1716 |
mateusz.vi |
288 |
/* compare attributes in a DTA node to mandatory and optional attributes. returns 1 on match, 0 otherwise */
|
|
|
289 |
static int filter_attribs(const struct DTA *dta, unsigned char attrfilter_must, unsigned char attrfilter_may) {
|
|
|
290 |
/* if mandatory attribs are requested, filter them now */
|
|
|
291 |
if ((attrfilter_must & dta->attr) != attrfilter_must) return(0);
|
|
|
292 |
|
|
|
293 |
/* if file contains attributes that are not allowed -> skip */
|
|
|
294 |
if ((~attrfilter_may & dta->attr) != 0) return(0);
|
|
|
295 |
|
|
|
296 |
return(1);
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
|
1719 |
mateusz.vi |
300 |
static struct {
|
|
|
301 |
struct TINYDTA far *dtabuf_root;
|
|
|
302 |
char order[8]; /* GNESD values (ucase = lower first ; lcase = higher first) */
|
1739 |
mateusz.vi |
303 |
unsigned char sortownia[256]; /* collation table (used for NLS-aware sorts) */
|
1719 |
mateusz.vi |
304 |
} glob_sortcmp_dat;
|
1716 |
mateusz.vi |
305 |
|
1719 |
mateusz.vi |
306 |
|
|
|
307 |
/* translates an order string like "GNE-S" into values fed into the order[]
|
|
|
308 |
* table of glob_sortcmp_dat. returns 0 on success, non-zero otherwise. */
|
1724 |
mateusz.vi |
309 |
static int dir_process_order_directive(const char *ordstring) {
|
1719 |
mateusz.vi |
310 |
const char *gnesd = "gnesd"; /* must be lower case */
|
|
|
311 |
int ordi, orderi = 0, i;
|
|
|
312 |
|
|
|
313 |
/* tabula rasa */
|
|
|
314 |
glob_sortcmp_dat.order[0] = 0;
|
|
|
315 |
|
1721 |
mateusz.vi |
316 |
/* /O alone is a short hand for /OGN */
|
|
|
317 |
if (*ordstring == 0) {
|
|
|
318 |
glob_sortcmp_dat.order[0] = 'G';
|
|
|
319 |
glob_sortcmp_dat.order[1] = 'N';
|
|
|
320 |
glob_sortcmp_dat.order[2] = 0;
|
|
|
321 |
}
|
|
|
322 |
|
1726 |
mateusz.vi |
323 |
/* stupid MSDOS compatibility ("DIR /O:GNE") */
|
|
|
324 |
if (*ordstring == ':') ordstring++;
|
|
|
325 |
|
1719 |
mateusz.vi |
326 |
/* parsing */
|
|
|
327 |
for (ordi = 0; ordstring[ordi] != 0; ordi++) {
|
|
|
328 |
if (ordstring[ordi] == '-') {
|
|
|
329 |
if ((ordstring[ordi + 1] == '-') || (ordstring[ordi + 1] == 0)) return(-1);
|
|
|
330 |
continue;
|
|
|
331 |
}
|
|
|
332 |
if (orderi == sizeof(glob_sortcmp_dat.order)) return(-1);
|
|
|
333 |
|
|
|
334 |
for (i = 0; gnesd[i] != 0; i++) {
|
|
|
335 |
if ((ordstring[ordi] | 32) == gnesd[i]) { /* | 32 is lcase-ing the char */
|
|
|
336 |
if ((ordi > 0) && (ordstring[ordi - 1] == '-')) {
|
|
|
337 |
glob_sortcmp_dat.order[orderi] = gnesd[i];
|
|
|
338 |
} else {
|
|
|
339 |
glob_sortcmp_dat.order[orderi] = gnesd[i] ^ 32;
|
|
|
340 |
}
|
|
|
341 |
orderi++;
|
|
|
342 |
break;
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
if (gnesd[i] == 0) return(-1);
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
return(0);
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
static int sortcmp(const void *dtaid1, const void *dtaid2) {
|
|
|
353 |
struct TINYDTA far *dta1 = &(glob_sortcmp_dat.dtabuf_root[*((unsigned short *)dtaid1)]);
|
|
|
354 |
struct TINYDTA far *dta2 = &(glob_sortcmp_dat.dtabuf_root[*((unsigned short *)dtaid2)]);
|
|
|
355 |
char *ordconf = glob_sortcmp_dat.order;
|
|
|
356 |
|
|
|
357 |
/* debug stuff
|
|
|
358 |
{
|
|
|
359 |
int i;
|
|
|
360 |
printf("%lu vs %lu | ", dta1->size, dta2->size);
|
|
|
361 |
for (i = 0; dta1->fname[i] != 0; i++) printf("%c", dta1->fname[i]);
|
|
|
362 |
printf(" vs ");
|
|
|
363 |
for (i = 0; dta2->fname[i] != 0; i++) printf("%c", dta2->fname[i]);
|
|
|
364 |
printf("\n");
|
|
|
365 |
} */
|
|
|
366 |
|
|
|
367 |
for (;;) {
|
|
|
368 |
int r = -1;
|
|
|
369 |
if (*ordconf & 32) r = 1;
|
|
|
370 |
|
|
|
371 |
switch (*ordconf | 32) {
|
|
|
372 |
case 'g': /* sort by type (directories first, then files) */
|
|
|
373 |
if ((dta1->time_sec2 & DOS_ATTR_DIR) > (dta2->time_sec2 & DOS_ATTR_DIR)) return(0 - r);
|
|
|
374 |
if ((dta1->time_sec2 & DOS_ATTR_DIR) < (dta2->time_sec2 & DOS_ATTR_DIR)) return(r);
|
|
|
375 |
break;
|
|
|
376 |
case ' ': /* default (last resort) sort: by name */
|
|
|
377 |
case 'e': /* sort by extension */
|
|
|
378 |
case 'n': /* sort by filename */
|
|
|
379 |
{
|
|
|
380 |
const char far *f1 = dta1->fname;
|
|
|
381 |
const char far *f2 = dta2->fname;
|
|
|
382 |
int i, limit = 12;
|
|
|
383 |
/* special handling for '.' and '..' entries */
|
|
|
384 |
if ((f1[0] == '.') && (f2[0] != '.')) return(0 - r);
|
|
|
385 |
if ((f2[0] == '.') && (f1[0] != '.')) return(r);
|
|
|
386 |
|
|
|
387 |
if ((*ordconf | 32) == 'e') {
|
|
|
388 |
/* fast-forward to extension or end of filename */
|
|
|
389 |
while ((*f1 != 0) && (*f1 != '.')) f1++;
|
|
|
390 |
while ((*f2 != 0) && (*f2 != '.')) f2++;
|
|
|
391 |
limit = 4; /* TINYDTA structs are not nul-terminated */
|
|
|
392 |
}
|
|
|
393 |
/* cmp */
|
|
|
394 |
for (i = 0; i < limit; i++) {
|
1739 |
mateusz.vi |
395 |
if ((glob_sortcmp_dat.sortownia[(unsigned char)(*f1)]) < (glob_sortcmp_dat.sortownia[(unsigned char)(*f2)])) return(0 - r);
|
|
|
396 |
if ((glob_sortcmp_dat.sortownia[(unsigned char)(*f1)]) > (glob_sortcmp_dat.sortownia[(unsigned char)(*f2)])) return(r);
|
1719 |
mateusz.vi |
397 |
if (*f1 == 0) break;
|
|
|
398 |
f1++;
|
|
|
399 |
f2++;
|
|
|
400 |
}
|
|
|
401 |
}
|
|
|
402 |
break;
|
|
|
403 |
case 's': /* sort by size */
|
|
|
404 |
if (dta1->size > dta2->size) return(r);
|
|
|
405 |
if (dta1->size < dta2->size) return(0 - r);
|
|
|
406 |
break;
|
|
|
407 |
case 'd': /* sort by date */
|
|
|
408 |
if (dta1->date_yr < dta2->date_yr) return(0 - r);
|
|
|
409 |
if (dta1->date_yr > dta2->date_yr) return(r);
|
|
|
410 |
if (dta1->date_mo < dta2->date_mo) return(0 - r);
|
|
|
411 |
if (dta1->date_mo > dta2->date_mo) return(r);
|
|
|
412 |
if (dta1->date_dy < dta2->date_dy) return(0 - r);
|
|
|
413 |
if (dta1->date_dy > dta2->date_dy) return(r);
|
|
|
414 |
if (dta1->time_hour < dta2->time_hour) return(0 - r);
|
|
|
415 |
if (dta1->time_hour > dta2->time_hour) return(r);
|
|
|
416 |
if (dta1->time_min < dta2->time_min) return(0 - r);
|
|
|
417 |
if (dta1->time_min > dta2->time_min) return(r);
|
|
|
418 |
break;
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
if (*ordconf == 0) break;
|
|
|
422 |
ordconf++;
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
return(0);
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
|
542 |
mateuszvis |
429 |
#define DIR_ATTR_DEFAULT (DOS_ATTR_RO | DOS_ATTR_DIR | DOS_ATTR_ARC)
|
|
|
430 |
|
1724 |
mateusz.vi |
431 |
struct dirrequest {
|
|
|
432 |
unsigned char attrfilter_may;
|
|
|
433 |
unsigned char attrfilter_must;
|
|
|
434 |
const char *filespecptr;
|
420 |
mateuszvis |
435 |
|
396 |
mateuszvis |
436 |
#define DIR_FLAG_PAUSE 1
|
|
|
437 |
#define DIR_FLAG_RECUR 4
|
420 |
mateuszvis |
438 |
#define DIR_FLAG_LCASE 8
|
1719 |
mateusz.vi |
439 |
#define DIR_FLAG_SORT 16
|
1724 |
mateusz.vi |
440 |
unsigned char flags;
|
368 |
mateuszvis |
441 |
|
420 |
mateuszvis |
442 |
#define DIR_OUTPUT_NORM 1
|
|
|
443 |
#define DIR_OUTPUT_WIDE 2
|
|
|
444 |
#define DIR_OUTPUT_BARE 3
|
1724 |
mateusz.vi |
445 |
unsigned char format;
|
|
|
446 |
};
|
420 |
mateuszvis |
447 |
|
1719 |
mateusz.vi |
448 |
|
1724 |
mateusz.vi |
449 |
static int dir_parse_cmdline(struct dirrequest *req, const char **argv) {
|
|
|
450 |
for (; *argv != NULL; argv++) {
|
|
|
451 |
if (*argv[0] == '/') {
|
|
|
452 |
const char *arg = *argv + 1;
|
396 |
mateuszvis |
453 |
char neg = 0;
|
|
|
454 |
/* detect negations and get actual argument */
|
542 |
mateuszvis |
455 |
if (*arg == '-') {
|
|
|
456 |
neg = 1;
|
|
|
457 |
arg++;
|
|
|
458 |
}
|
396 |
mateuszvis |
459 |
/* */
|
542 |
mateuszvis |
460 |
switch (*arg) {
|
396 |
mateuszvis |
461 |
case 'a':
|
|
|
462 |
case 'A':
|
542 |
mateuszvis |
463 |
arg++;
|
|
|
464 |
/* preset defaults */
|
1724 |
mateusz.vi |
465 |
req->attrfilter_may = DIR_ATTR_DEFAULT;
|
|
|
466 |
req->attrfilter_must = 0;
|
542 |
mateuszvis |
467 |
/* /-A only allowed without further parameters (used to cancel possible previous /Asmth) */
|
|
|
468 |
if (neg) {
|
|
|
469 |
if (*arg != 0) {
|
|
|
470 |
nls_outputnl_err(0, 2); /* invalid switch */
|
1724 |
mateusz.vi |
471 |
return(-1);
|
542 |
mateuszvis |
472 |
}
|
|
|
473 |
} else {
|
1085 |
mateusz.vi |
474 |
/* skip colon if present */
|
|
|
475 |
if (*arg == ':') arg++;
|
542 |
mateuszvis |
476 |
/* start with "allow everything" */
|
1724 |
mateusz.vi |
477 |
req->attrfilter_may = (DOS_ATTR_ARC | DOS_ATTR_DIR | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_RO);
|
|
|
478 |
if (dir_parse_attr_list(arg, &(req->attrfilter_may), &(req->attrfilter_must)) != 0) {
|
542 |
mateuszvis |
479 |
nls_outputnl_err(0, 3); /* invalid parameter format */
|
1724 |
mateusz.vi |
480 |
return(-1);
|
542 |
mateuszvis |
481 |
}
|
|
|
482 |
}
|
396 |
mateuszvis |
483 |
break;
|
399 |
mateuszvis |
484 |
case 'b':
|
|
|
485 |
case 'B':
|
1724 |
mateusz.vi |
486 |
req->format = DIR_OUTPUT_BARE;
|
399 |
mateuszvis |
487 |
break;
|
421 |
mateuszvis |
488 |
case 'l':
|
|
|
489 |
case 'L':
|
1724 |
mateusz.vi |
490 |
req->flags |= DIR_FLAG_LCASE;
|
420 |
mateuszvis |
491 |
break;
|
421 |
mateuszvis |
492 |
case 'o':
|
|
|
493 |
case 'O':
|
1720 |
mateusz.vi |
494 |
if (neg) {
|
1724 |
mateusz.vi |
495 |
req->flags &= (0xff ^ DIR_FLAG_SORT);
|
1720 |
mateusz.vi |
496 |
break;
|
|
|
497 |
}
|
1724 |
mateusz.vi |
498 |
if (dir_process_order_directive(arg+1) != 0) {
|
1719 |
mateusz.vi |
499 |
nls_output_err(0, 3); /* invalid parameter format */
|
|
|
500 |
output(": ");
|
|
|
501 |
outputnl(arg);
|
1724 |
mateusz.vi |
502 |
return(-1);
|
1719 |
mateusz.vi |
503 |
}
|
1724 |
mateusz.vi |
504 |
req->flags |= DIR_FLAG_SORT;
|
421 |
mateuszvis |
505 |
break;
|
396 |
mateuszvis |
506 |
case 'p':
|
|
|
507 |
case 'P':
|
1724 |
mateusz.vi |
508 |
req->flags |= DIR_FLAG_PAUSE;
|
|
|
509 |
if (neg) req->flags &= (0xff ^ DIR_FLAG_PAUSE);
|
396 |
mateuszvis |
510 |
break;
|
421 |
mateuszvis |
511 |
case 's':
|
|
|
512 |
case 'S':
|
2193 |
mateusz.vi |
513 |
req->flags |= DIR_FLAG_RECUR;
|
420 |
mateuszvis |
514 |
break;
|
421 |
mateuszvis |
515 |
case 'w':
|
|
|
516 |
case 'W':
|
1724 |
mateusz.vi |
517 |
req->format = DIR_OUTPUT_WIDE;
|
421 |
mateuszvis |
518 |
break;
|
393 |
mateuszvis |
519 |
default:
|
542 |
mateuszvis |
520 |
nls_outputnl_err(0, 2); /* invalid switch */
|
1724 |
mateusz.vi |
521 |
return(-1);
|
393 |
mateuszvis |
522 |
}
|
|
|
523 |
} else { /* filespec */
|
1724 |
mateusz.vi |
524 |
if (req->filespecptr != NULL) {
|
542 |
mateuszvis |
525 |
nls_outputnl_err(0, 4); /* too many parameters */
|
1724 |
mateusz.vi |
526 |
return(-1);
|
393 |
mateuszvis |
527 |
}
|
1724 |
mateusz.vi |
528 |
req->filespecptr = *argv;
|
393 |
mateuszvis |
529 |
}
|
|
|
530 |
}
|
368 |
mateuszvis |
531 |
|
1724 |
mateusz.vi |
532 |
return(0);
|
|
|
533 |
}
|
393 |
mateuszvis |
534 |
|
1724 |
mateusz.vi |
535 |
|
2201 |
mateusz.vi |
536 |
static void dir_print_summary_files(char *buff64, unsigned short uint32maxlen, unsigned long summary_totsz, unsigned long summary_fcount, unsigned short *availrows, unsigned char flags, const struct nls_patterns *nls) {
|
|
|
537 |
unsigned short i;
|
|
|
538 |
/* x file(s) (maximum of files in a FAT-32 directory is 65'535) */
|
|
|
539 |
memset(buff64, ' ', 8);
|
|
|
540 |
i = nls_format_number(buff64 + 8, summary_fcount, nls);
|
|
|
541 |
sprintf(buff64 + 8 + i, " %s ", svarlang_str(37,22)/*"file(s)"*/);
|
|
|
542 |
output(buff64 + i);
|
|
|
543 |
/* xxxx bytes */
|
|
|
544 |
memset(buff64, ' ', 14);
|
|
|
545 |
i = nls_format_number(buff64 + uint32maxlen, summary_totsz, nls);
|
|
|
546 |
output(buff64 + i + 1);
|
|
|
547 |
output(" ");
|
|
|
548 |
nls_outputnl(37,23); /* "bytes" */
|
|
|
549 |
if (flags & DIR_FLAG_PAUSE) dir_pagination(availrows);
|
|
|
550 |
}
|
|
|
551 |
|
|
|
552 |
|
2193 |
mateusz.vi |
553 |
#define MAX_SORTABLE_FILES 8192
|
|
|
554 |
|
1724 |
mateusz.vi |
555 |
static enum cmd_result cmd_dir(struct cmd_funcparam *p) {
|
|
|
556 |
struct DTA *dta = (void *)0x80; /* set DTA to its default location at 80h in PSP */
|
|
|
557 |
struct TINYDTA far *dtabuf = NULL; /* used to buffer results when sorting is enabled */
|
|
|
558 |
unsigned short dtabufcount = 0;
|
|
|
559 |
unsigned short i;
|
|
|
560 |
unsigned short availrows; /* counter of available rows on display (used for /P) */
|
|
|
561 |
unsigned short screenw = screen_getwidth();
|
|
|
562 |
unsigned short wcols = screenw / WCOLWIDTH; /* number of columns in wide mode */
|
|
|
563 |
unsigned char wcolcount;
|
|
|
564 |
struct {
|
|
|
565 |
struct nls_patterns nls;
|
|
|
566 |
char buff64[64];
|
|
|
567 |
char path[128];
|
2193 |
mateusz.vi |
568 |
struct DTA dtastack[64]; /* used for /S, max number of subdirs in DOS5 is 42 (A/B/C/...) */
|
|
|
569 |
unsigned char dtastacklen;
|
|
|
570 |
unsigned short orderidx[MAX_SORTABLE_FILES / sizeof(struct TINYDTA)];
|
|
|
571 |
} *buf;
|
2201 |
mateusz.vi |
572 |
unsigned long summary_recurs_fcount = 0; /* used for /s global summary */
|
|
|
573 |
unsigned long summary_recurs_totsz = 0; /* used for /s global summary */
|
|
|
574 |
unsigned long summary_fcount;
|
|
|
575 |
unsigned long summary_totsz;
|
1724 |
mateusz.vi |
576 |
unsigned char drv = 0;
|
|
|
577 |
struct dirrequest req;
|
2201 |
mateusz.vi |
578 |
unsigned short summary_alignpos = strlen(svarlang_str(37,22)) + 2;
|
2200 |
mateusz.vi |
579 |
unsigned short uint32maxlen = 14; /* 13 is the max len of a 32 bit number with thousand separators (4'000'000'000) */
|
|
|
580 |
if (screenw < 80) uint32maxlen = 10;
|
1724 |
mateusz.vi |
581 |
|
|
|
582 |
if (cmd_ishlp(p)) {
|
|
|
583 |
nls_outputnl(37,0); /* "Displays a list of files and subdirectories in a directory" */
|
|
|
584 |
outputnl("");
|
|
|
585 |
nls_outputnl(37,1); /* "DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]" */
|
|
|
586 |
outputnl("");
|
|
|
587 |
nls_outputnl(37,2); /* "/P Pauses after each screenful of information" */
|
|
|
588 |
nls_outputnl(37,3); /* "/W Uses wide list format" */
|
|
|
589 |
outputnl("");
|
|
|
590 |
nls_outputnl(37,4); /* "/A Displays files with specified attributes:" */
|
|
|
591 |
nls_outputnl(37,5); /* " D Directories R Read-only files H Hidden files" */
|
|
|
592 |
nls_outputnl(37,6); /* " A Ready for archiving S System files - prefix meaning "not"" */
|
|
|
593 |
outputnl("");
|
|
|
594 |
nls_outputnl(37,7); /* "/O List files in sorted order:" */
|
|
|
595 |
nls_outputnl(37,8); /* " N by name S by size E by extension" */
|
|
|
596 |
nls_outputnl(37,9); /* " D by date G group dirs first - prefix to reverse order" */
|
|
|
597 |
outputnl("");
|
|
|
598 |
nls_outputnl(37,10); /* "/S Displays files in specified directory and all subdirectories" */
|
|
|
599 |
nls_outputnl(37,11); /* "/B Uses bare format (no heading information or summary)" */
|
|
|
600 |
nls_outputnl(37,12); /* "/L Uses lowercases" */
|
2205 |
mateusz.vi |
601 |
goto GAMEOVER;
|
1724 |
mateusz.vi |
602 |
}
|
|
|
603 |
|
2193 |
mateusz.vi |
604 |
/* allocate buf */
|
|
|
605 |
buf = calloc(sizeof(*buf), 1);
|
|
|
606 |
if (buf == NULL) {
|
|
|
607 |
nls_output_err(255, 8); /* insufficient memory */
|
2205 |
mateusz.vi |
608 |
goto GAMEOVER;
|
2193 |
mateusz.vi |
609 |
}
|
|
|
610 |
|
1739 |
mateusz.vi |
611 |
/* zero out glob_sortcmp_dat and init the collation table */
|
|
|
612 |
bzero(&glob_sortcmp_dat, sizeof(glob_sortcmp_dat));
|
|
|
613 |
for (i = 0; i < 256; i++) {
|
|
|
614 |
glob_sortcmp_dat.sortownia[i] = i;
|
|
|
615 |
/* sorting should be case-insensitive */
|
1740 |
mateusz.vi |
616 |
if ((i >= 'A') && (i <= 'Z')) glob_sortcmp_dat.sortownia[i] |= 32;
|
1739 |
mateusz.vi |
617 |
}
|
|
|
618 |
|
1743 |
mateusz.vi |
619 |
/* try to replace (or complement) my naive collation table with an NLS-aware
|
1744 |
mateusz.vi |
620 |
* version provided by the kernel (or NLSFUNC)
|
1745 |
mateusz.vi |
621 |
* see https://github.com/SvarDOS/bugz/issues/68 for some thoughts */
|
|
|
622 |
{
|
1743 |
mateusz.vi |
623 |
_Packed struct nlsseqtab {
|
|
|
624 |
unsigned char id;
|
|
|
625 |
unsigned short taboff;
|
|
|
626 |
unsigned short tabseg;
|
|
|
627 |
} collat;
|
|
|
628 |
void *colptr = &collat;
|
|
|
629 |
unsigned char errflag = 1;
|
|
|
630 |
_asm {
|
|
|
631 |
push ax
|
|
|
632 |
push bx
|
|
|
633 |
push cx
|
|
|
634 |
push dx
|
|
|
635 |
push di
|
|
|
636 |
push es
|
|
|
637 |
|
|
|
638 |
mov ax, 0x6506 /* DOS 3.3+ - Get collating sequence table */
|
|
|
639 |
mov bx, 0xffff /* code page, FFFFh = "current" */
|
|
|
640 |
mov cx, 5 /* size of buffer at ES:DI */
|
|
|
641 |
mov dx, 0xffff /* country id, FFFFh = "current" */
|
|
|
642 |
push ds
|
|
|
643 |
pop es /* ES:DI = address of buffer for the 5-bytes struct */
|
|
|
644 |
mov di, colptr
|
|
|
645 |
int 0x21
|
|
|
646 |
jc FAIL
|
|
|
647 |
xor al, al
|
|
|
648 |
mov errflag, al
|
|
|
649 |
FAIL:
|
|
|
650 |
|
|
|
651 |
pop es
|
|
|
652 |
pop di
|
|
|
653 |
pop dx
|
|
|
654 |
pop cx
|
|
|
655 |
pop bx
|
|
|
656 |
pop ax
|
|
|
657 |
}
|
|
|
658 |
|
|
|
659 |
if ((errflag == 0) && (collat.id == 6)) {
|
|
|
660 |
unsigned char far *ptr = MK_FP(collat.tabseg, collat.taboff);
|
|
|
661 |
unsigned short count = *(unsigned short far *)ptr;
|
1745 |
mateusz.vi |
662 |
#ifdef DIR_DUMPNLSCOLLATE
|
|
|
663 |
printf("NLS AT %04X:%04X (%u elements)\n", collat.tabseg, collat.taboff, count);
|
|
|
664 |
#endif
|
1743 |
mateusz.vi |
665 |
if (count <= 256) { /* you never know */
|
|
|
666 |
ptr += 2; /* skip the count header */
|
|
|
667 |
for (i = 0; i < count; i++) {
|
|
|
668 |
glob_sortcmp_dat.sortownia[i] = ptr[i];
|
1745 |
mateusz.vi |
669 |
#ifdef DIR_DUMPNLSCOLLATE
|
|
|
670 |
printf(" %03u", ptr[i]);
|
|
|
671 |
if ((i & 15) == 15) {
|
|
|
672 |
printf("\n");
|
|
|
673 |
fflush(stdout);
|
|
|
674 |
}
|
|
|
675 |
#endif
|
1743 |
mateusz.vi |
676 |
}
|
|
|
677 |
}
|
|
|
678 |
}
|
|
|
679 |
}
|
|
|
680 |
|
1724 |
mateusz.vi |
681 |
i = nls_getpatterns(&(buf->nls));
|
|
|
682 |
if (i != 0) nls_outputnl_doserr(i);
|
|
|
683 |
|
|
|
684 |
/* disable usage of thousands separator on narrow screens */
|
|
|
685 |
if (screenw < 80) buf->nls.thousep[0] = 0;
|
|
|
686 |
|
1725 |
mateusz.vi |
687 |
/*** PARSING COMMAND LINE STARTS *******************************************/
|
|
|
688 |
|
|
|
689 |
/* init req with some defaults */
|
|
|
690 |
bzero(&req, sizeof(req));
|
|
|
691 |
req.attrfilter_may = DIR_ATTR_DEFAULT;
|
|
|
692 |
req.format = DIR_OUTPUT_NORM;
|
|
|
693 |
|
|
|
694 |
/* process DIRCMD first (so it can be overidden by user's cmdline) */
|
|
|
695 |
{
|
|
|
696 |
const char far *dircmd = env_lookup_val(p->env_seg, "DIRCMD");
|
|
|
697 |
if (dircmd != NULL) {
|
|
|
698 |
const char *argvptrs[32];
|
|
|
699 |
cmd_explode(buf->buff64, dircmd, argvptrs);
|
|
|
700 |
if ((dir_parse_cmdline(&req, argvptrs) != 0) || (req.filespecptr != NULL)) {
|
|
|
701 |
nls_output(255, 10);/* bad environment */
|
|
|
702 |
output(" - ");
|
|
|
703 |
outputnl("DIRCMD");
|
2205 |
mateusz.vi |
704 |
goto GAMEOVER;
|
1725 |
mateusz.vi |
705 |
}
|
|
|
706 |
}
|
|
|
707 |
}
|
|
|
708 |
|
|
|
709 |
/* parse user's command line */
|
2205 |
mateusz.vi |
710 |
if (dir_parse_cmdline(&req, p->argv) != 0) goto GAMEOVER;
|
1724 |
mateusz.vi |
711 |
|
2193 |
mateusz.vi |
712 |
/*** PARSING COMMAND LINE DONE *********************************************/
|
|
|
713 |
|
1725 |
mateusz.vi |
714 |
/* if no filespec provided, then it's about the current directory */
|
|
|
715 |
if (req.filespecptr == NULL) req.filespecptr = ".";
|
|
|
716 |
|
2202 |
mateusz.vi |
717 |
availrows = screen_getheight() - 1;
|
528 |
mateuszvis |
718 |
|
417 |
mateuszvis |
719 |
/* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
|
1724 |
mateusz.vi |
720 |
if ((req.filespecptr[0] != 0) && (req.filespecptr[1] == ':') && (req.filespecptr[2] == 0)) {
|
|
|
721 |
if ((req.filespecptr[0] >= 'a') && (req.filespecptr[0] <= 'z')) {
|
|
|
722 |
buf->path[0] = req.filespecptr[0] - ('a' - 1);
|
417 |
mateuszvis |
723 |
} else {
|
1724 |
mateusz.vi |
724 |
buf->path[0] = req.filespecptr[0] - ('A' - 1);
|
399 |
mateuszvis |
725 |
}
|
1717 |
mateusz.vi |
726 |
i = curpathfordrv(buf->path, buf->path[0]);
|
417 |
mateuszvis |
727 |
} else {
|
1724 |
mateusz.vi |
728 |
i = file_truename(req.filespecptr, buf->path);
|
399 |
mateuszvis |
729 |
}
|
417 |
mateuszvis |
730 |
if (i != 0) {
|
538 |
mateuszvis |
731 |
nls_outputnl_doserr(i);
|
2205 |
mateusz.vi |
732 |
goto GAMEOVER;
|
417 |
mateuszvis |
733 |
}
|
393 |
mateuszvis |
734 |
|
2198 |
mateusz.vi |
735 |
/* volume label and serial */
|
1724 |
mateusz.vi |
736 |
if (req.format != DIR_OUTPUT_BARE) {
|
1717 |
mateusz.vi |
737 |
drv = buf->path[0];
|
399 |
mateuszvis |
738 |
if (drv >= 'a') {
|
|
|
739 |
drv -= 'a';
|
|
|
740 |
} else {
|
|
|
741 |
drv -= 'A';
|
|
|
742 |
}
|
1717 |
mateusz.vi |
743 |
cmd_vol_internal(drv, buf->buff64);
|
2202 |
mateusz.vi |
744 |
availrows -= 2;
|
2198 |
mateusz.vi |
745 |
}
|
|
|
746 |
|
|
|
747 |
NEXT_ITER: /* re-entry point for /S recursing */
|
|
|
748 |
|
2201 |
mateusz.vi |
749 |
summary_fcount = 0;
|
|
|
750 |
summary_totsz = 0;
|
|
|
751 |
|
2203 |
mateusz.vi |
752 |
/* if dir: append a backslash (also get its len) */
|
|
|
753 |
i = path_appendbkslash_if_dir(buf->path);
|
|
|
754 |
|
|
|
755 |
/* if ends with a \ then append ????????.??? */
|
|
|
756 |
if (buf->path[i - 1] == '\\') strcat(buf->path, "????????.???");
|
|
|
757 |
|
|
|
758 |
/* ask DOS for list of files, but only with allowed attribs */
|
|
|
759 |
i = findfirst(dta, buf->path, req.attrfilter_may);
|
|
|
760 |
|
|
|
761 |
/* print "directory of" unless /B or /S mode with no match */
|
|
|
762 |
if ((req.format != DIR_OUTPUT_BARE) && (((req.flags & DIR_FLAG_RECUR) == 0) || (i == 0))) {
|
2204 |
mateusz.vi |
763 |
dir_print_dirof(buf->path, &availrows, req.flags & DIR_FLAG_PAUSE);
|
399 |
mateuszvis |
764 |
}
|
|
|
765 |
|
2203 |
mateusz.vi |
766 |
/* if no file match then abort */
|
417 |
mateuszvis |
767 |
if (i != 0) {
|
2197 |
mateusz.vi |
768 |
if (req.flags & DIR_FLAG_RECUR) goto CHECK_RECURS;
|
538 |
mateuszvis |
769 |
nls_outputnl_doserr(i);
|
2205 |
mateusz.vi |
770 |
goto GAMEOVER;
|
417 |
mateuszvis |
771 |
}
|
|
|
772 |
|
1716 |
mateusz.vi |
773 |
/* if sorting is involved, then let's buffer all results (and sort them) */
|
1724 |
mateusz.vi |
774 |
if (req.flags & DIR_FLAG_SORT) {
|
1716 |
mateusz.vi |
775 |
/* allocate a memory buffer - try several sizes until one succeeds */
|
2194 |
mateusz.vi |
776 |
unsigned short max_dta_bufcount;
|
|
|
777 |
|
|
|
778 |
/* compute the amount of DTAs I can buffer */
|
|
|
779 |
for (max_dta_bufcount = MAX_SORTABLE_FILES; max_dta_bufcount != 0; max_dta_bufcount /= 2) {
|
|
|
780 |
dtabuf = _fmalloc(max_dta_bufcount * sizeof(struct TINYDTA));
|
1716 |
mateusz.vi |
781 |
if (dtabuf != NULL) break;
|
|
|
782 |
}
|
2194 |
mateusz.vi |
783 |
/* printf("max_dta_bufcount = %u\n", max_dta_bufcount); */
|
1716 |
mateusz.vi |
784 |
|
|
|
785 |
if (dtabuf == NULL) {
|
|
|
786 |
nls_outputnl_doserr(8); /* out of memory */
|
2205 |
mateusz.vi |
787 |
goto GAMEOVER;
|
1716 |
mateusz.vi |
788 |
}
|
|
|
789 |
|
|
|
790 |
/* remember the address so I can free it afterwards */
|
1719 |
mateusz.vi |
791 |
glob_sortcmp_dat.dtabuf_root = dtabuf;
|
1716 |
mateusz.vi |
792 |
|
|
|
793 |
do {
|
|
|
794 |
/* filter out files with uninteresting attributes */
|
1724 |
mateusz.vi |
795 |
if (filter_attribs(dta, req.attrfilter_must, req.attrfilter_may) == 0) continue;
|
1716 |
mateusz.vi |
796 |
|
2207 |
mateusz.vi |
797 |
/* /B hides . and .. entries */
|
|
|
798 |
if ((req.format == DIR_OUTPUT_BARE) && (dta->fname[0] == '.')) continue;
|
|
|
799 |
|
1719 |
mateusz.vi |
800 |
/* normalize "size" of directories to zero because kernel returns garbage
|
|
|
801 |
* sizes for directories which might confuse the sorting routine later */
|
|
|
802 |
if (dta->attr & DOS_ATTR_DIR) dta->size = 0;
|
|
|
803 |
|
1716 |
mateusz.vi |
804 |
_fmemcpy(&(dtabuf[dtabufcount]), ((char *)dta) + 22, sizeof(struct TINYDTA));
|
|
|
805 |
|
|
|
806 |
/* save attribs in sec field, otherwise zero it (this field is not
|
|
|
807 |
* displayed and dropping the attr field saves 2 bytes per entry) */
|
|
|
808 |
dtabuf[dtabufcount++].time_sec2 = (dta->attr & 31);
|
|
|
809 |
|
|
|
810 |
/* do I have any space left? */
|
|
|
811 |
if (dtabufcount == max_dta_bufcount) {
|
1719 |
mateusz.vi |
812 |
//TODO some kind of user notification might be nice here
|
1716 |
mateusz.vi |
813 |
//outputnl("TOO MANY ENTRIES FOR SORTING! LIST IS UNSORTED");
|
|
|
814 |
break;
|
|
|
815 |
}
|
|
|
816 |
|
|
|
817 |
} while (findnext(dta) == 0);
|
|
|
818 |
|
1742 |
mateusz.vi |
819 |
/* no match? kein gluck! (this can happen when filtering attribs with /A:xxx
|
|
|
820 |
* because while findfirst() succeeds, all entries can be rejected) */
|
|
|
821 |
if (dtabufcount == 0) {
|
2207 |
mateusz.vi |
822 |
if (req.flags & DIR_FLAG_RECUR) goto CHECK_RECURS;
|
1742 |
mateusz.vi |
823 |
nls_outputnl_doserr(2); /* "File not found" */
|
2205 |
mateusz.vi |
824 |
goto GAMEOVER;
|
1742 |
mateusz.vi |
825 |
}
|
|
|
826 |
|
1716 |
mateusz.vi |
827 |
/* sort the list - the tricky part is that my array is a far address while
|
1719 |
mateusz.vi |
828 |
* qsort works only with near pointers, so I have to use an ugly (and
|
|
|
829 |
* global) auxiliary table */
|
|
|
830 |
for (i = 0; i < dtabufcount; i++) buf->orderidx[i] = i;
|
|
|
831 |
qsort(buf->orderidx, dtabufcount, 2, &sortcmp);
|
1716 |
mateusz.vi |
832 |
|
1719 |
mateusz.vi |
833 |
/* preload first entry (last from orderidx, since entries are sorted in reverse) */
|
1716 |
mateusz.vi |
834 |
dtabufcount--;
|
1719 |
mateusz.vi |
835 |
_fmemcpy(((unsigned char *)dta) + 22, &(dtabuf[buf->orderidx[dtabufcount]]), sizeof(struct TINYDTA));
|
|
|
836 |
dta->attr = dtabuf[buf->orderidx[dtabufcount]].time_sec2; /* restore attr from the abused time_sec2 field */
|
1716 |
mateusz.vi |
837 |
}
|
|
|
838 |
|
420 |
mateuszvis |
839 |
wcolcount = 0; /* may be used for columns counting with wide mode */
|
396 |
mateuszvis |
840 |
|
1716 |
mateusz.vi |
841 |
for (;;) {
|
542 |
mateuszvis |
842 |
|
1716 |
mateusz.vi |
843 |
/* filter out attributes (skip if entry comes from buffer, then it was already veted) */
|
1741 |
mateusz.vi |
844 |
if (filter_attribs(dta, req.attrfilter_must, req.attrfilter_may) == 0) goto NEXT_ENTRY;
|
542 |
mateuszvis |
845 |
|
2207 |
mateusz.vi |
846 |
/* /B hides . and .. entries */
|
|
|
847 |
if ((req.format == DIR_OUTPUT_BARE) && (dta->fname[0] == '.')) continue;
|
|
|
848 |
|
542 |
mateuszvis |
849 |
/* turn string lcase (/L) */
|
1724 |
mateusz.vi |
850 |
if (req.flags & DIR_FLAG_LCASE) _strlwr(dta->fname); /* OpenWatcom extension, probably does not care about NLS so results may be odd with non-A-Z characters... */
|
368 |
mateuszvis |
851 |
|
424 |
mateuszvis |
852 |
summary_fcount++;
|
|
|
853 |
if ((dta->attr & DOS_ATTR_DIR) == 0) summary_totsz += dta->size;
|
|
|
854 |
|
1724 |
mateusz.vi |
855 |
switch (req.format) {
|
420 |
mateuszvis |
856 |
case DIR_OUTPUT_NORM:
|
|
|
857 |
/* print fname-space-extension (unless it's "." or "..", then print as-is) */
|
|
|
858 |
if (dta->fname[0] == '.') {
|
|
|
859 |
output(dta->fname);
|
|
|
860 |
i = strlen(dta->fname);
|
|
|
861 |
while (i++ < 12) output(" ");
|
|
|
862 |
} else {
|
1717 |
mateusz.vi |
863 |
file_fname2fcb(buf->buff64, dta->fname);
|
|
|
864 |
memmove(buf->buff64 + 9, buf->buff64 + 8, 4);
|
|
|
865 |
buf->buff64[8] = ' ';
|
|
|
866 |
output(buf->buff64);
|
420 |
mateuszvis |
867 |
}
|
|
|
868 |
output(" ");
|
1960 |
mateusz.vi |
869 |
/* either <DIR> or right aligned 13 or 10 chars byte size, depending
|
|
|
870 |
* on the presence of a thousands delimiter (max 2'000'000'000) */
|
|
|
871 |
{
|
|
|
872 |
unsigned short szlen = 10 + (strlen(buf->nls.thousep) * 3);
|
|
|
873 |
memset(buf->buff64, ' ', 16);
|
|
|
874 |
if (dta->attr & DOS_ATTR_DIR) {
|
|
|
875 |
strcpy(buf->buff64 + szlen, svarlang_str(37,21));
|
|
|
876 |
} else {
|
|
|
877 |
nls_format_number(buf->buff64 + 12, dta->size, &(buf->nls));
|
|
|
878 |
}
|
|
|
879 |
output(buf->buff64 + strlen(buf->buff64) - szlen);
|
420 |
mateuszvis |
880 |
}
|
1960 |
mateusz.vi |
881 |
/* one spaces and NLS DATE */
|
1717 |
mateusz.vi |
882 |
buf->buff64[0] = ' ';
|
1141 |
mateusz.vi |
883 |
if (screenw >= 80) {
|
1960 |
mateusz.vi |
884 |
nls_format_date(buf->buff64 + 1, dta->date_yr + 1980, dta->date_mo, dta->date_dy, &(buf->nls));
|
1141 |
mateusz.vi |
885 |
} else {
|
1960 |
mateusz.vi |
886 |
nls_format_date(buf->buff64 + 1, (dta->date_yr + 80) % 100, dta->date_mo, dta->date_dy, &(buf->nls));
|
1141 |
mateusz.vi |
887 |
}
|
1717 |
mateusz.vi |
888 |
output(buf->buff64);
|
420 |
mateuszvis |
889 |
|
|
|
890 |
/* one space and NLS TIME */
|
1717 |
mateusz.vi |
891 |
nls_format_time(buf->buff64 + 1, dta->time_hour, dta->time_min, 0xff, &(buf->nls));
|
|
|
892 |
outputnl(buf->buff64);
|
420 |
mateuszvis |
893 |
break;
|
|
|
894 |
|
|
|
895 |
case DIR_OUTPUT_WIDE: /* display in columns of 12 chars per item */
|
|
|
896 |
i = strlen(dta->fname);
|
|
|
897 |
if (dta->attr & DOS_ATTR_DIR) {
|
|
|
898 |
i += 2;
|
|
|
899 |
output("[");
|
|
|
900 |
output(dta->fname);
|
|
|
901 |
output("]");
|
|
|
902 |
} else {
|
|
|
903 |
output(dta->fname);
|
|
|
904 |
}
|
|
|
905 |
while (i++ < WCOLWIDTH) output(" ");
|
|
|
906 |
if (++wcolcount == wcols) {
|
|
|
907 |
wcolcount = 0;
|
|
|
908 |
outputnl("");
|
528 |
mateuszvis |
909 |
} else {
|
|
|
910 |
availrows++; /* wide mode is the only one that does not write one line per file */
|
420 |
mateuszvis |
911 |
}
|
|
|
912 |
break;
|
|
|
913 |
|
|
|
914 |
case DIR_OUTPUT_BARE:
|
2206 |
mateusz.vi |
915 |
/* if /B used in combination with /S then files are displayed with full path */
|
|
|
916 |
if (req.flags & DIR_FLAG_RECUR) dir_print_dirprefix(buf->path);
|
420 |
mateuszvis |
917 |
outputnl(dta->fname);
|
|
|
918 |
break;
|
396 |
mateuszvis |
919 |
}
|
368 |
mateuszvis |
920 |
|
1724 |
mateusz.vi |
921 |
if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
|
420 |
mateuszvis |
922 |
|
1741 |
mateusz.vi |
923 |
NEXT_ENTRY:
|
1716 |
mateusz.vi |
924 |
/* take next entry, either from buf or disk */
|
|
|
925 |
if (dtabufcount > 0) {
|
|
|
926 |
dtabufcount--;
|
1719 |
mateusz.vi |
927 |
_fmemcpy(((unsigned char *)dta) + 22, &(dtabuf[buf->orderidx[dtabufcount]]), sizeof(struct TINYDTA));
|
|
|
928 |
dta->attr = dtabuf[buf->orderidx[dtabufcount]].time_sec2; /* restore attr from the abused time_sec2 field */
|
1716 |
mateusz.vi |
929 |
} else {
|
|
|
930 |
if (findnext(dta) != 0) break;
|
|
|
931 |
}
|
420 |
mateuszvis |
932 |
|
1716 |
mateusz.vi |
933 |
}
|
|
|
934 |
|
528 |
mateuszvis |
935 |
if (wcolcount != 0) {
|
|
|
936 |
outputnl(""); /* in wide mode make sure to end on a clear row */
|
1724 |
mateusz.vi |
937 |
if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
|
528 |
mateuszvis |
938 |
}
|
420 |
mateuszvis |
939 |
|
424 |
mateuszvis |
940 |
/* print out summary (unless bare output mode) */
|
1724 |
mateusz.vi |
941 |
if (req.format != DIR_OUTPUT_BARE) {
|
2201 |
mateusz.vi |
942 |
dir_print_summary_files(buf->buff64, uint32maxlen, summary_totsz, summary_fcount, &availrows, req.flags, &(buf->nls));
|
424 |
mateuszvis |
943 |
}
|
|
|
944 |
|
2201 |
mateusz.vi |
945 |
/* update global counters in case /s is used */
|
|
|
946 |
summary_recurs_fcount += summary_fcount;
|
|
|
947 |
summary_recurs_totsz += summary_totsz;
|
|
|
948 |
|
2193 |
mateusz.vi |
949 |
/* /S processing */
|
2197 |
mateusz.vi |
950 |
CHECK_RECURS:
|
|
|
951 |
/* if /S then look for a subdir */
|
|
|
952 |
if (req.flags & DIR_FLAG_RECUR) {
|
|
|
953 |
/* do the findfirst on *.* instead of reusing the user filter */
|
|
|
954 |
char *s;
|
|
|
955 |
char backup[4];
|
2200 |
mateusz.vi |
956 |
//printf("orig path='%s' new=", buf->path);
|
2197 |
mateusz.vi |
957 |
for (s = buf->path; *s != 0; s++);
|
|
|
958 |
for (; s[-1] != '\\'; s--);
|
|
|
959 |
memcpy_ltr(backup, s, 4);
|
|
|
960 |
memcpy_ltr(s, "*.*", 4);
|
2200 |
mateusz.vi |
961 |
//printf("'%s'\n", buf->path);
|
2197 |
mateusz.vi |
962 |
if (findfirst(dta, buf->path, DOS_ATTR_DIR) == 0) {
|
|
|
963 |
memcpy_ltr(s, backup, 4);
|
|
|
964 |
for (;;) {
|
|
|
965 |
if ((dta->fname[0] != '.') && (dta->attr & DOS_ATTR_DIR)) break;
|
|
|
966 |
if (findnext(dta) != 0) goto NOSUBDIR;
|
|
|
967 |
}
|
2200 |
mateusz.vi |
968 |
//printf("GOT DIR (/S): '%s'\n", dta->fname);
|
2197 |
mateusz.vi |
969 |
/* add dir to path and redo scan */
|
|
|
970 |
memcpy_ltr(&(buf->dtastack[buf->dtastacklen]), dta, sizeof(struct DTA));
|
|
|
971 |
buf->dtastacklen++;
|
|
|
972 |
path_add(buf->path, dta->fname);
|
|
|
973 |
goto NEXT_ITER;
|
|
|
974 |
}
|
|
|
975 |
memcpy_ltr(s, backup, 4);
|
2193 |
mateusz.vi |
976 |
}
|
2197 |
mateusz.vi |
977 |
NOSUBDIR:
|
|
|
978 |
|
2193 |
mateusz.vi |
979 |
while (buf->dtastacklen > 0) {
|
|
|
980 |
/* rewind path one directory back, pop the next dta and do a FindNext */
|
|
|
981 |
path_back(buf->path);
|
|
|
982 |
buf->dtastacklen--;
|
|
|
983 |
TRYNEXTENTRY:
|
|
|
984 |
if (findnext(&(buf->dtastack[buf->dtastacklen])) != 0) continue;
|
|
|
985 |
if ((buf->dtastack[buf->dtastacklen].attr & DOS_ATTR_DIR) == 0) goto TRYNEXTENTRY;
|
2200 |
mateusz.vi |
986 |
if (buf->dtastack[buf->dtastacklen].fname[0] == '.') goto TRYNEXTENTRY;
|
2193 |
mateusz.vi |
987 |
/* something found -> add dir to path and redo scan */
|
|
|
988 |
path_add(buf->path, buf->dtastack[buf->dtastacklen].fname);
|
|
|
989 |
goto NEXT_ITER;
|
|
|
990 |
}
|
|
|
991 |
|
2200 |
mateusz.vi |
992 |
/* print out disk space available (unless bare output mode) */
|
|
|
993 |
if (req.format != DIR_OUTPUT_BARE) {
|
2201 |
mateusz.vi |
994 |
/* if /s mode then print also global stats */
|
|
|
995 |
if (req.flags & DIR_FLAG_RECUR) {
|
2205 |
mateusz.vi |
996 |
if (summary_recurs_fcount == 0) {
|
|
|
997 |
file_truename(req.filespecptr, buf->path);
|
|
|
998 |
dir_print_dirof(buf->path, &availrows, req.flags & DIR_FLAG_PAUSE);
|
|
|
999 |
nls_outputnl_doserr(2); /* "File not found" */
|
|
|
1000 |
goto GAMEOVER;
|
|
|
1001 |
} else {
|
|
|
1002 |
outputnl("");
|
|
|
1003 |
if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
|
|
|
1004 |
nls_outputnl(37,25); /* Total files listed: */
|
|
|
1005 |
if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
|
|
|
1006 |
dir_print_summary_files(buf->buff64, uint32maxlen, summary_recurs_totsz, summary_recurs_fcount, &availrows, req.flags, &(buf->nls));
|
|
|
1007 |
}
|
2201 |
mateusz.vi |
1008 |
}
|
2200 |
mateusz.vi |
1009 |
/* xxxx bytes free */
|
|
|
1010 |
i = cmd_dir_df(&summary_totsz, drv);
|
|
|
1011 |
if (i != 0) nls_outputnl_doserr(i);
|
|
|
1012 |
memset(buf->buff64, ' ', summary_alignpos + 8 + uint32maxlen); /* align the freebytes value to same column as totbytes */
|
|
|
1013 |
i = nls_format_number(buf->buff64 + summary_alignpos + 8 + uint32maxlen, summary_totsz, &(buf->nls));
|
|
|
1014 |
output(buf->buff64 + i + 1);
|
|
|
1015 |
output(" ");
|
|
|
1016 |
nls_outputnl(37,24); /* "bytes free" */
|
|
|
1017 |
if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
|
|
|
1018 |
}
|
|
|
1019 |
|
2205 |
mateusz.vi |
1020 |
GAMEOVER:
|
|
|
1021 |
|
1716 |
mateusz.vi |
1022 |
/* free the buffer memory (if used) */
|
1719 |
mateusz.vi |
1023 |
if (glob_sortcmp_dat.dtabuf_root != NULL) _ffree(glob_sortcmp_dat.dtabuf_root);
|
1716 |
mateusz.vi |
1024 |
|
2193 |
mateusz.vi |
1025 |
free(buf);
|
533 |
mateuszvis |
1026 |
return(CMD_OK);
|
368 |
mateuszvis |
1027 |
}
|