Subversion Repositories SvarDOS

Rev

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

Rev 1721 Rev 1724
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-2024 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
59
/* a "tiny" DTA is a DTA that is stripped from bytes that are not needed for
60
 * DIR operations */
60
 * DIR operations */
61
_Packed struct TINYDTA {
61
_Packed struct TINYDTA {
62
/*  char reserved[21];
62
/*  char reserved[21];
63
  unsigned char attr; */
63
  unsigned char attr; */
64
  unsigned short time_sec2:5;
64
  unsigned short time_sec2:5;
65
  unsigned short time_min:6;
65
  unsigned short time_min:6;
66
  unsigned short time_hour:5;
66
  unsigned short time_hour:5;
67
  unsigned short date_dy:5;
67
  unsigned short date_dy:5;
68
  unsigned short date_mo:4;
68
  unsigned short date_mo:4;
69
  unsigned short date_yr:7;
69
  unsigned short date_yr:7;
70
  unsigned long size;
70
  unsigned long size;
71
/*  char fname[13]; */
71
/*  char fname[13]; */
72
  char fname[12];
72
  char fname[12];
73
};
73
};
74
 
74
 
75
 
75
 
76
/* fills freebytes with free bytes for drv (A=0, B=1, etc)
76
/* fills freebytes with free bytes for drv (A=0, B=1, etc)
77
 * returns DOS ERR code on failure */
77
 * returns DOS ERR code on failure */
78
static unsigned short cmd_dir_df(unsigned long *freebytes, unsigned char drv) {
78
static unsigned short cmd_dir_df(unsigned long *freebytes, unsigned char drv) {
79
  unsigned short res = 0;
79
  unsigned short res = 0;
80
  unsigned short sects_per_clust = 0, avail_clusts = 0, bytes_per_sect = 0;
80
  unsigned short sects_per_clust = 0, avail_clusts = 0, bytes_per_sect = 0;
81
 
81
 
82
  _asm {
82
  _asm {
83
    push ax
83
    push ax
84
    push bx
84
    push bx
85
    push cx
85
    push cx
86
    push dx
86
    push dx
87
 
87
 
88
    mov ah, 0x36  /* DOS 2+ -- Get Disk Free Space */
88
    mov ah, 0x36  /* DOS 2+ -- Get Disk Free Space */
89
    mov dl, [drv] /* A=1, B=2, etc (0 = DEFAULT DRIVE) */
89
    mov dl, [drv] /* A=1, B=2, etc (0 = DEFAULT DRIVE) */
90
    inc dl
90
    inc dl
91
    int 0x21      /* AX=sects_per_clust, BX=avail_clusts, CX=bytes_per_sect, DX=tot_clusters */
91
    int 0x21      /* AX=sects_per_clust, BX=avail_clusts, CX=bytes_per_sect, DX=tot_clusters */
92
    cmp ax, 0xffff /* AX=0xffff on error (invalid drive) */
92
    cmp ax, 0xffff /* AX=0xffff on error (invalid drive) */
93
    jne COMPUTEDF
93
    jne COMPUTEDF
94
    mov [res], 0x0f /* fill res with DOS error code 15 ("invalid drive") */
94
    mov [res], 0x0f /* fill res with DOS error code 15 ("invalid drive") */
95
    jmp DONE
95
    jmp DONE
96
 
96
 
97
    COMPUTEDF:
97
    COMPUTEDF:
98
    /* freebytes = AX * BX * CX */
98
    /* freebytes = AX * BX * CX */
99
    mov [sects_per_clust], ax
99
    mov [sects_per_clust], ax
100
    mov [avail_clusts], bx
100
    mov [avail_clusts], bx
101
    mov [bytes_per_sect], cx
101
    mov [bytes_per_sect], cx
102
 
102
 
103
    DONE:
103
    DONE:
104
    pop dx
104
    pop dx
105
    pop cx
105
    pop cx
106
    pop bx
106
    pop bx
107
    pop ax
107
    pop ax
108
  }
108
  }
109
 
109
 
110
  /* multiple steps to avoid uint16 overflow */
110
  /* multiple steps to avoid uint16 overflow */
111
  *freebytes = sects_per_clust;
111
  *freebytes = sects_per_clust;
112
  *freebytes *= avail_clusts;
112
  *freebytes *= avail_clusts;
113
  *freebytes *= bytes_per_sect;
113
  *freebytes *= bytes_per_sect;
114
 
114
 
115
  return(res);
115
  return(res);
116
}
116
}
117
 
117
 
118
 
118
 
119
static void dir_pagination(unsigned short *availrows) {
119
static void dir_pagination(unsigned short *availrows) {
120
  *availrows -= 1;
120
  *availrows -= 1;
121
  if (*availrows == 0) {
121
  if (*availrows == 0) {
122
    press_any_key();
122
    press_any_key();
123
    *availrows = screen_getheight() - 1;
123
    *availrows = screen_getheight() - 1;
124
  }
124
  }
125
}
125
}
126
 
126
 
127
 
127
 
128
/* parse an attr list like "Ar-hS" and fill bitfield into attrfilter_may and attrfilter_must.
128
/* parse an attr list like "Ar-hS" and fill bitfield into attrfilter_may and attrfilter_must.
129
 * /AHS   -> adds S and H to mandatory attribs ("must")
129
 * /AHS   -> adds S and H to mandatory attribs ("must")
130
 * /A-S   -> removes S from allowed attribs ("may")
130
 * /A-S   -> removes S from allowed attribs ("may")
131
 * returns non-zero on error. */
131
 * returns non-zero on error. */
132
static int dir_parse_attr_list(const char *arg, unsigned char *attrfilter_may, unsigned char *attrfilter_must) {
132
static int dir_parse_attr_list(const char *arg, unsigned char *attrfilter_may, unsigned char *attrfilter_must) {
133
  for (; *arg != 0; arg++) {
133
  for (; *arg != 0; arg++) {
134
    unsigned char curattr;
134
    unsigned char curattr;
135
    char not;
135
    char not;
136
    if (*arg == '-') {
136
    if (*arg == '-') {
137
      not = 1;
137
      not = 1;
138
      arg++;
138
      arg++;
139
    } else {
139
    } else {
140
      not = 0;
140
      not = 0;
141
    }
141
    }
142
    switch (*arg) {
142
    switch (*arg) {
143
      case 'd':
143
      case 'd':
144
      case 'D':
144
      case 'D':
145
        curattr = DOS_ATTR_DIR;
145
        curattr = DOS_ATTR_DIR;
146
        break;
146
        break;
147
      case 'r':
147
      case 'r':
148
      case 'R':
148
      case 'R':
149
        curattr = DOS_ATTR_RO;
149
        curattr = DOS_ATTR_RO;
150
        break;
150
        break;
151
      case 'a':
151
      case 'a':
152
      case 'A':
152
      case 'A':
153
        curattr = DOS_ATTR_ARC;
153
        curattr = DOS_ATTR_ARC;
154
        break;
154
        break;
155
      case 'h':
155
      case 'h':
156
      case 'H':
156
      case 'H':
157
        curattr = DOS_ATTR_HID;
157
        curattr = DOS_ATTR_HID;
158
        break;
158
        break;
159
      case 's':
159
      case 's':
160
      case 'S':
160
      case 'S':
161
        curattr = DOS_ATTR_SYS;
161
        curattr = DOS_ATTR_SYS;
162
        break;
162
        break;
163
      default:
163
      default:
164
        return(-1);
164
        return(-1);
165
    }
165
    }
166
    /* update res bitfield */
166
    /* update res bitfield */
167
    if (not) {
167
    if (not) {
168
      *attrfilter_may &= ~curattr;
168
      *attrfilter_may &= ~curattr;
169
    } else {
169
    } else {
170
      *attrfilter_must |= curattr;
170
      *attrfilter_must |= curattr;
171
    }
171
    }
172
  }
172
  }
173
  return(0);
173
  return(0);
174
}
174
}
175
 
175
 
176
 
176
 
177
/* compare attributes in a DTA node to mandatory and optional attributes. returns 1 on match, 0 otherwise */
177
/* compare attributes in a DTA node to mandatory and optional attributes. returns 1 on match, 0 otherwise */
178
static int filter_attribs(const struct DTA *dta, unsigned char attrfilter_must, unsigned char attrfilter_may) {
178
static int filter_attribs(const struct DTA *dta, unsigned char attrfilter_must, unsigned char attrfilter_may) {
179
  /* if mandatory attribs are requested, filter them now */
179
  /* if mandatory attribs are requested, filter them now */
180
  if ((attrfilter_must & dta->attr) != attrfilter_must) return(0);
180
  if ((attrfilter_must & dta->attr) != attrfilter_must) return(0);
181
 
181
 
182
  /* if file contains attributes that are not allowed -> skip */
182
  /* if file contains attributes that are not allowed -> skip */
183
  if ((~attrfilter_may & dta->attr) != 0) return(0);
183
  if ((~attrfilter_may & dta->attr) != 0) return(0);
184
 
184
 
185
  return(1);
185
  return(1);
186
}
186
}
187
 
187
 
188
 
188
 
189
static struct {
189
static struct {
190
  struct TINYDTA far *dtabuf_root;
190
  struct TINYDTA far *dtabuf_root;
191
  char order[8]; /* GNESD values (ucase = lower first ; lcase = higher first) */
191
  char order[8]; /* GNESD values (ucase = lower first ; lcase = higher first) */
192
} glob_sortcmp_dat;
192
} glob_sortcmp_dat;
193
 
193
 
194
 
194
 
195
/* translates an order string like "GNE-S" into values fed into the order[]
195
/* translates an order string like "GNE-S" into values fed into the order[]
196
 * table of glob_sortcmp_dat. returns 0 on success, non-zero otherwise. */
196
 * table of glob_sortcmp_dat. returns 0 on success, non-zero otherwise. */
197
static int process_order_directive(const char *ordstring) {
197
static int dir_process_order_directive(const char *ordstring) {
198
  const char *gnesd = "gnesd"; /* must be lower case */
198
  const char *gnesd = "gnesd"; /* must be lower case */
199
  int ordi, orderi = 0, i;
199
  int ordi, orderi = 0, i;
200
 
200
 
201
  /* tabula rasa */
201
  /* tabula rasa */
202
  glob_sortcmp_dat.order[0] = 0;
202
  glob_sortcmp_dat.order[0] = 0;
203
 
203
 
204
  /* /O alone is a short hand for /OGN */
204
  /* /O alone is a short hand for /OGN */
205
  if (*ordstring == 0) {
205
  if (*ordstring == 0) {
206
    glob_sortcmp_dat.order[0] = 'G';
206
    glob_sortcmp_dat.order[0] = 'G';
207
    glob_sortcmp_dat.order[1] = 'N';
207
    glob_sortcmp_dat.order[1] = 'N';
208
    glob_sortcmp_dat.order[2] = 0;
208
    glob_sortcmp_dat.order[2] = 0;
209
  }
209
  }
210
 
210
 
211
  /* parsing */
211
  /* parsing */
212
  for (ordi = 0; ordstring[ordi] != 0; ordi++) {
212
  for (ordi = 0; ordstring[ordi] != 0; ordi++) {
213
    if (ordstring[ordi] == '-') {
213
    if (ordstring[ordi] == '-') {
214
      if ((ordstring[ordi + 1] == '-') || (ordstring[ordi + 1] == 0)) return(-1);
214
      if ((ordstring[ordi + 1] == '-') || (ordstring[ordi + 1] == 0)) return(-1);
215
      continue;
215
      continue;
216
    }
216
    }
217
    if (orderi == sizeof(glob_sortcmp_dat.order)) return(-1);
217
    if (orderi == sizeof(glob_sortcmp_dat.order)) return(-1);
218
 
218
 
219
    for (i = 0; gnesd[i] != 0; i++) {
219
    for (i = 0; gnesd[i] != 0; i++) {
220
      if ((ordstring[ordi] | 32) == gnesd[i]) { /* | 32 is lcase-ing the char */
220
      if ((ordstring[ordi] | 32) == gnesd[i]) { /* | 32 is lcase-ing the char */
221
        if ((ordi > 0) && (ordstring[ordi - 1] == '-')) {
221
        if ((ordi > 0) && (ordstring[ordi - 1] == '-')) {
222
          glob_sortcmp_dat.order[orderi] = gnesd[i];
222
          glob_sortcmp_dat.order[orderi] = gnesd[i];
223
        } else {
223
        } else {
224
          glob_sortcmp_dat.order[orderi] = gnesd[i] ^ 32;
224
          glob_sortcmp_dat.order[orderi] = gnesd[i] ^ 32;
225
        }
225
        }
226
        orderi++;
226
        orderi++;
227
        break;
227
        break;
228
      }
228
      }
229
    }
229
    }
230
    if (gnesd[i] == 0) return(-1);
230
    if (gnesd[i] == 0) return(-1);
231
  }
231
  }
232
 
232
 
233
  return(0);
233
  return(0);
234
}
234
}
235
 
235
 
236
 
236
 
237
static int sortcmp(const void *dtaid1, const void *dtaid2) {
237
static int sortcmp(const void *dtaid1, const void *dtaid2) {
238
  struct TINYDTA far *dta1 = &(glob_sortcmp_dat.dtabuf_root[*((unsigned short *)dtaid1)]);
238
  struct TINYDTA far *dta1 = &(glob_sortcmp_dat.dtabuf_root[*((unsigned short *)dtaid1)]);
239
  struct TINYDTA far *dta2 = &(glob_sortcmp_dat.dtabuf_root[*((unsigned short *)dtaid2)]);
239
  struct TINYDTA far *dta2 = &(glob_sortcmp_dat.dtabuf_root[*((unsigned short *)dtaid2)]);
240
  char *ordconf = glob_sortcmp_dat.order;
240
  char *ordconf = glob_sortcmp_dat.order;
241
 
241
 
242
  /* debug stuff
242
  /* debug stuff
243
  {
243
  {
244
    int i;
244
    int i;
245
    printf("%lu vs %lu | ", dta1->size, dta2->size);
245
    printf("%lu vs %lu | ", dta1->size, dta2->size);
246
    for (i = 0; dta1->fname[i] != 0; i++) printf("%c", dta1->fname[i]);
246
    for (i = 0; dta1->fname[i] != 0; i++) printf("%c", dta1->fname[i]);
247
    printf(" vs ");
247
    printf(" vs ");
248
    for (i = 0; dta2->fname[i] != 0; i++) printf("%c", dta2->fname[i]);
248
    for (i = 0; dta2->fname[i] != 0; i++) printf("%c", dta2->fname[i]);
249
    printf("\n");
249
    printf("\n");
250
  } */
250
  } */
251
 
251
 
252
  for (;;) {
252
  for (;;) {
253
    int r = -1;
253
    int r = -1;
254
    if (*ordconf & 32) r = 1;
254
    if (*ordconf & 32) r = 1;
255
 
255
 
256
    switch (*ordconf | 32) {
256
    switch (*ordconf | 32) {
257
      case 'g': /* sort by type (directories first, then files) */
257
      case 'g': /* sort by type (directories first, then files) */
258
        if ((dta1->time_sec2 & DOS_ATTR_DIR) > (dta2->time_sec2 & DOS_ATTR_DIR)) return(0 - r);
258
        if ((dta1->time_sec2 & DOS_ATTR_DIR) > (dta2->time_sec2 & DOS_ATTR_DIR)) return(0 - r);
259
        if ((dta1->time_sec2 & DOS_ATTR_DIR) < (dta2->time_sec2 & DOS_ATTR_DIR)) return(r);
259
        if ((dta1->time_sec2 & DOS_ATTR_DIR) < (dta2->time_sec2 & DOS_ATTR_DIR)) return(r);
260
        break;
260
        break;
261
      case ' ': /* default (last resort) sort: by name */
261
      case ' ': /* default (last resort) sort: by name */
262
      case 'e': /* sort by extension */
262
      case 'e': /* sort by extension */
263
      case 'n': /* sort by filename */
263
      case 'n': /* sort by filename */
264
      {
264
      {
265
        const char far *f1 = dta1->fname;
265
        const char far *f1 = dta1->fname;
266
        const char far *f2 = dta2->fname;
266
        const char far *f2 = dta2->fname;
267
        int i, limit = 12;
267
        int i, limit = 12;
268
        /* special handling for '.' and '..' entries */
268
        /* special handling for '.' and '..' entries */
269
        if ((f1[0] == '.') && (f2[0] != '.')) return(0 - r);
269
        if ((f1[0] == '.') && (f2[0] != '.')) return(0 - r);
270
        if ((f2[0] == '.') && (f1[0] != '.')) return(r);
270
        if ((f2[0] == '.') && (f1[0] != '.')) return(r);
271
 
271
 
272
        if ((*ordconf | 32) == 'e') {
272
        if ((*ordconf | 32) == 'e') {
273
          /* fast-forward to extension or end of filename */
273
          /* fast-forward to extension or end of filename */
274
          while ((*f1 != 0) && (*f1 != '.')) f1++;
274
          while ((*f1 != 0) && (*f1 != '.')) f1++;
275
          while ((*f2 != 0) && (*f2 != '.')) f2++;
275
          while ((*f2 != 0) && (*f2 != '.')) f2++;
276
          limit = 4; /* TINYDTA structs are not nul-terminated */
276
          limit = 4; /* TINYDTA structs are not nul-terminated */
277
        }
277
        }
278
        /* cmp */
278
        /* cmp */
279
        for (i = 0; i < limit; i++) {
279
        for (i = 0; i < limit; i++) {
280
          if ((*f1 | 32) < (*f2 | 32)) return(0 - r);
280
          if ((*f1 | 32) < (*f2 | 32)) return(0 - r);
281
          if ((*f1 | 32) > (*f2 | 32)) return(r);
281
          if ((*f1 | 32) > (*f2 | 32)) return(r);
282
          if (*f1 == 0) break;
282
          if (*f1 == 0) break;
283
          f1++;
283
          f1++;
284
          f2++;
284
          f2++;
285
        }
285
        }
286
      }
286
      }
287
        break;
287
        break;
288
      case 's': /* sort by size */
288
      case 's': /* sort by size */
289
        if (dta1->size > dta2->size) return(r);
289
        if (dta1->size > dta2->size) return(r);
290
        if (dta1->size < dta2->size) return(0 - r);
290
        if (dta1->size < dta2->size) return(0 - r);
291
        break;
291
        break;
292
      case 'd': /* sort by date */
292
      case 'd': /* sort by date */
293
        if (dta1->date_yr < dta2->date_yr) return(0 - r);
293
        if (dta1->date_yr < dta2->date_yr) return(0 - r);
294
        if (dta1->date_yr > dta2->date_yr) return(r);
294
        if (dta1->date_yr > dta2->date_yr) return(r);
295
        if (dta1->date_mo < dta2->date_mo) return(0 - r);
295
        if (dta1->date_mo < dta2->date_mo) return(0 - r);
296
        if (dta1->date_mo > dta2->date_mo) return(r);
296
        if (dta1->date_mo > dta2->date_mo) return(r);
297
        if (dta1->date_dy < dta2->date_dy) return(0 - r);
297
        if (dta1->date_dy < dta2->date_dy) return(0 - r);
298
        if (dta1->date_dy > dta2->date_dy) return(r);
298
        if (dta1->date_dy > dta2->date_dy) return(r);
299
        if (dta1->time_hour < dta2->time_hour) return(0 - r);
299
        if (dta1->time_hour < dta2->time_hour) return(0 - r);
300
        if (dta1->time_hour > dta2->time_hour) return(r);
300
        if (dta1->time_hour > dta2->time_hour) return(r);
301
        if (dta1->time_min < dta2->time_min) return(0 - r);
301
        if (dta1->time_min < dta2->time_min) return(0 - r);
302
        if (dta1->time_min > dta2->time_min) return(r);
302
        if (dta1->time_min > dta2->time_min) return(r);
303
        break;
303
        break;
304
    }
304
    }
305
 
305
 
306
    if (*ordconf == 0) break;
306
    if (*ordconf == 0) break;
307
    ordconf++;
307
    ordconf++;
308
  }
308
  }
309
 
309
 
310
  return(0);
310
  return(0);
311
}
311
}
312
 
312
 
313
 
313
 
314
#define DIR_ATTR_DEFAULT (DOS_ATTR_RO | DOS_ATTR_DIR | DOS_ATTR_ARC)
314
#define DIR_ATTR_DEFAULT (DOS_ATTR_RO | DOS_ATTR_DIR | DOS_ATTR_ARC)
315
 
315
 
316
static enum cmd_result cmd_dir(struct cmd_funcparam *p) {
-
 
317
  const char *filespecptr = NULL;
-
 
318
  struct DTA *dta = (void *)0x80; /* set DTA to its default location at 80h in PSP */
-
 
319
  struct TINYDTA far *dtabuf = NULL; /* used to buffer results when sorting is enabled */
-
 
320
  unsigned short dtabufcount = 0;
-
 
321
  unsigned short i;
-
 
322
  unsigned short availrows;  /* counter of available rows on display (used for /P) */
-
 
323
  unsigned short screenw = screen_getwidth();
-
 
324
  unsigned short wcols = screenw / WCOLWIDTH; /* number of columns in wide mode */
-
 
325
  unsigned char wcolcount;
-
 
326
  struct {
-
 
327
    struct nls_patterns nls;
316
struct dirrequest {
328
    char buff64[64];
-
 
329
    char path[128];
-
 
330
    unsigned short orderidx[65535 / sizeof(struct TINYDTA)];
-
 
331
  } *buf = (void *)(p->BUFFER);
-
 
332
  unsigned long summary_fcount = 0;
-
 
333
  unsigned long summary_totsz = 0;
-
 
334
  unsigned char drv = 0;
317
  unsigned char attrfilter_may;
335
  unsigned char attrfilter_may = DIR_ATTR_DEFAULT;
318
  unsigned char attrfilter_must;
336
  unsigned char attrfilter_must = 0;
319
  const char *filespecptr;
337
 
320
 
338
  #define DIR_FLAG_PAUSE  1
321
  #define DIR_FLAG_PAUSE  1
339
  #define DIR_FLAG_RECUR  4
322
  #define DIR_FLAG_RECUR  4
340
  #define DIR_FLAG_LCASE  8
323
  #define DIR_FLAG_LCASE  8
341
  #define DIR_FLAG_SORT  16
324
  #define DIR_FLAG_SORT  16
342
  unsigned char flags = 0;
325
  unsigned char flags;
343
 
326
 
344
  #define DIR_OUTPUT_NORM 1
327
  #define DIR_OUTPUT_NORM 1
345
  #define DIR_OUTPUT_WIDE 2
328
  #define DIR_OUTPUT_WIDE 2
346
  #define DIR_OUTPUT_BARE 3
329
  #define DIR_OUTPUT_BARE 3
347
  unsigned char format = DIR_OUTPUT_NORM;
330
  unsigned char format;
348
 
-
 
349
  /* make sure there's no risk of buffer overflow */
-
 
350
  if (sizeof(buf) > p->BUFFERSZ) {
-
 
351
    outputnl("INTERNAL MEM ERROR IN " __FILE__);
-
 
352
    return(CMD_FAIL);
-
 
353
  }
-
 
354
 
-
 
355
  bzero(&glob_sortcmp_dat, sizeof(glob_sortcmp_dat));
-
 
356
 
-
 
357
  if (cmd_ishlp(p)) {
-
 
358
    nls_outputnl(37,0); /* "Displays a list of files and subdirectories in a directory" */
-
 
359
    outputnl("");
-
 
360
    nls_outputnl(37,1); /* "DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]" */
-
 
361
    outputnl("");
-
 
362
    nls_outputnl(37,2); /* "/P Pauses after each screenful of information" */
-
 
363
    nls_outputnl(37,3); /* "/W Uses wide list format" */
-
 
364
    outputnl("");
-
 
365
    nls_outputnl(37,4); /* "/A Displays files with specified attributes:" */
-
 
366
    nls_outputnl(37,5); /* "    D Directories            R Read-only files        H Hidden files" */
-
 
367
    nls_outputnl(37,6); /* "    A Ready for archiving    S System files           - prefix meaning "not"" */
-
 
368
    outputnl("");
-
 
369
    nls_outputnl(37,7); /* "/O List files in sorted order:" */
-
 
370
    nls_outputnl(37,8); /* "    N by name                S by size                E by extension" */
-
 
371
    nls_outputnl(37,9); /* "    D by date                G group dirs first       - prefix to reverse order" */
-
 
372
    outputnl("");
-
 
373
    nls_outputnl(37,10); /* "/S Displays files in specified directory and all subdirectories" */
-
 
374
    nls_outputnl(37,11); /* "/B Uses bare format (no heading information or summary)" */
-
 
375
    nls_outputnl(37,12); /* "/L Uses lowercases" */
-
 
376
    return(CMD_OK);
-
 
377
  }
-
 
378
 
331
};
379
  i = nls_getpatterns(&(buf->nls));
-
 
380
  if (i != 0) nls_outputnl_doserr(i);
-
 
381
 
332
 
382
  /* disable usage of thousands separator on narrow screens */
-
 
383
  if (screenw < 80) buf->nls.thousep[0] = 0;
-
 
384
 
333
 
385
  /* parse command line */
334
static int dir_parse_cmdline(struct dirrequest *req, const char **argv) {
386
  for (i = 0; i < p->argc; i++) {
335
  for (; *argv != NULL; argv++) {
387
    if (p->argv[i][0] == '/') {
336
    if (*argv[0] == '/') {
388
      const char *arg = p->argv[i] + 1;
337
      const char *arg = *argv + 1;
389
      char neg = 0;
338
      char neg = 0;
390
      /* detect negations and get actual argument */
339
      /* detect negations and get actual argument */
391
      if (*arg == '-') {
340
      if (*arg == '-') {
392
        neg = 1;
341
        neg = 1;
393
        arg++;
342
        arg++;
394
      }
343
      }
395
      /* */
344
      /* */
396
      switch (*arg) {
345
      switch (*arg) {
397
        case 'a':
346
        case 'a':
398
        case 'A':
347
        case 'A':
399
          arg++;
348
          arg++;
400
          /* preset defaults */
349
          /* preset defaults */
401
          attrfilter_may = DIR_ATTR_DEFAULT;
350
          req->attrfilter_may = DIR_ATTR_DEFAULT;
402
          attrfilter_must = 0;
351
          req->attrfilter_must = 0;
403
          /* /-A only allowed without further parameters (used to cancel possible previous /Asmth) */
352
          /* /-A only allowed without further parameters (used to cancel possible previous /Asmth) */
404
          if (neg) {
353
          if (neg) {
405
            if (*arg != 0) {
354
            if (*arg != 0) {
406
              nls_outputnl_err(0, 2); /* invalid switch */
355
              nls_outputnl_err(0, 2); /* invalid switch */
407
              return(CMD_FAIL);
356
              return(-1);
408
            }
357
            }
409
          } else {
358
          } else {
410
            /* skip colon if present */
359
            /* skip colon if present */
411
            if (*arg == ':') arg++;
360
            if (*arg == ':') arg++;
412
            /* start with "allow everything" */
361
            /* start with "allow everything" */
413
            attrfilter_may = (DOS_ATTR_ARC | DOS_ATTR_DIR | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_RO);
362
            req->attrfilter_may = (DOS_ATTR_ARC | DOS_ATTR_DIR | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_RO);
414
            if (dir_parse_attr_list(arg, &attrfilter_may, &attrfilter_must) != 0) {
363
            if (dir_parse_attr_list(arg, &(req->attrfilter_may), &(req->attrfilter_must)) != 0) {
415
              nls_outputnl_err(0, 3); /* invalid parameter format */
364
              nls_outputnl_err(0, 3); /* invalid parameter format */
416
              return(CMD_FAIL);
365
              return(-1);
417
            }
366
            }
418
          }
367
          }
419
          break;
368
          break;
420
        case 'b':
369
        case 'b':
421
        case 'B':
370
        case 'B':
422
          format = DIR_OUTPUT_BARE;
371
          req->format = DIR_OUTPUT_BARE;
423
          break;
372
          break;
424
        case 'l':
373
        case 'l':
425
        case 'L':
374
        case 'L':
426
          flags |= DIR_FLAG_LCASE;
375
          req->flags |= DIR_FLAG_LCASE;
427
          break;
376
          break;
428
        case 'o':
377
        case 'o':
429
        case 'O':
378
        case 'O':
430
          if (neg) {
379
          if (neg) {
431
            flags &= (0xff ^ DIR_FLAG_SORT);
380
            req->flags &= (0xff ^ DIR_FLAG_SORT);
432
            break;
381
            break;
433
          }
382
          }
434
          if (process_order_directive(arg+1) != 0) {
383
          if (dir_process_order_directive(arg+1) != 0) {
435
            nls_output_err(0, 3); /* invalid parameter format */
384
            nls_output_err(0, 3); /* invalid parameter format */
436
            output(": ");
385
            output(": ");
437
            outputnl(arg);
386
            outputnl(arg);
438
            return(CMD_FAIL);
387
            return(-1);
439
          }
388
          }
440
          flags |= DIR_FLAG_SORT;
389
          req->flags |= DIR_FLAG_SORT;
441
          break;
390
          break;
442
        case 'p':
391
        case 'p':
443
        case 'P':
392
        case 'P':
444
          flags |= DIR_FLAG_PAUSE;
393
          req->flags |= DIR_FLAG_PAUSE;
445
          if (neg) flags &= (0xff ^ DIR_FLAG_PAUSE);
394
          if (neg) req->flags &= (0xff ^ DIR_FLAG_PAUSE);
446
          break;
395
          break;
447
        case 's':
396
        case 's':
448
        case 'S':
397
        case 'S':
449
          /* TODO */
398
          /* TODO */
450
          outputnl("/S NOT IMPLEMENTED YET");
399
          outputnl("/S NOT IMPLEMENTED YET");
451
          return(CMD_FAIL);
400
          return(-1);
452
          break;
401
          break;
453
        case 'w':
402
        case 'w':
454
        case 'W':
403
        case 'W':
455
          format = DIR_OUTPUT_WIDE;
404
          req->format = DIR_OUTPUT_WIDE;
456
          break;
405
          break;
457
        default:
406
        default:
458
          nls_outputnl_err(0, 2); /* invalid switch */
407
          nls_outputnl_err(0, 2); /* invalid switch */
459
          return(CMD_FAIL);
408
          return(-1);
460
      }
409
      }
461
    } else {  /* filespec */
410
    } else {  /* filespec */
462
      if (filespecptr != NULL) {
411
      if (req->filespecptr != NULL) {
463
        nls_outputnl_err(0, 4); /* too many parameters */
412
        nls_outputnl_err(0, 4); /* too many parameters */
464
        return(CMD_FAIL);
413
        return(-1);
465
      }
414
      }
466
      filespecptr = p->argv[i];
415
      req->filespecptr = *argv;
467
    }
416
    }
468
  }
417
  }
469
 
418
 
470
  if (filespecptr == NULL) filespecptr = ".";
419
  if (req->filespecptr == NULL) req->filespecptr = ".";
-
 
420
  return(0);
-
 
421
}
-
 
422
 
-
 
423
 
-
 
424
static enum cmd_result cmd_dir(struct cmd_funcparam *p) {
-
 
425
  struct DTA *dta = (void *)0x80; /* set DTA to its default location at 80h in PSP */
-
 
426
  struct TINYDTA far *dtabuf = NULL; /* used to buffer results when sorting is enabled */
-
 
427
  unsigned short dtabufcount = 0;
-
 
428
  unsigned short i;
-
 
429
  unsigned short availrows;  /* counter of available rows on display (used for /P) */
-
 
430
  unsigned short screenw = screen_getwidth();
-
 
431
  unsigned short wcols = screenw / WCOLWIDTH; /* number of columns in wide mode */
-
 
432
  unsigned char wcolcount;
-
 
433
  struct {
-
 
434
    struct nls_patterns nls;
-
 
435
    char buff64[64];
-
 
436
    char path[128];
-
 
437
    unsigned short orderidx[65535 / sizeof(struct TINYDTA)];
-
 
438
  } *buf = (void *)(p->BUFFER);
-
 
439
  unsigned long summary_fcount = 0;
-
 
440
  unsigned long summary_totsz = 0;
-
 
441
  unsigned char drv = 0;
-
 
442
 
-
 
443
  struct dirrequest req;
-
 
444
 
-
 
445
  /* preload req with default values */
-
 
446
  bzero(&req, sizeof(req));
-
 
447
  req.attrfilter_may = DIR_ATTR_DEFAULT;
-
 
448
  /* req.attrfilter_must = 0;
-
 
449
  req.flags = 0; */
-
 
450
  req.format = DIR_OUTPUT_NORM;
-
 
451
 
-
 
452
  /* make sure there's no risk of buffer overflow */
-
 
453
  if (sizeof(buf) > p->BUFFERSZ) {
-
 
454
    outputnl("INTERNAL MEM ERROR IN " __FILE__);
-
 
455
    return(CMD_FAIL);
-
 
456
  }
-
 
457
 
-
 
458
  bzero(&glob_sortcmp_dat, sizeof(glob_sortcmp_dat));
-
 
459
 
-
 
460
  if (cmd_ishlp(p)) {
-
 
461
    nls_outputnl(37,0); /* "Displays a list of files and subdirectories in a directory" */
-
 
462
    outputnl("");
-
 
463
    nls_outputnl(37,1); /* "DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]" */
-
 
464
    outputnl("");
-
 
465
    nls_outputnl(37,2); /* "/P Pauses after each screenful of information" */
-
 
466
    nls_outputnl(37,3); /* "/W Uses wide list format" */
-
 
467
    outputnl("");
-
 
468
    nls_outputnl(37,4); /* "/A Displays files with specified attributes:" */
-
 
469
    nls_outputnl(37,5); /* "    D Directories            R Read-only files        H Hidden files" */
-
 
470
    nls_outputnl(37,6); /* "    A Ready for archiving    S System files           - prefix meaning "not"" */
-
 
471
    outputnl("");
-
 
472
    nls_outputnl(37,7); /* "/O List files in sorted order:" */
-
 
473
    nls_outputnl(37,8); /* "    N by name                S by size                E by extension" */
-
 
474
    nls_outputnl(37,9); /* "    D by date                G group dirs first       - prefix to reverse order" */
-
 
475
    outputnl("");
-
 
476
    nls_outputnl(37,10); /* "/S Displays files in specified directory and all subdirectories" */
-
 
477
    nls_outputnl(37,11); /* "/B Uses bare format (no heading information or summary)" */
-
 
478
    nls_outputnl(37,12); /* "/L Uses lowercases" */
-
 
479
    return(CMD_OK);
-
 
480
  }
-
 
481
 
-
 
482
  i = nls_getpatterns(&(buf->nls));
-
 
483
  if (i != 0) nls_outputnl_doserr(i);
-
 
484
 
-
 
485
  /* disable usage of thousands separator on narrow screens */
-
 
486
  if (screenw < 80) buf->nls.thousep[0] = 0;
-
 
487
 
-
 
488
  /* parse command line */
-
 
489
  if (dir_parse_cmdline(&req, p->argv) != 0) return(CMD_FAIL);
471
 
490
 
472
  availrows = screen_getheight() - 2;
491
  availrows = screen_getheight() - 2;
473
 
492
 
474
  /* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
493
  /* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
475
  if ((filespecptr[0] != 0) && (filespecptr[1] == ':') && (filespecptr[2] == 0)) {
494
  if ((req.filespecptr[0] != 0) && (req.filespecptr[1] == ':') && (req.filespecptr[2] == 0)) {
476
    if ((filespecptr[0] >= 'a') && (filespecptr[0] <= 'z')) {
495
    if ((req.filespecptr[0] >= 'a') && (req.filespecptr[0] <= 'z')) {
477
      buf->path[0] = filespecptr[0] - ('a' - 1);
496
      buf->path[0] = req.filespecptr[0] - ('a' - 1);
478
    } else {
497
    } else {
479
      buf->path[0] = filespecptr[0] - ('A' - 1);
498
      buf->path[0] = req.filespecptr[0] - ('A' - 1);
480
    }
499
    }
481
    i = curpathfordrv(buf->path, buf->path[0]);
500
    i = curpathfordrv(buf->path, buf->path[0]);
482
  } else {
501
  } else {
483
    i = file_truename(filespecptr, buf->path);
502
    i = file_truename(req.filespecptr, buf->path);
484
  }
503
  }
485
  if (i != 0) {
504
  if (i != 0) {
486
    nls_outputnl_doserr(i);
505
    nls_outputnl_doserr(i);
487
    return(CMD_FAIL);
506
    return(CMD_FAIL);
488
  }
507
  }
489
 
508
 
490
  if (format != DIR_OUTPUT_BARE) {
509
  if (req.format != DIR_OUTPUT_BARE) {
491
    drv = buf->path[0];
510
    drv = buf->path[0];
492
    if (drv >= 'a') {
511
    if (drv >= 'a') {
493
      drv -= 'a';
512
      drv -= 'a';
494
    } else {
513
    } else {
495
      drv -= 'A';
514
      drv -= 'A';
496
    }
515
    }
497
    cmd_vol_internal(drv, buf->buff64);
516
    cmd_vol_internal(drv, buf->buff64);
498
    sprintf(buf->buff64, svarlang_str(37,20)/*"Directory of %s"*/, buf->path);
517
    sprintf(buf->buff64, svarlang_str(37,20)/*"Directory of %s"*/, buf->path);
499
    /* trim at first '?', if any */
518
    /* trim at first '?', if any */
500
    for (i = 0; buf->buff64[i] != 0; i++) if (buf->buff64[i] == '?') buf->buff64[i] = 0;
519
    for (i = 0; buf->buff64[i] != 0; i++) if (buf->buff64[i] == '?') buf->buff64[i] = 0;
501
    outputnl(buf->buff64);
520
    outputnl(buf->buff64);
502
    outputnl("");
521
    outputnl("");
503
    availrows -= 3;
522
    availrows -= 3;
504
  }
523
  }
505
 
524
 
506
  /* if dir: append a backslash (also get its len) */
525
  /* if dir: append a backslash (also get its len) */
507
  i = path_appendbkslash_if_dir(buf->path);
526
  i = path_appendbkslash_if_dir(buf->path);
508
 
527
 
509
  /* if ends with a \ then append ????????.??? */
528
  /* if ends with a \ then append ????????.??? */
510
  if (buf->path[i - 1] == '\\') strcat(buf->path, "????????.???");
529
  if (buf->path[i - 1] == '\\') strcat(buf->path, "????????.???");
511
 
530
 
512
  /* ask DOS for list of files, but only with allowed attribs */
531
  /* ask DOS for list of files, but only with allowed attribs */
513
  i = findfirst(dta, buf->path, attrfilter_may);
532
  i = findfirst(dta, buf->path, req.attrfilter_may);
514
  if (i != 0) {
533
  if (i != 0) {
515
    nls_outputnl_doserr(i);
534
    nls_outputnl_doserr(i);
516
    return(CMD_FAIL);
535
    return(CMD_FAIL);
517
  }
536
  }
518
 
537
 
519
  /* if sorting is involved, then let's buffer all results (and sort them) */
538
  /* if sorting is involved, then let's buffer all results (and sort them) */
520
  if (flags & DIR_FLAG_SORT) {
539
  if (req.flags & DIR_FLAG_SORT) {
521
    /* allocate a memory buffer - try several sizes until one succeeds */
540
    /* allocate a memory buffer - try several sizes until one succeeds */
522
    const unsigned short memsz[] = {65500, 32000, 16000, 8000, 4000, 2000, 1000, 0};
541
    const unsigned short memsz[] = {65500, 32000, 16000, 8000, 4000, 2000, 1000, 0};
523
    unsigned short max_dta_bufcount = 0;
542
    unsigned short max_dta_bufcount = 0;
524
    for (i = 0; memsz[i] != 0; i++) {
543
    for (i = 0; memsz[i] != 0; i++) {
525
      dtabuf = _fmalloc(memsz[i]);
544
      dtabuf = _fmalloc(memsz[i]);
526
      if (dtabuf != NULL) break;
545
      if (dtabuf != NULL) break;
527
    }
546
    }
528
 
547
 
529
    if (dtabuf == NULL) {
548
    if (dtabuf == NULL) {
530
      nls_outputnl_doserr(8); /* out of memory */
549
      nls_outputnl_doserr(8); /* out of memory */
531
      return(CMD_FAIL);
550
      return(CMD_FAIL);
532
    }
551
    }
533
 
552
 
534
    /* remember the address so I can free it afterwards */
553
    /* remember the address so I can free it afterwards */
535
    glob_sortcmp_dat.dtabuf_root = dtabuf;
554
    glob_sortcmp_dat.dtabuf_root = dtabuf;
536
 
555
 
537
    /* compute the amount of DTAs I can buffer */
556
    /* compute the amount of DTAs I can buffer */
538
    max_dta_bufcount = memsz[i] / sizeof(struct TINYDTA);
557
    max_dta_bufcount = memsz[i] / sizeof(struct TINYDTA);
539
    /* printf("max_dta_bufcount = %u\n", max_dta_bufcount); */
558
    /* printf("max_dta_bufcount = %u\n", max_dta_bufcount); */
540
 
559
 
541
    do {
560
    do {
542
      /* filter out files with uninteresting attributes */
561
      /* filter out files with uninteresting attributes */
543
      if (filter_attribs(dta, attrfilter_must, attrfilter_may) == 0) continue;
562
      if (filter_attribs(dta, req.attrfilter_must, req.attrfilter_may) == 0) continue;
544
 
563
 
545
      /* normalize "size" of directories to zero because kernel returns garbage
564
      /* normalize "size" of directories to zero because kernel returns garbage
546
       * sizes for directories which might confuse the sorting routine later */
565
       * sizes for directories which might confuse the sorting routine later */
547
      if (dta->attr & DOS_ATTR_DIR) dta->size = 0;
566
      if (dta->attr & DOS_ATTR_DIR) dta->size = 0;
548
 
567
 
549
      _fmemcpy(&(dtabuf[dtabufcount]), ((char *)dta) + 22, sizeof(struct TINYDTA));
568
      _fmemcpy(&(dtabuf[dtabufcount]), ((char *)dta) + 22, sizeof(struct TINYDTA));
550
 
569
 
551
      /* save attribs in sec field, otherwise zero it (this field is not
570
      /* save attribs in sec field, otherwise zero it (this field is not
552
       * displayed and dropping the attr field saves 2 bytes per entry) */
571
       * displayed and dropping the attr field saves 2 bytes per entry) */
553
      dtabuf[dtabufcount++].time_sec2 = (dta->attr & 31);
572
      dtabuf[dtabufcount++].time_sec2 = (dta->attr & 31);
554
 
573
 
555
      /* do I have any space left? */
574
      /* do I have any space left? */
556
      if (dtabufcount == max_dta_bufcount) {
575
      if (dtabufcount == max_dta_bufcount) {
557
        //TODO some kind of user notification might be nice here
576
        //TODO some kind of user notification might be nice here
558
        //outputnl("TOO MANY ENTRIES FOR SORTING! LIST IS UNSORTED");
577
        //outputnl("TOO MANY ENTRIES FOR SORTING! LIST IS UNSORTED");
559
        break;
578
        break;
560
      }
579
      }
561
 
580
 
562
    } while (findnext(dta) == 0);
581
    } while (findnext(dta) == 0);
563
 
582
 
564
    /* sort the list - the tricky part is that my array is a far address while
583
    /* sort the list - the tricky part is that my array is a far address while
565
     * qsort works only with near pointers, so I have to use an ugly (and
584
     * qsort works only with near pointers, so I have to use an ugly (and
566
     * global) auxiliary table */
585
     * global) auxiliary table */
567
    for (i = 0; i < dtabufcount; i++) buf->orderidx[i] = i;
586
    for (i = 0; i < dtabufcount; i++) buf->orderidx[i] = i;
568
    qsort(buf->orderidx, dtabufcount, 2, &sortcmp);
587
    qsort(buf->orderidx, dtabufcount, 2, &sortcmp);
569
 
588
 
570
    /* preload first entry (last from orderidx, since entries are sorted in reverse) */
589
    /* preload first entry (last from orderidx, since entries are sorted in reverse) */
571
    dtabufcount--;
590
    dtabufcount--;
572
    _fmemcpy(((unsigned char *)dta) + 22, &(dtabuf[buf->orderidx[dtabufcount]]), sizeof(struct TINYDTA));
591
    _fmemcpy(((unsigned char *)dta) + 22, &(dtabuf[buf->orderidx[dtabufcount]]), sizeof(struct TINYDTA));
573
    dta->attr = dtabuf[buf->orderidx[dtabufcount]].time_sec2; /* restore attr from the abused time_sec2 field */
592
    dta->attr = dtabuf[buf->orderidx[dtabufcount]].time_sec2; /* restore attr from the abused time_sec2 field */
574
  }
593
  }
575
 
594
 
576
  wcolcount = 0; /* may be used for columns counting with wide mode */
595
  wcolcount = 0; /* may be used for columns counting with wide mode */
577
 
596
 
578
  for (;;) {
597
  for (;;) {
579
 
598
 
580
    /* filter out attributes (skip if entry comes from buffer, then it was already veted) */
599
    /* filter out attributes (skip if entry comes from buffer, then it was already veted) */
581
    if (filter_attribs(dta, attrfilter_must, attrfilter_may) == 0) continue;
600
    if (filter_attribs(dta, req.attrfilter_must, req.attrfilter_may) == 0) continue;
582
 
601
 
583
    /* turn string lcase (/L) */
602
    /* turn string lcase (/L) */
584
    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... */
603
    if (req.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... */
585
 
604
 
586
    summary_fcount++;
605
    summary_fcount++;
587
    if ((dta->attr & DOS_ATTR_DIR) == 0) summary_totsz += dta->size;
606
    if ((dta->attr & DOS_ATTR_DIR) == 0) summary_totsz += dta->size;
588
 
607
 
589
    switch (format) {
608
    switch (req.format) {
590
      case DIR_OUTPUT_NORM:
609
      case DIR_OUTPUT_NORM:
591
        /* print fname-space-extension (unless it's "." or "..", then print as-is) */
610
        /* print fname-space-extension (unless it's "." or "..", then print as-is) */
592
        if (dta->fname[0] == '.') {
611
        if (dta->fname[0] == '.') {
593
          output(dta->fname);
612
          output(dta->fname);
594
          i = strlen(dta->fname);
613
          i = strlen(dta->fname);
595
          while (i++ < 12) output(" ");
614
          while (i++ < 12) output(" ");
596
        } else {
615
        } else {
597
          file_fname2fcb(buf->buff64, dta->fname);
616
          file_fname2fcb(buf->buff64, dta->fname);
598
          memmove(buf->buff64 + 9, buf->buff64 + 8, 4);
617
          memmove(buf->buff64 + 9, buf->buff64 + 8, 4);
599
          buf->buff64[8] = ' ';
618
          buf->buff64[8] = ' ';
600
          output(buf->buff64);
619
          output(buf->buff64);
601
        }
620
        }
602
        output(" ");
621
        output(" ");
603
        /* either <DIR> or right aligned 10-chars byte size */
622
        /* either <DIR> or right aligned 10-chars byte size */
604
        memset(buf->buff64, ' ', 10);
623
        memset(buf->buff64, ' ', 10);
605
        if (dta->attr & DOS_ATTR_DIR) {
624
        if (dta->attr & DOS_ATTR_DIR) {
606
          strcpy(buf->buff64 + 10, svarlang_str(37,21));
625
          strcpy(buf->buff64 + 10, svarlang_str(37,21));
607
        } else {
626
        } else {
608
          nls_format_number(buf->buff64 + 10, dta->size, &(buf->nls));
627
          nls_format_number(buf->buff64 + 10, dta->size, &(buf->nls));
609
        }
628
        }
610
        output(buf->buff64 + strlen(buf->buff64) - 10);
629
        output(buf->buff64 + strlen(buf->buff64) - 10);
611
        /* two spaces and NLS DATE */
630
        /* two spaces and NLS DATE */
612
        buf->buff64[0] = ' ';
631
        buf->buff64[0] = ' ';
613
        buf->buff64[1] = ' ';
632
        buf->buff64[1] = ' ';
614
        if (screenw >= 80) {
633
        if (screenw >= 80) {
615
          nls_format_date(buf->buff64 + 2, dta->date_yr + 1980, dta->date_mo, dta->date_dy, &(buf->nls));
634
          nls_format_date(buf->buff64 + 2, dta->date_yr + 1980, dta->date_mo, dta->date_dy, &(buf->nls));
616
        } else {
635
        } else {
617
          nls_format_date(buf->buff64 + 2, (dta->date_yr + 80) % 100, dta->date_mo, dta->date_dy, &(buf->nls));
636
          nls_format_date(buf->buff64 + 2, (dta->date_yr + 80) % 100, dta->date_mo, dta->date_dy, &(buf->nls));
618
        }
637
        }
619
        output(buf->buff64);
638
        output(buf->buff64);
620
 
639
 
621
        /* one space and NLS TIME */
640
        /* one space and NLS TIME */
622
        nls_format_time(buf->buff64 + 1, dta->time_hour, dta->time_min, 0xff, &(buf->nls));
641
        nls_format_time(buf->buff64 + 1, dta->time_hour, dta->time_min, 0xff, &(buf->nls));
623
        outputnl(buf->buff64);
642
        outputnl(buf->buff64);
624
        break;
643
        break;
625
 
644
 
626
      case DIR_OUTPUT_WIDE: /* display in columns of 12 chars per item */
645
      case DIR_OUTPUT_WIDE: /* display in columns of 12 chars per item */
627
        i = strlen(dta->fname);
646
        i = strlen(dta->fname);
628
        if (dta->attr & DOS_ATTR_DIR) {
647
        if (dta->attr & DOS_ATTR_DIR) {
629
          i += 2;
648
          i += 2;
630
          output("[");
649
          output("[");
631
          output(dta->fname);
650
          output(dta->fname);
632
          output("]");
651
          output("]");
633
        } else {
652
        } else {
634
          output(dta->fname);
653
          output(dta->fname);
635
        }
654
        }
636
        while (i++ < WCOLWIDTH) output(" ");
655
        while (i++ < WCOLWIDTH) output(" ");
637
        if (++wcolcount == wcols) {
656
        if (++wcolcount == wcols) {
638
          wcolcount = 0;
657
          wcolcount = 0;
639
          outputnl("");
658
          outputnl("");
640
        } else {
659
        } else {
641
          availrows++; /* wide mode is the only one that does not write one line per file */
660
          availrows++; /* wide mode is the only one that does not write one line per file */
642
        }
661
        }
643
        break;
662
        break;
644
 
663
 
645
      case DIR_OUTPUT_BARE:
664
      case DIR_OUTPUT_BARE:
646
        outputnl(dta->fname);
665
        outputnl(dta->fname);
647
        break;
666
        break;
648
    }
667
    }
649
 
668
 
650
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
669
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
651
 
670
 
652
    /* take next entry, either from buf or disk */
671
    /* take next entry, either from buf or disk */
653
    if (dtabufcount > 0) {
672
    if (dtabufcount > 0) {
654
      dtabufcount--;
673
      dtabufcount--;
655
      _fmemcpy(((unsigned char *)dta) + 22, &(dtabuf[buf->orderidx[dtabufcount]]), sizeof(struct TINYDTA));
674
      _fmemcpy(((unsigned char *)dta) + 22, &(dtabuf[buf->orderidx[dtabufcount]]), sizeof(struct TINYDTA));
656
      dta->attr = dtabuf[buf->orderidx[dtabufcount]].time_sec2; /* restore attr from the abused time_sec2 field */
675
      dta->attr = dtabuf[buf->orderidx[dtabufcount]].time_sec2; /* restore attr from the abused time_sec2 field */
657
    } else {
676
    } else {
658
      if (findnext(dta) != 0) break;
677
      if (findnext(dta) != 0) break;
659
    }
678
    }
660
 
679
 
661
  }
680
  }
662
 
681
 
663
  if (wcolcount != 0) {
682
  if (wcolcount != 0) {
664
    outputnl(""); /* in wide mode make sure to end on a clear row */
683
    outputnl(""); /* in wide mode make sure to end on a clear row */
665
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
684
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
666
  }
685
  }
667
 
686
 
668
  /* print out summary (unless bare output mode) */
687
  /* print out summary (unless bare output mode) */
669
  if (format != DIR_OUTPUT_BARE) {
688
  if (req.format != DIR_OUTPUT_BARE) {
670
    unsigned short alignpos;
689
    unsigned short alignpos;
671
    unsigned char uint32maxlen = 13; /* 13 is the max len of a 32 bit number with thousand separators (4'000'000'000) */
690
    unsigned char uint32maxlen = 13; /* 13 is the max len of a 32 bit number with thousand separators (4'000'000'000) */
672
    if (screenw < 80) uint32maxlen = 10;
691
    if (screenw < 80) uint32maxlen = 10;
673
    /* x file(s) */
692
    /* x file(s) */
674
    memset(buf->buff64, ' ', uint32maxlen);
693
    memset(buf->buff64, ' ', uint32maxlen);
675
    i = nls_format_number(buf->buff64 + uint32maxlen, summary_fcount, &(buf->nls));
694
    i = nls_format_number(buf->buff64 + uint32maxlen, summary_fcount, &(buf->nls));
676
    alignpos = sprintf(buf->buff64 + uint32maxlen + i, " %s ", svarlang_str(37,22)/*"file(s)"*/);
695
    alignpos = sprintf(buf->buff64 + uint32maxlen + i, " %s ", svarlang_str(37,22)/*"file(s)"*/);
677
    output(buf->buff64 + i);
696
    output(buf->buff64 + i);
678
    /* xxxx bytes */
697
    /* xxxx bytes */
679
    i = nls_format_number(buf->buff64 + uint32maxlen, summary_totsz, &(buf->nls));
698
    i = nls_format_number(buf->buff64 + uint32maxlen, summary_totsz, &(buf->nls));
680
    output(buf->buff64 + i + 1);
699
    output(buf->buff64 + i + 1);
681
    output(" ");
700
    output(" ");
682
    nls_outputnl(37,23); /* "bytes" */
701
    nls_outputnl(37,23); /* "bytes" */
683
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
702
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
684
    /* xxxx bytes free */
703
    /* xxxx bytes free */
685
    i = cmd_dir_df(&summary_totsz, drv);
704
    i = cmd_dir_df(&summary_totsz, drv);
686
    if (i != 0) nls_outputnl_doserr(i);
705
    if (i != 0) nls_outputnl_doserr(i);
687
    alignpos += uint32maxlen * 2;
706
    alignpos += uint32maxlen * 2;
688
    memset(buf->buff64, ' ', alignpos); /* align the freebytes value to same column as totbytes */
707
    memset(buf->buff64, ' ', alignpos); /* align the freebytes value to same column as totbytes */
689
    i = nls_format_number(buf->buff64 + alignpos, summary_totsz, &(buf->nls));
708
    i = nls_format_number(buf->buff64 + alignpos, summary_totsz, &(buf->nls));
690
    output(buf->buff64 + i + 1);
709
    output(buf->buff64 + i + 1);
691
    output(" ");
710
    output(" ");
692
    nls_outputnl(37,24); /* "bytes free" */
711
    nls_outputnl(37,24); /* "bytes free" */
693
    if (flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
712
    if (req.flags & DIR_FLAG_PAUSE) dir_pagination(&availrows);
694
  }
713
  }
695
 
714
 
696
  /* free the buffer memory (if used) */
715
  /* free the buffer memory (if used) */
697
  if (glob_sortcmp_dat.dtabuf_root != NULL) _ffree(glob_sortcmp_dat.dtabuf_root);
716
  if (glob_sortcmp_dat.dtabuf_root != NULL) _ffree(glob_sortcmp_dat.dtabuf_root);
698
 
717
 
699
  return(CMD_OK);
718
  return(CMD_OK);
700
}
719
}
701
 
720