Put strcasecmp/strncasecmp on the banned list (except for needed calls
[jra/samba/.git] / source3 / lib / util_file.c
index c3b444ffa16880e4b8cce5aff8d784ed25a8dfb6..bd505ac921c37f0f89098da39677f4c646bf92e6 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
+ * Unix SMB/CIFS implementation.
+ * SMB parameters and setup
  * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
  * 
  * This program is free software; you can redistribute it and/or modify it under
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
 
 static int gotalarm;
 
@@ -41,9 +45,10 @@ BOOL do_file_lock(int fd, int waitsecs, int type)
 {
   SMB_STRUCT_FLOCK lock;
   int             ret;
+  void (*oldsig_handler)(int);
 
   gotalarm = 0;
-  CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
+  oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
 
   lock.l_type = type;
   lock.l_whence = SEEK_SET;
@@ -52,9 +57,10 @@ BOOL do_file_lock(int fd, int waitsecs, int type)
   lock.l_pid = 0;
 
   alarm(waitsecs);
+  /* Note we must *NOT* use sys_fcntl here ! JRA */
   ret = fcntl(fd, SMB_F_SETLKW, &lock);
   alarm(0);
-  CatchSignal(SIGALRM, SIGNAL_CAST SIG_DFL);
+  CatchSignal(SIGALRM, SIGNAL_CAST oldsig_handler);
 
   if (gotalarm) {
     DEBUG(0, ("do_file_lock: failed to %s file.\n",
@@ -217,6 +223,12 @@ int getfileline(void *vp, char *linebuf, int linebuf_size)
                 */
 
                linebuf_len = strlen(linebuf);
+               if (linebuf_len == 0)
+               {
+                       linebuf[0] = '\0';
+                       return 0;
+               }
+
                if (linebuf[linebuf_len - 1] != '\n')
                {
                        c = '\0';
@@ -249,7 +261,7 @@ int getfileline(void *vp, char *linebuf, int linebuf_size)
                        continue;
                }
 
-               p = (unsigned char *) strchr(linebuf, ':');
+               p = (unsigned char *) strchr_m(linebuf, ':');
                if (p == NULL)
                {
                        DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
@@ -266,29 +278,31 @@ 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 *fgets_slash(char *s2,int maxlen,FILE *f)
+char *fgets_slash(char *s2,int maxlen,XFILE *f)
 {
   char *s=s2;
   int len = 0;
   int c;
   BOOL start_of_line = True;
 
-  if (feof(f))
+  if (x_feof(f))
     return(NULL);
 
+  if (maxlen <2) return(NULL);
+
   if (!s2)
     {
       maxlen = MIN(maxlen,8);
-      s = (char *)Realloc(s,maxlen);
+      s = (char *)malloc(maxlen);
     }
 
-  if (!s || maxlen < 2) return(NULL);
+  if (!s) return(NULL);
 
   *s = 0;
 
   while (len < maxlen-1)
     {
-      c = getc(f);
+      c = x_getc(f);
       switch (c)
        {
        case '\r':
@@ -307,7 +321,7 @@ char *fgets_slash(char *s2,int maxlen,FILE *f)
          return(s);
        case EOF:
          if (len <= 0 && !s2) 
-           free(s);
+           SAFE_FREE(s);
          return(len>0?s:NULL);
        case ' ':
          if (start_of_line)
@@ -319,9 +333,15 @@ char *fgets_slash(char *s2,int maxlen,FILE *f)
        }
       if (!s2 && len > maxlen-3)
        {
+         char *t;
+         
          maxlen *= 2;
-         s = (char *)Realloc(s,maxlen);
-         if (!s) return(NULL);
+         t = (char *)Realloc(s,maxlen);
+         if (!t) {
+           DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
+           SAFE_FREE(s);
+           return(NULL);
+         } else s = t;
        }
     }
   return(s);
@@ -334,7 +354,7 @@ load from a pipe into memory
 char *file_pload(char *syscmd, size_t *size)
 {
        int fd, n;
-       char *p;
+       char *p, *tp;
        pstring buf;
        size_t total;
        
@@ -345,16 +365,21 @@ char *file_pload(char *syscmd, size_t *size)
        total = 0;
 
        while ((n = read(fd, buf, sizeof(buf))) > 0) {
-               p = Realloc(p, total + n + 1);
-               if (!p) {
+               tp = Realloc(p, total + n + 1);
+               if (!tp) {
+                       DEBUG(0,("file_pload: failed to expand buffer!\n"));
                        close(fd);
+                       SAFE_FREE(p);
                        return NULL;
-               }
+               } else p = tp;
                memcpy(p+total, buf, n);
                total += n;
        }
-       p[total] = 0;
+       if (p) p[total] = 0;
 
+       /* FIXME: Perhaps ought to check that the command completed
+        * successfully (returned 0); if not the data may be
+        * truncated. */
        sys_pclose(fd);
 
        if (size) *size = total;
@@ -362,37 +387,85 @@ char *file_pload(char *syscmd, size_t *size)
        return p;
 }
 
-
 /****************************************************************************
-load a file into memory
-****************************************************************************/
-char *file_load(char *fname, size_t *size)
+load a file into memory from a fd.
+****************************************************************************/ 
+
+char *fd_load(int fd, size_t *size)
 {
-       int fd;
        SMB_STRUCT_STAT sbuf;
        char *p;
 
-       if (!fname || !*fname) return NULL;
-       
-       fd = open(fname,O_RDONLY);
-       if (fd == -1) return NULL;
-
        if (sys_fstat(fd, &sbuf) != 0) return NULL;
 
-       if (sbuf.st_size == 0) return NULL;
-
        p = (char *)malloc(sbuf.st_size+1);
        if (!p) return NULL;
 
        if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
-               free(p);
+               SAFE_FREE(p);
                return NULL;
        }
        p[sbuf.st_size] = 0;
 
+       if (size) *size = sbuf.st_size;
+
+       return p;
+}
+
+/****************************************************************************
+load a file into memory
+****************************************************************************/
+char *file_load(const char *fname, size_t *size)
+{
+       int fd;
+       char *p;
+
+       if (!fname || !*fname) return NULL;
+       
+       fd = open(fname,O_RDONLY);
+       if (fd == -1) return NULL;
+
+       p = fd_load(fd, size);
+
        close(fd);
 
-       if (size) *size = sbuf.st_size;
+       return p;
+}
+
+
+/*******************************************************************
+mmap (if possible) or read a file
+********************************************************************/
+void *map_file(char *fname, size_t size)
+{
+       size_t s2 = 0;
+       void *p = NULL;
+#ifdef HAVE_MMAP
+       if (lp_use_mmap()) {
+               int fd;
+               fd = open(fname, O_RDONLY, 0);
+               if (fd == -1) {
+                       DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
+                       return NULL;
+               }
+               p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
+               close(fd);
+               if (p == MAP_FAILED) {
+                       DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
+                       return NULL;
+               }
+       }
+#endif
+       if (!p) {
+               p = file_load(fname, &s2);
+               if (!p) return NULL;
+               if (s2 != size) {
+                       DEBUG(1,("incorrect size for %s - got %lu expected %lu\n",
+                                fname, (unsigned long)s2, (unsigned long)size));
+                       if (p) free(p);
+                       return NULL;
+               }
+       }
 
        return p;
 }
@@ -414,7 +487,7 @@ static char **file_lines_parse(char *p, size_t size, int *numlines)
 
        ret = (char **)malloc(sizeof(ret[0])*(i+2));
        if (!ret) {
-               free(p);
+               SAFE_FREE(p);
                return NULL;
        }       
        memset(ret, 0, sizeof(ret[0])*(i+2));
@@ -436,9 +509,9 @@ 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 file_lines_free()
 ****************************************************************************/
-char **file_lines_load(char *fname, int *numlines)
+char **file_lines_load(const char *fname, int *numlines)
 {
        char *p;
        size_t size;
@@ -449,10 +522,26 @@ char **file_lines_load(char *fname, int *numlines)
        return file_lines_parse(p, size, numlines);
 }
 
+/****************************************************************************
+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
+the list.
+****************************************************************************/
+char **fd_lines_load(int fd, int *numlines)
+{
+       char *p;
+       size_t size;
+
+       p = fd_load(fd, &size);
+       if (!p) return NULL;
+
+       return file_lines_parse(p, size, numlines);
+}
+
 
 /****************************************************************************
 load a pipe into memory and return an array of pointers to lines in the data
-must be freed with file_lines_free()
+must be freed with file_lines_free()
 ****************************************************************************/
 char **file_lines_pload(char *syscmd, int *numlines)
 {
@@ -471,8 +560,8 @@ free lines loaded with file_lines_load
 void file_lines_free(char **lines)
 {
        if (!lines) return;
-       free(lines[0]);
-       free(lines);
+       SAFE_FREE(lines[0]);
+       SAFE_FREE(lines);
 }
 
 
@@ -498,3 +587,20 @@ void file_lines_slashcont(char **lines)
                }
        }
 }
+
+/*
+  save a lump of data into a file. Mostly used for debugging 
+*/
+BOOL file_save(const char *fname, void *packet, size_t length)
+{
+       int fd;
+       fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+       if (fd == -1) {
+               return False;
+       }
+       if (write(fd, packet, length) != (size_t)length) {
+               return False;
+       }
+       close(fd);
+       return True;
+}