Rolled back Lukes changes. Not quite ready for prime time.
[kai/samba.git] / source3 / lib / util.c
index 8730ae3143748a8079ea5b4b3a2e6f489815b698..9ebfdca88ea87c0c8bbbd27d995d0f2d336cf366 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    Samba utility functions
-   Copyright (C) Andrew Tridgell 1992-1995
+   Copyright (C) Andrew Tridgell 1992-1997
    
    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
@@ -73,6 +73,7 @@ pstring myhostname="";
 pstring user_socket_options="";   
 pstring sesssetup_user="";
 pstring myname = "";
+fstring myworkgroup = "";
 
 int smb_read_error = 0;
 
@@ -796,25 +797,32 @@ char *attrib_string(int mode)
 /*******************************************************************
   case insensitive string compararison
 ********************************************************************/
-int StrCaseCmp(char *s, char *t)
+int StrCaseCmp(const char *s, const char *t)
 {
-  for (; tolower(*s) == tolower(*t); ++s, ++t)
-    if (!*s) return 0;
+  /* compare until we run out of string, either t or s, or find a difference */
+  while (*s && *t && tolower(*s) == tolower(*t))
+  {
+    s++; t++;
+  }
 
-  return tolower(*s) - tolower(*t);
+  return(tolower(*s) - tolower(*t));
 }
 
 /*******************************************************************
   case insensitive string compararison, length limited
 ********************************************************************/
-int StrnCaseCmp(char *s, char *t, int n)
+int StrnCaseCmp(const char *s, const char *t, int n)
 {
-  while (n-- && *s && *t) {
-    if (tolower(*s) != tolower(*t)) return(tolower(*s) - tolower(*t));
+  /* compare until we run out of string, either t or s, or chars */
+  while (n-- && *s && *t && tolower(*s) == tolower(*t))
+  {
     s++; t++;
   }
+
+  /* not run out of chars - strings are different lengths */
   if (n) return(tolower(*s) - tolower(*t));
 
+  /* identical up to where we run out of chars, and strings are same length */
   return(0);
 }
 
@@ -957,9 +965,6 @@ void unix_format(char *fname)
 {
   pstring namecopy;
   string_replace(fname,'\\','/');
-#ifndef KANJI
-  dos2unix_format(fname, True);
-#endif /* KANJI */
 
   if (*fname == '/')
     {
@@ -974,9 +979,6 @@ void unix_format(char *fname)
 ****************************************************************************/
 void dos_format(char *fname)
 {
-#ifndef KANJI
-  unix2dos_format(fname, True);
-#endif /* KANJI */
   string_replace(fname,'/','\\');
 }
 
@@ -1183,6 +1185,13 @@ void unix_clean_name(char *s)
   /* remove any double slashes */
   string_sub(s, "//","/");
 
+  /* Remove leading ./ characters */
+  if(strncmp(s, "./", 2) == 0) {
+    trim_string(s, "./", NULL);
+    if(*s == 0)
+      strcpy(s,"./");
+  }
+
   while ((p = strstr(s,"/../")) != NULL)
     {
       pstring s1;
@@ -1377,6 +1386,10 @@ BOOL reduce_name(char *s,char *dir,BOOL widelinks)
          DEBUG(3,("Illegal file name? (%s)\n",s));
          return(False);
        }
+
+      if (strlen(s) == 0)
+        strcpy(s,"./");
+
       return(True);
     }
   
@@ -2892,12 +2905,15 @@ int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
 connect_again:
   ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
 
-  if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY) && loops--) {
+  /* Some systems return EAGAIN when they mean EINPROGRESS */
+  if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
+        errno == EAGAIN) && loops--) {
     msleep(connect_loop);
     goto connect_again;
   }
 
-  if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY)) {
+  if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
+         errno == EAGAIN)) {
       DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
       close(res);
       return -1;
@@ -3021,7 +3037,9 @@ BOOL zero_ip(struct in_addr ip)
 }
 
 
-/* matchname - determine if host name matches IP address */
+/*******************************************************************
+ matchname - determine if host name matches IP address 
+ ******************************************************************/
 static BOOL matchname(char *remotehost,struct in_addr  addr)
 {
   struct hostent *hp;
@@ -3064,7 +3082,24 @@ static BOOL matchname(char *remotehost,struct in_addr  addr)
   return False;
 }
 
-/* return the DNS name of the client */
+/*******************************************************************
+ Reset the 'done' variables so after a client process is created
+ from a fork call these calls will be re-done. This should be
+ expanded if more variables need reseting.
+ ******************************************************************/
+
+static BOOL global_client_name_done = False;
+static BOOL global_client_addr_done = False;
+
+void reset_globals_after_fork()
+{
+  global_client_name_done = False;
+  global_client_addr_done = False;
+}
+/*******************************************************************
+ return the DNS name of the client 
+ ******************************************************************/
 char *client_name(void)
 {
   extern int Client;
@@ -3072,13 +3107,11 @@ char *client_name(void)
   struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
   int     length = sizeof(sa);
   static pstring name_buf;
-  static BOOL done = False;
   struct hostent *hp;
 
-  if (done) 
+  if (global_client_name_done) 
     return name_buf;
 
-  done = True;
   strcpy(name_buf,"UNKNOWN");
 
   if (getpeername(Client, &sa, &length) < 0) {
@@ -3099,10 +3132,13 @@ char *client_name(void)
       strcpy(name_buf,"UNKNOWN");
     }
   }
+  global_client_name_done = True;
   return name_buf;
 }
 
-/* return the IP addr of the client as a string */
+/*******************************************************************
+ return the IP addr of the client as a string 
+ ******************************************************************/
 char *client_addr(void)
 {
   extern int Client;
@@ -3110,12 +3146,10 @@ char *client_addr(void)
   struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
   int     length = sizeof(sa);
   static fstring addr_buf;
-  static BOOL done = False;
 
-  if (done) 
+  if (global_client_addr_done) 
     return addr_buf;
 
-  done = True;
   strcpy(addr_buf,"0.0.0.0");
 
   if (getpeername(Client, &sa, &length) < 0) {
@@ -3125,6 +3159,7 @@ char *client_addr(void)
 
   strcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
 
+  global_client_addr_done = True;
   return addr_buf;
 }
 
@@ -3224,7 +3259,7 @@ struct hostent *Get_Hostbyname(char *name)
       return(NULL);
     }
 
-  ret = gethostbyname(name2);
+  ret = sys_gethostbyname(name2);
   if (ret != NULL)
     {
       free(name2);
@@ -3233,7 +3268,7 @@ struct hostent *Get_Hostbyname(char *name)
 
   /* try with all lowercase */
   strlower(name2);
-  ret = gethostbyname(name2);
+  ret = sys_gethostbyname(name2);
   if (ret != NULL)
     {
       free(name2);
@@ -3242,7 +3277,7 @@ struct hostent *Get_Hostbyname(char *name)
 
   /* try with all uppercase */
   strupper(name2);
-  ret = gethostbyname(name2);
+  ret = sys_gethostbyname(name2);
   if (ret != NULL)
     {
       free(name2);
@@ -3271,7 +3306,7 @@ BOOL process_exists(int pid)
     fstring s;
     if (!tested) {
       tested = True;
-      sprintf(s,"/proc/%05d",getpid());
+      sprintf(s,"/proc/%05d",(int)getpid());
       ok = file_exist(s,NULL);
     }
     if (ok) {
@@ -3362,14 +3397,12 @@ char *readdirname(void *p)
 
   dname = ptr->d_name;
 
-#ifdef KANJI
   {
     static pstring buf;
     strcpy(buf, dname);
     unix_to_dos(buf, True);
     dname = buf;
   }
-#endif
 
 #ifdef NEXT2
   if (telldir(p) < 0) return(NULL);
@@ -3421,7 +3454,7 @@ BOOL is_vetoed_name(char *name)
           nameptr++;
           continue;
         }
-      if(name_end = strchr(nameptr,'/')
+      if((name_end = strchr(nameptr,'/'))!=NULL
         {
           *name_end = 0;
         }
@@ -3462,7 +3495,7 @@ BOOL is_vetoed_path(char *name)
           nameptr++;
           continue;
         }
-      if(name_end = strchr(nameptr,'/')
+      if((name_end = strchr(nameptr,'/'))!=NULL
         {
           *name_end = 0;
         }