aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-08-21 17:35:29 -0500
committerRob Landley <rob@landley.net>2019-08-21 17:35:29 -0500
commit7d142c2c6e6b8d5ae62351dbab23967b78f12336 (patch)
tree408ca620e69b3f63eac5b106d4fb744c45281eee /lib
parente7a5d612d3c8c4f693c3059b75f4a8fa4e8f7d46 (diff)
downloadtoybox-7d142c2c6e6b8d5ae62351dbab23967b78f12336.tar.gz
Add xgetline (which the last sh checkin used, oops).
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.h1
-rw-r--r--lib/xwrap.c15
2 files changed, 16 insertions, 0 deletions
diff --git a/lib/lib.h b/lib/lib.h
index 9c5e9a37..25417105 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -186,6 +186,7 @@ void xsignal_flags(int signal, void *handler, int flags);
void xsignal(int signal, void *handler);
time_t xvali_date(struct tm *tm, char *str);
void xparsedate(char *str, time_t *t, unsigned *nano, int endian);
+char *xgetline(FILE *fp, int *len);
// lib.c
void verror_msg(char *msg, int err, va_list va);
diff --git a/lib/xwrap.c b/lib/xwrap.c
index be57097d..317b149f 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -1052,3 +1052,18 @@ void xparsedate(char *str, time_t *t, unsigned *nano, int endian)
if (oldtz) setenv("TZ", oldtz, 1);
free(oldtz);
}
+
+char *xgetline(FILE *fp, int *len)
+{
+ char *new = 0;
+ size_t linelen = 0;
+
+ errno = 0;
+ if (1>(linelen = getline(&new, &linelen, fp))) {
+ if (errno) perror_msg("getline");
+ new = 0;
+ } else if (new[linelen-1] == '\n') new[--linelen] = 0;
+ if (len) *len = linelen;
+
+ return new;
+}