mirror of
https://git.planet-casio.com/Vhex-Kernel-Core/fxlibc.git
synced 2024-12-29 13:03:38 +01:00
Merge pull request 'Add getline(3) & getdelim(3)' (#2) from Alice/fxlibc:dev into dev
Reviewed-on: https://gitea.planet-casio.com/Vhex-Kernel-Core/fxlibc/pulls/2
This commit is contained in:
commit
94faa6cbea
4 changed files with 63 additions and 0 deletions
|
@ -138,6 +138,8 @@ set(SOURCES
|
||||||
src/libc/stdio/getc.c
|
src/libc/stdio/getc.c
|
||||||
src/libc/stdio/getchar.c
|
src/libc/stdio/getchar.c
|
||||||
src/libc/stdio/gets.c
|
src/libc/stdio/gets.c
|
||||||
|
src/libc/stdio/getline.c
|
||||||
|
src/libc/stdio/getdelim.c
|
||||||
src/libc/stdio/perror.c
|
src/libc/stdio/perror.c
|
||||||
src/libc/stdio/printf.c
|
src/libc/stdio/printf.c
|
||||||
src/libc/stdio/printf/format_fixed.c
|
src/libc/stdio/printf/format_fixed.c
|
||||||
|
|
|
@ -289,6 +289,12 @@ extern int getchar(void);
|
||||||
/* (DEPRECATED; use fgets() instead) Read a string from stdin. */
|
/* (DEPRECATED; use fgets() instead) Read a string from stdin. */
|
||||||
extern char *gets(char *__s);
|
extern char *gets(char *__s);
|
||||||
|
|
||||||
|
/*get a line from stream*/
|
||||||
|
extern ssize_t getline(char **restrict __lineptr, size_t *restrict __n, FILE *restrict __fp);
|
||||||
|
|
||||||
|
/*like getline but with 'delim' instead of \n*/
|
||||||
|
extern ssize_t getdelim(char **restrict __lineptr, size_t *restrict __n, int __delim, FILE *restrict __fp);
|
||||||
|
|
||||||
extern int putc(int __c, FILE *__fp);
|
extern int putc(int __c, FILE *__fp);
|
||||||
#define putc fputc
|
#define putc fputc
|
||||||
|
|
||||||
|
|
49
src/libc/stdio/getdelim.c
Normal file
49
src/libc/stdio/getdelim.c
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include "fileutil.h"
|
||||||
|
|
||||||
|
ssize_t getdelim(char **restrict lineptr, size_t *restrict n, int delim, FILE *restrict fp)
|
||||||
|
{
|
||||||
|
ssize_t cur = 0;
|
||||||
|
char *new_lineptr = NULL;
|
||||||
|
size_t new_n;
|
||||||
|
|
||||||
|
if(lineptr == NULL || n == NULL || fp == NULL)
|
||||||
|
{
|
||||||
|
errno=EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*lineptr == NULL)
|
||||||
|
{
|
||||||
|
*n = 80;
|
||||||
|
*lineptr = (char *) malloc(*n);
|
||||||
|
if(*lineptr==NULL) return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
ssize_t read_size = __fp_fread2(fp, *lineptr + cur, *n - cur - 1, delim);
|
||||||
|
if(read_size <= 0) return -1;
|
||||||
|
cur += read_size;
|
||||||
|
if((*lineptr)[cur - 1] != delim && !feof(fp))
|
||||||
|
{
|
||||||
|
new_n = *n * 2;
|
||||||
|
new_lineptr = (char *) realloc(*lineptr, new_n);
|
||||||
|
if(new_lineptr == NULL) return -1;
|
||||||
|
*lineptr = new_lineptr;
|
||||||
|
*n = new_n;
|
||||||
|
}
|
||||||
|
}while((*lineptr)[cur-1] != delim && !feof(fp));
|
||||||
|
|
||||||
|
(*lineptr)[cur] = '\0';
|
||||||
|
|
||||||
|
if(feof(fp) && (*lineptr)[cur-1] != delim)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}else{
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
}
|
6
src/libc/stdio/getline.c
Normal file
6
src/libc/stdio/getline.c
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
ssize_t getline(char **restrict lineptr, size_t *restrict n, FILE *restrict fp)
|
||||||
|
{
|
||||||
|
return getdelim(lineptr, n, '\n', fp);
|
||||||
|
}
|
Loading…
Reference in a new issue