|
|
@@ -14,6 +14,21 @@
|
|
|
#include <stdlib.h>
|
|
|
#include <compiler.h>
|
|
|
|
|
|
+/**
|
|
|
+ * @brief This function is not responsible for creating files,
|
|
|
+ * it primarily handles associating the fd with the FILE structure.
|
|
|
+ *
|
|
|
+ * @param fd The file descriptor
|
|
|
+ * @param mode The file open mode.
|
|
|
+ * support:
|
|
|
+ * r: you can readonly
|
|
|
+ * w: clean the file and writeonly
|
|
|
+ * a: append the file
|
|
|
+ * r+: you can read and write file
|
|
|
+ * w+: file will clean first and you can read and write file
|
|
|
+ * a+: append the file
|
|
|
+ * @return FILE* A pointer to the FILE structure associated with the file descriptor.
|
|
|
+ */
|
|
|
mlibc_weak FILE *fdopen(int fd, const char *mode)
|
|
|
{
|
|
|
FILE *f;
|
|
|
@@ -22,13 +37,13 @@ mlibc_weak FILE *fdopen(int fd, const char *mode)
|
|
|
if(!strchr("rwa", *mode))
|
|
|
{
|
|
|
/* error operation */
|
|
|
- return 0;
|
|
|
+ return NULL;
|
|
|
}
|
|
|
|
|
|
/* allocate memory for file and buffer */
|
|
|
if(!(f=malloc(sizeof(FILE) + UNGET + BUFSIZ)))
|
|
|
{
|
|
|
- return 0;
|
|
|
+ return NULL;
|
|
|
}
|
|
|
|
|
|
/* zero-fill FILE */
|
|
|
@@ -42,6 +57,7 @@ mlibc_weak FILE *fdopen(int fd, const char *mode)
|
|
|
/* Set append mode on fd if opened for append */
|
|
|
if (*mode == 'a') {
|
|
|
int flags = __mlibc_sys_fcntl(fd, F_GETFL);
|
|
|
+
|
|
|
if (!(flags & O_APPEND))
|
|
|
{
|
|
|
__mlibc_sys_fcntl(fd, F_SETFL, flags | O_APPEND);
|
|
|
@@ -63,5 +79,5 @@ mlibc_weak FILE *fdopen(int fd, const char *mode)
|
|
|
f->seek = __mlibc_lseek;
|
|
|
f->close = __mlibc_close;
|
|
|
|
|
|
- return mlibc_file_add(f);
|
|
|
+ return __mlibc_file_add(f);
|
|
|
}
|