Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
392 mateuszvis 1
/*
2
 * del/erase
3
 */
4
 
5
static int cmd_del(struct cmd_funcparam *p) {
6
  const char *delspec = NULL;
7
  unsigned short err = 0;
8
  unsigned short confirmflag = 0;
9
  unsigned short i;
10
  unsigned short pathlimit = 0;
11
  char *buff = p->BUFFER;
12
 
13
  struct DTA *dta = (void *)0x80; /* use the default DTA at location 80h in PSP */
14
  char *fname = dta->fname;
15
 
16
  if (cmd_ishlp(p)) {
17
    outputnl("Deletes one or more files.");
18
    outputnl("");
19
    outputnl("DEL [drive:][path]filename [/P]");
20
    outputnl("ERASE [drive:][path]filename [/P]");
21
    outputnl("");
22
    outputnl("[drive:][path]filename  Specifies the file(s) to delete.");
23
    outputnl("/P  Prompts for confirmation before deleting each file.");
24
    return(-1);
25
  }
26
 
27
  if (p->argc == 0) {
28
    outputnl("Required parameter missing");
29
    return(-1);
30
  }
31
 
32
  /* scan argv for delspec and possible /p or /v */
33
  for (i = 0; i < p->argc; i++) {
34
    /* delspec? */
35
    if (p->argv[i][0] == '/') {
36
      if (imatch(p->argv[i], "/p")) {
37
        confirmflag = 1;
38
      } else {
39
        output("Invalid switch:");
40
        output(" ");
41
        outputnl(p->argv[i]);
42
        return(-1);
43
      }
44
    } else if (delspec != NULL) { /* otherwise its a delspec */
45
      outputnl("Too many parameters");
46
      return(-1);
47
    } else {
48
      delspec = p->argv[i];
49
    }
50
  }
51
 
52
  /* convert path to canonical form */
53
  file_truename(delspec, buff);
54
 
55
  /* is delspec pointing at a directory? if so, add a \*.* */
56
  { int attr = file_getattr(delspec);
57
    if ((attr > 0) && (attr & DOS_ATTR_DIR)) strcat(buff, "\\????????.???");
58
  }
59
 
60
  /* parse delspec in buff and remember where last backslash or slash is */
61
  for (i = 0; buff[i] != 0; i++) if (buff[i] == '\\') pathlimit = i + 1;
62
 
63
  /* is this about deleting all content inside a directory? if no per-file
64
   * confirmation set, ask for a global confirmation */
65
  if ((confirmflag == 0) && (imatch(buff + pathlimit, "????????.???"))) {
66
    outputnl("All files in directory will be deleted!");
67
    if (askchoice("Are you sure (Y/N)?", "YN") != 0) return(-1);
68
  }
69
 
70
  for (i = 0;; i = 1) {
71
 
72
    /* exec FindFirst or FindNext */
73
    if (i == 0) {
74
      err = findfirst(dta, buff, DOS_ATTR_RO | DOS_ATTR_SYS | DOS_ATTR_HID);
75
    } else {
76
      err = findnext(dta);
77
    }
78
 
79
    if (err != 0) break;
80
 
81
    /* ask if confirmation required: PLIK.TXT  Delete (Y/N)? */
82
    if (confirmflag) {
83
      strcpy(buff + pathlimit, fname); /* note: buff contained the search pattern but it no longer needed so I can reuse it now */
84
      output(buff);
85
      output(" \t");
86
      if (askchoice("Delete (Y/N)?", "YN") != 0) continue;
87
    }
88
 
89
    /* del found file */
90
    _asm {
91
      mov ah, 0x41      /* delete a file, DS:DX points to an ASCIIZ filespec (no wildcards allowed) */
92
      mov dx, fname
93
      int 0x21
94
      jnc DONE
95
      mov [err], ax
96
      DONE:
97
    }
98
 
99
    if (err != 0) {
100
      output(fname);
101
      output(": ");
102
      outputnl(doserr(err));
103
      break;
104
    }
105
  }
106
  return(-1);
107
}