]> git.sur5r.net Git - cc65/blob - libsrc/common/_fopen.s
Removed closeallstreams
[cc65] / libsrc / common / _fopen.s
1 ;
2 ; Ullrich von Bassewitz, 22.11.2002
3 ;
4 ; FILE* __fastcall__ _fopen (const char* name, const char* mode, FILE* f);
5 ; /* Open the specified file and fill the descriptor values into f */
6 ;
7
8         .export         __fopen
9
10         .import         _open
11         .import         pushax, incsp4, return0
12         .importzp       sp, ptr1
13
14
15         .include        "errno.inc"
16         .include        "fcntl.inc"
17         .include        "_file.inc"
18
19
20 ; ------------------------------------------------------------------------
21 ; Code
22
23 .proc   __fopen
24
25         sta     f
26         stx     f+1             ; Save f
27
28 ; Get a pointer to the mode string
29
30         ldy     #1
31         lda     (sp),y
32         sta     ptr1+1
33         dey
34         lda     (sp),y
35         sta     ptr1
36
37 ; Look at the first character in mode
38
39         ldx     #$00            ; Mode will be in X
40         lda     (ptr1),y        ; Get first char from mode
41         cmp     #'w'
42         bne     @L1
43         ldx     #(O_WRONLY | O_CREAT | O_TRUNC)
44         bne     @L3
45 @L1:    cmp     #'r'
46         bne     @L2
47         ldx     #O_RDONLY
48         bne     @L3
49 @L2:    cmp     #'a'
50         bne     invmode
51         ldx     #(O_WRONLY | O_CREAT | O_APPEND)
52
53 ; Look at more chars from the mode string
54
55 @L3:    iny                     ; Next char
56         beq     invmode
57         lda     (ptr1),y
58         beq     modeok          ; End of mode string reached
59         cmp     #'+'
60         bne     @L4
61         txa
62         ora     #O_RDWR         ; Always do r/w in addition to anything else
63         tax
64         bne     @L3
65 @L4:    cmp     #'b'
66         beq     @L3             ; Binary mode is ignored
67
68 ; Invalid mode
69
70 invmode:
71         lda     #EINVAL
72         sta     __errno
73         lda     #0
74         sta     __errno+1
75         tax
76         jmp     incsp4
77
78 ; Mode string successfully parsed. Store the binary mode onto the stack in
79 ; the same place where the mode string pointer was before. The call open()
80
81 modeok: ldy     #$00
82         txa                     ; Mode -> A
83         sta     (sp),y
84         tya
85         iny
86         sta     (sp),y
87         ldy     #4              ; Size of arguments in bytes
88         jsr     _open           ; Will cleanup the stack
89
90 ; Check the result of the open() call
91
92         cpx     #$FF
93         bne     openok
94         cmp     #$FF
95         bne     openok
96         jmp     return0         ; Failure, errno/_oserror already set
97
98 ; Open call succeeded
99
100 openok: ldy     f
101         sty     ptr1
102         ldy     f+1
103         sty     ptr1+1
104         ldy     #_FILE_f_fd
105         sta     (ptr1),y        ; f->f_fd = fd;
106         ldy     #_FILE_f_flags
107         lda     #_FOPEN
108         sta     (ptr1),y        ; f->f_flags = _FOPEN;
109
110 ; Return the pointer to the file structure
111
112         lda     ptr1
113         ldx     ptr1+1
114         rts
115
116 .endproc
117
118 ; ------------------------------------------------------------------------
119 ; Data
120
121 .bss
122 f:      .res    2
123
124