Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
382 mateuszvis 1
/*
2
 * type
3
 */
4
 
5
static int cmd_type(struct cmd_funcparam *p) {
6
  char *buff = p->BUFFER;
7
  const char *fname = p->argv[0];
8
  unsigned short err = 0;
9
 
387 mateuszvis 10
  if (cmd_ishlp(p)) {
382 mateuszvis 11
    outputnl("Displays the contents of a text file.");
12
    outputnl("");
13
    outputnl("TYPE [drive:][path]filename");
14
    return(-1);
15
  }
16
 
387 mateuszvis 17
  if (p->argc == 0) {
18
    outputnl("Required parameter missing");
19
    return(-1);
20
  }
21
 
382 mateuszvis 22
  if (p->argc > 1) {
23
    outputnl("Too many parameters");
24
    return(-1);
25
  }
26
 
27
  /* if here then display the file */
28
  _asm {
29
    push ax
30
    push bx
31
    push cx
32
    push dx
33
    push si
34
 
35
    mov ax, 0x3d00 /* open file via handle, access mode in AL (0 = read) */
36
    mov dx, fname
37
    int 0x21       /* file handle in ax on success (CF clear) */
38
    jnc FILE_OPEN_OK
39
    mov [err], ax  /* on error AX contains the DOS err code */
40
    jmp FOPENFAIL
41
    FILE_OPEN_OK:
42
    /* copy obtained file handle to BX */
43
    mov bx, ax
44
 
45
    READNEXTBLOCK:
46
    /* read file block by block */
47
    mov cx, 1024   /* read 1K at a time */
48
    mov dx, buff
49
    mov ah, 0x3f   /* read CX bytes from file handle in BX and write to DS:DX */
50
    int 0x21       /* CF set on error, AX=errno or AX=number of bytes read */
51
    jc GOTERROR    /* abort on error */
52
    test ax, ax    /* EOF? */
53
    jz ENDFILE
54
    /* display read block (AX=len) */
55
    mov si, dx     /* preset DS:SI to DS:DX (DL will be reused soon) */
56
    mov cx, ax     /* set loop count to CX */
57
    mov ah, 0x02   /* write character in DL to stdout */
58
    NEXTCHAR:
59
    mov dl, [si]
60
    inc si
61
    int 0x21
62
    loopnz NEXTCHAR /* CX-- ; jnz NEXTCHAR (display CX characters) */
63
    /* read (and display) next block */
64
    jmp READNEXTBLOCK
65
 
66
    GOTERROR:
67
    mov [err], ax
68
 
69
    ENDFILE:
70
    /* close file */
71
    mov ah, 0x3e   /* close file handle (file handle already in BX) */
72
    int 0x21
73
 
74
    FOPENFAIL:
75
 
76
    pop si
77
    pop dx
78
    pop cx
79
    pop bx
80
    pop ax
81
  }
82
 
83
  if (err != 0) outputnl(doserr(err));
84
 
85
  return(-1);
86
}