Subversion Repositories SvarDOS

Rev

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