Subversion Repositories SvarDOS

Rev

Rev 403 | Rev 420 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
368 mateuszvis 1
/*
2
 * dir
3
 *
4
 * Displays a list of files and subdirectories in a directory.
5
 *
6
 * DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]
7
 *
8
 * /P Pauses after each screenful of information.
9
 * /W Uses wide list format.
10
 *
11
 * /A Displays file with specified attributes:
12
 *     D Directories           R Read-only files     H Hidden files
13
 *     A Ready for archiving   S System files        - prefix meaning "not"
14
 *
15
 * /O List files in sorted order:
16
 *     N by name            S by size              E by extension
17
 *     D by date            G group dirs first     - prefix to reverse order
18
 *
19
 * /S Displays files in specified directory and all subdirectories.
20
 * /B Uses bare format (no heading information or summary)
21
 * /L Uses lowercases
22
 */
23
 
396 mateuszvis 24
/* NOTE: /A attributes are matched in an exclusive way, ie. only files with
25
 *       the specified attributes are matched. This is different from how DOS
26
 *       itself matches attributes hence DIR cannot rely on the attributes
27
 *       filter within FindFirst.
28
 *
29
 * NOTE: Multiple /A are not supported - only the last one is significant.
30
 */
31
 
372 mateuszvis 32
static int cmd_dir(struct cmd_funcparam *p) {
393 mateuszvis 33
  const char *filespecptr = NULL;
388 mateuszvis 34
  struct DTA *dta = (void *)0x80; /* set DTA to its default location at 80h in PSP */
417 mateuszvis 35
  unsigned short i;
396 mateuszvis 36
  unsigned short availrows;  /* counter of available rows on display (used for /P) */
37
  #define DIR_FLAG_PAUSE  1
38
  #define DIR_FLAG_WIDE   2
39
  #define DIR_FLAG_RECUR  4
40
  #define DIR_FLAG_BARE   8
41
  #define DIR_FLAG_LCASE 16
42
  unsigned char flags = 0;
43
//  unsigned char attribs_show = 0; /* show only files with ALL these attribs */
44
//  unsigned char attribs_hide = 0; /* hide files with ANY of these attribs */
368 mateuszvis 45
 
390 mateuszvis 46
  if (cmd_ishlp(p)) {
396 mateuszvis 47
    outputnl("Displays a list of files and subdirectories in a directory");
48
    outputnl("");
49
    outputnl("DIR [drive:][path][filename] [/P] [/W] [/A[:]attributes] [/O[[:]sortorder]] [/S] [/B] [/L]");
50
    outputnl("");
51
    outputnl("/P Pauses after each screenful of information");
52
    outputnl("/W Uses wide list format");
53
    outputnl("");
54
    outputnl("/A Displays files with specified attributes:");
55
    outputnl("    D Directories            R Read-only files        H Hidden files");
56
    outputnl("    A Ready for archiving    S System files           - prefix meaning \"not\"");
57
    outputnl("");
58
    outputnl("/O List files in sorted order:");
59
    outputnl("    N by name                S by size                E by extension");
60
    outputnl("    D by date                G group dirs first       - prefix to reverse order");
61
    outputnl("");
62
    outputnl("/S Displays files in specified directory and all subdirectories");
63
    outputnl("/B Uses bare format (no heading information or summary)");
64
    outputnl("/L Uses lowercases");
65
 
66
    /* TODO FIXME REMOVE THIS ONCE ALL IMPLEMENTED */
67
    outputnl("\r\n*** THIS COMMAND IS NOT FULLY IMPLEMENTED YET ***");
68
 
390 mateuszvis 69
    return(-1);
70
  }
71
 
393 mateuszvis 72
  /* parse command line */
73
  for (i = 0; i < p->argc; i++) {
74
    if (p->argv[i][0] == '/') {
396 mateuszvis 75
      char arg;
76
      char neg = 0;
77
      /* detect negations and get actual argument */
78
      if (p->argv[i][1] == '-') neg = 1;
79
      arg = p->argv[i][1 + neg];
80
      /* */
81
      switch (arg) {
82
        case 'a':
83
        case 'A':
84
          /* TODO */
85
          outputnl("/A NOT IMPLEMENTED YET");
86
          return(-1);
87
          break;
399 mateuszvis 88
        case 'b':
89
        case 'B':
90
          flags |= DIR_FLAG_BARE;
91
          break;
396 mateuszvis 92
        case 'p':
93
        case 'P':
94
          flags |= DIR_FLAG_PAUSE;
95
          if (neg) flags &= (0xff ^ DIR_FLAG_PAUSE);
96
          break;
393 mateuszvis 97
        default:
98
          outputnl("Invalid switch");
99
          return(-1);
100
      }
101
    } else {  /* filespec */
102
      if (filespecptr != NULL) {
103
        outputnl("Too many parameters");
104
        return(-1);
105
      }
106
      filespecptr = p->argv[i];
107
    }
108
  }
368 mateuszvis 109
 
393 mateuszvis 110
  if (filespecptr == NULL) filespecptr = ".";
111
 
417 mateuszvis 112
  /* special case: "DIR drive:" (truename() fails on "C:" under MS-DOS 6.0) */
113
  if ((filespecptr[0] != 0) && (filespecptr[1] == ':') && (filespecptr[2] == 0)) {
114
    if ((filespecptr[0] >= 'a') && (filespecptr[0] <= 'z')) {
115
      p->BUFFER[0] = filespecptr[0] - ('a' - 1);
116
    } else {
117
      p->BUFFER[0] = filespecptr[0] - ('A' - 1);
399 mateuszvis 118
    }
417 mateuszvis 119
    i = curpathfordrv(p->BUFFER, p->BUFFER[0]);
120
  } else {
121
    i = file_truename(filespecptr, p->BUFFER);
399 mateuszvis 122
  }
417 mateuszvis 123
  if (i != 0) {
124
    outputnl(doserr(i));
125
    return(-1);
126
  }
393 mateuszvis 127
 
399 mateuszvis 128
  if ((flags & DIR_FLAG_BARE) == 0) {
129
    unsigned char drv = p->BUFFER[0];
403 mateuszvis 130
    char *buff2 = p->BUFFER + (BUFFER_SIZE / 2);
399 mateuszvis 131
    if (drv >= 'a') {
132
      drv -= 'a';
133
    } else {
134
      drv -= 'A';
135
    }
403 mateuszvis 136
    cmd_vol_internal(drv, buff2);
137
    sprintf(buff2, "Directory of %s", p->BUFFER);
399 mateuszvis 138
    /* trim at first '?', if any */
403 mateuszvis 139
    for (i = 0; buff2[i] != 0; i++) if (buff2[i] == '?') buff2[i] = 0;
140
    outputnl(buff2);
399 mateuszvis 141
    outputnl("");
142
  }
143
 
417 mateuszvis 144
  /* if dir: append a backslash (also get its len) */
145
  i = path_appendbkslash_if_dir(p->BUFFER);
393 mateuszvis 146
 
417 mateuszvis 147
  /* if ends with a \ then append ????????.??? */
148
  if (p->BUFFER[i - 1] == '\\') strcat(p->BUFFER, "????????.???");
393 mateuszvis 149
 
417 mateuszvis 150
  i = findfirst(dta, p->BUFFER, DOS_ATTR_RO | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_DIR | DOS_ATTR_ARC);
151
  if (i != 0) {
152
    outputnl(doserr(i));
153
    return(-1);
154
  }
155
 
396 mateuszvis 156
  availrows = screen_getheight();
157
 
369 mateuszvis 158
  outputnl(dta->fname);
396 mateuszvis 159
  availrows--;
368 mateuszvis 160
 
396 mateuszvis 161
  while (findnext(dta) == 0) {
162
    outputnl(dta->fname);
163
    if (flags & DIR_FLAG_PAUSE) {
164
      availrows--;
165
      if (availrows < 2) {
166
        press_any_key();
167
        availrows = screen_getheight();
168
      }
169
    }
170
  }
368 mateuszvis 171
 
172
  return(-1);
173
}