libc: Add getline() and getdelim()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
5117b410db
commit
47d505dcbb
@ -33,7 +33,7 @@ char* getpass()
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char buf[1024];
|
static char buf[BUFSIZ];
|
||||||
char* rc = fgets(buf, sizeof(buf), stdin);
|
char* rc = fgets(buf, sizeof(buf), stdin);
|
||||||
|
|
||||||
restore_terminal();
|
restore_terminal();
|
||||||
|
@ -26,6 +26,8 @@ extern FILE* stderr;
|
|||||||
#define stdout stdout
|
#define stdout stdout
|
||||||
#define stderr stderr
|
#define stderr stderr
|
||||||
|
|
||||||
|
#define BUFSIZ 1024
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
@ -96,6 +98,12 @@ extern "C"
|
|||||||
/* Read a line from stream. */
|
/* Read a line from stream. */
|
||||||
char* fgets(char* buf, size_t size, FILE* stream);
|
char* fgets(char* buf, size_t size, FILE* stream);
|
||||||
|
|
||||||
|
/* Read a line from stream and store it in a dynamically-allocated buffer. */
|
||||||
|
ssize_t getline(char** linep, size_t* n, FILE* stream);
|
||||||
|
|
||||||
|
/* Read a line from stream and store it in a dynamically-allocated buffer. */
|
||||||
|
ssize_t getdelim(char** linep, size_t* n, int delim, FILE* stream);
|
||||||
|
|
||||||
/* Clear the error and end-of-file indicators in stream. */
|
/* Clear the error and end-of-file indicators in stream. */
|
||||||
void clearerr(FILE* stream);
|
void clearerr(FILE* stream);
|
||||||
|
|
||||||
|
@ -236,6 +236,66 @@ extern "C"
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ssize_t getline(char** linep, size_t* n, FILE* stream)
|
||||||
|
{
|
||||||
|
return getdelim(linep, n, '\n', stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t getdelim(char** linep, size_t* n, int delim, FILE* stream)
|
||||||
|
{
|
||||||
|
if (!n || !linep)
|
||||||
|
{
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* buf = *linep;
|
||||||
|
size_t size = *n;
|
||||||
|
size_t len = 0;
|
||||||
|
|
||||||
|
if (!buf)
|
||||||
|
{
|
||||||
|
buf = (char*)malloc(BUFSIZ);
|
||||||
|
size = BUFSIZ;
|
||||||
|
if (!buf) return -1;
|
||||||
|
*linep = buf;
|
||||||
|
*n = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int c = fgetc(stream);
|
||||||
|
if (c == EOF) break;
|
||||||
|
|
||||||
|
if (len == size)
|
||||||
|
{
|
||||||
|
buf = (char*)realloc(buf, size + 64);
|
||||||
|
size += 64;
|
||||||
|
if (!buf) return -1;
|
||||||
|
*linep = buf;
|
||||||
|
*n = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf[len++] = (char)c;
|
||||||
|
if (c == delim) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len == 0) return -1;
|
||||||
|
|
||||||
|
if (len == size)
|
||||||
|
{
|
||||||
|
buf = (char*)realloc(buf, size + 16);
|
||||||
|
size += 16;
|
||||||
|
if (!buf) return -1;
|
||||||
|
*linep = buf;
|
||||||
|
*n = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf[len] = '\0';
|
||||||
|
|
||||||
|
return (ssize_t)len;
|
||||||
|
}
|
||||||
|
|
||||||
void clearerr(FILE* stream)
|
void clearerr(FILE* stream)
|
||||||
{
|
{
|
||||||
stream->_eof = stream->_err = 0;
|
stream->_eof = stream->_err = 0;
|
||||||
|
@ -36,7 +36,7 @@ static bool try_format(strftime_state& state, const char* format, ...)
|
|||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
|
|
||||||
char buf[1024];
|
char buf[BUFSIZ];
|
||||||
vsnprintf(buf, sizeof(buf), format, ap);
|
vsnprintf(buf, sizeof(buf), format, ap);
|
||||||
|
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
@ -347,17 +348,17 @@ extern "C"
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
buf = (char*)malloc(1024);
|
buf = (char*)malloc(BUFSIZ);
|
||||||
if (!buf) return nullptr;
|
if (!buf) return nullptr;
|
||||||
|
|
||||||
ssize_t rc = __getcwd_wrapper(buf, 1024);
|
ssize_t rc = __getcwd_wrapper(buf, BUFSIZ);
|
||||||
if (rc < 0)
|
if (rc < 0)
|
||||||
{
|
{
|
||||||
free(buf);
|
free(buf);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc > 1024)
|
if (rc > BUFSIZ)
|
||||||
{
|
{
|
||||||
free(buf);
|
free(buf);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user