Skip to content

Commit

Permalink
lib: Add fgets_slash
Browse files Browse the repository at this point in the history
Copy x_fgets_slash with conversion to stdio and talloc.

Probably I'd do this functionality a bit differently, but for simplicity I
chose to make it the same as what is there.

Signed-off-by: Volker Lendecke <[email protected]>
Reviewed-by: Jeremy Allison <[email protected]>
  • Loading branch information
vlendec committed Dec 11, 2016
1 parent 7437150 commit 9a6243e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/util/samba_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ const char **str_list_make_v3_const(TALLOC_CTX *mem_ctx,
*/
_PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint);

char *fgets_slash(TALLOC_CTX *mem_ctx, char *s2, int maxlen, FILE *f);

/**
load a file into memory from a fd.
**/
Expand Down
82 changes: 82 additions & 0 deletions lib/util/util_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,88 @@ _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
return data;
}

char *fgets_slash(TALLOC_CTX *mem_ctx, char *s2, int maxlen, FILE *f)
{
char *s = s2;
int len = 0;
int c;
bool start_of_line = true;

if (feof(f)) {
return NULL;
}

if (maxlen < 2) {
return NULL;
}

if (s2 == NULL) {
maxlen = MIN(maxlen,8);
s = talloc_array(mem_ctx, char, maxlen);
}

if (s == NULL) {
return NULL;
}

*s = 0;

while (len < maxlen-1) {
c = 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 == NULL)) {
TALLOC_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 == NULL) && (len > maxlen-3)) {
int m;
char *t;

m = maxlen * 2;
if (m < maxlen) {
DBG_ERR("length overflow");
TALLOC_FREE(s);
return NULL;
}
maxlen = m;

t = talloc_realloc(mem_ctx, s, char, maxlen);
if (t == NULL) {
DBG_ERR("failed to expand buffer!\n");
TALLOC_FREE(s);
return NULL;
}

s = t;
}
}

return s;
}

/**
load a file into memory from a fd.
Expand Down

0 comments on commit 9a6243e

Please sign in to comment.