Subversion Repositories SvarDOS

Rev

Rev 1141 | Rev 1716 | 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
 *
990 mateusz.vi 4
 * Copyright (C) 2021-2022 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
46
 */
47
 
396 mateuszvis 48
/* NOTE: /A attributes are matched in an exclusive way, ie. only files with
49
 *       the specified attributes are matched. This is different from how DOS
50
 *       itself matches attributes hence DIR cannot rely on the attributes
51
 *       filter within FindFirst.
52
 *
53
 * NOTE: Multiple /A are not supported - only the last one is significant.
54
 */
55
 
420 mateuszvis 56
#define WCOLWIDTH 15  /* width of a column in wide mode output */
57
 
424 mateuszvis 58
 
59
/* fills freebytes with free bytes for drv (A=0, B=1, etc)
60
 * returns DOS ERR code on failure */
61
static unsigned short cmd_dir_df(unsigned long *freebytes, unsigned char drv) {
62
  unsigned short res = 0;
63
  unsigned short sects_per_clust = 0, avail_clusts = 0, bytes_per_sect = 0;
64
 
65
  _asm {
66
    push ax
67
    push bx
68
    push cx
69
    push dx
70
 
71
    mov ah, 0x36  /* DOS 2+ -- Get Disk Free Space */
72
    mov dl, [drv] /* A=1, B=2, etc (0 = DEFAULT DRIVE) */
73
    inc dl
74
    int 0x21      /* AX=sects_per_clust, BX=avail_clusts, CX=bytes_per_sect, DX=tot_clusters */
75
    cmp ax, 0xffff /* AX=0xffff on error (invalid drive) */
76
    jne COMPUTEDF
77
    mov [res], 0x0f /* fill res with DOS error code 15 ("invalid drive") */
78
    jmp DONE
79
 
80
    COMPUTEDF:
81
    /* freebytes = AX * BX * CX */
82
    mov [sects_per_clust], ax
83
    mov [avail_clusts], bx
84
    mov [bytes_per_sect], cx
85
 
86
    DONE:
87
    pop dx
88
    pop cx
89
    pop bx
90
    pop ax
91
  }
92
 
93
  /* multiple steps to avoid uint16 overflow */
94
  *freebytes = sects_per_clust;
95
  *freebytes *= avail_clusts;
96
  *freebytes *= bytes_per_sect;
97
 
98
  return(res);
99
}
100
 
101
 
528 mateuszvis 102
static void dir_pagination(unsigned short *availrows) {
103
  *availrows -= 1;
104
  if (*availrows == 0) {
105
    press_any_key();
106
    *availrows = screen_getheight() - 1;
107
  }
108
}
109
 
110
 
542 mateuszvis 111
/* parse an attr list like "Ar-hS" and fill bitfield into attrfilter_may and attrfilter_must.
112
 * /AHS   -> adds S and H to mandatory attribs ("must")
113
 * /A-S   -> removes S from allowed attribs ("may")
114
 * returns non-zero on error. */
115
static int dir_parse_attr_list(const char *arg, unsigned char *attrfilter_may, unsigned char *attrfilter_must) {
116
  for (; *arg != 0; arg++) {
117
    unsigned char curattr;
118
    char not;
119
    if (*arg == '-') {
120
      not = 1;
121
      arg++;
122
    } else {
123
      not = 0;
124
    }
125
    switch (*arg) {
126
      case 'd':
127
      case 'D':
128
        curattr = DOS_ATTR_DIR;
129
        break;
130
      case 'r':
131
      case 'R':
132
        curattr = DOS_ATTR_RO;
133
        break;
134
      case 'a':
135
      case 'A':
136
        curattr = DOS_ATTR_ARC;
137
        break;
138
      case 'h':
139
      case 'H':
140
        curattr = DOS_ATTR_HID;
141
        break;
142
      case 's':
143
      case 'S':
144
        curattr = DOS_ATTR_SYS;
145
        break;
146
      default:
147
        return(-1);
148
    }
149
    /* update res bitfield */
150
    if (not) {
151
      *attrfilter_may &= ~curattr;
152
    } else {
153
      *attrfilter_must |= curattr;
154
    }
155
  }
156
  return(0);
157
}
158
 
159
 
160
#define DIR_ATTR_DEFAULT (DOS_ATTR_RO | DOS_ATTR_DIR | DOS_ATTR_ARC)
161
 
533 mateuszvis 162
static enum cmd_result cmd_dir(struct cmd_funcparam *p) {
393 mateuszvis 163
  const char *filespecptr = NULL;
388 mateuszvis 164
  struct DTA *dta = (void *)0x80; /* set DTA to its default location at 80h in PSP */
417 mateuszvis 165
  unsigned short i;
396 mateuszvis 166
  unsigned short availrows;  /* counter of available rows on display (used for /P) */
1141 mateusz.vi 167
  unsigned short screenw = screen_getwidth();
168
  unsigned short wcols = screenw / WCOLWIDTH; /* number of columns in wide mode */
420 mateuszvis 169
  unsigned char wcolcount;
501 mateuszvis 170
  struct nls_patterns *nls = (void *)(p->BUFFER + (p->BUFFERSZ / 2));
171
  char *buff2 = p->BUFFER + (p->BUFFERSZ / 2) + sizeof(*nls);
424 mateuszvis 172
  unsigned long summary_fcount = 0;
173
  unsigned long summary_totsz = 0;
174
  unsigned char drv = 0;
542 mateuszvis 175
  unsigned char attrfilter_may = DIR_ATTR_DEFAULT;
176
  unsigned char attrfilter_must = 0;
420 mateuszvis 177
 
396 mateuszvis 178
  #define DIR_FLAG_PAUSE  1
179
  #define DIR_FLAG_RECUR  4
420 mateuszvis 180
  #define DIR_FLAG_LCASE  8
396 mateuszvis 181
  unsigned char flags = 0;
368 mateuszvis 182
 
420 mateuszvis 183
  #define DIR_OUTPUT_NORM 1
184
  #define DIR_OUTPUT_WIDE 2
185
  #define DIR_OUTPUT_BARE 3
186
  unsigned char format = DIR_OUTPUT_NORM;
187
 
390 mateuszvis 188
  if (cmd_ishlp(p)) {
990 mateusz.vi 189
    nls_outputnl(37,0); /* "Displays a list of files and subdirectories in a directory" */
396 mateuszvis 190
    outputnl("");
990 mateusz.vi 191
    nls_outputnl(37,1); /* "DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]" */
396 mateuszvis 192
    outputnl("");
990 mateusz.vi 193
    nls_outputnl(37,2); /* "/P Pauses after each screenful of information" */
194
    nls_outputnl(37,3); /* "/W Uses wide list format" */
396 mateuszvis 195
    outputnl("");
990 mateusz.vi 196
    nls_outputnl(37,4); /* "/A Displays files with specified attributes:" */
197
    nls_outputnl(37,5); /* "    D Directories            R Read-only files        H Hidden files" */
198
    nls_outputnl(37,6); /* "    A Ready for archiving    S System files           - prefix meaning "not"" */
396 mateuszvis 199
    outputnl("");
990 mateusz.vi 200
    nls_outputnl(37,7); /* "/O List files in sorted order:" */
201
    nls_outputnl(37,8); /* "    N by name                S by size                E by extension" */
202
    nls_outputnl(37,9); /* "    D by date                G group dirs first       - prefix to reverse order" */
396 mateuszvis 203
    outputnl("");
990 mateusz.vi 204
    nls_outputnl(37,10); /* "/S Displays files in specified directory and all subdirectories" */
205
    nls_outputnl(37,11); /* "/B Uses bare format (no heading information or summary)" */
206
    nls_outputnl(37,12); /* "/L Uses lowercases" */
533 mateuszvis 207
    return(CMD_OK);
390 mateuszvis 208
  }
209
 
420 mateuszvis 210
  i = nls_getpatterns(nls);
538 mateuszvis 211
  if (i != 0) nls_outputnl_doserr(i);
420 mateuszvis 212
 
1141 mateusz.vi 213
  /* disable usage of thousands separator on narrow screens */
214
  if (screenw < 80) nls->thousep[0] = 0;
215
 
393 mateuszvis 216
  /* parse command line */
217
  for (i = 0; i < p->argc; i++) {
218
    if (p->argv[i][0] == '/') {
542 mateuszvis 219
      const char *arg = p->argv[i] + 1;
396 mateuszvis 220
      char neg = 0;
221
      /* detect negations and get actual argument */
542 mateuszvis 222
      if (*arg == '-') {
223
        neg = 1;
224
        arg++;
225
      }
396 mateuszvis 226
      /* */
542 mateuszvis 227
      switch (*arg) {
396 mateuszvis 228
        case 'a':
229
        case 'A':
542 mateuszvis 230
          arg++;
231
          /* preset defaults */
232
          attrfilter_may = DIR_ATTR_DEFAULT;
233
          attrfilter_must = 0;
234
          /* /-A only allowed without further parameters (used to cancel possible previous /Asmth) */
235
          if (neg) {
236
            if (*arg != 0) {
237
              nls_outputnl_err(0, 2); /* invalid switch */
238
              return(CMD_FAIL);
239
            }
240
          } else {
1085 mateusz.vi 241
            /* skip colon if present */
242
            if (*arg == ':') arg++;
542 mateuszvis 243
            /* start with "allow everything" */
244
            attrfilter_may = (DOS_ATTR_ARC | DOS_ATTR_DIR | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_RO);
245
            if (dir_parse_attr_list(arg, &attrfilter_may, &attrfilter_must) != 0) {
246
              nls_outputnl_err(0, 3); /* invalid parameter format */
247
              return(CMD_FAIL);
248
            }
249
          }
396 mateuszvis 250
          break;
399 mateuszvis 251
        case 'b':
252
        case 'B':
420 mateuszvis 253
          format = DIR_OUTPUT_BARE;
399 mateuszvis 254
          break;
421 mateuszvis 255
        case 'l':
256
        case 'L':
257
          flags |= DIR_FLAG_LCASE;
420 mateuszvis 258
          break;
421 mateuszvis 259
        case 'o':
260
        case 'O':
261
          /* TODO */
262
          outputnl("/O NOT IMPLEMENTED YET");
533 mateuszvis 263
          return(CMD_FAIL);
421 mateuszvis 264
          break;
396 mateuszvis 265
        case 'p':
266
        case 'P':
267
          flags |= DIR_FLAG_PAUSE;
268
          if (neg) flags &= (0xff ^ DIR_FLAG_PAUSE);
269
          break;
421 mateuszvis 270
        case 's':
271
        case 'S':
272
          /* TODO */
273
          outputnl("/S NOT IMPLEMENTED YET");
533 mateuszvis 274
          return(CMD_FAIL);
420 mateuszvis 275
          break;
421 mateuszvis 276
        case 'w':
277
        case 'W':
278
          format = DIR_OUTPUT_WIDE;
279
          break;
393 mateuszvis 280
        default:
542 mateuszvis 281
          nls_outputnl_err(0, 2); /* invalid switch */
533 mateuszvis 282
          return(CMD_FAIL);
393 mateuszvis 283
      }
284
    } else {  /* filespec */
285
      if (filespecptr != NULL) {
542 mateuszvis 286
        nls_outputnl_err(0, 4); /* too many parameters */
533 mateuszvis 287
        return(CMD_FAIL);
393 mateuszvis 288
      }
289
      filespecptr = p->argv[i];
290
    }
291
  }
368 mateuszvis 292
 
393 mateuszvis 293
  if (filespecptr == NULL) filespecptr = ".";
294
 
528 mateuszvis 295
  availrows = screen_getheight() - 2;
296
 
417 mateuszvis 297
  /* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
298
  if ((filespecptr[0] != 0) && (filespecptr[1] == ':') && (filespecptr[2] == 0)) {
299
    if ((filespecptr[0] >= 'a') && (filespecptr[0] <= 'z')) {
300
      p->BUFFER[0] = filespecptr[0] - ('a' - 1);
301
    } else {
302
      p->BUFFER[0] = filespecptr[0] - ('A' - 1);
399 mateuszvis 303
    }
417 mateuszvis 304
    i = curpathfordrv(p->BUFFER, p->BUFFER[0]);
305
  } else {
306
    i = file_truename(filespecptr, p->BUFFER);
399 mateuszvis 307
  }
417 mateuszvis 308
  if (i != 0) {
538 mateuszvis 309
    nls_outputnl_doserr(i);
533 mateuszvis 310
    return(CMD_FAIL);
417 mateuszvis 311
  }
393 mateuszvis 312
 
420 mateuszvis 313
  if (format != DIR_OUTPUT_BARE) {
424 mateuszvis 314
    drv = p->BUFFER[0];
399 mateuszvis 315
    if (drv >= 'a') {
316
      drv -= 'a';
317
    } else {
318
      drv -= 'A';
319
    }
403 mateuszvis 320
    cmd_vol_internal(drv, buff2);
990 mateusz.vi 321
    sprintf(buff2, svarlang_str(37,20)/*"Directory of %s"*/, p->BUFFER);
399 mateuszvis 322
    /* trim at first '?', if any */
403 mateuszvis 323
    for (i = 0; buff2[i] != 0; i++) if (buff2[i] == '?') buff2[i] = 0;
324
    outputnl(buff2);
399 mateuszvis 325
    outputnl("");
528 mateuszvis 326
    availrows -= 3;
399 mateuszvis 327
  }
328
 
417 mateuszvis 329
  /* if dir: append a backslash (also get its len) */
330
  i = path_appendbkslash_if_dir(p->BUFFER);
393 mateuszvis 331
 
417 mateuszvis 332
  /* if ends with a \ then append ????????.??? */
333
  if (p->BUFFER[i - 1] == '\\') strcat(p->BUFFER, "????????.???");
393 mateuszvis 334
 
542 mateuszvis 335
  /* ask DOS for list of files, but only with allowed attribs */
336
  i = findfirst(dta, p->BUFFER, attrfilter_may);
417 mateuszvis 337
  if (i != 0) {
538 mateuszvis 338
    nls_outputnl_doserr(i);
533 mateuszvis 339
    return(CMD_FAIL);
417 mateuszvis 340
  }
341
 
420 mateuszvis 342
  wcolcount = 0; /* may be used for columns counting with wide mode */
396 mateuszvis 343
 
420 mateuszvis 344
  do {
542 mateuszvis 345
    /* if mandatory attribs are requested, filter them now */
346
    if ((attrfilter_must & dta->attr) != attrfilter_must) continue;
347
 
348
    /* if file contains attributes that are not allowed -> skip */
349
    if ((~attrfilter_may & dta->attr) != 0) continue;
350
 
351
    /* turn string lcase (/L) */
420 mateuszvis 352
    if (flags & DIR_FLAG_LCASE) _strlwr(dta->fname); /* OpenWatcom extension, probably does not care about NLS so results may be odd with non-A-Z characters... */
368 mateuszvis 353
 
424 mateuszvis 354
    summary_fcount++;
355
    if ((dta->attr & DOS_ATTR_DIR) == 0) summary_totsz += dta->size;
356
 
420 mateuszvis 357
    switch (format) {
358
      case DIR_OUTPUT_NORM:
359
        /* print fname-space-extension (unless it's "." or "..", then print as-is) */
360
        if (dta->fname[0] == '.') {
361
          output(dta->fname);
362
          i = strlen(dta->fname);
363
          while (i++ < 12) output(" ");
364
        } else {
365
          file_fname2fcb(buff2, dta->fname);
366
          memmove(buff2 + 9, buff2 + 8, 4);
367
          buff2[8] = ' ';
368
          output(buff2);
369
        }
370
        output(" ");
371
        /* either <DIR> or right aligned 10-chars byte size */
372
        memset(buff2, ' ', 10);
373
        if (dta->attr & DOS_ATTR_DIR) {
990 mateusz.vi 374
          strcpy(buff2 + 10, svarlang_str(37,21));
420 mateuszvis 375
        } else {
1142 mateusz.vi 376
          nls_format_number(buff2 + 10, dta->size, nls);
420 mateuszvis 377
        }
378
        output(buff2 + strlen(buff2) - 10);
379
        /* two spaces and NLS DATE */
380
        buff2[0] = ' ';
381
        buff2[1] = ' ';
1141 mateusz.vi 382
        if (screenw >= 80) {
383
          nls_format_date(buff2 + 2, dta->date_yr + 1980, dta->date_mo, dta->date_dy, nls);
384
        } else {
385
          nls_format_date(buff2 + 2, (dta->date_yr + 80) % 100, dta->date_mo, dta->date_dy, nls);
386
        }
420 mateuszvis 387
        output(buff2);
388
 
389
        /* one space and NLS TIME */
426 mateuszvis 390
        nls_format_time(buff2 + 1, dta->time_hour, dta->time_min, 0xff, nls);
420 mateuszvis 391
        outputnl(buff2);
392
        break;
393
 
394
      case DIR_OUTPUT_WIDE: /* display in columns of 12 chars per item */
395
        i = strlen(dta->fname);
396
        if (dta->attr & DOS_ATTR_DIR) {
397
          i += 2;
398
          output("[");
399
          output(dta->fname);
400
          output("]");
401
        } else {
402
          output(dta->fname);
403
        }
404
        while (i++ < WCOLWIDTH) output(" ");
405
        if (++wcolcount == wcols) {
406
          wcolcount = 0;
407
          outputnl("");
528 mateuszvis 408
        } else {
409
          availrows++; /* wide mode is the only one that does not write one line per file */
420 mateuszvis 410
        }
411
        break;
412
 
413
      case DIR_OUTPUT_BARE:
414
        outputnl(dta->fname);
415
        break;
396 mateuszvis 416
    }
368 mateuszvis 417
 
528 mateuszvis 418
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
420 mateuszvis 419
 
420
  } while (findnext(dta) == 0);
421
 
528 mateuszvis 422
  if (wcolcount != 0) {
423
    outputnl(""); /* in wide mode make sure to end on a clear row */
424
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
425
  }
420 mateuszvis 426
 
424 mateuszvis 427
  /* print out summary (unless bare output mode) */
428
  if (format != DIR_OUTPUT_BARE) {
429
    unsigned short alignpos;
1141 mateusz.vi 430
    unsigned char uint32maxlen = 13; /* 13 is the max len of a 32 bit number with thousand separators (4'000'000'000) */
431
    if (screenw < 80) uint32maxlen = 10;
424 mateuszvis 432
    /* x file(s) */
1141 mateusz.vi 433
    memset(buff2, ' ', uint32maxlen);
434
    i = nls_format_number(buff2 + uint32maxlen, summary_fcount, nls);
435
    alignpos = sprintf(buff2 + uint32maxlen + i, " %s ", svarlang_str(37,22)/*"file(s)"*/);
424 mateuszvis 436
    output(buff2 + i);
437
    /* xxxx bytes */
1141 mateusz.vi 438
    i = nls_format_number(buff2 + uint32maxlen, summary_totsz, nls);
439
    output(buff2 + i + 1);
424 mateuszvis 440
    output(" ");
990 mateusz.vi 441
    nls_outputnl(37,23); /* "bytes" */
528 mateuszvis 442
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
424 mateuszvis 443
    /* xxxx bytes free */
444
    i = cmd_dir_df(&summary_totsz, drv);
538 mateuszvis 445
    if (i != 0) nls_outputnl_doserr(i);
1141 mateusz.vi 446
    alignpos += uint32maxlen * 2;
424 mateuszvis 447
    memset(buff2, ' ', alignpos); /* align the freebytes value to same column as totbytes */
448
    i = nls_format_number(buff2 + alignpos, summary_totsz, nls);
1141 mateusz.vi 449
    output(buff2 + i + 1);
424 mateuszvis 450
    output(" ");
990 mateusz.vi 451
    nls_outputnl(37,24); /* "bytes free" */
528 mateuszvis 452
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
424 mateuszvis 453
  }
454
 
533 mateuszvis 455
  return(CMD_OK);
368 mateuszvis 456
}