r10246: Remove unused function
[bbaumbach/samba-autobuild/.git] / source4 / lib / util_file.c
index e02198754d5a71098ab75677e4c39cbedfc891b5..156d09aaf5b6a002b66aeb0e7af771dccd855fa4 100644 (file)
 #include "system/shmem.h"
 #include "system/filesys.h"
 
-/*************************************************************************
- gets a line out of a file.
- line is of format "xxxx:xxxxxx:xxxxx:".
- lines with "#" at the front are ignored.
-*************************************************************************/
-int getfileline(void *vp, char *linebuf, int linebuf_size)
-{
-       /* Static buffers we will return. */
-       FILE *fp = (FILE *)vp;
-       uint8_t   c;
-       uint8_t  *p;
-       size_t            linebuf_len;
-
-       if (fp == NULL)
-       {
-               DEBUG(0,("getfileline: Bad file pointer.\n"));
-               return -1;
-       }
-
-       /*
-        * Scan the file, a line at a time.
-        */
-       while (!feof(fp))
-       {
-               linebuf[0] = '\0';
-
-               fgets(linebuf, linebuf_size, fp);
-               if (ferror(fp))
-               {
-                       return -1;
-               }
-
-               /*
-                * Check if the string is terminated with a newline - if not
-                * then we must keep reading and discard until we get one.
-                */
-
-               linebuf_len = strlen(linebuf);
-               if (linebuf_len == 0)
-               {
-                       linebuf[0] = '\0';
-                       return 0;
-               }
-
-               if (linebuf[linebuf_len - 1] != '\n')
-               {
-                       c = '\0';
-                       while (!ferror(fp) && !feof(fp))
-                       {
-                               c = fgetc(fp);
-                               if (c == '\n')
-                               {
-                                       break;
-                               }
-                       }
-               }
-               else
-               {
-                       linebuf[linebuf_len - 1] = '\0';
-               }
-
-#ifdef DEBUG_PASSWORD
-               DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
-#endif
-               if ((linebuf[0] == 0) && feof(fp))
-               {
-                       DEBUG(4, ("getfileline: end of file reached\n"));
-                       return 0;
-               }
-
-               if (linebuf[0] == '#' || linebuf[0] == '\0')
-               {
-                       DEBUG(6, ("getfileline: skipping comment or blank line\n"));
-                       continue;
-               }
-
-               p = (uint8_t *) strchr_m(linebuf, ':');
-               if (p == NULL)
-               {
-                       DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
-                       continue;
-               }
-               return linebuf_len;
-       }
-       return -1;
-}
-
-
 /****************************************************************************
 read a line from a file with possible \ continuation chars. 
 Blanks at the start or end of a line are stripped.
@@ -190,18 +102,18 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f)
 load a file into memory from a fd.
 ****************************************************************************/ 
 
-char *fd_load(int fd, size_t *size)
+char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx)
 {
        struct stat sbuf;
        char *p;
 
        if (fstat(fd, &sbuf) != 0) return NULL;
 
-       p = (char *)malloc(sbuf.st_size+1);
+       p = (char *)talloc_size(mem_ctx, sbuf.st_size+1);
        if (!p) return NULL;
 
        if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
-               SAFE_FREE(p);
+               talloc_free(p);
                return NULL;
        }
        p[sbuf.st_size] = 0;
@@ -214,7 +126,7 @@ char *fd_load(int fd, size_t *size)
 /****************************************************************************
 load a file into memory
 ****************************************************************************/
-char *file_load(const char *fname, size_t *size)
+char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx)
 {
        int fd;
        char *p;
@@ -224,7 +136,7 @@ char *file_load(const char *fname, size_t *size)
        fd = open(fname,O_RDONLY);
        if (fd == -1) return NULL;
 
-       p = fd_load(fd, size);
+       p = fd_load(fd, size, mem_ctx);
 
        close(fd);
 
@@ -254,12 +166,12 @@ void *map_file(char *fname, size_t size)
        }
 #endif
        if (!p) {
-               p = file_load(fname, &s2);
+               p = file_load(fname, &s2, talloc_autofree_context());
                if (!p) return NULL;
                if (s2 != size) {
                        DEBUG(1,("incorrect size for %s - got %d expected %d\n",
-                                fname, s2, size));
-                       if (p) free(p);
+                                fname, (int)s2, (int)size));
+                       talloc_free(p);
                        return NULL;
                }
        }
@@ -271,7 +183,7 @@ void *map_file(char *fname, size_t size)
 /****************************************************************************
 parse a buffer into lines
 ****************************************************************************/
-static char **file_lines_parse(char *p, size_t size, int *numlines)
+static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
 {
        int i;
        char *s, **ret;
@@ -282,11 +194,14 @@ static char **file_lines_parse(char *p, size_t size, int *numlines)
                if (s[0] == '\n') i++;
        }
 
-       ret = malloc_array_p(char *, i+2);
+       ret = talloc_array(mem_ctx, char *, i+2);
        if (!ret) {
-               SAFE_FREE(p);
+               talloc_free(p);
                return NULL;
        }       
+       
+       talloc_reference(ret, p);
+       
        memset(ret, 0, sizeof(ret[0])*(i+2));
        if (numlines) *numlines = i;
 
@@ -306,49 +221,48 @@ static char **file_lines_parse(char *p, size_t size, int *numlines)
 
 /****************************************************************************
 load a file into memory and return an array of pointers to lines in the file
-must be freed with file_lines_free(). 
+must be freed with talloc_free(). 
 ****************************************************************************/
-char **file_lines_load(const char *fname, int *numlines)
+char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx)
 {
        char *p;
+       char **lines;
        size_t size;
 
-       p = file_load(fname, &size);
+       p = file_load(fname, &size, mem_ctx);
        if (!p) return NULL;
 
-       return file_lines_parse(p, size, numlines);
+       lines = file_lines_parse(p, size, numlines, mem_ctx);
+
+       talloc_free(p);
+
+       return lines;
 }
 
 /****************************************************************************
 load a fd into memory and return an array of pointers to lines in the file
-must be freed with file_lines_free(). If convert is true calls unix_to_dos on
+must be freed with talloc_free(). If convert is true calls unix_to_dos on
 the list.
 ****************************************************************************/
-char **fd_lines_load(int fd, int *numlines)
+char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx)
 {
        char *p;
+       char **lines;
        size_t size;
 
-       p = fd_load(fd, &size);
+       p = fd_load(fd, &size, mem_ctx);
        if (!p) return NULL;
 
-       return file_lines_parse(p, size, numlines);
-}
+       lines = file_lines_parse(p, size, numlines, mem_ctx);
 
+       talloc_free(p);
 
-/****************************************************************************
-free lines loaded with file_lines_load
-****************************************************************************/
-void file_lines_free(char **lines)
-{
-       if (!lines) return;
-       SAFE_FREE(lines[0]);
-       SAFE_FREE(lines);
+       return lines;
 }
 
 
 /****************************************************************************
-take a lislist of lines and modify them to produce a list where \ continues
+take a list of lines and modify them to produce a list where \ continues
 a line
 ****************************************************************************/
 void file_lines_slashcont(char **lines)
@@ -395,3 +309,29 @@ BOOL file_exists(const char *path)
        struct stat st;
        return (stat(path, &st) == 0);
 }
+
+int vfdprintf(int fd, const char *format, va_list ap) 
+{
+       char *p;
+       int len, ret;
+       va_list ap2;
+
+       VA_COPY(ap2, ap);
+
+       len = vasprintf(&p, format, ap2);
+       if (len <= 0) return len;
+       ret = write(fd, p, len);
+       SAFE_FREE(p);
+       return ret;
+}
+
+int fdprintf(int fd, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
+{
+       va_list ap;
+       int ret;
+
+       va_start(ap, format);
+       ret = vfdprintf(fd, format, ap);
+       va_end(ap);
+       return ret;
+}