2000-05-28 13:40:48 +00:00
|
|
|
/*
|
|
|
|
* _fopen.c
|
|
|
|
*
|
|
|
|
* Ullrich von bassewitz, 17.06.1997
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "_file.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned char amode_to_bmode (const char* mode)
|
|
|
|
/* Convert ASCII mode (like for fopen) to binary mode (for open) */
|
|
|
|
{
|
2002-11-15 23:52:39 +00:00
|
|
|
unsigned char binmode;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-11-15 23:52:39 +00:00
|
|
|
switch (*mode++) {
|
2002-05-16 15:25:48 +00:00
|
|
|
case 'w':
|
2002-11-15 23:52:39 +00:00
|
|
|
binmode = O_WRONLY | O_CREAT | O_TRUNC;
|
2002-05-16 15:25:48 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
binmode = O_RDONLY;
|
|
|
|
break;
|
|
|
|
case 'a':
|
2002-11-19 14:35:07 +00:00
|
|
|
binmode = O_WRONLY | O_CREAT | O_APPEND;
|
2002-05-16 15:25:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0; /* invalid char */
|
|
|
|
}
|
|
|
|
|
2002-11-15 23:52:39 +00:00
|
|
|
while (1) {
|
|
|
|
switch (*mode++) {
|
2000-05-28 13:40:48 +00:00
|
|
|
case '+':
|
2002-11-15 23:52:39 +00:00
|
|
|
/* always to r/w in addition to anything already set */
|
|
|
|
binmode |= O_RDWR;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2002-05-16 15:25:48 +00:00
|
|
|
case 'b':
|
|
|
|
/* currently ignored */
|
|
|
|
break;
|
2002-11-15 23:52:39 +00:00
|
|
|
case '\0':
|
|
|
|
/* end of mode string reached */
|
|
|
|
return binmode;
|
2002-05-16 15:25:48 +00:00
|
|
|
default:
|
2002-11-15 23:52:39 +00:00
|
|
|
/* invalid char in mode string */
|
|
|
|
return 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FILE* _fopen (const char* name, const char* mode, FILE* f)
|
|
|
|
/* Open the specified file and fill the descriptor values into f */
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
unsigned char binmode;
|
|
|
|
|
|
|
|
|
|
|
|
/* Convert ASCII mode to binary mode */
|
|
|
|
if ((binmode = amode_to_bmode (mode)) == 0) {
|
2002-05-16 15:25:48 +00:00
|
|
|
/* Invalid mode */
|
|
|
|
_errno = EINVAL;
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file */
|
|
|
|
fd = open (name, binmode);
|
|
|
|
if (fd == -1) {
|
|
|
|
/* Error - _oserror is set */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember fd, mark the file as opened */
|
|
|
|
f->f_fd = fd;
|
|
|
|
f->f_flags = _FOPEN;
|
|
|
|
|
|
|
|
/* Return the file descriptor */
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|