Subversion Repositories SvarDOS

Rev

Rev 2242 | 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
 
2243 mateusz.vi 651
  /* reserve buf space within the upstream-supplied buffer */
652
  if (sizeof(*buf) > p->BUFFERSZ) {
2193 mateusz.vi 653
    nls_output_err(255, 8); /* insufficient memory */
2205 mateusz.vi 654
    goto GAMEOVER;
2193 mateusz.vi 655
  }
2243 mateusz.vi 656
  buf = (void *)(p->BUFFER);
657
  sv_bzero(buf, sizeof(*buf));
2193 mateusz.vi 658
 
1739 mateusz.vi 659
  /* zero out glob_sortcmp_dat and init the collation table */
2213 mateusz.vi 660
  sv_bzero(&glob_sortcmp_dat, sizeof(glob_sortcmp_dat));
1739 mateusz.vi 661
  for (i = 0; i < 256; i++) {
662
    glob_sortcmp_dat.sortownia[i] = i;
663
    /* sorting should be case-insensitive */
1740 mateusz.vi 664
    if ((i >= 'A') && (i <= 'Z')) glob_sortcmp_dat.sortownia[i] |= 32;
1739 mateusz.vi 665
  }
666
 
1743 mateusz.vi 667
  /* try to replace (or complement) my naive collation table with an NLS-aware
1744 mateusz.vi 668
   * version provided by the kernel (or NLSFUNC)
1745 mateusz.vi 669
   * see https://github.com/SvarDOS/bugz/issues/68 for some thoughts */
670
  {
1743 mateusz.vi 671
    _Packed struct nlsseqtab {
672
      unsigned char id;
673
      unsigned short taboff;
674
      unsigned short tabseg;
675
    } collat;
676
    void *colptr = &collat;
677
    unsigned char errflag = 1;
678
    _asm {
679
      push ax
680
      push bx
681
      push cx
682
      push dx
683
      push di
684
      push es
685
 
686
      mov ax, 0x6506  /* DOS 3.3+ - Get collating sequence table */
687
      mov bx, 0xffff  /* code page, FFFFh = "current" */
688
      mov cx, 5       /* size of buffer at ES:DI */
689
      mov dx, 0xffff  /* country id, FFFFh = "current" */
690
      push ds
691
      pop es          /* ES:DI = address of buffer for the 5-bytes struct */
692
      mov di, colptr
693
      int 0x21
694
      jc FAIL
695
      xor al, al
696
      mov errflag, al
697
      FAIL:
698
 
699
      pop es
700
      pop di
701
      pop dx
702
      pop cx
703
      pop bx
704
      pop ax
705
    }
706
 
707
    if ((errflag == 0) && (collat.id == 6)) {
708
      unsigned char far *ptr = MK_FP(collat.tabseg, collat.taboff);
709
      unsigned short count = *(unsigned short far *)ptr;
1745 mateusz.vi 710
#ifdef DIR_DUMPNLSCOLLATE
711
      printf("NLS AT %04X:%04X (%u elements)\n", collat.tabseg, collat.taboff, count);
712
#endif
1743 mateusz.vi 713
      if (count <= 256) { /* you never know */
714
        ptr += 2; /* skip the count header */
715
        for (i = 0; i < count; i++) {
716
          glob_sortcmp_dat.sortownia[i] = ptr[i];
1745 mateusz.vi 717
#ifdef DIR_DUMPNLSCOLLATE
718
          printf(" %03u", ptr[i]);
719
          if ((i & 15) == 15) {
720
            printf("\n");
721
            fflush(stdout);
722
          }
723
#endif
1743 mateusz.vi 724
        }
725
      }
726
    }
727
  }
728
 
1724 mateusz.vi 729
  i = nls_getpatterns(&(buf->nls));
730
  if (i != 0) nls_outputnl_doserr(i);
731
 
732
  /* disable usage of thousands separator on narrow screens */
733
  if (screenw < 80) buf->nls.thousep[0] = 0;
734
 
1725 mateusz.vi 735
  /*** PARSING COMMAND LINE STARTS *******************************************/
736
 
737
  /* init req with some defaults */
2213 mateusz.vi 738
  sv_bzero(&req, sizeof(req));
1725 mateusz.vi 739
  req.attrfilter_may = DIR_ATTR_DEFAULT;
740
  req.format = DIR_OUTPUT_NORM;
741
 
742
  /* process DIRCMD first (so it can be overidden by user's cmdline) */
743
  {
744
  const char far *dircmd = env_lookup_val(p->env_seg, "DIRCMD");
745
  if (dircmd != NULL) {
746
    const char *argvptrs[32];
747
    cmd_explode(buf->buff64, dircmd, argvptrs);
748
    if ((dir_parse_cmdline(&req, argvptrs) != 0) || (req.filespecptr != NULL)) {
749
      nls_output(255, 10);/* bad environment */
750
      output(" - ");
751
      outputnl("DIRCMD");
2205 mateusz.vi 752
      goto GAMEOVER;
1725 mateusz.vi 753
    }
754
  }
755
  }
756
 
757
  /* parse user's command line */
2205 mateusz.vi 758
  if (dir_parse_cmdline(&req, p->argv) != 0) goto GAMEOVER;
1724 mateusz.vi 759
 
2193 mateusz.vi 760
  /*** PARSING COMMAND LINE DONE *********************************************/
761
 
1725 mateusz.vi 762
  /* if no filespec provided, then it's about the current directory */
763
  if (req.filespecptr == NULL) req.filespecptr = ".";
764
 
2202 mateusz.vi 765
  availrows = screen_getheight() - 1;
528 mateuszvis 766
 
417 mateuszvis 767
  /* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
1724 mateusz.vi 768
  if ((req.filespecptr[0] != 0) && (req.filespecptr[1] == ':') && (req.filespecptr[2] == 0)) {
769
    if ((req.filespecptr[0] >= 'a') && (req.filespecptr[0] <= 'z')) {
770
      buf->path[0] = req.filespecptr[0] - ('a' - 1);
417 mateuszvis 771
    } else {
1724 mateusz.vi 772
      buf->path[0] = req.filespecptr[0] - ('A' - 1);
399 mateuszvis 773
    }
1717 mateusz.vi 774
    i = curpathfordrv(buf->path, buf->path[0]);
417 mateuszvis 775
  } else {
1724 mateusz.vi 776
    i = file_truename(req.filespecptr, buf->path);
399 mateuszvis 777
  }
417 mateuszvis 778
  if (i != 0) {
538 mateuszvis 779
    nls_outputnl_doserr(i);
2205 mateusz.vi 780
    goto GAMEOVER;
417 mateuszvis 781
  }
393 mateuszvis 782
 
2198 mateusz.vi 783
  /* volume label and serial */
1724 mateusz.vi 784
  if (req.format != DIR_OUTPUT_BARE) {
1717 mateusz.vi 785
    drv = buf->path[0];
399 mateuszvis 786
    if (drv >= 'a') {
787
      drv -= 'a';
788
    } else {
789
      drv -= 'A';
790
    }
1717 mateusz.vi 791
    cmd_vol_internal(drv, buf->buff64);
2202 mateusz.vi 792
    availrows -= 2;
2198 mateusz.vi 793
  }
794
 
795
  NEXT_ITER: /* re-entry point for /S recursing */
796
 
2201 mateusz.vi 797
  summary_fcount = 0;
798
  summary_totsz = 0;
799
 
2203 mateusz.vi 800
  /* if dir: append a backslash (also get its len) */
801
  i = path_appendbkslash_if_dir(buf->path);
802
 
803
  /* if ends with a \ then append ????????.??? */
2215 mateusz.vi 804
  if (buf->path[i - 1] == '\\') sv_strcat(buf->path, "????????.???");
2203 mateusz.vi 805
 
806
  /* ask DOS for list of files, but only with allowed attribs */
807
  i = findfirst(dta, buf->path, req.attrfilter_may);
808
 
809
  /* print "directory of" unless /B or /S mode with no match */
810
  if ((req.format != DIR_OUTPUT_BARE) && (((req.flags & DIR_FLAG_RECUR) == 0) || (i == 0))) {
2204 mateusz.vi 811
    dir_print_dirof(buf->path, &availrows, req.flags & DIR_FLAG_PAUSE);
399 mateuszvis 812
  }
813
 
2203 mateusz.vi 814
  /* if no file match then abort */
417 mateuszvis 815
  if (i != 0) {
2197 mateusz.vi 816
    if (req.flags & DIR_FLAG_RECUR) goto CHECK_RECURS;
538 mateuszvis 817
    nls_outputnl_doserr(i);
2205 mateusz.vi 818
    goto GAMEOVER;
417 mateuszvis 819
  }
820
 
1716 mateusz.vi 821
  /* if sorting is involved, then let's buffer all results (and sort them) */
1724 mateusz.vi 822
  if (req.flags & DIR_FLAG_SORT) {
1716 mateusz.vi 823
    /* allocate a memory buffer - try several sizes until one succeeds */
2194 mateusz.vi 824
    unsigned short max_dta_bufcount;
825
 
826
    /* compute the amount of DTAs I can buffer */
827
    for (max_dta_bufcount = MAX_SORTABLE_FILES; max_dta_bufcount != 0; max_dta_bufcount /= 2) {
2230 mateusz.vi 828
      dtabuf = cmd_dir_farmalloc(max_dta_bufcount * sizeof(struct TINYDTA) / 16);
1716 mateusz.vi 829
      if (dtabuf != NULL) break;
830
    }
2194 mateusz.vi 831
    /* printf("max_dta_bufcount = %u\n", max_dta_bufcount); */
1716 mateusz.vi 832
 
833
    if (dtabuf == NULL) {
834
      nls_outputnl_doserr(8); /* out of memory */
2205 mateusz.vi 835
      goto GAMEOVER;
1716 mateusz.vi 836
    }
837
 
838
    /* remember the address so I can free it afterwards */
1719 mateusz.vi 839
    glob_sortcmp_dat.dtabuf_root = dtabuf;
1716 mateusz.vi 840
 
841
    do {
842
      /* filter out files with uninteresting attributes */
1724 mateusz.vi 843
      if (filter_attribs(dta, req.attrfilter_must, req.attrfilter_may) == 0) continue;
1716 mateusz.vi 844
 
2207 mateusz.vi 845
      /* /B hides . and .. entries */
846
      if ((req.format == DIR_OUTPUT_BARE) && (dta->fname[0] == '.')) continue;
847
 
1719 mateusz.vi 848
      /* normalize "size" of directories to zero because kernel returns garbage
849
       * sizes for directories which might confuse the sorting routine later */
850
      if (dta->attr & DOS_ATTR_DIR) dta->size = 0;
851
 
2217 mateusz.vi 852
      memcpy_ltr_far(&(dtabuf[dtabufcount]), ((char *)dta) + 22, sizeof(struct TINYDTA));
1716 mateusz.vi 853
 
854
      /* save attribs in sec field, otherwise zero it (this field is not
855
       * displayed and dropping the attr field saves 2 bytes per entry) */
856
      dtabuf[dtabufcount++].time_sec2 = (dta->attr & 31);
857
 
858
      /* do I have any space left? */
859
      if (dtabufcount == max_dta_bufcount) {
1719 mateusz.vi 860
        //TODO some kind of user notification might be nice here
1716 mateusz.vi 861
        //outputnl("TOO MANY ENTRIES FOR SORTING! LIST IS UNSORTED");
862
        break;
863
      }
864
 
865
    } while (findnext(dta) == 0);
866
 
1742 mateusz.vi 867
    /* no match? kein gluck! (this can happen when filtering attribs with /A:xxx
868
     * because while findfirst() succeeds, all entries can be rejected) */
869
    if (dtabufcount == 0) {
2207 mateusz.vi 870
      if (req.flags & DIR_FLAG_RECUR) goto CHECK_RECURS;
1742 mateusz.vi 871
      nls_outputnl_doserr(2); /* "File not found" */
2205 mateusz.vi 872
      goto GAMEOVER;
1742 mateusz.vi 873
    }
874
 
2242 mateusz.vi 875
    /* sort the list */
876
    cmd_dir_sort(dtabuf, dtabufcount);
1716 mateusz.vi 877
 
2242 mateusz.vi 878
    /* preload first entry (last, since entries are sorted in reverse) */
1716 mateusz.vi 879
    dtabufcount--;
2242 mateusz.vi 880
    memcpy_ltr_far(((unsigned char *)dta) + 22, dtabuf + dtabufcount, sizeof(struct TINYDTA));
881
    dta->attr = dtabuf[dtabufcount].time_sec2; /* restore attr from the abused time_sec2 field */
1716 mateusz.vi 882
  }
883
 
420 mateuszvis 884
  wcolcount = 0; /* may be used for columns counting with wide mode */
396 mateuszvis 885
 
1716 mateusz.vi 886
  for (;;) {
542 mateuszvis 887
 
1716 mateusz.vi 888
    /* filter out attributes (skip if entry comes from buffer, then it was already veted) */
1741 mateusz.vi 889
    if (filter_attribs(dta, req.attrfilter_must, req.attrfilter_may) == 0) goto NEXT_ENTRY;
542 mateuszvis 890
 
2207 mateusz.vi 891
    /* /B hides . and .. entries */
892
    if ((req.format == DIR_OUTPUT_BARE) && (dta->fname[0] == '.')) continue;
893
 
2218 mateusz.vi 894
    /* turn string lcase (/L) - naive method, only low-ascii */
895
    if (req.flags & DIR_FLAG_LCASE) {
896
      char *s = dta->fname;
897
      while (*s != 0) {
898
        if ((*s >= 'A') && (*s <= 'Z')) *s |= 0x20;
899
        s++;
900
      }
901
    }
368 mateuszvis 902
 
424 mateuszvis 903
    summary_fcount++;
904
    if ((dta->attr & DOS_ATTR_DIR) == 0) summary_totsz += dta->size;
905
 
1724 mateusz.vi 906
    switch (req.format) {
420 mateuszvis 907
      case DIR_OUTPUT_NORM:
908
        /* print fname-space-extension (unless it's "." or "..", then print as-is) */
909
        if (dta->fname[0] == '.') {
910
          output(dta->fname);
2214 mateusz.vi 911
          i = sv_strlen(dta->fname);
420 mateuszvis 912
          while (i++ < 12) output(" ");
913
        } else {
1717 mateusz.vi 914
          file_fname2fcb(buf->buff64, dta->fname);
2214 mateusz.vi 915
          memcpy_rtl(buf->buff64 + 9, buf->buff64 + 8, 4);
1717 mateusz.vi 916
          buf->buff64[8] = ' ';
917
          output(buf->buff64);
420 mateuszvis 918
        }
919
        output(" ");
1960 mateusz.vi 920
        /* either <DIR> or right aligned 13 or 10 chars byte size, depending
921
         * on the presence of a thousands delimiter (max 2'000'000'000) */
922
        {
2214 mateusz.vi 923
          unsigned short szlen = 10 + (sv_strlen(buf->nls.thousep) * 3);
2218 mateusz.vi 924
          sv_memset(buf->buff64, ' ', 16);
1960 mateusz.vi 925
          if (dta->attr & DOS_ATTR_DIR) {
2216 mateusz.vi 926
            sv_strcpy(buf->buff64 + szlen, svarlang_str(37,21));
1960 mateusz.vi 927
          } else {
928
            nls_format_number(buf->buff64 + 12, dta->size, &(buf->nls));
929
          }
2214 mateusz.vi 930
          output(buf->buff64 + sv_strlen(buf->buff64) - szlen);
420 mateuszvis 931
        }
1960 mateusz.vi 932
        /* one spaces and NLS DATE */
1717 mateusz.vi 933
        buf->buff64[0] = ' ';
1141 mateusz.vi 934
        if (screenw >= 80) {
1960 mateusz.vi 935
          nls_format_date(buf->buff64 + 1, dta->date_yr + 1980, dta->date_mo, dta->date_dy, &(buf->nls));
1141 mateusz.vi 936
        } else {
1960 mateusz.vi 937
          nls_format_date(buf->buff64 + 1, (dta->date_yr + 80) % 100, dta->date_mo, dta->date_dy, &(buf->nls));
1141 mateusz.vi 938
        }
1717 mateusz.vi 939
        output(buf->buff64);
420 mateuszvis 940
 
941
        /* one space and NLS TIME */
1717 mateusz.vi 942
        nls_format_time(buf->buff64 + 1, dta->time_hour, dta->time_min, 0xff, &(buf->nls));
943
        outputnl(buf->buff64);
420 mateuszvis 944
        break;
945
 
946
      case DIR_OUTPUT_WIDE: /* display in columns of 12 chars per item */
2214 mateusz.vi 947
        i = sv_strlen(dta->fname);
420 mateuszvis 948
        if (dta->attr & DOS_ATTR_DIR) {
949
          i += 2;
950
          output("[");
951
          output(dta->fname);
952
          output("]");
953
        } else {
954
          output(dta->fname);
955
        }
956
        while (i++ < WCOLWIDTH) output(" ");
957
        if (++wcolcount == wcols) {
958
          wcolcount = 0;
959
          outputnl("");
528 mateuszvis 960
        } else {
961
          availrows++; /* wide mode is the only one that does not write one line per file */
420 mateuszvis 962
        }
963
        break;
964
 
965
      case DIR_OUTPUT_BARE:
2206 mateusz.vi 966
        /* if /B used in combination with /S then files are displayed with full path */
967
        if (req.flags & DIR_FLAG_RECUR) dir_print_dirprefix(buf->path);
420 mateuszvis 968
        outputnl(dta->fname);
969
        break;
396 mateuszvis 970
    }
368 mateuszvis 971
 
1724 mateusz.vi 972
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
420 mateuszvis 973
 
1741 mateusz.vi 974
    NEXT_ENTRY:
1716 mateusz.vi 975
    /* take next entry, either from buf or disk */
976
    if (dtabufcount > 0) {
977
      dtabufcount--;
2242 mateusz.vi 978
      memcpy_ltr_far(((unsigned char *)dta) + 22, dtabuf + dtabufcount, sizeof(struct TINYDTA));
979
      dta->attr = dtabuf[dtabufcount].time_sec2; /* restore attr from the abused time_sec2 field */
1716 mateusz.vi 980
    } else {
981
      if (findnext(dta) != 0) break;
982
    }
420 mateuszvis 983
 
1716 mateusz.vi 984
  }
985
 
528 mateuszvis 986
  if (wcolcount != 0) {
987
    outputnl(""); /* in wide mode make sure to end on a clear row */
1724 mateusz.vi 988
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
528 mateuszvis 989
  }
420 mateuszvis 990
 
424 mateuszvis 991
  /* print out summary (unless bare output mode) */
1724 mateusz.vi 992
  if (req.format != DIR_OUTPUT_BARE) {
2201 mateusz.vi 993
    dir_print_summary_files(buf->buff64, uint32maxlen, summary_totsz, summary_fcount, &availrows, req.flags, &(buf->nls));
424 mateuszvis 994
  }
995
 
2201 mateusz.vi 996
  /* update global counters in case /s is used */
997
  summary_recurs_fcount += summary_fcount;
998
  summary_recurs_totsz += summary_totsz;
999
 
2193 mateusz.vi 1000
  /* /S processing */
2197 mateusz.vi 1001
  CHECK_RECURS:
1002
  /* if /S then look for a subdir */
1003
  if (req.flags & DIR_FLAG_RECUR) {
1004
    /* do the findfirst on *.* instead of reusing the user filter */
1005
    char *s;
1006
    char backup[4];
2200 mateusz.vi 1007
    //printf("orig path='%s' new=", buf->path);
2197 mateusz.vi 1008
    for (s = buf->path; *s != 0; s++);
1009
    for (; s[-1] != '\\'; s--);
1010
    memcpy_ltr(backup, s, 4);
1011
    memcpy_ltr(s, "*.*", 4);
2200 mateusz.vi 1012
    //printf("'%s'\n", buf->path);
2197 mateusz.vi 1013
    if (findfirst(dta, buf->path, DOS_ATTR_DIR) == 0) {
1014
      memcpy_ltr(s, backup, 4);
1015
      for (;;) {
1016
        if ((dta->fname[0] != '.') && (dta->attr & DOS_ATTR_DIR)) break;
1017
        if (findnext(dta) != 0) goto NOSUBDIR;
1018
      }
2200 mateusz.vi 1019
      //printf("GOT DIR (/S): '%s'\n", dta->fname);
2197 mateusz.vi 1020
      /* add dir to path and redo scan */
1021
      memcpy_ltr(&(buf->dtastack[buf->dtastacklen]), dta, sizeof(struct DTA));
1022
      buf->dtastacklen++;
1023
      path_add(buf->path, dta->fname);
1024
      goto NEXT_ITER;
1025
    }
1026
    memcpy_ltr(s, backup, 4);
2193 mateusz.vi 1027
  }
2197 mateusz.vi 1028
  NOSUBDIR:
1029
 
2193 mateusz.vi 1030
  while (buf->dtastacklen > 0) {
1031
    /* rewind path one directory back, pop the next dta and do a FindNext */
1032
    path_back(buf->path);
1033
    buf->dtastacklen--;
1034
    TRYNEXTENTRY:
1035
    if (findnext(&(buf->dtastack[buf->dtastacklen])) != 0) continue;
1036
    if ((buf->dtastack[buf->dtastacklen].attr & DOS_ATTR_DIR) == 0) goto TRYNEXTENTRY;
2200 mateusz.vi 1037
    if (buf->dtastack[buf->dtastacklen].fname[0] == '.') goto TRYNEXTENTRY;
2193 mateusz.vi 1038
    /* something found -> add dir to path and redo scan */
1039
    path_add(buf->path, buf->dtastack[buf->dtastacklen].fname);
1040
    goto NEXT_ITER;
1041
  }
1042
 
2200 mateusz.vi 1043
  /* print out disk space available (unless bare output mode) */
1044
  if (req.format != DIR_OUTPUT_BARE) {
2201 mateusz.vi 1045
    /* if /s mode then print also global stats */
1046
    if (req.flags & DIR_FLAG_RECUR) {
2205 mateusz.vi 1047
      if (summary_recurs_fcount == 0) {
1048
        file_truename(req.filespecptr, buf->path);
1049
        dir_print_dirof(buf->path, &availrows, req.flags & DIR_FLAG_PAUSE);
1050
        nls_outputnl_doserr(2); /* "File not found" */
1051
        goto GAMEOVER;
1052
      } else {
1053
        outputnl("");
1054
        if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
1055
        nls_outputnl(37,25); /* Total files listed: */
1056
        if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
1057
        dir_print_summary_files(buf->buff64, uint32maxlen, summary_recurs_totsz, summary_recurs_fcount, &availrows, req.flags, &(buf->nls));
1058
      }
2201 mateusz.vi 1059
    }
2200 mateusz.vi 1060
    /* xxxx bytes free */
1061
    i = cmd_dir_df(&summary_totsz, drv);
1062
    if (i != 0) nls_outputnl_doserr(i);
2218 mateusz.vi 1063
    sv_memset(buf->buff64, ' ', summary_alignpos + 8 + uint32maxlen); /* align the freebytes value to same column as totbytes */
2200 mateusz.vi 1064
    i = nls_format_number(buf->buff64 + summary_alignpos + 8 + uint32maxlen, summary_totsz, &(buf->nls));
1065
    output(buf->buff64 + i + 1);
1066
    output(" ");
1067
    nls_outputnl(37,24); /* "bytes free" */
1068
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
1069
  }
1070
 
2205 mateusz.vi 1071
  GAMEOVER:
1072
 
1716 mateusz.vi 1073
  /* free the buffer memory (if used) */
2230 mateusz.vi 1074
  if (glob_sortcmp_dat.dtabuf_root != NULL) cmd_dir_farfree(glob_sortcmp_dat.dtabuf_root);
1716 mateusz.vi 1075
 
533 mateuszvis 1076
  return(CMD_OK);
368 mateuszvis 1077
}