Subversion Repositories SvarDOS

Rev

Rev 364 | Go to most recent revision | Details | 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
 
13
static int cmd_cd(int argc, char const **argv) {
14
  /* two arguments max */
15
  if (argc > 2) {
16
    puts("Too many parameters");
17
  }
18
 
19
  /* no argument? display current drive and dir ("CWD") */
20
  if (argc == 1) {
21
    char buff[64];
22
    char *buffptr = buff;
23
    _asm {
24
      push ax
25
      push dx
26
      push si
27
      mov ah, 0x19  /* get current default drive */
28
      int 0x21      /* al = drive (00h = A:, 01h = B:, etc) */
29
      add al, 'A'
30
      /* print drive to stdout */
31
      mov dl, al
32
      mov ah, 0x02
33
      int 0x21
34
      mov dl, ':'
35
      int 0x21
36
      mov dl, '\'
37
      int 0x21
38
      /* get current dir */
39
      mov ah, 0x47
40
      xor dl, dl       /* select drive (0 = current drive) */
41
      mov si, buffptr  /* 64-byte buffer for ASCIZ pathname */
42
      int 0x21
43
      pop ax
44
      pop dx
45
      pop si
46
    }
47
    puts(buff);
48
  }
49
 
50
  /* argument can be either a drive (D:) or a path */
51
  if (argc == 2) {
52
    /* drive (CD B:) */
53
    if ((argv[1][0] != '\\') && (argv[1][1] == ':') && (argv[1][2] == 0)) {
54
      char buff[64];
55
      char *buffptr = buff;
56
      unsigned char drive = argv[1][0];
57
      unsigned short err = 0;
58
      if (drive >= 'a') {
59
        drive -= 'a';
60
      } else {
61
        drive -= 'A';
62
      }
63
      drive++; /* A: = 1, B: = 2, etc*/
64
      _asm {
65
        push si
66
        push ax
67
        push dx
68
        mov ah, 0x47      /* get current directory */
69
        mov dl, [drive]   /* A: = 1, B: = 2, etc */
70
        mov si, buffptr
71
        int 0x21
72
        jnc DONE
73
        mov [err], ax
74
        DONE:
75
        pop dx
76
        pop ax
77
        pop si
78
      }
79
      if (err != 0) {
80
        if (err != 0) puts(doserr(err));
81
      } else {
82
        printf("%c:\\%s\r\n", drive + 'A' - 1, buff);
83
      }
84
    } else { /* path */
85
      char const *dir = argv[1];
86
      unsigned short err = 0;
87
      _asm {
88
        push dx
89
        push ax
90
        mov ah, 0x3B  /* CHDIR (set current directory) */
91
        mov dx, dir
92
        int 0x21
93
        jnc DONE
94
        mov [err], ax
95
        DONE:
96
        pop ax
97
        pop dx
98
      }
99
      if (err != 0) puts(doserr(err));
100
    }
101
  }
102
 
103
  return(-1);
104
}