r25624: Remove ipv4_addr hack. Only causes 4 extra includes of system/network.h becau...
[bbaumbach/samba-autobuild/.git] / source4 / lib / util / util.c
index ec25a9cbc74ea7ebcf5649a4a214859c612fa90d..2a2813f9af565f71874f62830eb24e46915a36fe 100644 (file)
@@ -9,7 +9,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
+   the Free 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,
    GNU General Public License for 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.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
-#include "dynconfig.h"
 #include "system/network.h"
-#include "system/iconv.h"
 #include "system/filesys.h"
+#include "system/locale.h"
 
 /**
  * @file
@@ -49,12 +47,12 @@ _PUBLIC_ const char *tmpdir(void)
 /**
  Check if a file exists - call vfs_file_exist for samba files.
 **/
-_PUBLIC_ BOOL file_exist(const char *fname)
+_PUBLIC_ bool file_exist(const char *fname)
 {
        struct stat st;
 
        if (stat(fname, &st) != 0) {
-               return False;
+               return false;
        }
 
        return ((S_ISREG(st.st_mode)) || (S_ISFIFO(st.st_mode)));
@@ -78,13 +76,13 @@ _PUBLIC_ time_t file_modtime(const char *fname)
  Check if a directory exists.
 **/
 
-_PUBLIC_ BOOL directory_exist(const char *dname)
+_PUBLIC_ bool directory_exist(const char *dname)
 {
        struct stat st;
-       BOOL ret;
+       bool ret;
 
        if (stat(dname,&st) != 0) {
-               return False;
+               return false;
        }
 
        ret = S_ISDIR(st.st_mode);
@@ -96,10 +94,10 @@ _PUBLIC_ BOOL directory_exist(const char *dname)
 /**
  * Try to create the specified directory if it didn't exist.
  *
- * @retval True if the directory already existed and has the right permissions 
+ * @retval true if the directory already existed and has the right permissions 
  * or was successfully created.
  */
-_PUBLIC_ BOOL directory_create_or_exist(const char *dname, uid_t uid, 
+_PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid, 
                               mode_t dir_perms)
 {
        mode_t old_umask;
@@ -114,13 +112,13 @@ _PUBLIC_ BOOL directory_create_or_exist(const char *dname, uid_t uid,
                                          "%s: %s\n", dname, 
                                          strerror(errno)));
                                umask(old_umask);
-                               return False;
+                               return false;
                        }
                } else {
                        DEBUG(0, ("lstat failed on directory %s: %s\n",
                                  dname, strerror(errno)));
                        umask(old_umask);
-                       return False;
+                       return false;
                }
        } else {
                /* Check ownership and permission on existing directory */
@@ -128,56 +126,20 @@ _PUBLIC_ BOOL directory_create_or_exist(const char *dname, uid_t uid,
                        DEBUG(0, ("directory %s isn't a directory\n",
                                dname));
                        umask(old_umask);
-                       return False;
+                       return false;
                }
                if ((st.st_uid != uid) || 
                    ((st.st_mode & 0777) != dir_perms)) {
                        DEBUG(0, ("invalid permissions on directory "
                                  "%s\n", dname));
                        umask(old_umask);
-                       return False;
+                       return false;
                }
        }
-       return True;
+       return true;
 }       
 
 
-/*******************************************************************
- Close the low 3 fd's and open dev/null in their place.
-********************************************************************/
-static void close_low_fds(BOOL stderr_too)
-{
-#ifndef VALGRIND
-       int fd;
-       int i;
-
-       close(0);
-       close(1); 
-
-       if (stderr_too)
-               close(2);
-
-       /* try and use up these file descriptors, so silly
-               library routines writing to stdout etc won't cause havoc */
-       for (i=0;i<3;i++) {
-               if (i == 2 && !stderr_too)
-                       continue;
-
-               fd = open("/dev/null",O_RDWR,0);
-               if (fd < 0)
-                       fd = open("/dev/null",O_WRONLY,0);
-               if (fd < 0) {
-                       DEBUG(0,("Can't open /dev/null\n"));
-                       return;
-               }
-               if (fd != i) {
-                       DEBUG(0,("Didn't get file descriptor %d\n",i));
-                       return;
-               }
-       }
-#endif
-}
-
 /**
  Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
  else
@@ -185,7 +147,7 @@ static void close_low_fds(BOOL stderr_too)
   if BSD use FNDELAY
 **/
 
-_PUBLIC_ int set_blocking(int fd, BOOL set)
+_PUBLIC_ int set_blocking(int fd, bool set)
 {
        int val;
 #ifdef O_NONBLOCK
@@ -213,7 +175,7 @@ _PUBLIC_ int set_blocking(int fd, BOOL set)
  Sleep for a specified number of milliseconds.
 **/
 
-_PUBLIC_ void msleep(uint_t t)
+_PUBLIC_ void msleep(unsigned int t)
 {
        struct timeval tval;  
 
@@ -224,99 +186,31 @@ _PUBLIC_ void msleep(uint_t t)
        select(0,NULL,NULL,NULL,&tval);
 }
 
-/**
- Become a daemon, discarding the controlling terminal.
-**/
-
-_PUBLIC_ void become_daemon(BOOL Fork)
-{
-       if (Fork) {
-               if (fork()) {
-                       _exit(0);
-               }
-       }
-
-  /* detach from the terminal */
-#ifdef HAVE_SETSID
-       setsid();
-#elif defined(TIOCNOTTY)
-       {
-               int i = open("/dev/tty", O_RDWR, 0);
-               if (i != -1) {
-                       ioctl(i, (int) TIOCNOTTY, (char *)0);      
-                       close(i);
-               }
-       }
-#endif /* HAVE_SETSID */
-
-       /* Close fd's 0,1,2. Needed if started by rsh */
-       close_low_fds(False);  /* Don't close stderr, let the debug system
-                                 attach it to the logfile */
-}
-
-
-/**
- Free memory, checks for NULL.
- Use directly SAFE_FREE()
- Exists only because we need to pass a function pointer somewhere --SSS
-**/
-
-_PUBLIC_ void safe_free(void *p)
-{
-       SAFE_FREE(p);
-}
-
-
-/**
-  see if a string matches either our primary or one of our secondary 
-  netbios aliases. do a case insensitive match
-*/
-_PUBLIC_ BOOL is_myname(const char *name)
-{
-       const char **aliases;
-       int i;
-
-       if (strcasecmp(name, lp_netbios_name()) == 0) {
-               return True;
-       }
-
-       aliases = lp_netbios_aliases();
-       for (i=0; aliases && aliases[i]; i++) {
-               if (strcasecmp(name, aliases[i]) == 0) {
-                       return True;
-               }
-       }
-
-       return False;
-}
-
-
 /**
  Get my own name, return in malloc'ed storage.
 **/
 
-_PUBLIC_ charget_myname(void)
+_PUBLIC_ char *get_myname(void)
 {
        char *hostname;
-       const int host_name_max = 255;
        char *p;
 
-       hostname = malloc(host_name_max+1);
+       hostname = (char *)malloc(MAXHOSTNAMELEN+1);
        *hostname = 0;
 
        /* get my host name */
-       if (gethostname(hostname, host_name_max+1) == -1) {
+       if (gethostname(hostname, MAXHOSTNAMELEN+1) == -1) {
                DEBUG(0,("gethostname failed\n"));
                return NULL;
        } 
 
        /* Ensure null termination. */
-       hostname[host_name_max] = '\0';
+       hostname[MAXHOSTNAMELEN] = '\0';
 
        /* split off any parts after an initial . */
-       p = strchr_m(hostname,'.');
+       p = strchr(hostname, '.');
 
-       if (p)
+       if (p != NULL)
                *p = 0;
        
        return hostname;
@@ -326,17 +220,19 @@ _PUBLIC_ char* get_myname(void)
  Return true if a string could be a pure IP address.
 **/
 
-_PUBLIC_ BOOL is_ipaddress(const char *str)
+_PUBLIC_ bool is_ipaddress(const char *str)
 {
-       BOOL pure_address = True;
+       bool pure_address = true;
        int i;
-  
+
+       if (str == NULL) return false;
+
        for (i=0; pure_address && str[i]; i++)
                if (!(isdigit((int)str[i]) || str[i] == '.'))
-                       pure_address = False;
+                       pure_address = false;
 
        /* Check that a pure number is not misinterpreted as an IP */
-       pure_address = pure_address && (strchr_m(str, '.') != NULL);
+       pure_address = pure_address && (strchr(str, '.') != NULL);
 
        return pure_address;
 }
@@ -389,11 +285,11 @@ _PUBLIC_ uint32_t interpret_addr(const char *str)
 /**
  A convenient addition to interpret_addr().
 **/
-_PUBLIC_ struct ipv4_addr interpret_addr2(const char *str)
+_PUBLIC_ struct in_addr interpret_addr2(const char *str)
 {
-       struct ipv4_addr ret;
+       struct in_addr ret;
        uint32_t a = interpret_addr(str);
-       ret.addr = a;
+       ret.s_addr = a;
        return ret;
 }
 
@@ -401,22 +297,22 @@ _PUBLIC_ struct ipv4_addr interpret_addr2(const char *str)
  Check if an IP is the 0.0.0.0.
 **/
 
-_PUBLIC_ BOOL is_zero_ip(struct ipv4_addr ip)
+_PUBLIC_ bool is_zero_ip(struct in_addr ip)
 {
-       return ip.addr == 0;
+       return ip.s_addr == 0;
 }
 
 /**
  Are two IPs on the same subnet?
 **/
 
-_PUBLIC_ BOOL same_net(struct ipv4_addr ip1,struct ipv4_addr ip2,struct ipv4_addr mask)
+_PUBLIC_ bool same_net(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
 {
        uint32_t net1,net2,nmask;
 
-       nmask = ntohl(mask.addr);
-       net1  = ntohl(ip1.addr);
-       net2  = ntohl(ip2.addr);
+       nmask = ntohl(mask.s_addr);
+       net1  = ntohl(ip1.s_addr);
+       net2  = ntohl(ip2.s_addr);
             
        return((net1 & nmask) == (net2 & nmask));
 }
@@ -426,7 +322,7 @@ _PUBLIC_ BOOL same_net(struct ipv4_addr ip1,struct ipv4_addr ip2,struct ipv4_add
  Check if a process exists. Does this work on all unixes?
 **/
 
-_PUBLIC_ BOOL process_exists(pid_t pid)
+_PUBLIC_ bool process_exists(pid_t pid)
 {
        /* Doing kill with a non-positive pid causes messages to be
         * sent to places we don't want. */
@@ -439,7 +335,7 @@ _PUBLIC_ BOOL process_exists(pid_t pid)
  is dealt with in posix.c
 **/
 
-_PUBLIC_ BOOL fcntl_lock(int fd, int op, off_t offset, off_t count, int type)
+_PUBLIC_ bool fcntl_lock(int fd, int op, off_t offset, off_t count, int type)
 {
        struct flock lock;
        int ret;
@@ -464,24 +360,24 @@ _PUBLIC_ BOOL fcntl_lock(int fd, int op, off_t offset, off_t count, int type)
                                (lock.l_pid != 0) && 
                                (lock.l_pid != getpid())) {
                        DEBUG(3,("fcntl_lock: fd %d is locked by pid %d\n",fd,(int)lock.l_pid));
-                       return(True);
+                       return true;
                }
 
                /* it must be not locked or locked by me */
-               return(False);
+               return false;
        }
 
        /* a lock set or unset */
        if (ret == -1) {
                DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
                        (double)offset,(double)count,op,type,strerror(errno)));
-               return(False);
+               return false;
        }
 
        /* everything went OK */
        DEBUG(8,("fcntl_lock: Lock call successful\n"));
 
-       return(True);
+       return true;
 }
 
 
@@ -584,139 +480,6 @@ _PUBLIC_ void *memdup(const void *p, size_t size)
        return p2;
 }
 
-/**
- A useful function for returning a path in the Samba lock directory.
-**/
-_PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
-{
-       char *fname, *dname;
-       if (name == NULL) {
-               return NULL;
-       }
-       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
-               return talloc_strdup(mem_ctx, name);
-       }
-
-       dname = talloc_strdup(mem_ctx, lp_lockdir());
-       trim_string(dname,"","/");
-       
-       if (!directory_exist(dname)) {
-               mkdir(dname,0755);
-       }
-       
-       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
-
-       talloc_free(dname);
-
-       return fname;
-}
-
-
-/**
- A useful function for returning a path in the Samba piddir directory.
-**/
-static char *pid_path(TALLOC_CTX* mem_ctx, const char *name)
-{
-       char *fname, *dname;
-
-       dname = talloc_strdup(mem_ctx, lp_piddir());
-       trim_string(dname,"","/");
-       
-       if (!directory_exist(dname)) {
-               mkdir(dname,0755);
-       }
-       
-       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
-
-       talloc_free(dname);
-
-       return fname;
-}
-
-
-/**
- * @brief Returns an absolute path to a file in the Samba lib directory.
- *
- * @param name File to find, relative to LIBDIR.
- *
- * @retval Pointer to a talloc'ed string containing the full path.
- **/
-
-_PUBLIC_ char *lib_path(TALLOC_CTX* mem_ctx, const char *name)
-{
-       char *fname;
-       fname = talloc_asprintf(mem_ctx, "%s/%s", dyn_LIBDIR, name);
-       return fname;
-}
-
-/**
- * @brief Returns an absolute path to a file in the Samba private directory.
- *
- * @param name File to find, relative to PRIVATEDIR.
- * if name is not relative, then use it as-is
- *
- * @retval Pointer to a talloc'ed string containing the full path.
- **/
-_PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, const char *name)
-{
-       char *fname;
-       if (name == NULL) {
-               return NULL;
-       }
-       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
-               return talloc_strdup(mem_ctx, name);
-       }
-       fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(), name);
-       return fname;
-}
-
-/**
-  return a path in the smbd.tmp directory, where all temporary file
-  for smbd go. If NULL is passed for name then return the directory 
-  path itself
-*/
-_PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
-{
-       char *fname, *dname;
-
-       dname = pid_path(mem_ctx, "smbd.tmp");
-       if (!directory_exist(dname)) {
-               mkdir(dname,0755);
-       }
-
-       if (name == NULL) {
-               return dname;
-       }
-
-       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
-       talloc_free(dname);
-
-       return fname;
-}
-
-static char *modules_path(TALLOC_CTX* mem_ctx, const char *name)
-{
-       return talloc_asprintf(mem_ctx, "%s/%s", dyn_MODULESDIR, name);
-}
-
-/**
- * Load the initialization functions from DSO files for a specific subsystem.
- *
- * Will return an array of function pointers to initialization functions
- */
-
-_PUBLIC_ init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
-{
-       char *path = modules_path(mem_ctx, subsystem);
-       init_module_fn *ret;
-
-       ret = load_modules(mem_ctx, path);
-
-       talloc_free(path);
-
-       return ret;
-}
-
 /**
  * Write a password to the log file.
  *
@@ -739,14 +502,14 @@ _PUBLIC_ void dump_data_pw(const char *msg, const uint8_t * data, size_t len)
  * see if a range of memory is all zero. A NULL pointer is considered
  * to be all zero 
  */
-_PUBLIC_ BOOL all_zero(const uint8_t *ptr, uint_t size)
+_PUBLIC_ bool all_zero(const uint8_t *ptr, size_t size)
 {
        int i;
-       if (!ptr) return True;
+       if (!ptr) return true;
        for (i=0;i<size;i++) {
-               if (ptr[i]) return False;
+               if (ptr[i]) return false;
        }
-       return True;
+       return true;
 }
 
 /**