@@ -275,12 +275,12 @@ static int android_write(void *cookie, const char *buf, int size);
275275static fpos_t android_seek (void * cookie , fpos_t offset , int whence );
276276static int android_close (void * cookie );
277277
278- FILE * android_fopen (const char * fileName , const char * mode ); // Replacement for fopen() -> Read-only!
278+ FILE * __real_fopen (const char * fileName , const char * mode ); // Real fopen, provided by the linker (--wrap=fopen)
279+ FILE * __wrap_fopen (const char * fileName , const char * mode ); // Replacement for fopen()
280+
279281FILE * funopen (const void * cookie , int (* readfn )(void * , char * , int ), int (* writefn )(void * , const char * , int ),
280282 fpos_t (* seekfn )(void * , fpos_t , int ), int (* closefn )(void * ));
281283
282- #define fopen (name , mode ) android_fopen(name, mode)
283-
284284//----------------------------------------------------------------------------------
285285// Module Functions Declaration
286286//----------------------------------------------------------------------------------
@@ -1524,25 +1524,20 @@ static void SetupFramebuffer(int width, int height)
15241524 }
15251525}
15261526
1527- // Replacement for fopen()
1527+ // Replacement for fopen(), used as linker wrap entry point (-Wl,--wrap=fopen)
15281528// REF: https://developer.android.com/ndk/reference/group/asset
1529- FILE * android_fopen (const char * fileName , const char * mode )
1529+ FILE * __wrap_fopen (const char * fileName , const char * mode )
15301530{
15311531 FILE * file = NULL ;
1532-
1532+
1533+ // NOTE: AAsset provides access to read-only asset, write operations use regular fopen
15331534 if (mode [0 ] == 'w' )
15341535 {
1535- // NOTE: fopen() is mapped to android_fopen() that only grants read access to
1536- // assets directory through AAssetManager but it could be required to write data
1537- // using the standard stdio FILE access functions
1538- // REF: https://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only
1539- #undef fopen
1540- file = fopen (TextFormat ("%s/%s" , platform .app -> activity -> internalDataPath , fileName ), mode );
1541- #define fopen (name , mode ) android_fopen(name, mode)
1536+ file = __real_fopen (TextFormat ("%s/%s" , platform .app -> activity -> internalDataPath , fileName ), mode );
1537+ if (file == NULL ) file = __real_fopen (fileName , mode );
15421538 }
15431539 else
15441540 {
1545- // NOTE: AAsset provides access to read-only asset
15461541 AAsset * asset = AAssetManager_open (platform .app -> activity -> assetManager , fileName , AASSET_MODE_UNKNOWN );
15471542
15481543 if (asset != NULL )
@@ -1552,14 +1547,12 @@ FILE *android_fopen(const char *fileName, const char *mode)
15521547 }
15531548 else
15541549 {
1555- #undef fopen
15561550 // Just do a regular open if file is not found in the assets
1557- file = fopen (TextFormat ("%s/%s" , platform .app -> activity -> internalDataPath , fileName ), mode );
1558- if (file == NULL ) file = fopen (fileName , mode );
1559- #define fopen (name , mode ) android_fopen(name, mode)
1551+ file = __real_fopen (TextFormat ("%s/%s" , platform .app -> activity -> internalDataPath , fileName ), mode );
1552+ if (file == NULL ) file = __real_fopen (fileName , mode );
15601553 }
15611554 }
1562-
1555+
15631556 return file ;
15641557}
15651558
0 commit comments