From d50e44c563137f77cf33155e19c9bb3697e015cc Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Thu, 31 Mar 2022 10:12:01 +0100 Subject: [PATCH] C++ __restrict__, update STATUS, minor formatting --- STATUS | 2 ++ include/stdio.h | 12 +++++++----- src/libc/stdio/getdelim.c | 28 ++++++++++++---------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/STATUS b/STATUS index 5178afa..d6016d6 100644 --- a/STATUS +++ b/STATUS @@ -130,6 +130,8 @@ TEST: Function/symbol/macro needs to be tested 7.19.7.9 putchar - 7.19.7.10 puts - 7.19.7.11 ungetc - + (EXT) getline - + (EXT) getdelim - 7.19.8.1 fread - 7.19.8.2 fwrite - diff --git a/include/stdio.h b/include/stdio.h index 2a090bd..3e79b73 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -277,7 +277,7 @@ extern char *fgets(char * __restrict__ __s, int __n, extern int fputc(int __c, FILE *__fp); /* Write a string to a stream (excluding the NUL nyte). */ -extern int fputs(char const * __restrict__ s, FILE * __restrict__ fp); +extern int fputs(char const * __restrict__ __s, FILE * __restrict__ __fp); extern int getc(FILE *__fp); #define getc fgetc @@ -289,11 +289,13 @@ extern int getchar(void); /* (DEPRECATED; use fgets() instead) Read a string from stdin. */ extern char *gets(char *__s); -/*get a line from stream*/ -extern ssize_t getline(char **restrict __lineptr, size_t *restrict __n, FILE *restrict __fp); +/* Get a line from stream, with dynamic allocation */ +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); +/* 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); #define putc fputc diff --git a/src/libc/stdio/getdelim.c b/src/libc/stdio/getdelim.c index d2b5014..555c7c8 100644 --- a/src/libc/stdio/getdelim.c +++ b/src/libc/stdio/getdelim.c @@ -4,46 +4,42 @@ #include #include "fileutil.h" -ssize_t getdelim(char **restrict lineptr, size_t *restrict n, int delim, FILE *restrict fp) +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; + if(lineptr == NULL || n == NULL || fp == NULL) { + errno = EINVAL; return -1; } - if(*lineptr == NULL) - { + 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); + 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)) - { + + 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)); + } + while((*lineptr)[cur-1] != delim && !feof(fp)); (*lineptr)[cur] = '\0'; if(feof(fp) && (*lineptr)[cur-1] != delim) - { return -1; - }else{ - return cur; - } + return cur; }