/** * fopen * * @return FILE pointer - upon successful completion, * NULL - otherwise */ FILE * fopen (const char *path, /**< file path */ const char *mode) /**< file open mode */ { bool may_read = false; bool may_write = false; bool truncate = false; bool create_if_not_exist = false; bool position_at_end = false; LIBC_ASSERT (path != NULL && mode != NULL); LIBC_ASSERT (mode[1] == '+' || mode[1] == '\0'); switch (mode[0]) { case 'r': { may_read = true; may_write = (mode[1] == '+'); break; } case 'w': { may_write = true; truncate = true; create_if_not_exist = true; may_read = (mode[1] == '+'); break; } case 'a': { may_write = true; position_at_end = true; create_if_not_exist = true; if (mode[1] == '+') { /* Not supported */ LIBC_UNREACHABLE (); } break; } default: { LIBC_UNREACHABLE (); } } int flags = 0; int access = S_IRUSR | S_IWUSR; if (may_read && !may_write) { flags = O_RDONLY; } else if (!may_read && may_write) { flags = O_WRONLY; } else { LIBC_ASSERT (may_read && may_write); flags = O_RDWR; } if (truncate) { flags |= O_TRUNC; } if (create_if_not_exist) { flags |= O_CREAT; } if (position_at_end) { flags |= O_APPEND; } long int ret = syscall_3 (__NR_open, (long int) path, flags, access); return (void *) (uintptr_t) (ret); } /* fopen */
/** * The rewind () function sets the file position indicator * for the stream pointed to by STREAM to the beginning of the file. */ void rewind (FILE *stream) /**< stream pointer */ { syscall_3 (__NR_lseek, (long int) stream, 0, SEEK_SET); } /* rewind */
/** * fopen * * @return FILE pointer - upon successful completion, * NULL - otherwise */ FILE * fopen (const char *path, /**< file path */ const char *mode) /**< file open mode */ { bool may_read = false; bool may_write = false; bool truncate = false; bool create_if_not_exist = false; bool position_at_end = false; assert (path != NULL && mode != NULL); assert (mode[1] == '+' || mode[1] == '\0'); switch (mode[0]) { case 'r': { may_read = true; may_write = (mode[1] == '+'); break; } case 'w': { may_write = true; truncate = true; create_if_not_exist = true; may_read = (mode[1] == '+'); break; } case 'a': { may_write = true; position_at_end = true; create_if_not_exist = true; if (mode[1] == '+') { assert (false && "unsupported mode a+"); } break; } default: { assert (false && "unsupported mode"); } } int flags = 0; int access = S_IRUSR | S_IWUSR; if (may_read && !may_write) { flags = O_RDONLY; } else if (!may_read && may_write) { flags = O_WRONLY; } else { assert (may_read && may_write); flags = O_RDWR; } if (truncate) { flags |= O_TRUNC; } if (create_if_not_exist) { flags |= O_CREAT; } if (position_at_end) { flags |= O_APPEND; } long int ret = syscall_3 (SYSCALL_NO (open), (long int) path, flags, access); return ((ret < 0) ? NULL : (void *) (uintptr_t) (ret)); } /* fopen */