Install public header files again and include required prototypes.
[jelmer/samba4-debian.git] / source / lib / util / util_file.c
index 430472f25a42dc28757897823567d9fea1f75dc2..c3e22196c0cb69cc95fb6a1b86936e4042dc1d99 100644 (file)
@@ -7,7 +7,7 @@
  * 
  * This program is free software; you can redistribute it and/or modify it under
  * the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
+ * Software Foundation; either version 3 of the License, or (at your option)
  * any later version.
  * 
  * This program is distributed in the hope that it will be useful, but WITHOUT
@@ -16,8 +16,7 @@
  * more details.
  * 
  * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 675
- * Mass Ave, Cambridge, MA 02139, USA.
+ * this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
@@ -39,7 +38,7 @@ _PUBLIC_ char *fgets_slash(char *s2,int maxlen,XFILE *f)
   char *s=s2;
   int len = 0;
   int c;
-  BOOL start_of_line = True;
+  bool start_of_line = true;
 
   if (x_feof(f))
     return(NULL);
@@ -71,7 +70,7 @@ _PUBLIC_ char *fgets_slash(char *s2,int maxlen,XFILE *f)
          if (len > 0 && s[len-1] == '\\')
            {
              s[--len] = 0;
-             start_of_line = True;
+             start_of_line = true;
              break;
            }
          return(s);
@@ -82,8 +81,9 @@ _PUBLIC_ char *fgets_slash(char *s2,int maxlen,XFILE *f)
        case ' ':
          if (start_of_line)
            break;
+         /* fall through */
        default:
-         start_of_line = False;
+         start_of_line = false;
          s[len++] = c;
          s[len] = 0;
        }
@@ -124,6 +124,10 @@ _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
 
                ret = read(fd, data + offset, hint);
 
+               if (ret == 0) {
+                       return NULL;
+               }
+
                if (ret == -1) {
                        talloc_free(data);
                        return NULL;
@@ -236,6 +240,7 @@ _PUBLIC_ void *map_file(const char *fname, size_t size)
 
 /**
 parse a buffer into lines
+'p' will be freed on error, and otherwise will be made a child of the returned array
 **/
 static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
 {
@@ -254,10 +259,9 @@ static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *
                return NULL;
        }       
        
-       talloc_reference(ret, p);
+       talloc_steal(ret, p);
        
        memset(ret, 0, sizeof(ret[0])*(i+2));
-       if (numlines) *numlines = i;
 
        ret[0] = p;
        for (s = p, i=0; s < p+size; s++) {
@@ -269,6 +273,13 @@ static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *
                if (s[0] == '\r') s[0] = 0;
        }
 
+       /* remove any blank lines at the end */
+       while (i > 0 && ret[i-1][0] == 0) {
+               i--;
+       }
+
+       if (numlines) *numlines = i;
+
        return ret;
 }
 
@@ -280,17 +291,12 @@ must be freed with talloc_free().
 _PUBLIC_ 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, mem_ctx);
        if (!p) return NULL;
 
-       lines = file_lines_parse(p, size, numlines, mem_ctx);
-
-       talloc_free(p);
-
-       return lines;
+       return file_lines_parse(p, size, numlines, mem_ctx);
 }
 
 /**
@@ -301,17 +307,12 @@ the list.
 _PUBLIC_ char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx)
 {
        char *p;
-       char **lines;
        size_t size;
 
        p = fd_load(fd, &size, mem_ctx);
        if (!p) return NULL;
 
-       lines = file_lines_parse(p, size, numlines, mem_ctx);
-
-       talloc_free(p);
-
-       return lines;
+       return file_lines_parse(p, size, numlines, mem_ctx);
 }
 
 
@@ -341,45 +342,36 @@ _PUBLIC_ void file_lines_slashcont(char **lines)
 /**
   save a lump of data into a file. Mostly used for debugging 
 */
-_PUBLIC_ BOOL file_save(const char *fname, const void *packet, size_t length)
+_PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
 {
        int fd;
        fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
        if (fd == -1) {
-               return False;
+               return false;
        }
        if (write(fd, packet, length) != (size_t)length) {
-               return False;
+               return false;
        }
        close(fd);
-       return True;
+       return true;
 }
 
-/**
-  see if a file exists
-*/
-_PUBLIC_ BOOL file_exists(const char *path)
-{
-       struct stat st;
-       return (stat(path, &st) == 0);
-}
-
-int vfdprintf(int fd, const char *format, va_list ap) _PRINTF_ATTRIBUTE(2,0)
+_PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
 {
        char *p;
        int len, ret;
        va_list ap2;
 
-       VA_COPY(ap2, ap);
-
+       va_copy(ap2, ap);
        len = vasprintf(&p, format, ap2);
+       va_end(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)
+_PUBLIC_ int fdprintf(int fd, const char *format, ...)
 {
        va_list ap;
        int ret;
@@ -389,3 +381,24 @@ int fdprintf(int fd, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
        va_end(ap);
        return ret;
 }
+
+
+/*
+  try to determine if the filesystem supports large files
+*/
+_PUBLIC_ bool large_file_support(const char *path)
+{
+       int fd;
+       ssize_t ret;
+       char c;
+
+       fd = open(path, O_RDWR|O_CREAT, 0600);
+       unlink(path);
+       if (fd == -1) {
+               /* have to assume large files are OK */
+               return true;
+       }
+       ret = pread(fd, &c, 1, ((uint64_t)1)<<32);
+       close(fd);
+       return ret == 0;
+}