lib: Move x_fgets_slash to xfile.c
authorVolker Lendecke <vl@samba.org>
Wed, 16 Nov 2016 16:54:38 +0000 (16:54 +0000)
committerJeremy Allison <jra@samba.org>
Sun, 20 Nov 2016 01:28:11 +0000 (02:28 +0100)
This makes it easier to remove the global #include xfile.h

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/samba_util.h
lib/util/util_file.c
lib/util/xfile.c
lib/util/xfile.h

index 74adb25df240e1e4a81294bd2853738202bd1114..2f8de9e7890edb3dcfbec0b6adfd78594ea30229 100644 (file)
@@ -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 
  */
index 52749ea0509b325b683ab8530d1c004a95efcd14..4948afb16b62046a148a26f83edf8054c5a6e024 100644 (file)
 #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
  */
index 62dd1213a75d9983db265c63d18781b6963bb108..b22cb9871f28e118c393f81d6caac270a70bebe0 100644 (file)
@@ -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);
+}
index f52596dc8a8a2924eba563888b8f671ef6388412..5fb6341dfc7239352e562090f03e95ad915fcf36 100644 (file)
@@ -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_ */