Subversion Repositories SvarDOS

Rev

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