nwrap: Fix resolving hostnames with a trailing dot.
[sfrench/samba-autobuild/.git] / lib / util / xfile.c
index 7cfb68018e95ef6b858079acbf56034fa1eee95f..62dd1213a75d9983db265c63d18781b6963bb108 100644 (file)
   and doesn't support O_RDWR. That keeps the code simple.
 */
 
-#include "includes.h"
+#include "replace.h"
 #include "system/filesys.h"
-
-#if _SAMBA_BUILD_ == 3
-#undef malloc
-#define malloc SMB_MALLOC
-#undef malloc_p
-#define malloc_p SMB_MALLOC_P
-#endif
+#include "memory.h"
+#include "xfile.h"
 
 #define XBUFSIZE BUFSIZ
 
@@ -107,7 +102,7 @@ XFILE *x_fopen(const char *fname, int flags, mode_t mode)
 {
        XFILE *ret;
 
-       ret = malloc_p(XFILE);
+       ret = (XFILE *)malloc_p(XFILE);
        if (!ret) return NULL;
 
        memset(ret, 0, sizeof(XFILE));
@@ -115,6 +110,7 @@ XFILE *x_fopen(const char *fname, int flags, mode_t mode)
        if ((flags & O_ACCMODE) == O_RDWR) {
                /* we don't support RDWR in XFILE - use file 
                   descriptors instead */
+               SAFE_FREE(ret);
                errno = EINVAL;
                return NULL;
        }
@@ -332,12 +328,27 @@ int x_fgetc(XFILE *f)
 /** simulate fread */
 size_t x_fread(void *p, size_t size, size_t nmemb, XFILE *f)
 {
+       size_t remaining = size * nmemb;
        size_t total = 0;
-       while (total < size*nmemb) {
-               int c = x_fgetc(f);
-               if (c == EOF) break;
-               (total+(char *)p)[0] = (char)c;
-               total++;
+
+       while (remaining > 0) {
+               size_t thistime;
+
+               x_fillbuf(f);
+
+               if (f->bufused == 0) {
+                       f->flags |= X_FLAG_EOF;
+                       break;
+               }
+
+               thistime = MIN(f->bufused, remaining);
+
+               memcpy((char *)p+total, f->next, thistime);
+
+               f->next += thistime;
+               f->bufused -= thistime;
+               remaining -= thistime;
+               total += thistime;
        }
        return total/size;
 }
@@ -405,7 +416,7 @@ XFILE *x_fdup(const XFILE *f)
                return NULL;
        }
 
-       ret = malloc_p(XFILE);
+       ret = (XFILE *)malloc_p(XFILE);
        if (!ret) {
                close(fd);
                return NULL;