Subversion Repositories SvarDOS

Rev

Rev 363 | Rev 369 | 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
 
364 mateuszvis 13
static int cmd_cd(const struct cmd_funcparam *p) {
363 mateuszvis 14
  /* two arguments max */
364 mateuszvis 15
  if (p->argc > 1) {
363 mateuszvis 16
    puts("Too many parameters");
17
  }
18
 
19
  /* no argument? display current drive and dir ("CWD") */
364 mateuszvis 20
  if (p->argc == 0) {
363 mateuszvis 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
364 mateuszvis 43
      pop si
44
      pop dx
363 mateuszvis 45
      pop ax
46
    }
47
    puts(buff);
48
  }
49
 
50
  /* argument can be either a drive (D:) or a path */
364 mateuszvis 51
  if (p->argc == 1) {
52
    const char *arg = p->argv[0];
53
    unsigned short err = 0;
363 mateuszvis 54
    /* drive (CD B:) */
364 mateuszvis 55
    if ((arg[0] != '\\') && (arg[1] == ':') && (arg[2] == 0)) {
363 mateuszvis 56
      char buff[64];
57
      char *buffptr = buff;
364 mateuszvis 58
      unsigned char drive = arg[0];
363 mateuszvis 59
      if (drive >= 'a') {
60
        drive -= 'a';
61
      } else {
62
        drive -= 'A';
63
      }
64
      drive++; /* A: = 1, B: = 2, etc*/
65
      _asm {
66
        push si
67
        push ax
68
        push dx
69
        mov ah, 0x47      /* get current directory */
70
        mov dl, [drive]   /* A: = 1, B: = 2, etc */
71
        mov si, buffptr
72
        int 0x21
73
        jnc DONE
74
        mov [err], ax
75
        DONE:
76
        pop dx
77
        pop ax
78
        pop si
79
      }
364 mateuszvis 80
      if (err == 0) printf("%c:\\%s\r\n", drive + 'A' - 1, buff);
363 mateuszvis 81
    } else { /* path */
82
      _asm {
83
        push dx
84
        push ax
85
        mov ah, 0x3B  /* CHDIR (set current directory) */
364 mateuszvis 86
        mov dx, arg
363 mateuszvis 87
        int 0x21
88
        jnc DONE
89
        mov [err], ax
90
        DONE:
91
        pop ax
92
        pop dx
93
      }
94
    }
364 mateuszvis 95
    if (err != 0) puts(doserr(err));
363 mateuszvis 96
  }
97
 
98
  return(-1);
99
}