From 15cbb92dffc855e6016f0c21cfbeff97a578f1d9 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 5 Dec 2021 05:03:05 -0600 Subject: [PATCH] Move get_line() out of lib/ into last user. --- lib/lib.c | 19 ------------------- lib/pending.h | 7 ------- toys/posix/patch.c | 19 +++++++++++++++++++ 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 04d444f0..b219a5d5 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -754,25 +754,6 @@ void loopfiles_lines(char **argv, void (*function)(char **pline, long len)) loopfiles_rw(argv, O_RDONLY|WARN_ONLY, 0, loopfile_lines_bridge); } -// Slow, but small. -char *get_line(int fd) -{ - char c, *buf = NULL; - long len = 0; - - for (;;) { - if (1>read(fd, &c, 1)) break; - if (!(len & 63)) buf=xrealloc(buf, len+65); - if ((buf[len++]=c) == '\n') break; - } - if (buf) { - buf[len]=0; - if (buf[--len]=='\n') buf[len]=0; - } - - return buf; -} - int wfchmodat(int fd, char *name, mode_t mode) { int rc = fchmodat(fd, name, mode, 0); diff --git a/lib/pending.h b/lib/pending.h index 3c563947..c67d81c8 100644 --- a/lib/pending.h +++ b/lib/pending.h @@ -4,10 +4,3 @@ #define MAX_SALT_LEN 20 //3 for id, 16 for key, 1 for '\0' int read_password(char * buff, int buflen, char* mesg); int update_password(char *filename, char* username, char* encrypted); - -// lib.c -// This should be switched to posix-2008 getline() -char *get_line(int fd); - - -// TODO this goes away when lib/password.c cleaned up diff --git a/toys/posix/patch.c b/toys/posix/patch.c index 656ef422..4b8c61c3 100644 --- a/toys/posix/patch.c +++ b/toys/posix/patch.c @@ -54,6 +54,25 @@ GLOBALS( char *tempname; ) +// TODO xgetline() instead, but replace_tempfile() wants fd... +char *get_line(int fd) +{ + char c, *buf = NULL; + long len = 0; + + for (;;) { + if (1>read(fd, &c, 1)) break; + if (!(len & 63)) buf=xrealloc(buf, len+65); + if ((buf[len++]=c) == '\n') break; + } + if (buf) { + buf[len]=0; + if (buf[--len]=='\n') buf[len]=0; + } + + return buf; +} + // Dispose of a line of input, either by writing it out or discarding it. // state < 2: just free -- 2.39.2