Subversion Repositories SvarDOS

Rev

Rev 2230 | 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
 
375
static int sortcmp(const void *dtaid1, const void *dtaid2) {
376
  struct TINYDTA far *dta1 = &(glob_sortcmp_dat.dtabuf_root[*((unsigned short *)dtaid1)]);
377
  struct TINYDTA far *dta2 = &(glob_sortcmp_dat.dtabuf_root[*((unsigned short *)dtaid2)]);
378
  char *ordconf = glob_sortcmp_dat.order;
379
 
380
  /* debug stuff
381
  {
382
    int i;
383
    printf("%lu vs %lu | ", dta1->size, dta2->size);
384
    for (i = 0; dta1->fname[i] != 0; i++) printf("%c", dta1->fname[i]);
385
    printf(" vs ");
386
    for (i = 0; dta2->fname[i] != 0; i++) printf("%c", dta2->fname[i]);
387
    printf("\n");
388
  } */
389
 
390
  for (;;) {
391
    int r = -1;
392
    if (*ordconf & 32) r = 1;
393
 
394
    switch (*ordconf | 32) {
395
      case 'g': /* sort by type (directories first, then files) */
396
        if ((dta1->time_sec2 & DOS_ATTR_DIR) > (dta2->time_sec2 & DOS_ATTR_DIR)) return(0 - r);
397
        if ((dta1->time_sec2 & DOS_ATTR_DIR) < (dta2->time_sec2 & DOS_ATTR_DIR)) return(r);
398
        break;
399
      case ' ': /* default (last resort) sort: by name */
400
      case 'e': /* sort by extension */
401
      case 'n': /* sort by filename */
402
      {
403
        const char far *f1 = dta1->fname;
404
        const char far *f2 = dta2->fname;
405
        int i, limit = 12;
406
        /* special handling for '.' and '..' entries */
407
        if ((f1[0] == '.') && (f2[0] != '.')) return(0 - r);
408
        if ((f2[0] == '.') && (f1[0] != '.')) return(r);
409
 
410
        if ((*ordconf | 32) == 'e') {
411
          /* fast-forward to extension or end of filename */
412
          while ((*f1 != 0) && (*f1 != '.')) f1++;
413
          while ((*f2 != 0) && (*f2 != '.')) f2++;
414
          limit = 4; /* TINYDTA structs are not nul-terminated */
415
        }
416
        /* cmp */
417
        for (i = 0; i < limit; i++) {
1739 mateusz.vi 418
          if ((glob_sortcmp_dat.sortownia[(unsigned char)(*f1)]) < (glob_sortcmp_dat.sortownia[(unsigned char)(*f2)])) return(0 - r);
419
          if ((glob_sortcmp_dat.sortownia[(unsigned char)(*f1)]) > (glob_sortcmp_dat.sortownia[(unsigned char)(*f2)])) return(r);
1719 mateusz.vi 420
          if (*f1 == 0) break;
421
          f1++;
422
          f2++;
423
        }
424
      }
425
        break;
426
      case 's': /* sort by size */
427
        if (dta1->size > dta2->size) return(r);
428
        if (dta1->size < dta2->size) return(0 - r);
429
        break;
430
      case 'd': /* sort by date */
431
        if (dta1->date_yr < dta2->date_yr) return(0 - r);
432
        if (dta1->date_yr > dta2->date_yr) return(r);
433
        if (dta1->date_mo < dta2->date_mo) return(0 - r);
434
        if (dta1->date_mo > dta2->date_mo) return(r);
435
        if (dta1->date_dy < dta2->date_dy) return(0 - r);
436
        if (dta1->date_dy > dta2->date_dy) return(r);
437
        if (dta1->time_hour < dta2->time_hour) return(0 - r);
438
        if (dta1->time_hour > dta2->time_hour) return(r);
439
        if (dta1->time_min < dta2->time_min) return(0 - r);
440
        if (dta1->time_min > dta2->time_min) return(r);
441
        break;
442
    }
443
 
444
    if (*ordconf == 0) break;
445
    ordconf++;
446
  }
447
 
448
  return(0);
449
}
450
 
451
 
542 mateuszvis 452
#define DIR_ATTR_DEFAULT (DOS_ATTR_RO | DOS_ATTR_DIR | DOS_ATTR_ARC)
453
 
1724 mateusz.vi 454
struct dirrequest {
455
  unsigned char attrfilter_may;
456
  unsigned char attrfilter_must;
457
  const char *filespecptr;
420 mateuszvis 458
 
396 mateuszvis 459
  #define DIR_FLAG_PAUSE  1
460
  #define DIR_FLAG_RECUR  4
420 mateuszvis 461
  #define DIR_FLAG_LCASE  8
1719 mateusz.vi 462
  #define DIR_FLAG_SORT  16
1724 mateusz.vi 463
  unsigned char flags;
368 mateuszvis 464
 
420 mateuszvis 465
  #define DIR_OUTPUT_NORM 1
466
  #define DIR_OUTPUT_WIDE 2
467
  #define DIR_OUTPUT_BARE 3
1724 mateusz.vi 468
  unsigned char format;
469
};
420 mateuszvis 470
 
1719 mateusz.vi 471
 
1724 mateusz.vi 472
static int dir_parse_cmdline(struct dirrequest *req, const char **argv) {
473
  for (; *argv != NULL; argv++) {
474
    if (*argv[0] == '/') {
475
      const char *arg = *argv + 1;
396 mateuszvis 476
      char neg = 0;
477
      /* detect negations and get actual argument */
542 mateuszvis 478
      if (*arg == '-') {
479
        neg = 1;
480
        arg++;
481
      }
396 mateuszvis 482
      /* */
542 mateuszvis 483
      switch (*arg) {
396 mateuszvis 484
        case 'a':
485
        case 'A':
542 mateuszvis 486
          arg++;
487
          /* preset defaults */
1724 mateusz.vi 488
          req->attrfilter_may = DIR_ATTR_DEFAULT;
489
          req->attrfilter_must = 0;
542 mateuszvis 490
          /* /-A only allowed without further parameters (used to cancel possible previous /Asmth) */
491
          if (neg) {
492
            if (*arg != 0) {
493
              nls_outputnl_err(0, 2); /* invalid switch */
1724 mateusz.vi 494
              return(-1);
542 mateuszvis 495
            }
496
          } else {
1085 mateusz.vi 497
            /* skip colon if present */
498
            if (*arg == ':') arg++;
542 mateuszvis 499
            /* start with "allow everything" */
1724 mateusz.vi 500
            req->attrfilter_may = (DOS_ATTR_ARC | DOS_ATTR_DIR | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_RO);
501
            if (dir_parse_attr_list(arg, &(req->attrfilter_may), &(req->attrfilter_must)) != 0) {
542 mateuszvis 502
              nls_outputnl_err(0, 3); /* invalid parameter format */
1724 mateusz.vi 503
              return(-1);
542 mateuszvis 504
            }
505
          }
396 mateuszvis 506
          break;
399 mateuszvis 507
        case 'b':
508
        case 'B':
1724 mateusz.vi 509
          req->format = DIR_OUTPUT_BARE;
399 mateuszvis 510
          break;
421 mateuszvis 511
        case 'l':
512
        case 'L':
1724 mateusz.vi 513
          req->flags |= DIR_FLAG_LCASE;
420 mateuszvis 514
          break;
421 mateuszvis 515
        case 'o':
516
        case 'O':
1720 mateusz.vi 517
          if (neg) {
1724 mateusz.vi 518
            req->flags &= (0xff ^ DIR_FLAG_SORT);
1720 mateusz.vi 519
            break;
520
          }
1724 mateusz.vi 521
          if (dir_process_order_directive(arg+1) != 0) {
1719 mateusz.vi 522
            nls_output_err(0, 3); /* invalid parameter format */
523
            output(": ");
524
            outputnl(arg);
1724 mateusz.vi 525
            return(-1);
1719 mateusz.vi 526
          }
1724 mateusz.vi 527
          req->flags |= DIR_FLAG_SORT;
421 mateuszvis 528
          break;
396 mateuszvis 529
        case 'p':
530
        case 'P':
1724 mateusz.vi 531
          req->flags |= DIR_FLAG_PAUSE;
532
          if (neg) req->flags &= (0xff ^ DIR_FLAG_PAUSE);
396 mateuszvis 533
          break;
421 mateuszvis 534
        case 's':
535
        case 'S':
2193 mateusz.vi 536
          req->flags |= DIR_FLAG_RECUR;
420 mateuszvis 537
          break;
421 mateuszvis 538
        case 'w':
539
        case 'W':
1724 mateusz.vi 540
          req->format = DIR_OUTPUT_WIDE;
421 mateuszvis 541
          break;
393 mateuszvis 542
        default:
542 mateuszvis 543
          nls_outputnl_err(0, 2); /* invalid switch */
1724 mateusz.vi 544
          return(-1);
393 mateuszvis 545
      }
546
    } else {  /* filespec */
1724 mateusz.vi 547
      if (req->filespecptr != NULL) {
542 mateuszvis 548
        nls_outputnl_err(0, 4); /* too many parameters */
1724 mateusz.vi 549
        return(-1);
393 mateuszvis 550
      }
1724 mateusz.vi 551
      req->filespecptr = *argv;
393 mateuszvis 552
    }
553
  }
368 mateuszvis 554
 
1724 mateusz.vi 555
  return(0);
556
}
393 mateuszvis 557
 
1724 mateusz.vi 558
 
2201 mateusz.vi 559
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) {
560
  unsigned short i;
2225 mateusz.vi 561
 
2201 mateusz.vi 562
  /* x file(s) (maximum of files in a FAT-32 directory is 65'535) */
2218 mateusz.vi 563
  sv_memset(buff64, ' ', 8);
2225 mateusz.vi 564
  buff64[8] = 0;
2201 mateusz.vi 565
  i = nls_format_number(buff64 + 8, summary_fcount, nls);
566
  output(buff64 + i);
2225 mateusz.vi 567
  output(" ");
568
  output(svarlang_str(37,22)); /* "file(s)" */
569
  output(" ");
570
 
2201 mateusz.vi 571
  /* xxxx bytes */
2218 mateusz.vi 572
  sv_memset(buff64, ' ', 14);
2201 mateusz.vi 573
  i = nls_format_number(buff64 + uint32maxlen, summary_totsz, nls);
574
  output(buff64 + i + 1);
575
  output(" ");
576
  nls_outputnl(37,23); /* "bytes" */
577
  if (flags & DIR_FLAG_PAUSE) dir_pagination(availrows);
578
}
579
 
580
 
2241 mateusz.vi 581
/* max amount of files to sort - limited by the memory block I will allocate
582
 * to store the TINYDTA of each entry */
583
#define MAX_SORTABLE_FILES (65500 / sizeof(struct TINYDTA))
2193 mateusz.vi 584
 
1724 mateusz.vi 585
static enum cmd_result cmd_dir(struct cmd_funcparam *p) {
586
  struct DTA *dta = (void *)0x80; /* set DTA to its default location at 80h in PSP */
587
  struct TINYDTA far *dtabuf = NULL; /* used to buffer results when sorting is enabled */
588
  unsigned short dtabufcount = 0;
589
  unsigned short i;
590
  unsigned short availrows;  /* counter of available rows on display (used for /P) */
591
  unsigned short screenw = screen_getwidth();
592
  unsigned short wcols = screenw / WCOLWIDTH; /* number of columns in wide mode */
593
  unsigned char wcolcount;
594
  struct {
595
    struct nls_patterns nls;
596
    char buff64[64];
597
    char path[128];
2193 mateusz.vi 598
    struct DTA dtastack[64]; /* used for /S, max number of subdirs in DOS5 is 42 (A/B/C/...) */
599
    unsigned char dtastacklen;
600
    unsigned short orderidx[MAX_SORTABLE_FILES / sizeof(struct TINYDTA)];
601
  } *buf;
2201 mateusz.vi 602
  unsigned long summary_recurs_fcount = 0; /* used for /s global summary */
603
  unsigned long summary_recurs_totsz = 0;  /* used for /s global summary */
604
  unsigned long summary_fcount;
605
  unsigned long summary_totsz;
1724 mateusz.vi 606
  unsigned char drv = 0;
607
  struct dirrequest req;
2214 mateusz.vi 608
  unsigned short summary_alignpos = sv_strlen(svarlang_str(37,22)) + 2;
2200 mateusz.vi 609
  unsigned short uint32maxlen = 14; /* 13 is the max len of a 32 bit number with thousand separators (4'000'000'000) */
610
  if (screenw < 80) uint32maxlen = 10;
1724 mateusz.vi 611
 
612
  if (cmd_ishlp(p)) {
613
    nls_outputnl(37,0); /* "Displays a list of files and subdirectories in a directory" */
614
    outputnl("");
615
    nls_outputnl(37,1); /* "DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]" */
616
    outputnl("");
617
    nls_outputnl(37,2); /* "/P Pauses after each screenful of information" */
618
    nls_outputnl(37,3); /* "/W Uses wide list format" */
619
    outputnl("");
620
    nls_outputnl(37,4); /* "/A Displays files with specified attributes:" */
621
    nls_outputnl(37,5); /* "    D Directories            R Read-only files        H Hidden files" */
622
    nls_outputnl(37,6); /* "    A Ready for archiving    S System files           - prefix meaning "not"" */
623
    outputnl("");
624
    nls_outputnl(37,7); /* "/O List files in sorted order:" */
625
    nls_outputnl(37,8); /* "    N by name                S by size                E by extension" */
626
    nls_outputnl(37,9); /* "    D by date                G group dirs first       - prefix to reverse order" */
627
    outputnl("");
628
    nls_outputnl(37,10); /* "/S Displays files in specified directory and all subdirectories" */
629
    nls_outputnl(37,11); /* "/B Uses bare format (no heading information or summary)" */
630
    nls_outputnl(37,12); /* "/L Uses lowercases" */
2205 mateusz.vi 631
    goto GAMEOVER;
1724 mateusz.vi 632
  }
633
 
2193 mateusz.vi 634
  /* allocate buf */
635
  buf = calloc(sizeof(*buf), 1);
636
  if (buf == NULL) {
637
    nls_output_err(255, 8); /* insufficient memory */
2205 mateusz.vi 638
    goto GAMEOVER;
2193 mateusz.vi 639
  }
640
 
1739 mateusz.vi 641
  /* zero out glob_sortcmp_dat and init the collation table */
2213 mateusz.vi 642
  sv_bzero(&glob_sortcmp_dat, sizeof(glob_sortcmp_dat));
1739 mateusz.vi 643
  for (i = 0; i < 256; i++) {
644
    glob_sortcmp_dat.sortownia[i] = i;
645
    /* sorting should be case-insensitive */
1740 mateusz.vi 646
    if ((i >= 'A') && (i <= 'Z')) glob_sortcmp_dat.sortownia[i] |= 32;
1739 mateusz.vi 647
  }
648
 
1743 mateusz.vi 649
  /* try to replace (or complement) my naive collation table with an NLS-aware
1744 mateusz.vi 650
   * version provided by the kernel (or NLSFUNC)
1745 mateusz.vi 651
   * see https://github.com/SvarDOS/bugz/issues/68 for some thoughts */
652
  {
1743 mateusz.vi 653
    _Packed struct nlsseqtab {
654
      unsigned char id;
655
      unsigned short taboff;
656
      unsigned short tabseg;
657
    } collat;
658
    void *colptr = &collat;
659
    unsigned char errflag = 1;
660
    _asm {
661
      push ax
662
      push bx
663
      push cx
664
      push dx
665
      push di
666
      push es
667
 
668
      mov ax, 0x6506  /* DOS 3.3+ - Get collating sequence table */
669
      mov bx, 0xffff  /* code page, FFFFh = "current" */
670
      mov cx, 5       /* size of buffer at ES:DI */
671
      mov dx, 0xffff  /* country id, FFFFh = "current" */
672
      push ds
673
      pop es          /* ES:DI = address of buffer for the 5-bytes struct */
674
      mov di, colptr
675
      int 0x21
676
      jc FAIL
677
      xor al, al
678
      mov errflag, al
679
      FAIL:
680
 
681
      pop es
682
      pop di
683
      pop dx
684
      pop cx
685
      pop bx
686
      pop ax
687
    }
688
 
689
    if ((errflag == 0) && (collat.id == 6)) {
690
      unsigned char far *ptr = MK_FP(collat.tabseg, collat.taboff);
691
      unsigned short count = *(unsigned short far *)ptr;
1745 mateusz.vi 692
#ifdef DIR_DUMPNLSCOLLATE
693
      printf("NLS AT %04X:%04X (%u elements)\n", collat.tabseg, collat.taboff, count);
694
#endif
1743 mateusz.vi 695
      if (count <= 256) { /* you never know */
696
        ptr += 2; /* skip the count header */
697
        for (i = 0; i < count; i++) {
698
          glob_sortcmp_dat.sortownia[i] = ptr[i];
1745 mateusz.vi 699
#ifdef DIR_DUMPNLSCOLLATE
700
          printf(" %03u", ptr[i]);
701
          if ((i & 15) == 15) {
702
            printf("\n");
703
            fflush(stdout);
704
          }
705
#endif
1743 mateusz.vi 706
        }
707
      }
708
    }
709
  }
710
 
1724 mateusz.vi 711
  i = nls_getpatterns(&(buf->nls));
712
  if (i != 0) nls_outputnl_doserr(i);
713
 
714
  /* disable usage of thousands separator on narrow screens */
715
  if (screenw < 80) buf->nls.thousep[0] = 0;
716
 
1725 mateusz.vi 717
  /*** PARSING COMMAND LINE STARTS *******************************************/
718
 
719
  /* init req with some defaults */
2213 mateusz.vi 720
  sv_bzero(&req, sizeof(req));
1725 mateusz.vi 721
  req.attrfilter_may = DIR_ATTR_DEFAULT;
722
  req.format = DIR_OUTPUT_NORM;
723
 
724
  /* process DIRCMD first (so it can be overidden by user's cmdline) */
725
  {
726
  const char far *dircmd = env_lookup_val(p->env_seg, "DIRCMD");
727
  if (dircmd != NULL) {
728
    const char *argvptrs[32];
729
    cmd_explode(buf->buff64, dircmd, argvptrs);
730
    if ((dir_parse_cmdline(&req, argvptrs) != 0) || (req.filespecptr != NULL)) {
731
      nls_output(255, 10);/* bad environment */
732
      output(" - ");
733
      outputnl("DIRCMD");
2205 mateusz.vi 734
      goto GAMEOVER;
1725 mateusz.vi 735
    }
736
  }
737
  }
738
 
739
  /* parse user's command line */
2205 mateusz.vi 740
  if (dir_parse_cmdline(&req, p->argv) != 0) goto GAMEOVER;
1724 mateusz.vi 741
 
2193 mateusz.vi 742
  /*** PARSING COMMAND LINE DONE *********************************************/
743
 
1725 mateusz.vi 744
  /* if no filespec provided, then it's about the current directory */
745
  if (req.filespecptr == NULL) req.filespecptr = ".";
746
 
2202 mateusz.vi 747
  availrows = screen_getheight() - 1;
528 mateuszvis 748
 
417 mateuszvis 749
  /* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
1724 mateusz.vi 750
  if ((req.filespecptr[0] != 0) && (req.filespecptr[1] == ':') && (req.filespecptr[2] == 0)) {
751
    if ((req.filespecptr[0] >= 'a') && (req.filespecptr[0] <= 'z')) {
752
      buf->path[0] = req.filespecptr[0] - ('a' - 1);
417 mateuszvis 753
    } else {
1724 mateusz.vi 754
      buf->path[0] = req.filespecptr[0] - ('A' - 1);
399 mateuszvis 755
    }
1717 mateusz.vi 756
    i = curpathfordrv(buf->path, buf->path[0]);
417 mateuszvis 757
  } else {
1724 mateusz.vi 758
    i = file_truename(req.filespecptr, buf->path);
399 mateuszvis 759
  }
417 mateuszvis 760
  if (i != 0) {
538 mateuszvis 761
    nls_outputnl_doserr(i);
2205 mateusz.vi 762
    goto GAMEOVER;
417 mateuszvis 763
  }
393 mateuszvis 764
 
2198 mateusz.vi 765
  /* volume label and serial */
1724 mateusz.vi 766
  if (req.format != DIR_OUTPUT_BARE) {
1717 mateusz.vi 767
    drv = buf->path[0];
399 mateuszvis 768
    if (drv >= 'a') {
769
      drv -= 'a';
770
    } else {
771
      drv -= 'A';
772
    }
1717 mateusz.vi 773
    cmd_vol_internal(drv, buf->buff64);
2202 mateusz.vi 774
    availrows -= 2;
2198 mateusz.vi 775
  }
776
 
777
  NEXT_ITER: /* re-entry point for /S recursing */
778
 
2201 mateusz.vi 779
  summary_fcount = 0;
780
  summary_totsz = 0;
781
 
2203 mateusz.vi 782
  /* if dir: append a backslash (also get its len) */
783
  i = path_appendbkslash_if_dir(buf->path);
784
 
785
  /* if ends with a \ then append ????????.??? */
2215 mateusz.vi 786
  if (buf->path[i - 1] == '\\') sv_strcat(buf->path, "????????.???");
2203 mateusz.vi 787
 
788
  /* ask DOS for list of files, but only with allowed attribs */
789
  i = findfirst(dta, buf->path, req.attrfilter_may);
790
 
791
  /* print "directory of" unless /B or /S mode with no match */
792
  if ((req.format != DIR_OUTPUT_BARE) && (((req.flags & DIR_FLAG_RECUR) == 0) || (i == 0))) {
2204 mateusz.vi 793
    dir_print_dirof(buf->path, &availrows, req.flags & DIR_FLAG_PAUSE);
399 mateuszvis 794
  }
795
 
2203 mateusz.vi 796
  /* if no file match then abort */
417 mateuszvis 797
  if (i != 0) {
2197 mateusz.vi 798
    if (req.flags & DIR_FLAG_RECUR) goto CHECK_RECURS;
538 mateuszvis 799
    nls_outputnl_doserr(i);
2205 mateusz.vi 800
    goto GAMEOVER;
417 mateuszvis 801
  }
802
 
1716 mateusz.vi 803
  /* if sorting is involved, then let's buffer all results (and sort them) */
1724 mateusz.vi 804
  if (req.flags & DIR_FLAG_SORT) {
1716 mateusz.vi 805
    /* allocate a memory buffer - try several sizes until one succeeds */
2194 mateusz.vi 806
    unsigned short max_dta_bufcount;
807
 
808
    /* compute the amount of DTAs I can buffer */
809
    for (max_dta_bufcount = MAX_SORTABLE_FILES; max_dta_bufcount != 0; max_dta_bufcount /= 2) {
2230 mateusz.vi 810
      dtabuf = cmd_dir_farmalloc(max_dta_bufcount * sizeof(struct TINYDTA) / 16);
1716 mateusz.vi 811
      if (dtabuf != NULL) break;
812
    }
2194 mateusz.vi 813
    /* printf("max_dta_bufcount = %u\n", max_dta_bufcount); */
1716 mateusz.vi 814
 
815
    if (dtabuf == NULL) {
816
      nls_outputnl_doserr(8); /* out of memory */
2205 mateusz.vi 817
      goto GAMEOVER;
1716 mateusz.vi 818
    }
819
 
820
    /* remember the address so I can free it afterwards */
1719 mateusz.vi 821
    glob_sortcmp_dat.dtabuf_root = dtabuf;
1716 mateusz.vi 822
 
823
    do {
824
      /* filter out files with uninteresting attributes */
1724 mateusz.vi 825
      if (filter_attribs(dta, req.attrfilter_must, req.attrfilter_may) == 0) continue;
1716 mateusz.vi 826
 
2207 mateusz.vi 827
      /* /B hides . and .. entries */
828
      if ((req.format == DIR_OUTPUT_BARE) && (dta->fname[0] == '.')) continue;
829
 
1719 mateusz.vi 830
      /* normalize "size" of directories to zero because kernel returns garbage
831
       * sizes for directories which might confuse the sorting routine later */
832
      if (dta->attr & DOS_ATTR_DIR) dta->size = 0;
833
 
2217 mateusz.vi 834
      memcpy_ltr_far(&(dtabuf[dtabufcount]), ((char *)dta) + 22, sizeof(struct TINYDTA));
1716 mateusz.vi 835
 
836
      /* save attribs in sec field, otherwise zero it (this field is not
837
       * displayed and dropping the attr field saves 2 bytes per entry) */
838
      dtabuf[dtabufcount++].time_sec2 = (dta->attr & 31);
839
 
840
      /* do I have any space left? */
841
      if (dtabufcount == max_dta_bufcount) {
1719 mateusz.vi 842
        //TODO some kind of user notification might be nice here
1716 mateusz.vi 843
        //outputnl("TOO MANY ENTRIES FOR SORTING! LIST IS UNSORTED");
844
        break;
845
      }
846
 
847
    } while (findnext(dta) == 0);
848
 
1742 mateusz.vi 849
    /* no match? kein gluck! (this can happen when filtering attribs with /A:xxx
850
     * because while findfirst() succeeds, all entries can be rejected) */
851
    if (dtabufcount == 0) {
2207 mateusz.vi 852
      if (req.flags & DIR_FLAG_RECUR) goto CHECK_RECURS;
1742 mateusz.vi 853
      nls_outputnl_doserr(2); /* "File not found" */
2205 mateusz.vi 854
      goto GAMEOVER;
1742 mateusz.vi 855
    }
856
 
1716 mateusz.vi 857
    /* sort the list - the tricky part is that my array is a far address while
1719 mateusz.vi 858
     * qsort works only with near pointers, so I have to use an ugly (and
859
     * global) auxiliary table */
860
    for (i = 0; i < dtabufcount; i++) buf->orderidx[i] = i;
861
    qsort(buf->orderidx, dtabufcount, 2, &sortcmp);
1716 mateusz.vi 862
 
1719 mateusz.vi 863
    /* preload first entry (last from orderidx, since entries are sorted in reverse) */
1716 mateusz.vi 864
    dtabufcount--;
2217 mateusz.vi 865
    memcpy_ltr_far(((unsigned char *)dta) + 22, &(dtabuf[buf->orderidx[dtabufcount]]), sizeof(struct TINYDTA));
1719 mateusz.vi 866
    dta->attr = dtabuf[buf->orderidx[dtabufcount]].time_sec2; /* restore attr from the abused time_sec2 field */
1716 mateusz.vi 867
  }
868
 
420 mateuszvis 869
  wcolcount = 0; /* may be used for columns counting with wide mode */
396 mateuszvis 870
 
1716 mateusz.vi 871
  for (;;) {
542 mateuszvis 872
 
1716 mateusz.vi 873
    /* filter out attributes (skip if entry comes from buffer, then it was already veted) */
1741 mateusz.vi 874
    if (filter_attribs(dta, req.attrfilter_must, req.attrfilter_may) == 0) goto NEXT_ENTRY;
542 mateuszvis 875
 
2207 mateusz.vi 876
    /* /B hides . and .. entries */
877
    if ((req.format == DIR_OUTPUT_BARE) && (dta->fname[0] == '.')) continue;
878
 
2218 mateusz.vi 879
    /* turn string lcase (/L) - naive method, only low-ascii */
880
    if (req.flags & DIR_FLAG_LCASE) {
881
      char *s = dta->fname;
882
      while (*s != 0) {
883
        if ((*s >= 'A') && (*s <= 'Z')) *s |= 0x20;
884
        s++;
885
      }
886
    }
368 mateuszvis 887
 
424 mateuszvis 888
    summary_fcount++;
889
    if ((dta->attr & DOS_ATTR_DIR) == 0) summary_totsz += dta->size;
890
 
1724 mateusz.vi 891
    switch (req.format) {
420 mateuszvis 892
      case DIR_OUTPUT_NORM:
893
        /* print fname-space-extension (unless it's "." or "..", then print as-is) */
894
        if (dta->fname[0] == '.') {
895
          output(dta->fname);
2214 mateusz.vi 896
          i = sv_strlen(dta->fname);
420 mateuszvis 897
          while (i++ < 12) output(" ");
898
        } else {
1717 mateusz.vi 899
          file_fname2fcb(buf->buff64, dta->fname);
2214 mateusz.vi 900
          memcpy_rtl(buf->buff64 + 9, buf->buff64 + 8, 4);
1717 mateusz.vi 901
          buf->buff64[8] = ' ';
902
          output(buf->buff64);
420 mateuszvis 903
        }
904
        output(" ");
1960 mateusz.vi 905
        /* either <DIR> or right aligned 13 or 10 chars byte size, depending
906
         * on the presence of a thousands delimiter (max 2'000'000'000) */
907
        {
2214 mateusz.vi 908
          unsigned short szlen = 10 + (sv_strlen(buf->nls.thousep) * 3);
2218 mateusz.vi 909
          sv_memset(buf->buff64, ' ', 16);
1960 mateusz.vi 910
          if (dta->attr & DOS_ATTR_DIR) {
2216 mateusz.vi 911
            sv_strcpy(buf->buff64 + szlen, svarlang_str(37,21));
1960 mateusz.vi 912
          } else {
913
            nls_format_number(buf->buff64 + 12, dta->size, &(buf->nls));
914
          }
2214 mateusz.vi 915
          output(buf->buff64 + sv_strlen(buf->buff64) - szlen);
420 mateuszvis 916
        }
1960 mateusz.vi 917
        /* one spaces and NLS DATE */
1717 mateusz.vi 918
        buf->buff64[0] = ' ';
1141 mateusz.vi 919
        if (screenw >= 80) {
1960 mateusz.vi 920
          nls_format_date(buf->buff64 + 1, dta->date_yr + 1980, dta->date_mo, dta->date_dy, &(buf->nls));
1141 mateusz.vi 921
        } else {
1960 mateusz.vi 922
          nls_format_date(buf->buff64 + 1, (dta->date_yr + 80) % 100, dta->date_mo, dta->date_dy, &(buf->nls));
1141 mateusz.vi 923
        }
1717 mateusz.vi 924
        output(buf->buff64);
420 mateuszvis 925
 
926
        /* one space and NLS TIME */
1717 mateusz.vi 927
        nls_format_time(buf->buff64 + 1, dta->time_hour, dta->time_min, 0xff, &(buf->nls));
928
        outputnl(buf->buff64);
420 mateuszvis 929
        break;
930
 
931
      case DIR_OUTPUT_WIDE: /* display in columns of 12 chars per item */
2214 mateusz.vi 932
        i = sv_strlen(dta->fname);
420 mateuszvis 933
        if (dta->attr & DOS_ATTR_DIR) {
934
          i += 2;
935
          output("[");
936
          output(dta->fname);
937
          output("]");
938
        } else {
939
          output(dta->fname);
940
        }
941
        while (i++ < WCOLWIDTH) output(" ");
942
        if (++wcolcount == wcols) {
943
          wcolcount = 0;
944
          outputnl("");
528 mateuszvis 945
        } else {
946
          availrows++; /* wide mode is the only one that does not write one line per file */
420 mateuszvis 947
        }
948
        break;
949
 
950
      case DIR_OUTPUT_BARE:
2206 mateusz.vi 951
        /* if /B used in combination with /S then files are displayed with full path */
952
        if (req.flags & DIR_FLAG_RECUR) dir_print_dirprefix(buf->path);
420 mateuszvis 953
        outputnl(dta->fname);
954
        break;
396 mateuszvis 955
    }
368 mateuszvis 956
 
1724 mateusz.vi 957
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
420 mateuszvis 958
 
1741 mateusz.vi 959
    NEXT_ENTRY:
1716 mateusz.vi 960
    /* take next entry, either from buf or disk */
961
    if (dtabufcount > 0) {
962
      dtabufcount--;
2217 mateusz.vi 963
      memcpy_ltr_far(((unsigned char *)dta) + 22, &(dtabuf[buf->orderidx[dtabufcount]]), sizeof(struct TINYDTA));
1719 mateusz.vi 964
      dta->attr = dtabuf[buf->orderidx[dtabufcount]].time_sec2; /* restore attr from the abused time_sec2 field */
1716 mateusz.vi 965
    } else {
966
      if (findnext(dta) != 0) break;
967
    }
420 mateuszvis 968
 
1716 mateusz.vi 969
  }
970
 
528 mateuszvis 971
  if (wcolcount != 0) {
972
    outputnl(""); /* in wide mode make sure to end on a clear row */
1724 mateusz.vi 973
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
528 mateuszvis 974
  }
420 mateuszvis 975
 
424 mateuszvis 976
  /* print out summary (unless bare output mode) */
1724 mateusz.vi 977
  if (req.format != DIR_OUTPUT_BARE) {
2201 mateusz.vi 978
    dir_print_summary_files(buf->buff64, uint32maxlen, summary_totsz, summary_fcount, &availrows, req.flags, &(buf->nls));
424 mateuszvis 979
  }
980
 
2201 mateusz.vi 981
  /* update global counters in case /s is used */
982
  summary_recurs_fcount += summary_fcount;
983
  summary_recurs_totsz += summary_totsz;
984
 
2193 mateusz.vi 985
  /* /S processing */
2197 mateusz.vi 986
  CHECK_RECURS:
987
  /* if /S then look for a subdir */
988
  if (req.flags & DIR_FLAG_RECUR) {
989
    /* do the findfirst on *.* instead of reusing the user filter */
990
    char *s;
991
    char backup[4];
2200 mateusz.vi 992
    //printf("orig path='%s' new=", buf->path);
2197 mateusz.vi 993
    for (s = buf->path; *s != 0; s++);
994
    for (; s[-1] != '\\'; s--);
995
    memcpy_ltr(backup, s, 4);
996
    memcpy_ltr(s, "*.*", 4);
2200 mateusz.vi 997
    //printf("'%s'\n", buf->path);
2197 mateusz.vi 998
    if (findfirst(dta, buf->path, DOS_ATTR_DIR) == 0) {
999
      memcpy_ltr(s, backup, 4);
1000
      for (;;) {
1001
        if ((dta->fname[0] != '.') && (dta->attr & DOS_ATTR_DIR)) break;
1002
        if (findnext(dta) != 0) goto NOSUBDIR;
1003
      }
2200 mateusz.vi 1004
      //printf("GOT DIR (/S): '%s'\n", dta->fname);
2197 mateusz.vi 1005
      /* add dir to path and redo scan */
1006
      memcpy_ltr(&(buf->dtastack[buf->dtastacklen]), dta, sizeof(struct DTA));
1007
      buf->dtastacklen++;
1008
      path_add(buf->path, dta->fname);
1009
      goto NEXT_ITER;
1010
    }
1011
    memcpy_ltr(s, backup, 4);
2193 mateusz.vi 1012
  }
2197 mateusz.vi 1013
  NOSUBDIR:
1014
 
2193 mateusz.vi 1015
  while (buf->dtastacklen > 0) {
1016
    /* rewind path one directory back, pop the next dta and do a FindNext */
1017
    path_back(buf->path);
1018
    buf->dtastacklen--;
1019
    TRYNEXTENTRY:
1020
    if (findnext(&(buf->dtastack[buf->dtastacklen])) != 0) continue;
1021
    if ((buf->dtastack[buf->dtastacklen].attr & DOS_ATTR_DIR) == 0) goto TRYNEXTENTRY;
2200 mateusz.vi 1022
    if (buf->dtastack[buf->dtastacklen].fname[0] == '.') goto TRYNEXTENTRY;
2193 mateusz.vi 1023
    /* something found -> add dir to path and redo scan */
1024
    path_add(buf->path, buf->dtastack[buf->dtastacklen].fname);
1025
    goto NEXT_ITER;
1026
  }
1027
 
2200 mateusz.vi 1028
  /* print out disk space available (unless bare output mode) */
1029
  if (req.format != DIR_OUTPUT_BARE) {
2201 mateusz.vi 1030
    /* if /s mode then print also global stats */
1031
    if (req.flags & DIR_FLAG_RECUR) {
2205 mateusz.vi 1032
      if (summary_recurs_fcount == 0) {
1033
        file_truename(req.filespecptr, buf->path);
1034
        dir_print_dirof(buf->path, &availrows, req.flags & DIR_FLAG_PAUSE);
1035
        nls_outputnl_doserr(2); /* "File not found" */
1036
        goto GAMEOVER;
1037
      } else {
1038
        outputnl("");
1039
        if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
1040
        nls_outputnl(37,25); /* Total files listed: */
1041
        if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
1042
        dir_print_summary_files(buf->buff64, uint32maxlen, summary_recurs_totsz, summary_recurs_fcount, &availrows, req.flags, &(buf->nls));
1043
      }
2201 mateusz.vi 1044
    }
2200 mateusz.vi 1045
    /* xxxx bytes free */
1046
    i = cmd_dir_df(&summary_totsz, drv);
1047
    if (i != 0) nls_outputnl_doserr(i);
2218 mateusz.vi 1048
    sv_memset(buf->buff64, ' ', summary_alignpos + 8 + uint32maxlen); /* align the freebytes value to same column as totbytes */
2200 mateusz.vi 1049
    i = nls_format_number(buf->buff64 + summary_alignpos + 8 + uint32maxlen, summary_totsz, &(buf->nls));
1050
    output(buf->buff64 + i + 1);
1051
    output(" ");
1052
    nls_outputnl(37,24); /* "bytes free" */
1053
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
1054
  }
1055
 
2205 mateusz.vi 1056
  GAMEOVER:
1057
 
1716 mateusz.vi 1058
  /* free the buffer memory (if used) */
2230 mateusz.vi 1059
  if (glob_sortcmp_dat.dtabuf_root != NULL) cmd_dir_farfree(glob_sortcmp_dat.dtabuf_root);
1716 mateusz.vi 1060
 
2193 mateusz.vi 1061
  free(buf);
533 mateuszvis 1062
  return(CMD_OK);
368 mateuszvis 1063
}