Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
363 mateuszvis 1
/*
2
 * chdir
3
 *
4
 * displays the name of or changes the current directory.
5
 *
6
 * CHDIR [drive:][path]
7
 * CD..
8
 *
9
 * Type CD drive: to display the current directory in the specified drive.
10
 * Type CD without parameters to display the current drive and directory.
11
 */
12
 
375 mateuszvis 13
 
14
/* display current path drive d (A=1, B=2, etc)
15
 * returns 0 on success, doserr otherwise */
16
static unsigned short cmd_cd_curpathfordrv(char *buff, unsigned char d) {
17
  unsigned short r = 0;
18
 
19
  buff[0] = d + 'A' - 1;
20
  buff[1] = ':';
21
  buff[2] = '\\';
22
 
23
  _asm {
24
    push si
25
    push ax
26
    push dx
27
    mov ah, 0x47      /* get current directory */
28
    mov dl, [d]       /* A: = 1, B: = 2, etc */
29
    mov si, buff      /* append cur dir to buffer */
30
    add si, 3         /* skip the present drive:\ prefix */
31
    int 0x21
32
    jnc DONE
33
    mov [r], ax       /* copy result from ax */
34
    DONE:
35
    pop dx
36
    pop ax
37
    pop si
38
  }
39
 
40
  return(r);
41
}
42
 
43
 
372 mateuszvis 44
static int cmd_cd(struct cmd_funcparam *p) {
374 mateuszvis 45
  char *buffptr = p->BUFFER;
46
 
47
  /* CD /? */
387 mateuszvis 48
  if (cmd_ishlp(p)) {
374 mateuszvis 49
    outputnl("Displays the name of or changes the current directory.");
50
    outputnl("");
51
    outputnl("CHDIR [drive:][path]");
52
    outputnl("CHDIR[..]");
53
    outputnl("CD [drive:][path]");
54
    outputnl("CD[..]");
55
    outputnl("");
56
    outputnl(".. Specifies that you want to change to the parent directory.");
57
    outputnl("");
58
    outputnl("Type CD drive: to display the current directory in the specified drive.");
59
    outputnl("Type CD without parameters to display the current drive and directory.");
60
    return(-1);
61
  }
62
 
387 mateuszvis 63
  /* one argument max */
64
  if (p->argc > 1) {
65
    outputnl("Too many parameters");
66
    return(-1);
67
  }
68
 
363 mateuszvis 69
  /* no argument? display current drive and dir ("CWD") */
364 mateuszvis 70
  if (p->argc == 0) {
375 mateuszvis 71
    unsigned char drv = 0;
72
 
363 mateuszvis 73
    _asm {
74
      push ax
75
      push dx
76
      push si
77
      mov ah, 0x19  /* get current default drive */
78
      int 0x21      /* al = drive (00h = A:, 01h = B:, etc) */
375 mateuszvis 79
      inc al        /* convert to 1=A, 2=B, etc */
80
      mov [drv], al
363 mateuszvis 81
    }
375 mateuszvis 82
 
83
    cmd_cd_curpathfordrv(buffptr, drv);
374 mateuszvis 84
    outputnl(buffptr);
375 mateuszvis 85
 
374 mateuszvis 86
    return(-1);
363 mateuszvis 87
  }
88
 
89
  /* argument can be either a drive (D:) or a path */
364 mateuszvis 90
  if (p->argc == 1) {
91
    const char *arg = p->argv[0];
92
    unsigned short err = 0;
363 mateuszvis 93
    /* drive (CD B:) */
364 mateuszvis 94
    if ((arg[0] != '\\') && (arg[1] == ':') && (arg[2] == 0)) {
95
      unsigned char drive = arg[0];
363 mateuszvis 96
      if (drive >= 'a') {
375 mateuszvis 97
        drive -= ('a' - 1);
363 mateuszvis 98
      } else {
375 mateuszvis 99
        drive -= ('A' - 1);
363 mateuszvis 100
      }
375 mateuszvis 101
 
102
      err = cmd_cd_curpathfordrv(buffptr, drive);
103
      if (err == 0) outputnl(buffptr);
363 mateuszvis 104
    } else { /* path */
105
      _asm {
106
        push dx
107
        push ax
108
        mov ah, 0x3B  /* CHDIR (set current directory) */
364 mateuszvis 109
        mov dx, arg
363 mateuszvis 110
        int 0x21
111
        jnc DONE
112
        mov [err], ax
113
        DONE:
114
        pop ax
115
        pop dx
116
      }
117
    }
369 mateuszvis 118
    if (err != 0) {
119
      outputnl(doserr(err));
120
    }
363 mateuszvis 121
  }
122
 
123
  return(-1);
124
}