Subversion Repositories SvarDOS

Rev

Rev 1142 | Rev 1717 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1142 Rev 1716
1
/* This file is part of the SvarCOM project and is published under the terms
1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021-2022 Mateusz Viste
4
 * Copyright (C) 2021-2024 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
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
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
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
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
25
/*
25
/*
26
 * dir
26
 * dir
27
 *
27
 *
28
 * Displays a list of files and subdirectories in a directory.
28
 * Displays a list of files and subdirectories in a directory.
29
 *
29
 *
30
 * DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]
30
 * DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]
31
 *
31
 *
32
 * /P Pauses after each screenful of information.
32
 * /P Pauses after each screenful of information.
33
 * /W Uses wide list format.
33
 * /W Uses wide list format.
34
 *
34
 *
35
 * /A Displays file with specified attributes:
35
 * /A Displays file with specified attributes:
36
 *     D Directories           R Read-only files     H Hidden files
36
 *     D Directories           R Read-only files     H Hidden files
37
 *     A Ready for archiving   S System files        - prefix meaning "not"
37
 *     A Ready for archiving   S System files        - prefix meaning "not"
38
 *
38
 *
39
 * /O List files in sorted order:
39
 * /O List files in sorted order:
40
 *     N by name            S by size              E by extension
40
 *     N by name            S by size              E by extension
41
 *     D by date            G group dirs first     - prefix to reverse order
41
 *     D by date            G group dirs first     - prefix to reverse order
42
 *
42
 *
43
 * /S Displays files in specified directory and all subdirectories.
43
 * /S Displays files in specified directory and all subdirectories.
44
 * /B Uses bare format (no heading information or summary)
44
 * /B Uses bare format (no heading information or summary)
45
 * /L Uses lowercases
45
 * /L Uses lowercases
46
 */
46
 */
47
 
47
 
48
/* NOTE: /A attributes are matched in an exclusive way, ie. only files with
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
49
 *       the specified attributes are matched. This is different from how DOS
50
 *       itself matches attributes hence DIR cannot rely on the attributes
50
 *       itself matches attributes hence DIR cannot rely on the attributes
51
 *       filter within FindFirst.
51
 *       filter within FindFirst.
52
 *
52
 *
53
 * NOTE: Multiple /A are not supported - only the last one is significant.
53
 * NOTE: Multiple /A are not supported - only the last one is significant.
54
 */
54
 */
55
 
55
 
56
#define WCOLWIDTH 15  /* width of a column in wide mode output */
56
#define WCOLWIDTH 15  /* width of a column in wide mode output */
57
 
57
 
58
 
58
 
-
 
59
/* a "tiny" DTA is a DTA that is stripped from bytes that are not needed for
-
 
60
 * DIR operations */
-
 
61
_Packed struct TINYDTA {
-
 
62
/*  char reserved[21];
-
 
63
  unsigned char attr; */
-
 
64
  unsigned short time_sec2:5;
-
 
65
  unsigned short time_min:6;
-
 
66
  unsigned short time_hour:5;
-
 
67
  unsigned short date_dy:5;
-
 
68
  unsigned short date_mo:4;
-
 
69
  unsigned short date_yr:7;
-
 
70
  unsigned long size;
-
 
71
/*  char fname[13]; */
-
 
72
  char fname[12];
-
 
73
};
-
 
74
 
-
 
75
/* max amount of DTAs that can be buffered (not using 65535 because fmalloc a bit of overhead space) */
-
 
76
#define MAX_DTA_BUFCOUNT (65500 / sizeof(struct TINYDTA))
-
 
77
 
-
 
78
 
59
/* fills freebytes with free bytes for drv (A=0, B=1, etc)
79
/* fills freebytes with free bytes for drv (A=0, B=1, etc)
60
 * returns DOS ERR code on failure */
80
 * returns DOS ERR code on failure */
61
static unsigned short cmd_dir_df(unsigned long *freebytes, unsigned char drv) {
81
static unsigned short cmd_dir_df(unsigned long *freebytes, unsigned char drv) {
62
  unsigned short res = 0;
82
  unsigned short res = 0;
63
  unsigned short sects_per_clust = 0, avail_clusts = 0, bytes_per_sect = 0;
83
  unsigned short sects_per_clust = 0, avail_clusts = 0, bytes_per_sect = 0;
64
 
84
 
65
  _asm {
85
  _asm {
66
    push ax
86
    push ax
67
    push bx
87
    push bx
68
    push cx
88
    push cx
69
    push dx
89
    push dx
70
 
90
 
71
    mov ah, 0x36  /* DOS 2+ -- Get Disk Free Space */
91
    mov ah, 0x36  /* DOS 2+ -- Get Disk Free Space */
72
    mov dl, [drv] /* A=1, B=2, etc (0 = DEFAULT DRIVE) */
92
    mov dl, [drv] /* A=1, B=2, etc (0 = DEFAULT DRIVE) */
73
    inc dl
93
    inc dl
74
    int 0x21      /* AX=sects_per_clust, BX=avail_clusts, CX=bytes_per_sect, DX=tot_clusters */
94
    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) */
95
    cmp ax, 0xffff /* AX=0xffff on error (invalid drive) */
76
    jne COMPUTEDF
96
    jne COMPUTEDF
77
    mov [res], 0x0f /* fill res with DOS error code 15 ("invalid drive") */
97
    mov [res], 0x0f /* fill res with DOS error code 15 ("invalid drive") */
78
    jmp DONE
98
    jmp DONE
79
 
99
 
80
    COMPUTEDF:
100
    COMPUTEDF:
81
    /* freebytes = AX * BX * CX */
101
    /* freebytes = AX * BX * CX */
82
    mov [sects_per_clust], ax
102
    mov [sects_per_clust], ax
83
    mov [avail_clusts], bx
103
    mov [avail_clusts], bx
84
    mov [bytes_per_sect], cx
104
    mov [bytes_per_sect], cx
85
 
105
 
86
    DONE:
106
    DONE:
87
    pop dx
107
    pop dx
88
    pop cx
108
    pop cx
89
    pop bx
109
    pop bx
90
    pop ax
110
    pop ax
91
  }
111
  }
92
 
112
 
93
  /* multiple steps to avoid uint16 overflow */
113
  /* multiple steps to avoid uint16 overflow */
94
  *freebytes = sects_per_clust;
114
  *freebytes = sects_per_clust;
95
  *freebytes *= avail_clusts;
115
  *freebytes *= avail_clusts;
96
  *freebytes *= bytes_per_sect;
116
  *freebytes *= bytes_per_sect;
97
 
117
 
98
  return(res);
118
  return(res);
99
}
119
}
100
 
120
 
101
 
121
 
102
static void dir_pagination(unsigned short *availrows) {
122
static void dir_pagination(unsigned short *availrows) {
103
  *availrows -= 1;
123
  *availrows -= 1;
104
  if (*availrows == 0) {
124
  if (*availrows == 0) {
105
    press_any_key();
125
    press_any_key();
106
    *availrows = screen_getheight() - 1;
126
    *availrows = screen_getheight() - 1;
107
  }
127
  }
108
}
128
}
109
 
129
 
110
 
130
 
111
/* parse an attr list like "Ar-hS" and fill bitfield into attrfilter_may and attrfilter_must.
131
/* 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")
132
 * /AHS   -> adds S and H to mandatory attribs ("must")
113
 * /A-S   -> removes S from allowed attribs ("may")
133
 * /A-S   -> removes S from allowed attribs ("may")
114
 * returns non-zero on error. */
134
 * returns non-zero on error. */
115
static int dir_parse_attr_list(const char *arg, unsigned char *attrfilter_may, unsigned char *attrfilter_must) {
135
static int dir_parse_attr_list(const char *arg, unsigned char *attrfilter_may, unsigned char *attrfilter_must) {
116
  for (; *arg != 0; arg++) {
136
  for (; *arg != 0; arg++) {
117
    unsigned char curattr;
137
    unsigned char curattr;
118
    char not;
138
    char not;
119
    if (*arg == '-') {
139
    if (*arg == '-') {
120
      not = 1;
140
      not = 1;
121
      arg++;
141
      arg++;
122
    } else {
142
    } else {
123
      not = 0;
143
      not = 0;
124
    }
144
    }
125
    switch (*arg) {
145
    switch (*arg) {
126
      case 'd':
146
      case 'd':
127
      case 'D':
147
      case 'D':
128
        curattr = DOS_ATTR_DIR;
148
        curattr = DOS_ATTR_DIR;
129
        break;
149
        break;
130
      case 'r':
150
      case 'r':
131
      case 'R':
151
      case 'R':
132
        curattr = DOS_ATTR_RO;
152
        curattr = DOS_ATTR_RO;
133
        break;
153
        break;
134
      case 'a':
154
      case 'a':
135
      case 'A':
155
      case 'A':
136
        curattr = DOS_ATTR_ARC;
156
        curattr = DOS_ATTR_ARC;
137
        break;
157
        break;
138
      case 'h':
158
      case 'h':
139
      case 'H':
159
      case 'H':
140
        curattr = DOS_ATTR_HID;
160
        curattr = DOS_ATTR_HID;
141
        break;
161
        break;
142
      case 's':
162
      case 's':
143
      case 'S':
163
      case 'S':
144
        curattr = DOS_ATTR_SYS;
164
        curattr = DOS_ATTR_SYS;
145
        break;
165
        break;
146
      default:
166
      default:
147
        return(-1);
167
        return(-1);
148
    }
168
    }
149
    /* update res bitfield */
169
    /* update res bitfield */
150
    if (not) {
170
    if (not) {
151
      *attrfilter_may &= ~curattr;
171
      *attrfilter_may &= ~curattr;
152
    } else {
172
    } else {
153
      *attrfilter_must |= curattr;
173
      *attrfilter_must |= curattr;
154
    }
174
    }
155
  }
175
  }
156
  return(0);
176
  return(0);
157
}
177
}
158
 
178
 
159
 
179
 
-
 
180
/* compare attributes in a DTA node to mandatory and optional attributes. returns 1 on match, 0 otherwise */
-
 
181
static int filter_attribs(const struct DTA *dta, unsigned char attrfilter_must, unsigned char attrfilter_may) {
-
 
182
  /* if mandatory attribs are requested, filter them now */
-
 
183
  if ((attrfilter_must & dta->attr) != attrfilter_must) return(0);
-
 
184
 
-
 
185
  /* if file contains attributes that are not allowed -> skip */
-
 
186
  if ((~attrfilter_may & dta->attr) != 0) return(0);
-
 
187
 
-
 
188
  return(1);
-
 
189
}
-
 
190
 
-
 
191
 
-
 
192
 
160
#define DIR_ATTR_DEFAULT (DOS_ATTR_RO | DOS_ATTR_DIR | DOS_ATTR_ARC)
193
#define DIR_ATTR_DEFAULT (DOS_ATTR_RO | DOS_ATTR_DIR | DOS_ATTR_ARC)
161
 
194
 
162
static enum cmd_result cmd_dir(struct cmd_funcparam *p) {
195
static enum cmd_result cmd_dir(struct cmd_funcparam *p) {
163
  const char *filespecptr = NULL;
196
  const char *filespecptr = NULL;
164
  struct DTA *dta = (void *)0x80; /* set DTA to its default location at 80h in PSP */
197
  struct DTA *dta = (void *)0x80; /* set DTA to its default location at 80h in PSP */
-
 
198
  struct DTA far *dtabuf = NULL; /* used to buffer results when sorting is enabled */
-
 
199
  struct DTA far *dtabuf_root = NULL;
-
 
200
  unsigned short dtabufcount = 0;
165
  unsigned short i;
201
  unsigned short i;
166
  unsigned short availrows;  /* counter of available rows on display (used for /P) */
202
  unsigned short availrows;  /* counter of available rows on display (used for /P) */
167
  unsigned short screenw = screen_getwidth();
203
  unsigned short screenw = screen_getwidth();
168
  unsigned short wcols = screenw / WCOLWIDTH; /* number of columns in wide mode */
204
  unsigned short wcols = screenw / WCOLWIDTH; /* number of columns in wide mode */
169
  unsigned char wcolcount;
205
  unsigned char wcolcount;
170
  struct nls_patterns *nls = (void *)(p->BUFFER + (p->BUFFERSZ / 2));
206
  struct nls_patterns *nls = (void *)(p->BUFFER + (p->BUFFERSZ / 2));
171
  char *buff2 = p->BUFFER + (p->BUFFERSZ / 2) + sizeof(*nls);
207
  char *buff2 = p->BUFFER + (p->BUFFERSZ / 2) + sizeof(*nls);
172
  unsigned long summary_fcount = 0;
208
  unsigned long summary_fcount = 0;
173
  unsigned long summary_totsz = 0;
209
  unsigned long summary_totsz = 0;
174
  unsigned char drv = 0;
210
  unsigned char drv = 0;
175
  unsigned char attrfilter_may = DIR_ATTR_DEFAULT;
211
  unsigned char attrfilter_may = DIR_ATTR_DEFAULT;
176
  unsigned char attrfilter_must = 0;
212
  unsigned char attrfilter_must = 0;
-
 
213
  const char far *order = NULL; /* order string (like "GNE-SD"), this may come
-
 
214
                                   either from the /O argument or from the
-
 
215
                                   DIRCMD env variable (NULL = "no sort")
-
 
216
                                   note 1: the '-' reverse relates only to
-
 
217
                                   the letter that immediately follows it
-
 
218
                                   note 2: '/O' is a shorthand for '/OGNE' */
177
 
219
 
178
  #define DIR_FLAG_PAUSE  1
220
  #define DIR_FLAG_PAUSE  1
179
  #define DIR_FLAG_RECUR  4
221
  #define DIR_FLAG_RECUR  4
180
  #define DIR_FLAG_LCASE  8
222
  #define DIR_FLAG_LCASE  8
181
  unsigned char flags = 0;
223
  unsigned char flags = 0;
182
 
224
 
183
  #define DIR_OUTPUT_NORM 1
225
  #define DIR_OUTPUT_NORM 1
184
  #define DIR_OUTPUT_WIDE 2
226
  #define DIR_OUTPUT_WIDE 2
185
  #define DIR_OUTPUT_BARE 3
227
  #define DIR_OUTPUT_BARE 3
186
  unsigned char format = DIR_OUTPUT_NORM;
228
  unsigned char format = DIR_OUTPUT_NORM;
187
 
229
 
188
  if (cmd_ishlp(p)) {
230
  if (cmd_ishlp(p)) {
189
    nls_outputnl(37,0); /* "Displays a list of files and subdirectories in a directory" */
231
    nls_outputnl(37,0); /* "Displays a list of files and subdirectories in a directory" */
190
    outputnl("");
232
    outputnl("");
191
    nls_outputnl(37,1); /* "DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]" */
233
    nls_outputnl(37,1); /* "DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]" */
192
    outputnl("");
234
    outputnl("");
193
    nls_outputnl(37,2); /* "/P Pauses after each screenful of information" */
235
    nls_outputnl(37,2); /* "/P Pauses after each screenful of information" */
194
    nls_outputnl(37,3); /* "/W Uses wide list format" */
236
    nls_outputnl(37,3); /* "/W Uses wide list format" */
195
    outputnl("");
237
    outputnl("");
196
    nls_outputnl(37,4); /* "/A Displays files with specified attributes:" */
238
    nls_outputnl(37,4); /* "/A Displays files with specified attributes:" */
197
    nls_outputnl(37,5); /* "    D Directories            R Read-only files        H Hidden files" */
239
    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"" */
240
    nls_outputnl(37,6); /* "    A Ready for archiving    S System files           - prefix meaning "not"" */
199
    outputnl("");
241
    outputnl("");
200
    nls_outputnl(37,7); /* "/O List files in sorted order:" */
242
    nls_outputnl(37,7); /* "/O List files in sorted order:" */
201
    nls_outputnl(37,8); /* "    N by name                S by size                E by extension" */
243
    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" */
244
    nls_outputnl(37,9); /* "    D by date                G group dirs first       - prefix to reverse order" */
203
    outputnl("");
245
    outputnl("");
204
    nls_outputnl(37,10); /* "/S Displays files in specified directory and all subdirectories" */
246
    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)" */
247
    nls_outputnl(37,11); /* "/B Uses bare format (no heading information or summary)" */
206
    nls_outputnl(37,12); /* "/L Uses lowercases" */
248
    nls_outputnl(37,12); /* "/L Uses lowercases" */
207
    return(CMD_OK);
249
    return(CMD_OK);
208
  }
250
  }
209
 
251
 
210
  i = nls_getpatterns(nls);
252
  i = nls_getpatterns(nls);
211
  if (i != 0) nls_outputnl_doserr(i);
253
  if (i != 0) nls_outputnl_doserr(i);
212
 
254
 
213
  /* disable usage of thousands separator on narrow screens */
255
  /* disable usage of thousands separator on narrow screens */
214
  if (screenw < 80) nls->thousep[0] = 0;
256
  if (screenw < 80) nls->thousep[0] = 0;
215
 
257
 
216
  /* parse command line */
258
  /* parse command line */
217
  for (i = 0; i < p->argc; i++) {
259
  for (i = 0; i < p->argc; i++) {
218
    if (p->argv[i][0] == '/') {
260
    if (p->argv[i][0] == '/') {
219
      const char *arg = p->argv[i] + 1;
261
      const char *arg = p->argv[i] + 1;
220
      char neg = 0;
262
      char neg = 0;
221
      /* detect negations and get actual argument */
263
      /* detect negations and get actual argument */
222
      if (*arg == '-') {
264
      if (*arg == '-') {
223
        neg = 1;
265
        neg = 1;
224
        arg++;
266
        arg++;
225
      }
267
      }
226
      /* */
268
      /* */
227
      switch (*arg) {
269
      switch (*arg) {
228
        case 'a':
270
        case 'a':
229
        case 'A':
271
        case 'A':
230
          arg++;
272
          arg++;
231
          /* preset defaults */
273
          /* preset defaults */
232
          attrfilter_may = DIR_ATTR_DEFAULT;
274
          attrfilter_may = DIR_ATTR_DEFAULT;
233
          attrfilter_must = 0;
275
          attrfilter_must = 0;
234
          /* /-A only allowed without further parameters (used to cancel possible previous /Asmth) */
276
          /* /-A only allowed without further parameters (used to cancel possible previous /Asmth) */
235
          if (neg) {
277
          if (neg) {
236
            if (*arg != 0) {
278
            if (*arg != 0) {
237
              nls_outputnl_err(0, 2); /* invalid switch */
279
              nls_outputnl_err(0, 2); /* invalid switch */
238
              return(CMD_FAIL);
280
              return(CMD_FAIL);
239
            }
281
            }
240
          } else {
282
          } else {
241
            /* skip colon if present */
283
            /* skip colon if present */
242
            if (*arg == ':') arg++;
284
            if (*arg == ':') arg++;
243
            /* start with "allow everything" */
285
            /* start with "allow everything" */
244
            attrfilter_may = (DOS_ATTR_ARC | DOS_ATTR_DIR | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_RO);
286
            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) {
287
            if (dir_parse_attr_list(arg, &attrfilter_may, &attrfilter_must) != 0) {
246
              nls_outputnl_err(0, 3); /* invalid parameter format */
288
              nls_outputnl_err(0, 3); /* invalid parameter format */
247
              return(CMD_FAIL);
289
              return(CMD_FAIL);
248
            }
290
            }
249
          }
291
          }
250
          break;
292
          break;
251
        case 'b':
293
        case 'b':
252
        case 'B':
294
        case 'B':
253
          format = DIR_OUTPUT_BARE;
295
          format = DIR_OUTPUT_BARE;
254
          break;
296
          break;
255
        case 'l':
297
        case 'l':
256
        case 'L':
298
        case 'L':
257
          flags |= DIR_FLAG_LCASE;
299
          flags |= DIR_FLAG_LCASE;
258
          break;
300
          break;
259
        case 'o':
301
        case 'o':
260
        case 'O':
302
        case 'O':
261
          /* TODO */
303
          order = arg+1;
262
          outputnl("/O NOT IMPLEMENTED YET");
-
 
263
          return(CMD_FAIL);
-
 
264
          break;
304
          break;
265
        case 'p':
305
        case 'p':
266
        case 'P':
306
        case 'P':
267
          flags |= DIR_FLAG_PAUSE;
307
          flags |= DIR_FLAG_PAUSE;
268
          if (neg) flags &= (0xff ^ DIR_FLAG_PAUSE);
308
          if (neg) flags &= (0xff ^ DIR_FLAG_PAUSE);
269
          break;
309
          break;
270
        case 's':
310
        case 's':
271
        case 'S':
311
        case 'S':
272
          /* TODO */
312
          /* TODO */
273
          outputnl("/S NOT IMPLEMENTED YET");
313
          outputnl("/S NOT IMPLEMENTED YET");
274
          return(CMD_FAIL);
314
          return(CMD_FAIL);
275
          break;
315
          break;
276
        case 'w':
316
        case 'w':
277
        case 'W':
317
        case 'W':
278
          format = DIR_OUTPUT_WIDE;
318
          format = DIR_OUTPUT_WIDE;
279
          break;
319
          break;
280
        default:
320
        default:
281
          nls_outputnl_err(0, 2); /* invalid switch */
321
          nls_outputnl_err(0, 2); /* invalid switch */
282
          return(CMD_FAIL);
322
          return(CMD_FAIL);
283
      }
323
      }
284
    } else {  /* filespec */
324
    } else {  /* filespec */
285
      if (filespecptr != NULL) {
325
      if (filespecptr != NULL) {
286
        nls_outputnl_err(0, 4); /* too many parameters */
326
        nls_outputnl_err(0, 4); /* too many parameters */
287
        return(CMD_FAIL);
327
        return(CMD_FAIL);
288
      }
328
      }
289
      filespecptr = p->argv[i];
329
      filespecptr = p->argv[i];
290
    }
330
    }
291
  }
331
  }
292
 
332
 
293
  if (filespecptr == NULL) filespecptr = ".";
333
  if (filespecptr == NULL) filespecptr = ".";
294
 
334
 
295
  availrows = screen_getheight() - 2;
335
  availrows = screen_getheight() - 2;
296
 
336
 
297
  /* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
337
  /* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
298
  if ((filespecptr[0] != 0) && (filespecptr[1] == ':') && (filespecptr[2] == 0)) {
338
  if ((filespecptr[0] != 0) && (filespecptr[1] == ':') && (filespecptr[2] == 0)) {
299
    if ((filespecptr[0] >= 'a') && (filespecptr[0] <= 'z')) {
339
    if ((filespecptr[0] >= 'a') && (filespecptr[0] <= 'z')) {
300
      p->BUFFER[0] = filespecptr[0] - ('a' - 1);
340
      p->BUFFER[0] = filespecptr[0] - ('a' - 1);
301
    } else {
341
    } else {
302
      p->BUFFER[0] = filespecptr[0] - ('A' - 1);
342
      p->BUFFER[0] = filespecptr[0] - ('A' - 1);
303
    }
343
    }
304
    i = curpathfordrv(p->BUFFER, p->BUFFER[0]);
344
    i = curpathfordrv(p->BUFFER, p->BUFFER[0]);
305
  } else {
345
  } else {
306
    i = file_truename(filespecptr, p->BUFFER);
346
    i = file_truename(filespecptr, p->BUFFER);
307
  }
347
  }
308
  if (i != 0) {
348
  if (i != 0) {
309
    nls_outputnl_doserr(i);
349
    nls_outputnl_doserr(i);
310
    return(CMD_FAIL);
350
    return(CMD_FAIL);
311
  }
351
  }
312
 
352
 
313
  if (format != DIR_OUTPUT_BARE) {
353
  if (format != DIR_OUTPUT_BARE) {
314
    drv = p->BUFFER[0];
354
    drv = p->BUFFER[0];
315
    if (drv >= 'a') {
355
    if (drv >= 'a') {
316
      drv -= 'a';
356
      drv -= 'a';
317
    } else {
357
    } else {
318
      drv -= 'A';
358
      drv -= 'A';
319
    }
359
    }
320
    cmd_vol_internal(drv, buff2);
360
    cmd_vol_internal(drv, buff2);
321
    sprintf(buff2, svarlang_str(37,20)/*"Directory of %s"*/, p->BUFFER);
361
    sprintf(buff2, svarlang_str(37,20)/*"Directory of %s"*/, p->BUFFER);
322
    /* trim at first '?', if any */
362
    /* trim at first '?', if any */
323
    for (i = 0; buff2[i] != 0; i++) if (buff2[i] == '?') buff2[i] = 0;
363
    for (i = 0; buff2[i] != 0; i++) if (buff2[i] == '?') buff2[i] = 0;
324
    outputnl(buff2);
364
    outputnl(buff2);
325
    outputnl("");
365
    outputnl("");
326
    availrows -= 3;
366
    availrows -= 3;
327
  }
367
  }
328
 
368
 
329
  /* if dir: append a backslash (also get its len) */
369
  /* if dir: append a backslash (also get its len) */
330
  i = path_appendbkslash_if_dir(p->BUFFER);
370
  i = path_appendbkslash_if_dir(p->BUFFER);
331
 
371
 
332
  /* if ends with a \ then append ????????.??? */
372
  /* if ends with a \ then append ????????.??? */
333
  if (p->BUFFER[i - 1] == '\\') strcat(p->BUFFER, "????????.???");
373
  if (p->BUFFER[i - 1] == '\\') strcat(p->BUFFER, "????????.???");
334
 
374
 
335
  /* ask DOS for list of files, but only with allowed attribs */
375
  /* ask DOS for list of files, but only with allowed attribs */
336
  i = findfirst(dta, p->BUFFER, attrfilter_may);
376
  i = findfirst(dta, p->BUFFER, attrfilter_may);
337
  if (i != 0) {
377
  if (i != 0) {
338
    nls_outputnl_doserr(i);
378
    nls_outputnl_doserr(i);
339
    return(CMD_FAIL);
379
    return(CMD_FAIL);
340
  }
380
  }
341
 
381
 
-
 
382
  /* if sorting is involved, then let's buffer all results (and sort them) */
-
 
383
  if (order != NULL) {
-
 
384
    /* allocate a memory buffer - try several sizes until one succeeds */
-
 
385
    unsigned short memsz[] = {65500, 32000, 16000, 8000, 4000, 2000, 0};
-
 
386
    unsigned short max_dta_bufcount = 0;
-
 
387
    for (i = 0; memsz[i] != 0; i++) {
-
 
388
      dtabuf = _fmalloc(memsz[i]);
-
 
389
      if (dtabuf != NULL) break;
-
 
390
    }
-
 
391
 
-
 
392
    if (dtabuf == NULL) {
-
 
393
      nls_outputnl_doserr(8); /* out of memory */
-
 
394
      return(CMD_FAIL);
-
 
395
    }
-
 
396
 
-
 
397
    /* remember the address so I can free it afterwards */
-
 
398
    dtabuf_root = dtabuf;
-
 
399
 
-
 
400
    /* compute the amount of DTAs I can buffer */
-
 
401
    max_dta_bufcount = 1; //memsz[i] / sizeof(struct TINYDTA);
-
 
402
    printf("max_dta_bufcount = %u\n", max_dta_bufcount);
-
 
403
 
-
 
404
    do {
-
 
405
      /* filter out files with uninteresting attributes */
-
 
406
      if (filter_attribs(dta, attrfilter_must, attrfilter_may) == 0) continue;
-
 
407
 
-
 
408
      _fmemcpy(&(dtabuf[dtabufcount]), ((char *)dta) + 22, sizeof(struct TINYDTA));
-
 
409
 
-
 
410
      /* save attribs in sec field, otherwise zero it (this field is not
-
 
411
       * displayed and dropping the attr field saves 2 bytes per entry) */
-
 
412
      dtabuf[dtabufcount++].time_sec2 = (dta->attr & 31);
-
 
413
 
-
 
414
      /* do I have any space left? */
-
 
415
      if (dtabufcount == max_dta_bufcount) {
-
 
416
        //outputnl("TOO MANY ENTRIES FOR SORTING! LIST IS UNSORTED");
-
 
417
        break;
-
 
418
      }
-
 
419
 
-
 
420
    } while (findnext(dta) == 0);
-
 
421
 
-
 
422
    /* sort the list - the tricky part is that my array is a far address while
-
 
423
     * qsort works only with near pointers, so I have to use an ugly auxiliary
-
 
424
     * table */
-
 
425
    // qsort(dt); TODO
-
 
426
 
-
 
427
    /* preload first entry */
-
 
428
    _fmemcpy(((unsigned char *)dta) + 22, dtabuf, sizeof(struct TINYDTA));
-
 
429
    dta->attr = dtabuf->time_sec2;
-
 
430
    dtabuf++;
-
 
431
    dtabufcount--;
-
 
432
  }
-
 
433
 
342
  wcolcount = 0; /* may be used for columns counting with wide mode */
434
  wcolcount = 0; /* may be used for columns counting with wide mode */
343
 
435
 
344
  do {
436
  for (;;) {
345
    /* if mandatory attribs are requested, filter them now */
-
 
346
    if ((attrfilter_must & dta->attr) != attrfilter_must) continue;
-
 
347
 
437
 
348
    /* if file contains attributes that are not allowed -> skip */
438
    /* filter out attributes (skip if entry comes from buffer, then it was already veted) */
349
    if ((~attrfilter_may & dta->attr) != 0) continue;
439
    if (filter_attribs(dta, attrfilter_must, attrfilter_may) == 0) continue;
350
 
440
 
351
    /* turn string lcase (/L) */
441
    /* turn string lcase (/L) */
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... */
442
    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... */
353
 
443
 
354
    summary_fcount++;
444
    summary_fcount++;
355
    if ((dta->attr & DOS_ATTR_DIR) == 0) summary_totsz += dta->size;
445
    if ((dta->attr & DOS_ATTR_DIR) == 0) summary_totsz += dta->size;
356
 
446
 
357
    switch (format) {
447
    switch (format) {
358
      case DIR_OUTPUT_NORM:
448
      case DIR_OUTPUT_NORM:
359
        /* print fname-space-extension (unless it's "." or "..", then print as-is) */
449
        /* print fname-space-extension (unless it's "." or "..", then print as-is) */
360
        if (dta->fname[0] == '.') {
450
        if (dta->fname[0] == '.') {
361
          output(dta->fname);
451
          output(dta->fname);
362
          i = strlen(dta->fname);
452
          i = strlen(dta->fname);
363
          while (i++ < 12) output(" ");
453
          while (i++ < 12) output(" ");
364
        } else {
454
        } else {
365
          file_fname2fcb(buff2, dta->fname);
455
          file_fname2fcb(buff2, dta->fname);
366
          memmove(buff2 + 9, buff2 + 8, 4);
456
          memmove(buff2 + 9, buff2 + 8, 4);
367
          buff2[8] = ' ';
457
          buff2[8] = ' ';
368
          output(buff2);
458
          output(buff2);
369
        }
459
        }
370
        output(" ");
460
        output(" ");
371
        /* either <DIR> or right aligned 10-chars byte size */
461
        /* either <DIR> or right aligned 10-chars byte size */
372
        memset(buff2, ' ', 10);
462
        memset(buff2, ' ', 10);
373
        if (dta->attr & DOS_ATTR_DIR) {
463
        if (dta->attr & DOS_ATTR_DIR) {
374
          strcpy(buff2 + 10, svarlang_str(37,21));
464
          strcpy(buff2 + 10, svarlang_str(37,21));
375
        } else {
465
        } else {
376
          nls_format_number(buff2 + 10, dta->size, nls);
466
          nls_format_number(buff2 + 10, dta->size, nls);
377
        }
467
        }
378
        output(buff2 + strlen(buff2) - 10);
468
        output(buff2 + strlen(buff2) - 10);
379
        /* two spaces and NLS DATE */
469
        /* two spaces and NLS DATE */
380
        buff2[0] = ' ';
470
        buff2[0] = ' ';
381
        buff2[1] = ' ';
471
        buff2[1] = ' ';
382
        if (screenw >= 80) {
472
        if (screenw >= 80) {
383
          nls_format_date(buff2 + 2, dta->date_yr + 1980, dta->date_mo, dta->date_dy, nls);
473
          nls_format_date(buff2 + 2, dta->date_yr + 1980, dta->date_mo, dta->date_dy, nls);
384
        } else {
474
        } else {
385
          nls_format_date(buff2 + 2, (dta->date_yr + 80) % 100, dta->date_mo, dta->date_dy, nls);
475
          nls_format_date(buff2 + 2, (dta->date_yr + 80) % 100, dta->date_mo, dta->date_dy, nls);
386
        }
476
        }
387
        output(buff2);
477
        output(buff2);
388
 
478
 
389
        /* one space and NLS TIME */
479
        /* one space and NLS TIME */
390
        nls_format_time(buff2 + 1, dta->time_hour, dta->time_min, 0xff, nls);
480
        nls_format_time(buff2 + 1, dta->time_hour, dta->time_min, 0xff, nls);
391
        outputnl(buff2);
481
        outputnl(buff2);
392
        break;
482
        break;
393
 
483
 
394
      case DIR_OUTPUT_WIDE: /* display in columns of 12 chars per item */
484
      case DIR_OUTPUT_WIDE: /* display in columns of 12 chars per item */
395
        i = strlen(dta->fname);
485
        i = strlen(dta->fname);
396
        if (dta->attr & DOS_ATTR_DIR) {
486
        if (dta->attr & DOS_ATTR_DIR) {
397
          i += 2;
487
          i += 2;
398
          output("[");
488
          output("[");
399
          output(dta->fname);
489
          output(dta->fname);
400
          output("]");
490
          output("]");
401
        } else {
491
        } else {
402
          output(dta->fname);
492
          output(dta->fname);
403
        }
493
        }
404
        while (i++ < WCOLWIDTH) output(" ");
494
        while (i++ < WCOLWIDTH) output(" ");
405
        if (++wcolcount == wcols) {
495
        if (++wcolcount == wcols) {
406
          wcolcount = 0;
496
          wcolcount = 0;
407
          outputnl("");
497
          outputnl("");
408
        } else {
498
        } else {
409
          availrows++; /* wide mode is the only one that does not write one line per file */
499
          availrows++; /* wide mode is the only one that does not write one line per file */
410
        }
500
        }
411
        break;
501
        break;
412
 
502
 
413
      case DIR_OUTPUT_BARE:
503
      case DIR_OUTPUT_BARE:
414
        outputnl(dta->fname);
504
        outputnl(dta->fname);
415
        break;
505
        break;
416
    }
506
    }
417
 
507
 
418
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
508
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
419
 
509
 
-
 
510
    /* take next entry, either from buf or disk */
-
 
511
    if (dtabufcount > 0) {
-
 
512
      /* preload first entry */
-
 
513
      _fmemcpy(((unsigned char *)dta) + 22, dtabuf, sizeof(struct TINYDTA));
-
 
514
      dta->attr = dtabuf->time_sec2;
-
 
515
      dtabuf++;
-
 
516
      dtabufcount--;
-
 
517
    } else {
420
  } while (findnext(dta) == 0);
518
      if (findnext(dta) != 0) break;
-
 
519
    }
-
 
520
 
-
 
521
  }
421
 
522
 
422
  if (wcolcount != 0) {
523
  if (wcolcount != 0) {
423
    outputnl(""); /* in wide mode make sure to end on a clear row */
524
    outputnl(""); /* in wide mode make sure to end on a clear row */
424
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
525
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
425
  }
526
  }
426
 
527
 
427
  /* print out summary (unless bare output mode) */
528
  /* print out summary (unless bare output mode) */
428
  if (format != DIR_OUTPUT_BARE) {
529
  if (format != DIR_OUTPUT_BARE) {
429
    unsigned short alignpos;
530
    unsigned short alignpos;
430
    unsigned char uint32maxlen = 13; /* 13 is the max len of a 32 bit number with thousand separators (4'000'000'000) */
531
    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;
532
    if (screenw < 80) uint32maxlen = 10;
432
    /* x file(s) */
533
    /* x file(s) */
433
    memset(buff2, ' ', uint32maxlen);
534
    memset(buff2, ' ', uint32maxlen);
434
    i = nls_format_number(buff2 + uint32maxlen, summary_fcount, nls);
535
    i = nls_format_number(buff2 + uint32maxlen, summary_fcount, nls);
435
    alignpos = sprintf(buff2 + uint32maxlen + i, " %s ", svarlang_str(37,22)/*"file(s)"*/);
536
    alignpos = sprintf(buff2 + uint32maxlen + i, " %s ", svarlang_str(37,22)/*"file(s)"*/);
436
    output(buff2 + i);
537
    output(buff2 + i);
437
    /* xxxx bytes */
538
    /* xxxx bytes */
438
    i = nls_format_number(buff2 + uint32maxlen, summary_totsz, nls);
539
    i = nls_format_number(buff2 + uint32maxlen, summary_totsz, nls);
439
    output(buff2 + i + 1);
540
    output(buff2 + i + 1);
440
    output(" ");
541
    output(" ");
441
    nls_outputnl(37,23); /* "bytes" */
542
    nls_outputnl(37,23); /* "bytes" */
442
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
543
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
443
    /* xxxx bytes free */
544
    /* xxxx bytes free */
444
    i = cmd_dir_df(&summary_totsz, drv);
545
    i = cmd_dir_df(&summary_totsz, drv);
445
    if (i != 0) nls_outputnl_doserr(i);
546
    if (i != 0) nls_outputnl_doserr(i);
446
    alignpos += uint32maxlen * 2;
547
    alignpos += uint32maxlen * 2;
447
    memset(buff2, ' ', alignpos); /* align the freebytes value to same column as totbytes */
548
    memset(buff2, ' ', alignpos); /* align the freebytes value to same column as totbytes */
448
    i = nls_format_number(buff2 + alignpos, summary_totsz, nls);
549
    i = nls_format_number(buff2 + alignpos, summary_totsz, nls);
449
    output(buff2 + i + 1);
550
    output(buff2 + i + 1);
450
    output(" ");
551
    output(" ");
451
    nls_outputnl(37,24); /* "bytes free" */
552
    nls_outputnl(37,24); /* "bytes free" */
452
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
553
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
453
  }
554
  }
454
 
555
 
-
 
556
  /* free the buffer memory (if used) */
-
 
557
  if (dtabuf_root != NULL) _ffree(dtabuf_root);
-
 
558
 
455
  return(CMD_OK);
559
  return(CMD_OK);
456
}
560
}
457
 
561