a C standard library for fx Casio calculators
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
fxlibc/src/libc/stdio/getdelim.c

45 lines
936 B

#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;
return cur;
}