From 6543e1436ee5795d5ac5b9e7833a99093893fd5e Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sat, 16 Feb 2019 08:26:37 +0100 Subject: init: add limited length versions of print/println This adds printn() and printnln() to limit the portion of the string to a certain length. Two more callers were converted and simplified. --- init/init.c | 54 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/init/init.c b/init/init.c index a061ded..906fcbe 100644 --- a/init/init.c +++ b/init/init.c @@ -331,27 +331,40 @@ static char tmp_path[MAXPATHLEN]; */ +/* prints character */ +static void pchar(char c) +{ + write(1, &c, 1); +} + +/* prints string up to chars */ +static void printn(const char *c, int n) +{ + const char *p = c; + + while (*p && n--) + p++; + + write(1, c, p-c); +} + +/* same as above but also adds a trailing '\n' */ +static void printnln(const char *c, int n) +{ + printn(c, n); + pchar('\n'); +} + /* Used to emit informative messages */ -static void print(char *c) +static void print(const char *c) { - char *p = c; - - while (*p) - p++; - - write(1, c, p-c); + printn(c, -1); } /* same as above but also adds a trailing '\n' */ -static void println(char *c) +static void println(const char *c) { - char *p = c; - - while (*p) - p++; - - write(1, c, p-c); - write(1, "\n", 1); + printnln(c, -1); } /* Used only to emit debugging messages when compiled with -DDEBUG */ @@ -870,11 +883,8 @@ static int tar_extract(const char *action, const char *file, const char *dir) len += my_strlcpy(dest + len, blk.hdr.prefix, 156); // max 155 copied. len += my_strlcpy(dest + len, blk.hdr.name, 101); // max 100 copied. - if (action[0] == 't' || (action[0] == 'x' && action[1] == 'v')) { - dest[len] = '\n'; - write(1, dest, len + 1); - dest[len] = 0; - } + if (action[0] == 't' || (action[0] == 'x' && action[1] == 'v')) + printnln(dest, len); /* now concatenate , "/" and into */ pos = 0; @@ -1062,7 +1072,7 @@ static int list_dir(const char *fmt, const char *dir) default : str = "? "; break; } #endif - write(1, str, 2); + printn(str, 2); } println(d->d_name); } @@ -1811,7 +1821,7 @@ int main(int argc, char **argv, char **envp) *p++ = '}'; } *p++ = ' '; - write(1, prompt, p-prompt); + printn(prompt, p-prompt); len = read(0, cmd_line, sizeof(cmd_line)-1); if (len > 0) { -- 2.20.1