Newlib:修订间差异
跳转到导航
跳转到搜索
(创建页面,内容为“== 源代码 == === 主要 === https://sourceware.org/git/newlib-cygwin.git == 说明 == newlib是一个用于嵌入式系统的C运行库 == 官方资料 == 网站: http://sourceware.org/newlib/”) |
(→说明) |
||
第6行: | 第6行: | ||
== 说明 == | == 说明 == | ||
newlib是一个用于嵌入式系统的C运行库 | newlib是一个用于嵌入式系统的C运行库 | ||
== 系统调用 == | |||
对于嵌入式系统而言,环境复杂多样,有的支持多线程有的不支持,因此部分关于OS的接口C运行库不能预先实现。 | |||
对于newlib而言,用户需要手动实现一些操作系统接口。 | |||
=== 基本OS接口 === | |||
基本OS接口用于单进程(线程)环境或者不使用OS相关特性(只要求编译通过而不使用系统调用)的场景。 | |||
实现OS接口时应当包含errno.h头文件(用于使用错误代码)。 | |||
{| class="wikitable" | |||
!名称 | |||
!类型 | |||
!说明 | |||
!备注 | |||
|- | |||
|<code>_exit</code> | |||
|函数 | |||
|退出程序并清理文件 | |||
| | |||
|- | |||
|<code>close</code> | |||
|函数 | |||
|关闭文件 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int close(int file) | |||
{ | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_close_r | |||
|- | |||
|<code>environ</code> | |||
|指针 | |||
|环境变量 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
char *__env[1] = { 0 }; | |||
char **environ = __env; | |||
</syntaxhighlight> | |||
|- | |||
|<code>execve</code> | |||
|函数 | |||
|将控制权交给新进程 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
#include <errno.h> | |||
#undef errno | |||
extern int errno; | |||
int execve(char *name, char **argv, char **env) | |||
{ | |||
errno = ENOMEM; | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_execve_r | |||
|- | |||
|<code>fork</code> | |||
|函数 | |||
|创建新进程 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
#include <errno.h> | |||
#undef errno | |||
extern int errno; | |||
int fork(void) | |||
{ | |||
errno = EAGAIN; | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_fork_r | |||
|- | |||
|<code>fstat</code> | |||
|函数 | |||
|获取打开的文件的状态 | |||
|默认示例:<syntaxhighlight lang="c"> | |||
#include <sys/stat.h> | |||
int fstat(int file, struct stat *st) | |||
{ | |||
st->st_mode = S_IFCHR; | |||
return 0; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_fstat_r | |||
|- | |||
|<code>getpid</code> | |||
|函数 | |||
|获取进程ID | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int getpid(void) | |||
{ | |||
return 1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_getpid_r | |||
|- | |||
|<code>isatty</code> | |||
|函数 | |||
|查询输出流是否为终端 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int isatty(int file) | |||
{ | |||
return 1; | |||
} | |||
</syntaxhighlight> | |||
|- | |||
|<code>kill</code> | |||
|函数 | |||
|发送信号 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
#include <errno.h> | |||
#undef errno | |||
extern int errno; | |||
int kill(int pid, int sig) | |||
{ | |||
errno = EINVAL; | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_kill_r | |||
|- | |||
|<code>link</code> | |||
|函数 | |||
|给已有文件创建一个新名字 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
#include <errno.h> | |||
#undef errno | |||
extern int errno; | |||
int link(char *old, char *new) | |||
{ | |||
errno = EMLINK; | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_link_r | |||
|- | |||
|<code>lseek</code> | |||
|函数 | |||
|设置文件位置 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int lseek(int file, int ptr, int dir) | |||
{ | |||
return 0; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_lseek_r | |||
|- | |||
|<code>open</code> | |||
|函数 | |||
|打开文件 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int open(const char *name, int flags, int mode) | |||
{ | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_open_r | |||
|- | |||
|<code>read</code> | |||
|函数 | |||
|读取文件 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int read(int file, char *ptr, int len) | |||
{ | |||
return 0; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_read_r | |||
|- | |||
|<code>sbrk</code> | |||
|函数 | |||
|增大程序数据空间,默认的内存分配函数依赖此函数 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
caddr_t sbrk(int incr) | |||
{ | |||
extern char _end; /* Defined by the linker */ | |||
static char *heap_end; | |||
char *prev_heap_end; | |||
if (heap_end == 0) { | |||
heap_end = &_end; | |||
} | |||
prev_heap_end = heap_end; | |||
if (heap_end + incr > stack_ptr) { | |||
write (1, "Heap and stack collision\n", 25); | |||
abort (); | |||
} | |||
heap_end += incr; | |||
return (caddr_t) prev_heap_end; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_sbrk_r | |||
|- | |||
|<code>stat</code> | |||
|函数 | |||
|获取文件状态 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int stat(char *file, struct stat *st) | |||
{ | |||
st->st_mode = S_IFCHR; | |||
return 0; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_stat_r | |||
|- | |||
|<code>times</code> | |||
|函数 | |||
|当前进程时间信息 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int times(struct tms *buf) | |||
{ | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_times_r | |||
|- | |||
|<code>unlink</code> | |||
|函数 | |||
|移除文件的目录入口,相当于删除 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
#include <errno.h> | |||
#undef errno | |||
extern int errno; | |||
int unlink(char *name) | |||
{ | |||
errno = ENOENT; | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_unlink_r | |||
|- | |||
|<code>wait</code> | |||
|函数 | |||
|等待子进程 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
#include <errno.h> | |||
#undef errno | |||
extern int errno; | |||
int wait(int *status) | |||
{ | |||
errno = ECHILD; | |||
return -1; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_wait_r | |||
|- | |||
|<code>write</code> | |||
|函数 | |||
|写入数据到文件 | |||
|默认示例:<syntaxhighlight lang="c" line="1"> | |||
int write(int file, char *ptr, int len) | |||
{ | |||
int todo; | |||
for (todo = 0; todo < len; todo++) { | |||
outbyte (*ptr++); | |||
} | |||
return len; | |||
} | |||
</syntaxhighlight>可重入版本(用于多进程(线程)环境):_write_r | |||
|} | |||
== 官方资料 == | == 官方资料 == | ||
网站: http://sourceware.org/newlib/ | 网站: http://sourceware.org/newlib/ |
2023年8月25日 (五) 03:56的版本
源代码
主要
https://sourceware.org/git/newlib-cygwin.git
说明
newlib是一个用于嵌入式系统的C运行库
系统调用
对于嵌入式系统而言,环境复杂多样,有的支持多线程有的不支持,因此部分关于OS的接口C运行库不能预先实现。
对于newlib而言,用户需要手动实现一些操作系统接口。
基本OS接口
基本OS接口用于单进程(线程)环境或者不使用OS相关特性(只要求编译通过而不使用系统调用)的场景。
实现OS接口时应当包含errno.h头文件(用于使用错误代码)。
名称 | 类型 | 说明 | 备注 |
---|---|---|---|
_exit
|
函数 | 退出程序并清理文件 | |
close
|
函数 | 关闭文件 | 默认示例:int close(int file)
{
return -1;
}
|
environ
|
指针 | 环境变量 | 默认示例:char *__env[1] = { 0 };
char **environ = __env;
|
execve
|
函数 | 将控制权交给新进程 | 默认示例:#include <errno.h>
#undef errno
extern int errno;
int execve(char *name, char **argv, char **env)
{
errno = ENOMEM;
return -1;
}
|
fork
|
函数 | 创建新进程 | 默认示例:#include <errno.h>
#undef errno
extern int errno;
int fork(void)
{
errno = EAGAIN;
return -1;
}
|
fstat
|
函数 | 获取打开的文件的状态 | 默认示例:#include <sys/stat.h>
int fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
|
getpid
|
函数 | 获取进程ID | 默认示例:int getpid(void)
{
return 1;
}
|
isatty
|
函数 | 查询输出流是否为终端 | 默认示例:int isatty(int file)
{
return 1;
}
|
kill
|
函数 | 发送信号 | 默认示例:#include <errno.h>
#undef errno
extern int errno;
int kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}
|
link
|
函数 | 给已有文件创建一个新名字 | 默认示例:#include <errno.h>
#undef errno
extern int errno;
int link(char *old, char *new)
{
errno = EMLINK;
return -1;
}
|
lseek
|
函数 | 设置文件位置 | 默认示例:int lseek(int file, int ptr, int dir)
{
return 0;
}
|
open
|
函数 | 打开文件 | 默认示例:int open(const char *name, int flags, int mode)
{
return -1;
}
|
read
|
函数 | 读取文件 | 默认示例:int read(int file, char *ptr, int len)
{
return 0;
}
|
sbrk
|
函数 | 增大程序数据空间,默认的内存分配函数依赖此函数 | 默认示例:caddr_t sbrk(int incr)
{
extern char _end; /* Defined by the linker */
static char *heap_end;
char *prev_heap_end;
if (heap_end == 0) {
heap_end = &_end;
}
prev_heap_end = heap_end;
if (heap_end + incr > stack_ptr) {
write (1, "Heap and stack collision\n", 25);
abort ();
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
|
stat
|
函数 | 获取文件状态 | 默认示例:int stat(char *file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
|
times
|
函数 | 当前进程时间信息 | 默认示例:int times(struct tms *buf)
{
return -1;
}
|
unlink
|
函数 | 移除文件的目录入口,相当于删除 | 默认示例:#include <errno.h>
#undef errno
extern int errno;
int unlink(char *name)
{
errno = ENOENT;
return -1;
}
|
wait
|
函数 | 等待子进程 | 默认示例:#include <errno.h>
#undef errno
extern int errno;
int wait(int *status)
{
errno = ECHILD;
return -1;
}
|
write
|
函数 | 写入数据到文件 | 默认示例:int write(int file, char *ptr, int len)
{
int todo;
for (todo = 0; todo < len; todo++) {
outbyte (*ptr++);
}
return len;
}
|