From c06b78d0be5066c7e56c7715798f5a7b0b676451 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Wed, 16 Nov 2016 16:54:38 +0000 Subject: lib: Move x_fgets_slash to xfile.c This makes it easier to remove the global #include xfile.h Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison --- lib/util/samba_util.h | 7 ---- lib/util/util_file.c | 92 ------------------------------------------------- lib/util/xfile.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/util/xfile.h | 7 ++++ 4 files changed, 101 insertions(+), 99 deletions(-) diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index 74adb25df24..2f8de9e7890 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -332,13 +332,6 @@ const char **str_list_make_v3_const(TALLOC_CTX *mem_ctx, /* The following definitions come from lib/util/util_file.c */ -/** -read a line from a file with possible \ continuation chars. -Blanks at the start or end of a line are stripped. -The string will be allocated if s2 is NULL -**/ -_PUBLIC_ char *x_fgets_slash(char *s2,int maxlen,XFILE *f); - /** * Read one line (data until next newline or eof) and allocate it */ diff --git a/lib/util/util_file.c b/lib/util/util_file.c index 52749ea0509..4948afb16b6 100644 --- a/lib/util/util_file.c +++ b/lib/util/util_file.c @@ -26,98 +26,6 @@ #include "lib/util/samba_util.h" #include "lib/util/debug.h" -/** - * @file - * @brief File-related utility functions - */ - -/** -read a line from a file with possible \ continuation chars. -Blanks at the start or end of a line are stripped. -The string will be allocated if s2 is NULL -**/ -_PUBLIC_ char *x_fgets_slash(char *s2,int maxlen,XFILE *f) -{ - char *s=s2; - int len = 0; - int c; - bool start_of_line = true; - - if (x_feof(f)) { - return(NULL); - } - - if (maxlen <2) { - return(NULL); - } - - if (!s2) { - maxlen = MIN(maxlen,8); - s = (char *)malloc(maxlen); - } - - if (!s) { - return(NULL); - } - - *s = 0; - - while (len < maxlen-1) { - c = x_getc(f); - switch (c) - { - case '\r': - break; - case '\n': - while (len > 0 && s[len-1] == ' ') { - s[--len] = 0; - } - if (len > 0 && s[len-1] == '\\') { - s[--len] = 0; - start_of_line = true; - break; - } - return(s); - case EOF: - if (len <= 0 && !s2) { - SAFE_FREE(s); - } - return(len>0?s:NULL); - case ' ': - if (start_of_line) { - break; - } - /* fall through */ - default: - start_of_line = false; - s[len++] = c; - s[len] = 0; - } - if (!s2 && len > maxlen-3) { - int m; - char *t; - - m = maxlen * 2; - if (m < maxlen) { - DBG_ERR("length overflow"); - SAFE_FREE(s); - return NULL; - } - maxlen = m; - - t = realloc_p(s, char, maxlen); - if (!t) { - DBG_ERR("failed to expand buffer!\n"); - SAFE_FREE(s); - return(NULL); - } - - s = t; - } - } - return(s); -} - /** * Read one line (data until next newline or eof) and allocate it */ diff --git a/lib/util/xfile.c b/lib/util/xfile.c index 62dd1213a75..b22cb9871f2 100644 --- a/lib/util/xfile.c +++ b/lib/util/xfile.c @@ -37,6 +37,8 @@ #include "system/filesys.h" #include "memory.h" #include "xfile.h" +#include "lib/util/debug.h" +#include "lib/util/samba_util.h" #define XBUFSIZE BUFSIZ @@ -428,3 +430,95 @@ XFILE *x_fdup(const XFILE *f) x_setvbuf(ret, NULL, X_IOFBF, XBUFSIZE); return ret; } + +/** + * @file + * @brief File-related utility functions + */ + +/** +read a line from a file with possible \ continuation chars. +Blanks at the start or end of a line are stripped. +The string will be allocated if s2 is NULL +**/ +char *x_fgets_slash(char *s2,int maxlen,XFILE *f) +{ + char *s=s2; + int len = 0; + int c; + bool start_of_line = true; + + if (x_feof(f)) { + return(NULL); + } + + if (maxlen <2) { + return(NULL); + } + + if (!s2) { + maxlen = MIN(maxlen,8); + s = (char *)malloc(maxlen); + } + + if (!s) { + return(NULL); + } + + *s = 0; + + while (len < maxlen-1) { + c = x_getc(f); + switch (c) + { + case '\r': + break; + case '\n': + while (len > 0 && s[len-1] == ' ') { + s[--len] = 0; + } + if (len > 0 && s[len-1] == '\\') { + s[--len] = 0; + start_of_line = true; + break; + } + return(s); + case EOF: + if (len <= 0 && !s2) { + SAFE_FREE(s); + } + return(len>0?s:NULL); + case ' ': + if (start_of_line) { + break; + } + /* fall through */ + default: + start_of_line = false; + s[len++] = c; + s[len] = 0; + } + if (!s2 && len > maxlen-3) { + int m; + char *t; + + m = maxlen * 2; + if (m < maxlen) { + DBG_ERR("length overflow"); + SAFE_FREE(s); + return NULL; + } + maxlen = m; + + t = realloc_p(s, char, maxlen); + if (!t) { + DBG_ERR("failed to expand buffer!\n"); + SAFE_FREE(s); + return(NULL); + } + + s = t; + } + } + return(s); +} diff --git a/lib/util/xfile.h b/lib/util/xfile.h index f52596dc8a8..5fb6341dfc7 100644 --- a/lib/util/xfile.h +++ b/lib/util/xfile.h @@ -104,4 +104,11 @@ off_t x_tseek(XFILE *f, off_t offset, int whence); XFILE *x_fdup(const XFILE *f); +/** +read a line from a file with possible \ continuation chars. +Blanks at the start or end of a line are stripped. +The string will be allocated if s2 is NULL +**/ +_PUBLIC_ char *x_fgets_slash(char *s2,int maxlen,XFILE *f); + #endif /* _XFILE_H_ */ -- cgit v1.2.1