未验证 提交 9840e307 编写于 作者: O openharmony_ci 提交者: Gitee

!956 Optimize fgets API in C library

Merge pull request !956 from Wang Yaofeng/fgets_opt
......@@ -2183,6 +2183,7 @@ musl_src_porting_file = [
"src/stdio/__toread.c",
"src/stdio/__towrite.c",
"src/stdio/stderr.c",
"src/stdio/fgets.c",
"src/internal/stdio_impl.h",
"src/internal/vdso.c",
"src/time/clock_gettime.c",
......
#include "stdio_impl.h"
#include <string.h>
#define MIN(a,b) ((a)<(b) ? (a) : (b))
char *fgets(char *restrict s, int n, FILE *restrict f)
{
char *p = s;
unsigned char *z;
size_t k;
int c;
FLOCK(f);
if (n--<=1) {
f->mode |= f->mode-1;
FUNLOCK(f);
if (n) {
return NULL;
}
*s = '\0';
return s;
}
while (n) {
if (f->rpos == f->rend && __fill_buffer(f)) {
if (p == s || !feof(f)) {
s = 0;
}
break;
}
/* search minimal length */
k = MIN(f->rend - f->rpos, n);
z = memchr(f->rpos, '\n', k);
if (z != NULL) {
k = z - f->rpos + 1;
}
memcpy(p, f->rpos, k);
f->rpos += k;
p += k;
n -= k;
if (z || !n) {
break;
}
}
if (s) {
*p = 0;
}
FUNLOCK(f);
return s;
}
weak_alias(fgets, fgets_unlocked);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册