2012-11-23 11:26:09 +08:00
|
|
|
/*
|
2018-10-14 19:28:18 +08:00
|
|
|
* Copyright (c) 2006-2018, RT-Thread Development Team
|
2012-11-23 11:26:09 +08:00
|
|
|
*
|
2018-10-14 19:28:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-11-23 11:26:09 +08:00
|
|
|
*
|
|
|
|
* 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.
|
2015-01-28 14:14:30 +08:00
|
|
|
* 2014-08-03 bernard If using msh, use system() implementation
|
2014-08-03 14:31:19 +08:00
|
|
|
* in msh.
|
2020-08-05 02:47:27 +08:00
|
|
|
* 2020-08-05 Meco Man fixed _sys_flen() compiling-warning when
|
|
|
|
* RT_USING_DFS is not defined
|
2021-02-13 13:30:40 +08:00
|
|
|
* 2020-02-13 Meco Man re-implement exit() and abort()
|
2021-02-14 18:46:55 +08:00
|
|
|
* 2020-02-14 Meco Man implement _sys_tmpnam()
|
2012-11-22 11:39:22 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <rt_sys.h>
|
|
|
|
|
2021-02-14 18:47:48 +08:00
|
|
|
#include <rtthread.h>
|
2017-10-17 09:48:35 +08:00
|
|
|
#include "libc.h"
|
2012-11-22 11:39:22 +08:00
|
|
|
|
2012-11-23 11:46:05 +08:00
|
|
|
#ifdef RT_USING_DFS
|
2021-02-14 18:47:48 +08:00
|
|
|
#include <dfs_posix.h>
|
2012-11-23 11:46:05 +08:00
|
|
|
#endif
|
|
|
|
|
2018-09-23 12:08:44 +08:00
|
|
|
#ifdef __CLANG_ARM
|
|
|
|
__asm(".global __use_no_semihosting\n\t");
|
|
|
|
#else
|
2012-11-22 11:39:22 +08:00
|
|
|
#pragma import(__use_no_semihosting_swi)
|
2018-09-23 12:08:44 +08:00
|
|
|
#endif
|
2012-11-22 11:39:22 +08:00
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
/* Standard IO device handles. */
|
|
|
|
#define STDIN 0
|
|
|
|
#define STDOUT 1
|
|
|
|
#define STDERR 2
|
2012-11-22 11:39:22 +08:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2015-01-28 14:14:30 +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
|
2015-01-28 14:14:30 +08:00
|
|
|
|
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
|
2015-01-28 14:14:30 +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)
|
|
|
|
{
|
2014-10-30 11:53:22 +08:00
|
|
|
mode |= (O_WRONLY | O_APPEND | O_CREAT);
|
2015-01-28 14:14:30 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-30 11:53:22 +08:00
|
|
|
|
|
|
|
fd = open(name, mode, 0);
|
2015-01-28 14:14:30 +08:00
|
|
|
if (fd < 0)
|
2013-11-24 23:11:17 +08:00
|
|
|
return -1;
|
|
|
|
else
|
2017-10-15 22:41:59 +08:00
|
|
|
return fd;
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int _sys_close(FILEHANDLE fh)
|
|
|
|
{
|
|
|
|
#ifndef RT_USING_DFS
|
|
|
|
return 0;
|
|
|
|
#else
|
2017-10-15 22:41:59 +08:00
|
|
|
if (fh <= STDERR) return 0;
|
2013-11-24 23:11:17 +08:00
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
return close(fh);
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
/*
|
|
|
|
* Read from a file. Can return:
|
|
|
|
* - zero if the read was completely successful
|
|
|
|
* - the number of bytes _not_ read, if the read was partially successful
|
|
|
|
* - the number of bytes not read, plus the top bit set (0x80000000), if
|
|
|
|
* the read was partially successful due to end of file
|
|
|
|
* - -1 if some error other than EOF occurred
|
2012-11-23 11:26:09 +08:00
|
|
|
*
|
2017-10-15 22:41:59 +08:00
|
|
|
* It is also legal to signal EOF by returning no data but
|
|
|
|
* signalling no error (i.e. the top-bit-set mechanism need never
|
|
|
|
* be used).
|
|
|
|
*
|
|
|
|
* So if (for example) the user is trying to read 8 bytes at a time
|
|
|
|
* from a file in which only 5 remain, this routine can do three
|
|
|
|
* equally valid things:
|
|
|
|
*
|
|
|
|
* - it can return 0x80000003 (3 bytes not read due to EOF)
|
|
|
|
* - OR it can return 3 (3 bytes not read), and then return
|
|
|
|
* 0x80000008 (8 bytes not read due to EOF) on the next attempt
|
|
|
|
* - OR it can return 3 (3 bytes not read), and then return
|
|
|
|
* 8 (8 bytes not read, meaning 0 read, meaning EOF) on the next
|
|
|
|
* attempt
|
|
|
|
*
|
|
|
|
* `mode' exists for historical reasons and must be ignored.
|
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)
|
|
|
|
{
|
2015-01-28 14:14:30 +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
|
2015-01-28 14:14:30 +08:00
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
if (fh == STDIN)
|
|
|
|
{
|
2017-10-17 22:27:06 +08:00
|
|
|
#ifdef RT_USING_POSIX
|
2017-10-15 22:41:59 +08:00
|
|
|
size = libc_stdio_read(buf, len);
|
|
|
|
return len - size;
|
|
|
|
#else
|
|
|
|
/* no stdin */
|
|
|
|
return -1;
|
|
|
|
#endif
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
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
|
2017-10-15 22:41:59 +08:00
|
|
|
size = read(fh, buf, len);
|
2015-01-28 14:14:30 +08:00
|
|
|
if (size >= 0)
|
2013-11-24 23:11:17 +08:00
|
|
|
return len - size;
|
|
|
|
else
|
|
|
|
return -1;
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
/*
|
|
|
|
* Write to a file. Returns 0 on success, negative on error, and
|
|
|
|
* the number of characters _not_ written on partial success.
|
|
|
|
* `mode' exists for historical reasons and must be ignored.
|
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
|
2015-01-28 14:14:30 +08:00
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
if ((fh == STDOUT) || (fh == STDERR))
|
|
|
|
{
|
2018-07-17 16:52:54 +08:00
|
|
|
#if !defined(RT_USING_CONSOLE) || !defined(RT_USING_DEVICE)
|
2012-11-22 11:39:22 +08:00
|
|
|
return 0;
|
|
|
|
#else
|
2017-10-17 22:27:06 +08:00
|
|
|
#ifdef RT_USING_POSIX
|
2017-10-15 22:41:59 +08:00
|
|
|
size = libc_stdio_write(buf, len);
|
|
|
|
return len - size;
|
|
|
|
#else
|
|
|
|
if (rt_console_get_device())
|
|
|
|
{
|
|
|
|
rt_device_write(rt_console_get_device(), -1, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-31 11:53:12 +08:00
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
return -1;
|
|
|
|
#endif
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
2013-11-24 23:11:17 +08:00
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
if (fh == STDIN) return -1;
|
2013-11-24 23:11:17 +08:00
|
|
|
|
2012-11-22 11:39:22 +08:00
|
|
|
#ifndef RT_USING_DFS
|
|
|
|
return 0;
|
|
|
|
#else
|
2017-10-15 22:41:59 +08:00
|
|
|
size = write(fh, buf, len);
|
2015-01-28 14:14:30 +08:00
|
|
|
if (size >= 0)
|
2013-11-24 23:11:17 +08:00
|
|
|
return len - size;
|
|
|
|
else
|
|
|
|
return -1;
|
2012-11-22 11:39:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
/*
|
|
|
|
* Move the file position to a given offset from the file start.
|
|
|
|
* Returns >=0 on success, <0 on failure.
|
2012-11-23 11:26:09 +08:00
|
|
|
*/
|
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 */
|
2017-10-15 22:41:59 +08:00
|
|
|
return lseek(fh, 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
|
|
|
{
|
2021-02-14 18:46:55 +08:00
|
|
|
rt_snprintf(name, maxlength, "tem%03d", fileno);
|
|
|
|
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 */
|
2018-09-11 23:36:01 +08:00
|
|
|
return RT_NULL;
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
|
|
|
|
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-11-23 00:04:58 +08:00
|
|
|
#ifdef RT_USING_CONSOLE
|
2014-08-03 14:31:19 +08:00
|
|
|
char c;
|
|
|
|
|
|
|
|
c = (char)ch;
|
|
|
|
rt_kprintf(&c);
|
2014-11-23 00:04:58 +08:00
|
|
|
#endif
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
|
|
|
|
2019-02-17 18:46:30 +08:00
|
|
|
RT_WEAK void _sys_exit(int return_code)
|
2012-11-22 11:39:22 +08:00
|
|
|
{
|
2021-02-17 00:18:49 +08:00
|
|
|
extern void __exit__(int status);
|
2021-02-17 00:02:28 +08:00
|
|
|
__exit__(return_code);
|
2021-02-17 00:18:49 +08:00
|
|
|
while(1);
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2020-08-05 02:47:27 +08:00
|
|
|
#ifdef RT_USING_DFS
|
2019-01-24 09:05:41 +08:00
|
|
|
struct stat stat;
|
2020-08-05 09:28:10 +08:00
|
|
|
|
2019-01-24 09:05:41 +08:00
|
|
|
if (fh < STDERR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fstat(fh, &stat);
|
|
|
|
return stat.st_size;
|
2020-08-05 09:28:10 +08:00
|
|
|
#else
|
|
|
|
return -1;
|
2019-01-24 09:05:41 +08:00
|
|
|
#endif
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int _sys_istty(FILEHANDLE fh)
|
|
|
|
{
|
2018-12-23 19:15:20 +08:00
|
|
|
if((STDIN <= fh) && (fh <= STDERR))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2012-11-22 11:39:22 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2021-02-17 00:18:49 +08:00
|
|
|
extern int __system__(const char *string);
|
2021-02-17 00:02:28 +08:00
|
|
|
return __system__(string);
|
2012-11-23 11:26:09 +08:00
|
|
|
}
|
2014-08-03 14:31:19 +08:00
|
|
|
#endif
|
2016-02-01 12:19:22 +08:00
|
|
|
|
|
|
|
#ifdef __MICROLIB
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int fputc(int c, FILE *f)
|
|
|
|
{
|
2018-09-23 14:43:21 +08:00
|
|
|
char ch[2] = {0};
|
2016-02-01 12:19:22 +08:00
|
|
|
|
2018-09-25 11:04:02 +08:00
|
|
|
ch[0] = c;
|
2018-09-23 14:43:21 +08:00
|
|
|
rt_kprintf(&ch[0]);
|
2016-02-01 12:19:22 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
int fgetc(FILE *f)
|
|
|
|
{
|
2018-09-23 14:43:21 +08:00
|
|
|
#ifdef RT_USING_POSIX
|
2017-10-15 22:41:59 +08:00
|
|
|
char ch;
|
|
|
|
|
|
|
|
if (libc_stdio_read(&ch, 1) == 1)
|
|
|
|
return ch;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return -1;
|
2016-02-01 12:19:22 +08:00
|
|
|
}
|
|
|
|
#endif
|