Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
421 mateuszvis 1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
23
 */
24
 
392 mateuszvis 25
/*
26
 * del/erase
27
 */
28
 
29
static int cmd_del(struct cmd_funcparam *p) {
30
  const char *delspec = NULL;
31
  unsigned short err = 0;
32
  unsigned short confirmflag = 0;
33
  unsigned short i;
34
  unsigned short pathlimit = 0;
35
  char *buff = p->BUFFER;
36
 
37
  struct DTA *dta = (void *)0x80; /* use the default DTA at location 80h in PSP */
38
  char *fname = dta->fname;
39
 
40
  if (cmd_ishlp(p)) {
41
    outputnl("Deletes one or more files.");
42
    outputnl("");
43
    outputnl("DEL [drive:][path]filename [/P]");
44
    outputnl("ERASE [drive:][path]filename [/P]");
45
    outputnl("");
46
    outputnl("[drive:][path]filename  Specifies the file(s) to delete.");
47
    outputnl("/P  Prompts for confirmation before deleting each file.");
48
    return(-1);
49
  }
50
 
51
  if (p->argc == 0) {
52
    outputnl("Required parameter missing");
53
    return(-1);
54
  }
55
 
56
  /* scan argv for delspec and possible /p or /v */
57
  for (i = 0; i < p->argc; i++) {
58
    /* delspec? */
59
    if (p->argv[i][0] == '/') {
60
      if (imatch(p->argv[i], "/p")) {
61
        confirmflag = 1;
62
      } else {
63
        output("Invalid switch:");
64
        output(" ");
65
        outputnl(p->argv[i]);
66
        return(-1);
67
      }
68
    } else if (delspec != NULL) { /* otherwise its a delspec */
69
      outputnl("Too many parameters");
70
      return(-1);
71
    } else {
72
      delspec = p->argv[i];
73
    }
74
  }
75
 
76
  /* convert path to canonical form */
77
  file_truename(delspec, buff);
78
 
79
  /* is delspec pointing at a directory? if so, add a \*.* */
80
  { int attr = file_getattr(delspec);
81
    if ((attr > 0) && (attr & DOS_ATTR_DIR)) strcat(buff, "\\????????.???");
82
  }
83
 
84
  /* parse delspec in buff and remember where last backslash or slash is */
85
  for (i = 0; buff[i] != 0; i++) if (buff[i] == '\\') pathlimit = i + 1;
86
 
87
  /* is this about deleting all content inside a directory? if no per-file
88
   * confirmation set, ask for a global confirmation */
89
  if ((confirmflag == 0) && (imatch(buff + pathlimit, "????????.???"))) {
90
    outputnl("All files in directory will be deleted!");
91
    if (askchoice("Are you sure (Y/N)?", "YN") != 0) return(-1);
92
  }
93
 
94
  for (i = 0;; i = 1) {
95
 
96
    /* exec FindFirst or FindNext */
97
    if (i == 0) {
98
      err = findfirst(dta, buff, DOS_ATTR_RO | DOS_ATTR_SYS | DOS_ATTR_HID);
99
    } else {
100
      err = findnext(dta);
101
    }
102
 
103
    if (err != 0) break;
104
 
105
    /* ask if confirmation required: PLIK.TXT  Delete (Y/N)? */
106
    if (confirmflag) {
107
      strcpy(buff + pathlimit, fname); /* note: buff contained the search pattern but it no longer needed so I can reuse it now */
108
      output(buff);
109
      output(" \t");
110
      if (askchoice("Delete (Y/N)?", "YN") != 0) continue;
111
    }
112
 
113
    /* del found file */
114
    _asm {
115
      mov ah, 0x41      /* delete a file, DS:DX points to an ASCIIZ filespec (no wildcards allowed) */
116
      mov dx, fname
117
      int 0x21
118
      jnc DONE
119
      mov [err], ax
120
      DONE:
121
    }
122
 
123
    if (err != 0) {
124
      output(fname);
125
      output(": ");
126
      outputnl(doserr(err));
127
      break;
128
    }
129
  }
130
  return(-1);
131
}