2012-11-23 11:26:09 +08:00
|
|
|
/*
|
|
|
|
* File : stubs.c
|
|
|
|
* Brief : reimplement some basic functions of arm standard c library
|
|
|
|
*
|
|
|
|
* This file is part of Device File System in RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2004-2012, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* The license and distribution terms for this file may be
|
|
|
|
* found in the file LICENSE in this distribution or at
|
|
|
|
* http://www.rt-thread.org/license/LICENSE.
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-11-23 Yihui The first version
|
2013-11-24 23:11:17 +08:00
|
|
|
* 2013-11-24 aozima fixed _sys_read()/_sys_write() issues.
|
2014-08-03 14:31:19 +08:00
|
|
|
* 2014-08-03 bernard If using msh, use system() implementation
|
|
|
|
* in msh.
|
2012-11-22 11:39:22 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <rt_sys.h>
|
|
|
|
|
|
|
|
#include "rtthread.h"
|
|
|
|
|
2012-11-23 11:46:05 +08:00
|
|
|
#ifdef RT_USING_DFS
|
|
|
|
#include "dfs_posix.h"
|
|
|
|
#endif
|
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
#pragma import(__use_no_semihosting_swi)
|
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
/* TODO: Standard IO device handles. */
|
2012-11-22 11:39:22 +08:00
|
|
|
#define STDIN 1
|
|
|
|
#define STDOUT 2
|
|
|
|
#define STDERR 3
|
|
|
|
|
|
|
|
/* Standard IO device name defines. */
|
|
|
|
const char __stdin_name[] = "STDIN";
|
|
|
|
const char __stdout_name[] = "STDOUT";
|
|
|
|
const char __stderr_name[] = "STDERR";
|
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
/**
|
|
|
|
* required by fopen() and freopen().
|
|
|
|
*
|
|
|
|
* @param name - file name with path.
|
|
|
|
* @param openmode - a bitmap hose bits mostly correspond directly to
|
|
|
|
* the ISO mode specification.
|
|
|
|
* @return -1 if an error occurs.
|
|
|
|
*/
|
2012-11-22 11:39:22 +08:00
|
|
|
FILEHANDLE _sys_open(const char *name, int openmode)
|
|
|
|
{
|
2014-06-25 17:20:57 +08:00
|
|
|
#ifdef RT_USING_DFS
|
2013-11-24 23:11:17 +08:00
|
|
|
int fd;
|
2014-10-30 11:53:22 +08:00
|
|
|
int mode = O_RDONLY;
|
2014-06-25 17:20:57 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
/* Register standard Input Output devices. */
|
|
|
|
if (strcmp(name, __stdin_name) == 0)
|
|
|
|
return (STDIN);
|
|
|
|
if (strcmp(name, __stdout_name) == 0)
|
|
|
|
return (STDOUT);
|
|
|
|
if (strcmp(name, __stderr_name) == 0)
|
|
|
|
return (STDERR);
|
|
|
|
|
|
|
|
#ifndef RT_USING_DFS
|
2012-11-23 11:26:09 +08:00
|
|
|
return -1;
|
2012-11-22 11:39:22 +08:00
|
|
|
#else
|
2014-10-30 11:53:22 +08:00
|
|
|
/* Correct openmode from fopen to open */
|
|
|
|
if (openmode & OPEN_PLUS)
|
|
|
|
{
|
|
|
|
if (openmode & OPEN_W)
|
|
|
|
{
|
|
|
|
mode |= (O_RDWR | O_TRUNC | O_CREAT);
|
|
|
|
}
|
|
|
|
else if (openmode & OPEN_A)
|
|
|
|
{
|
|
|
|
mode |= (O_RDWR | O_APPEND | O_CREAT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mode |= O_RDWR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (openmode & OPEN_W)
|
|
|
|
{
|
|
|
|
mode |= (O_WRONLY | O_TRUNC | O_CREAT);
|
|
|
|
}
|
|
|
|
else if (openmode & OPEN_A)
|
|
|
|
{
|
|
|
|
mode |= (O_WRONLY | O_APPEND | O_CREAT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(name, mode, 0);
|
2013-11-24 23:11:17 +08:00
|
|
|
if(fd < 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return fd + STDERR + 1;
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int _sys_close(FILEHANDLE fh)
|
|
|
|
{
|
|
|
|
#ifndef RT_USING_DFS
|
|
|
|
return 0;
|
|
|
|
#else
|
2013-11-24 23:11:17 +08:00
|
|
|
if (fh < STDERR)
|
2012-11-22 11:39:22 +08:00
|
|
|
return 0;
|
2013-11-24 23:11:17 +08:00
|
|
|
|
|
|
|
return close(fh - STDERR - 1);
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
/**
|
|
|
|
* read data
|
|
|
|
*
|
|
|
|
* @param fh - file handle
|
|
|
|
* @param buf - buffer to save read data
|
|
|
|
* @param len - max length of data buffer
|
|
|
|
* @param mode - useless, for historical reasons
|
2013-11-24 23:11:17 +08:00
|
|
|
* @return The number of bytes not read.
|
2012-11-23 11:26:09 +08:00
|
|
|
*/
|
2012-11-22 11:39:22 +08:00
|
|
|
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
|
|
|
|
{
|
2014-06-25 17:20:57 +08:00
|
|
|
#ifdef RT_USING_DFS
|
2013-11-24 23:11:17 +08:00
|
|
|
int size;
|
2014-06-25 17:20:57 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
if (fh == STDIN)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-24 23:11:17 +08:00
|
|
|
|
|
|
|
if ((fh == STDOUT) || (fh == STDERR))
|
|
|
|
return -1;
|
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
#ifndef RT_USING_DFS
|
|
|
|
return 0;
|
|
|
|
#else
|
2013-11-24 23:11:17 +08:00
|
|
|
size = read(fh - STDERR - 1, buf, len);
|
|
|
|
if(size >= 0)
|
|
|
|
return len - size;
|
|
|
|
else
|
|
|
|
return -1;
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
/**
|
|
|
|
* write data
|
|
|
|
*
|
|
|
|
* @param fh - file handle
|
|
|
|
* @param buf - data buffer
|
|
|
|
* @param len - buffer length
|
|
|
|
* @param mode - useless, for historical reasons
|
2013-11-24 23:11:17 +08:00
|
|
|
* @return a positive number representing the number of characters not written.
|
2012-11-23 11:26:09 +08:00
|
|
|
*/
|
2012-11-22 11:39:22 +08:00
|
|
|
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
|
|
|
|
{
|
2014-06-25 17:20:57 +08:00
|
|
|
#ifdef RT_USING_DFS
|
2013-11-24 23:11:17 +08:00
|
|
|
int size;
|
2014-06-25 17:20:57 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
if ((fh == STDOUT) || (fh == STDERR))
|
|
|
|
{
|
|
|
|
#ifndef RT_USING_CONSOLE
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
rt_device_t console_device;
|
|
|
|
|
|
|
|
console_device = rt_console_get_device();
|
|
|
|
if (console_device != 0) rt_device_write(console_device, 0, buf, len);
|
|
|
|
return len;
|
|
|
|
#endif
|
|
|
|
}
|
2013-11-24 23:11:17 +08:00
|
|
|
|
|
|
|
if(fh == STDIN)
|
|
|
|
return -1;
|
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
#ifndef RT_USING_DFS
|
|
|
|
return 0;
|
|
|
|
#else
|
2013-11-24 23:11:17 +08:00
|
|
|
size = write(fh - STDERR - 1, buf, len);
|
|
|
|
if(size >= 0)
|
|
|
|
return len - size;
|
|
|
|
else
|
|
|
|
return -1;
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
/**
|
|
|
|
* put he file pointer at offset pos from the beginning of the file.
|
|
|
|
*
|
|
|
|
* @param pos - offset
|
|
|
|
* @return the current file position, or -1 on failed
|
|
|
|
*/
|
2012-11-22 11:39:22 +08:00
|
|
|
int _sys_seek(FILEHANDLE fh, long pos)
|
|
|
|
{
|
2013-11-24 23:11:17 +08:00
|
|
|
if (fh < STDERR)
|
|
|
|
return -1;
|
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
#ifndef RT_USING_DFS
|
2012-11-23 11:26:09 +08:00
|
|
|
return -1;
|
2012-11-22 11:39:22 +08:00
|
|
|
#else
|
2013-11-24 23:11:17 +08:00
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
/* position is relative to the start of file fh */
|
2013-11-24 23:11:17 +08:00
|
|
|
return lseek(fh - STDERR - 1, pos, 0);
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
/**
|
|
|
|
* used by tmpnam() or tmpfile()
|
|
|
|
*/
|
2012-11-23 11:46:05 +08:00
|
|
|
int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
|
2012-11-22 11:39:22 +08:00
|
|
|
{
|
2012-11-23 11:46:05 +08:00
|
|
|
return -1;
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char *_sys_command_string(char *cmd, int len)
|
|
|
|
{
|
2012-11-23 11:26:09 +08:00
|
|
|
/* no support */
|
2012-11-22 11:39:22 +08:00
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:31:19 +08:00
|
|
|
/* This function writes a character to the console. */
|
2012-11-22 11:39:22 +08:00
|
|
|
void _ttywrch(int ch)
|
|
|
|
{
|
2014-08-03 14:31:19 +08:00
|
|
|
char c;
|
|
|
|
|
|
|
|
c = (char)ch;
|
|
|
|
rt_kprintf(&c);
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void _sys_exit(int return_code)
|
|
|
|
{
|
2012-11-23 11:26:09 +08:00
|
|
|
/* TODO: perhaps exit the thread which is invoking this function */
|
2012-11-22 11:39:22 +08:00
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
/**
|
2013-11-24 23:11:17 +08:00
|
|
|
* return current length of file.
|
2012-11-23 11:26:09 +08:00
|
|
|
*
|
|
|
|
* @param fh - file handle
|
|
|
|
* @return file length, or -1 on failed
|
|
|
|
*/
|
2012-11-22 11:39:22 +08:00
|
|
|
long _sys_flen(FILEHANDLE fh)
|
|
|
|
{
|
2012-11-23 11:26:09 +08:00
|
|
|
return -1;
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int _sys_istty(FILEHANDLE fh)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-23 11:26:09 +08:00
|
|
|
int remove(const char *filename)
|
|
|
|
{
|
2012-11-23 11:46:05 +08:00
|
|
|
#ifndef RT_USING_DFS
|
|
|
|
return -1;
|
|
|
|
#else
|
2012-11-23 11:26:09 +08:00
|
|
|
return unlink(filename);
|
2012-11-23 11:46:05 +08:00
|
|
|
#endif
|
2012-11-23 11:26:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:31:19 +08:00
|
|
|
#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) && defined(RT_USING_MODULE) && defined(RT_USING_DFS)
|
2014-10-30 11:53:22 +08:00
|
|
|
/* use system(const char *string) implementation in the msh */
|
2014-08-03 14:31:19 +08:00
|
|
|
#else
|
2012-11-23 11:26:09 +08:00
|
|
|
int system(const char *string)
|
|
|
|
{
|
|
|
|
RT_ASSERT(0);
|
2013-11-24 23:11:17 +08:00
|
|
|
for(;;);
|
2012-11-23 11:26:09 +08:00
|
|
|
}
|
2014-08-03 14:31:19 +08:00
|
|
|
#endif
|