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