Subversion Repositories SvarDOS

Rev

Rev 2241 | Rev 2243 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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