Subversion Repositories SvarDOS

Rev

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

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