Use system headers a bit more: use host's fcntl.h for O_RDONLY etc.

Removes the need to fix up those in the simulator.

Also work around some posix-mingw incompatibilities (e.g. getcwd()).

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27904 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Thomas Martitz 2010-08-27 12:38:25 +00:00
parent 79798ff5f3
commit 2c2e261648
10 changed files with 85 additions and 43 deletions

View file

@ -77,7 +77,7 @@ static unsigned char *bitpos IBSS_ATTR;
/********************* rockbox helper functions *************************/
/* emulate stdio functions */
static int fread(void *ptr, size_t size, size_t nobj)
static size_t tta_fread(void *ptr, size_t size, size_t nobj)
{
size_t read_size;
unsigned char *buffer = ci->request_buffer(&read_size, size * nobj);
@ -90,7 +90,7 @@ static int fread(void *ptr, size_t size, size_t nobj)
return read_size;
}
static int fseek(long offset, int origin)
static int tta_fseek(long offset, int origin)
{
switch (origin)
{
@ -129,7 +129,7 @@ crc32 (unsigned char *buffer, unsigned int len) {
#define GET_BINARY(value, bits) \
while (bit_count < bits) { \
if (bitpos == iso_buffers_end) { \
if (!fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
if (!tta_fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
ttainfo->STATE = READ_ERROR; \
return -1; \
} \
@ -149,7 +149,7 @@ crc32 (unsigned char *buffer, unsigned int len) {
value = 0; \
while (!(bit_cache ^ bit_mask[bit_count])) { \
if (bitpos == iso_buffers_end) { \
if (!fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
if (!tta_fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
ttainfo->STATE = READ_ERROR; \
return -1; \
} \
@ -207,7 +207,7 @@ static int done_buffer_read(void) {
if (rbytes < sizeof(int)) {
ci->memcpy(isobuffers, bitpos, 4);
if (!fread(isobuffers + rbytes, 1, ISO_BUFFERS_SIZE - rbytes))
if (!tta_fread(isobuffers + rbytes, 1, ISO_BUFFERS_SIZE - rbytes))
return -1;
bitpos = isobuffers;
}
@ -249,10 +249,10 @@ int set_tta_info (tta_info *info)
ci->memset (info, 0, sizeof(tta_info));
/* skip id3v2 tags */
fseek(ci->id3->id3v2len, SEEK_SET);
tta_fseek(ci->id3->id3v2len, SEEK_SET);
/* read TTA header */
if (fread (&ttahdr, 1, sizeof (ttahdr)) == 0) {
if (tta_fread (&ttahdr, 1, sizeof (ttahdr)) == 0) {
info->STATE = READ_ERROR;
return -1;
}
@ -374,7 +374,7 @@ int set_position (unsigned int pos, enum tta_seek_type type)
return -1;
}
seek_pos = ttainfo->DATAPOS + seek_table[data_pos = pos];
if (fseek(seek_pos, SEEK_SET) < 0) {
if (tta_fseek(seek_pos, SEEK_SET) < 0) {
ttainfo->STATE = READ_ERROR;
return -1;
}
@ -418,7 +418,7 @@ int player_init (tta_info *info) {
}
/* read seek table */
if (!fread(seek_table, st_size, 1)) {
if (!tta_fread(seek_table, st_size, 1)) {
ttainfo->STATE = READ_ERROR;
return -1;
}

View file

@ -19,7 +19,7 @@
*
****************************************************************************/
#include <file.h>
#include "file.h"
#include "language.h"
#include "lang.h"

View file

@ -23,8 +23,8 @@
#define _METADATA_H
#include <stdbool.h>
#include "file.h"
#include "config.h"
#include "file.h"
/* Audio file types. */

View file

@ -511,13 +511,13 @@ void resume_directory(const char *dir)
/* Returns the current working directory and also writes cwd to buf if
non-NULL. In case of error, returns NULL. */
char *getcwd(char *buf, size_t size)
char *getcwd(char *buf, getcwd_size_t size)
{
if (!buf)
return tc.currdir;
else if (size)
{
if (strlcpy(buf, tc.currdir, size) < size)
if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
return buf;
}
/* size == 0, or truncation in strlcpy */

View file

@ -79,7 +79,13 @@ void set_current_file(char *path);
int rockbox_browse(const char *root, int dirfilter);
bool create_playlist(void);
void resume_directory(const char *dir);
char *getcwd(char *buf, size_t size);
#ifdef WIN32
/* it takes an int on windows */
#define getcwd_size_t int
#else
#define getcwd_size_t size_t
#endif
char *getcwd(char *buf, getcwd_size_t size);
void reload_directory(void);
bool check_rockboxdir(void);
struct tree_context* tree_get_context(void);

View file

@ -42,7 +42,13 @@ static inline void lc_close(void *handle) { (void)handle; }
/* don't call these directly for loading code
* they're to be wrapped by platform specific functions */
extern void *_lc_open(const char *filename, char *buf, size_t buf_size);
#ifdef WIN32
/* windows' LoadLibrary can only handle ucs2, no utf-8 */
#define _lc_open_char wchar_t
#else
#define _lc_open_char char
#endif
extern void *_lc_open(const _lc_open_char *filename, char *buf, size_t buf_size);
extern void *_lc_get_header(void *handle);
extern void _lc_close(void *handle);

View file

@ -25,31 +25,19 @@
#include <sys/types.h>
#include "config.h"
#include "gcc_extensions.h"
#include <fcntl.h>
#ifdef WIN32
/* this has SEEK_SET et al */
#include <stdio.h>
#endif
#undef MAX_PATH /* this avoids problems when building simulator */
#define MAX_PATH 260
#define MAX_OPEN_FILES 11
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
#ifndef O_RDONLY
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 4
#define O_APPEND 8
#define O_TRUNC 0x10
#endif
#if (CONFIG_PLATFORM & PLATFORM_HOSTED) && !defined(PLUGIN) && !defined(CODEC)
#if !defined(PLUGIN) && !defined(CODEC)
#if (CONFIG_PLATFORM & PLATFORM_HOSTED)
#define open(x, ...) sim_open(x, __VA_ARGS__)
#define creat(x,m) sim_creat(x,m)
#define remove(x) sim_remove(x)
@ -96,4 +84,5 @@ extern int ftruncate(int fd, off_t length);
extern off_t filesize(int fd);
extern int release_files(int volume);
int fdprintf (int fd, const char *fmt, ...) ATTRIBUTE_PRINTF(2, 3);
#endif /* !CODEC && !PLUGIN */
#endif

View file

@ -0,0 +1,40 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2002 by Björn Stenberg
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef __FCNTL_H__
#define __FCNTL_H__
#ifndef O_RDONLY
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 4
#define O_APPEND 8
#define O_TRUNC 0x10
#endif
#ifndef SEEK_SET
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif
#endif /* __FCNTL_H__ */

View file

@ -80,13 +80,12 @@ static inline char *_dlerror(void)
#else
/* unix */
#include <dlfcn.h>
#define O_BINARY 0
#endif
#include <stdio.h>
#include "rbpaths.h"
#include "general.h"
void * _lc_open(const char *filename, char *buf, size_t buf_size)
void * _lc_open(const _lc_open_char *filename, char *buf, size_t buf_size)
{
(void)buf;
(void)buf_size;
@ -116,14 +115,13 @@ void *lc_open_from_mem(void *addr, size_t blob_size)
char name[MAX_PATH];
const char *_name = get_user_file_path(ROCKBOX_DIR, NEED_WRITE, name, sizeof(name));
snprintf(temp_filename, sizeof(temp_filename),
"%slibtemp_binary_%d.dll", _name, i);
"%s/libtemp_binary_%d.dll", _name, i);
#endif
fd = open(temp_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0766);
fd = open(temp_filename, O_WRONLY|O_CREAT|O_TRUNC, 0700);
if (fd >= 0)
break; /* Created a file ok */
}
DEBUGF("Creating %s\n", temp_filename);
if (fd < 0)
{
DEBUGF("open failed\n");

View file

@ -179,9 +179,10 @@ struct mydir {
typedef struct mydir MYDIR;
#if 1 /* maybe this needs disabling for MSVC... */
static unsigned int rockbox2sim(int opt)
{
#if 0
/* this shouldn't be needed since we use the host's versions */
int newopt = O_BINARY;
if(opt & 1)
@ -196,8 +197,10 @@ static unsigned int rockbox2sim(int opt)
newopt |= O_TRUNC;
return newopt;
}
#else
return opt|O_BINARY;
#endif
}
/** Simulator I/O engine routines **/
#define IO_YIELD_THRESHOLD 512
@ -537,7 +540,7 @@ int sim_fsync(int fd)
void *lc_open(const char *filename, char *buf, size_t buf_size)
{
const char *sim_path = get_sim_pathname(filename);
void *handle = _lc_open((const char*)UTF8_TO_OS(sim_path), buf, buf_size);
void *handle = _lc_open(UTF8_TO_OS(sim_path), buf, buf_size);
if (handle == NULL)
{