ftpiiu_plugin/source/main.c

183 lines
3.2 KiB
C
Raw Normal View History

2018-02-02 15:35:22 +01:00
2017-07-26 04:55:16 +02:00
#include <errno.h>
2014-11-23 23:39:00 +01:00
#include <malloc.h>
#include <stdarg.h>
#include <stdio.h>
2017-07-26 04:55:16 +02:00
#include <unistd.h>
2014-11-23 23:39:00 +01:00
#ifdef _3DS
#include <3ds.h>
2018-05-04 02:19:18 +02:00
#elif defined(__SWITCH__)
2018-02-02 15:35:22 +01:00
#include <switch.h>
2014-11-23 23:39:00 +01:00
#endif
#include "console.h"
#include "ftp.h"
/*! looping mechanism
*
* @param[in] callback function to call during each iteration
2016-01-16 07:38:58 +01:00
*
* @returns loop status
2014-11-23 23:39:00 +01:00
*/
2016-01-16 07:38:58 +01:00
static loop_status_t
loop(loop_status_t (*callback)(void))
2014-11-23 23:39:00 +01:00
{
2016-01-16 07:38:58 +01:00
loop_status_t status = LOOP_CONTINUE;
2014-11-23 23:39:00 +01:00
#ifdef _3DS
2014-11-25 20:26:37 +01:00
while(aptMainLoop())
2014-11-23 23:39:00 +01:00
{
2016-01-16 07:38:58 +01:00
status = callback();
console_render();
if(status != LOOP_CONTINUE)
return status;
2014-11-23 23:39:00 +01:00
}
2018-02-02 15:35:22 +01:00
return LOOP_EXIT;
2018-05-04 02:19:18 +02:00
#elif defined(__SWITCH__)
2018-02-02 15:35:22 +01:00
while(appletMainLoop())
{
console_render();
status = callback();
2018-02-02 15:35:22 +01:00
if(status != LOOP_CONTINUE)
return status;
}
2016-01-16 07:38:58 +01:00
return LOOP_EXIT;
2014-11-23 23:39:00 +01:00
#else
2016-01-16 07:41:49 +01:00
while(status == LOOP_CONTINUE)
status = callback();
return status;
2014-11-23 23:39:00 +01:00
#endif
}
2016-08-11 06:08:08 +02:00
#ifdef _3DS
2014-11-23 23:39:00 +01:00
/*! wait until the B button is pressed
*
2016-01-16 07:38:58 +01:00
* @returns loop status
2014-11-23 23:39:00 +01:00
*/
2016-01-16 07:38:58 +01:00
static loop_status_t
2014-11-23 23:39:00 +01:00
wait_for_b(void)
{
/* update button state */
hidScanInput();
/* check if B was pressed */
if(hidKeysDown() & KEY_B)
2016-01-16 07:38:58 +01:00
return LOOP_EXIT;
2014-11-23 23:39:00 +01:00
/* B was not pressed */
2016-01-16 07:38:58 +01:00
return LOOP_CONTINUE;
2014-11-23 23:39:00 +01:00
}
2018-05-04 02:19:18 +02:00
#elif defined(__SWITCH__)
2018-02-02 15:35:22 +01:00
/*! wait until the B button is pressed
*
* @returns loop status
*/
static loop_status_t
wait_for_b(void)
{
/* update button state */
hidScanInput();
/* check if B was pressed */
if(hidKeysDown(CONTROLLER_P1_AUTO) & KEY_B)
return LOOP_EXIT;
/* B was not pressed */
return LOOP_CONTINUE;
}
2016-08-11 06:08:08 +02:00
#endif
2014-11-23 23:39:00 +01:00
/*! entry point
*
* @param[in] argc unused
* @param[in] argv unused
*
* returns exit status
*/
int
main(int argc,
char *argv[])
{
2016-01-16 07:38:58 +01:00
loop_status_t status = LOOP_RESTART;
2014-11-23 23:39:00 +01:00
#ifdef _3DS
/* initialize needed 3DS services */
2016-01-16 07:22:26 +01:00
acInit();
2015-01-08 15:01:33 +01:00
gfxInitDefault();
2014-11-23 23:39:00 +01:00
gfxSet3D(false);
2016-01-12 23:24:01 +01:00
sdmcWriteSafe(false);
/* initialize needed Switch services */
#elif defined(__SWITCH__)
nifmInitialize();
2014-11-23 23:39:00 +01:00
#endif
/* initialize console subsystem */
console_init();
2017-07-26 04:55:16 +02:00
#ifdef ENABLE_LOGGING
/* open log file */
#ifdef _3DS
FILE *fp = freopen("/ftpd.log", "wb", stderr);
#else
FILE *fp = freopen("ftpd.log", "wb", stderr);
#endif
if(fp == NULL)
{
console_print(RED "freopen: 0x%08X\n" RESET, errno);
goto log_fail;
}
/* truncate log file */
if(ftruncate(fileno(fp), 0) != 0)
{
console_print(RED "ftruncate: 0x%08X\n" RESET, errno);
goto log_fail;
}
#endif
console_set_status("\n" GREEN STATUS_STRING
#ifdef ENABLE_LOGGING
" DEBUG"
#endif
RESET);
2014-11-23 23:39:00 +01:00
2016-01-16 07:38:58 +01:00
while(status == LOOP_RESTART)
2014-11-23 23:39:00 +01:00
{
2016-01-16 07:38:58 +01:00
/* initialize ftp subsystem */
if(ftp_init() == 0)
{
/* ftp loop */
status = loop(ftp_loop);
2014-11-23 23:39:00 +01:00
2016-01-16 07:38:58 +01:00
/* done with ftp */
ftp_exit();
}
else
status = LOOP_EXIT;
2014-11-23 23:39:00 +01:00
}
2018-05-04 02:19:18 +02:00
#if defined(_3DS) || defined(__SWITCH__)
console_print("Press B to exit\n");
2017-07-26 04:55:16 +02:00
#endif
#ifdef ENABLE_LOGGING
log_fail:
if(fclose(stderr) != 0)
console_print(RED "fclose(%d): 0x%08X\n" RESET, fileno(stderr), errno);
#endif
#ifdef _3DS
loop(wait_for_b);
2016-08-11 06:08:08 +02:00
2014-11-23 23:39:00 +01:00
/* deinitialize 3DS services */
gfxExit();
2016-01-16 07:22:26 +01:00
acExit();
2018-05-04 02:19:18 +02:00
#elif defined(__SWITCH__)
2018-02-02 15:35:22 +01:00
loop(wait_for_b);
2014-11-23 23:39:00 +01:00
2018-02-02 15:35:22 +01:00
/* deinitialize Switch services */
consoleExit(NULL);
nifmExit();
2018-02-02 15:35:22 +01:00
#endif
2014-11-23 23:39:00 +01:00
return 0;
}