]> git.sur5r.net Git - cc65/blob - libsrc/common/_fopen.c
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / libsrc / common / _fopen.c
1 /*
2  * _fopen.c
3  *
4  * Ullrich von bassewitz, 17.06.1997
5  */
6
7
8
9 #include <fcntl.h>
10 #include <errno.h>
11 #include "_file.h"
12
13
14
15 static unsigned char amode_to_bmode (const char* mode)
16 /* Convert ASCII mode (like for fopen) to binary mode (for open) */
17 {
18     char          c;
19     char          flag = 0;
20     unsigned char binmode = 0;
21
22     while (c = *mode++) {
23         switch(c) {
24             case 'w':
25                 binmode = O_WRONLY;
26                 break;
27             case 'r':
28                 binmode = O_RDONLY;
29                 break;
30             case '+':
31                 binmode = O_RDWR;
32                 break;
33             /* a,b missing */
34         }
35     }
36     if (binmode == 0) {
37         _errno = EINVAL;
38     }
39     return binmode;
40 }
41
42
43
44 FILE* _fopen (const char* name, const char* mode, FILE* f)
45 /* Open the specified file and fill the descriptor values into f */
46 {
47     int           fd;
48     unsigned char binmode;
49
50
51     /* Convert ASCII mode to binary mode */
52     if ((binmode = amode_to_bmode (mode)) == 0) {
53         /* Invalid mode, _errno already set */
54         return 0;
55     }
56
57     /* Open the file */
58     fd = open (name, binmode);
59     if (fd == -1) {
60         /* Error - _oserror is set */
61         return 0;
62     }
63
64     /* Remember fd, mark the file as opened */
65     f->f_fd    = fd;
66     f->f_flags = _FOPEN;
67
68     /* Return the file descriptor */
69     return f;
70 }
71
72
73