Keep coding this boring stuff to lay out security descriptors ...
[kai/samba.git] / source3 / lib / replace.c
index 1421233c1ef9cbc302661a378e1aeef4adb796db..0c62ec9bfa5795526b232074e03ce4abc36b9c87 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    replacement routines for broken systems
    Copyright (C) Andrew Tridgell 1992-1998
    
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
-
-
- void replace_dummy(void) 
-{}
-
+ void replace_dummy(void);
+ void replace_dummy(void) {}
 
 #ifndef HAVE_FTRUNCATE
  /*******************************************************************
 ftruncate for operating systems that don't have it
 ********************************************************************/
- int ftruncate(int f,long l)
+ int ftruncate(int f,SMB_OFF_T l)
 {
       struct  flock   fl;
 
@@ -42,14 +37,52 @@ ftruncate for operating systems that don't have it
       fl.l_type = F_WRLCK;
       return fcntl(f, F_FREESP, &fl);
 }
+#endif /* HAVE_FTRUNCATE */
+
+
+#ifndef HAVE_STRLCPY
+/* like strncpy but does not 0 fill the buffer and always null 
+   terminates. bufsize is the size of the destination buffer */
+ size_t strlcpy(char *d, const char *s, size_t bufsize)
+{
+       size_t len = strlen(s);
+       size_t ret = len;
+       if (bufsize <= 0) return 0;
+       if (len >= bufsize) len = bufsize-1;
+       memcpy(d, s, len);
+       d[len] = 0;
+       return ret;
+}
 #endif
 
+#ifndef HAVE_STRLCAT
+/* like strncat but does not 0 fill the buffer and always null 
+   terminates. bufsize is the length of the buffer, which should
+   be one more than the maximum resulting string length */
+ size_t strlcat(char *d, const char *s, size_t bufsize)
+{
+       size_t len1 = strlen(d);
+       size_t len2 = strlen(s);
+       size_t ret = len1 + len2;
+
+       if (len1+len2 >= bufsize) {
+               len2 = bufsize - (len1+1);
+       }
+       if (len2 > 0) {
+               memcpy(d+len1, s, len2);
+               d[len1+len2] = 0;
+       }
+       return ret;
+}
+#endif
 
 #ifndef HAVE_MKTIME
 /*******************************************************************
 a mktime() replacement for those who don't have it - contributed by 
 C.A. Lademann <cal@zls.com>
+Corrections by richard.kettlewell@kewill.com
 ********************************************************************/
+
 #define  MINUTE  60
 #define  HOUR    60*MINUTE
 #define  DAY             24*HOUR
@@ -58,16 +91,18 @@ C.A. Lademann <cal@zls.com>
 {
   struct tm       *u;
   time_t  epoch = 0;
+  int n;
   int             mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
   y, m, i;
 
   if(t->tm_year < 70)
     return((time_t)-1);
 
+  n = t->tm_year + 1900 - 1;
   epoch = (t->tm_year - 70) * YEAR + 
-    (t->tm_year / 4 - 70 / 4 - t->tm_year / 100) * DAY;
+    ((n / 4 - n / 100 + n / 400) - (1969 / 4 - 1969 / 100 + 1969 / 400)) * DAY;
 
-  y = t->tm_year;
+  y = t->tm_year + 1900;
   m = 0;
 
   for(i = 0; i < t->tm_mon; i++) {
@@ -116,14 +151,15 @@ C.A. Lademann <cal@zls.com>
     }
   return unlink (zfrom);
 }
-#endif
+#endif /* HAVE_RENAME */
 
 
 #ifndef HAVE_INNETGR
+#if defined(HAVE_SETNETGRENT) && defined(HAVE_GETNETGRENT) && defined(HAVE_ENDNETGRENT)
 /*
  * Search for a match in a netgroup. This replaces it on broken systems.
  */
- int innetgr(char *group,char *host,char *user,char *dom)
+ int innetgr(const char *group,const char *host,const char *user,const char *dom)
 {
        char *hst, *usr, *dm;
   
@@ -139,7 +175,8 @@ C.A. Lademann <cal@zls.com>
        endnetgrent();
        return (0);
 }
-#endif
+#endif /* HAVE_SETNETGRENT HAVE_GETNETGRENT HAVE_ENDNETGRENT */
+#endif /* HAVE_INNETGR */
 
 
 
@@ -157,16 +194,22 @@ C.A. Lademann <cal@zls.com>
        }
        /* yikes! no SETGROUPS or INITGROUPS? how can this work? */
        return(0);
-#else
-       gid_t  grouplst[NGROUPS_MAX];
+#else /* HAVE_SETGROUPS */
+       gid_t *grouplst = NULL;
+       int max_gr = groups_max();
+       int ret;
        int    i,j;
        struct group *g;
        char   *gr;
        
+       if((grouplst = (gid_t *)malloc(sizeof(gid_t) * max_gr)) == NULL) {
+               DEBUG(0,("initgroups: malloc fail !\n"));
+               return -1;
+       }
+
        grouplst[0] = id;
        i = 1;
-       while (i < NGROUPS_MAX && 
-              ((g = (struct group *)getgrent()) != (struct group *)NULL)) {
+       while (i < max_gr && ((g = (struct group *)getgrent()) != (struct group *)NULL)) {
                if (g->gr_gid == id)
                        continue;
                j = 0;
@@ -182,10 +225,12 @@ C.A. Lademann <cal@zls.com>
                }
        }
        endgrent();
-       return(setgroups(i,grouplst));
-#endif
+       ret = sys_setgroups(i,grouplst);
+       SAFE_FREE(grouplst);
+       return ret;
+#endif /* HAVE_SETGROUPS */
 }
-#endif
+#endif /* HAVE_INITGROUPS */
 
 
 #if (defined(SecureWare) && defined(SCO))
@@ -257,7 +302,7 @@ needs.
        }
        return(dest);
 }
-#endif
+#endif /* HAVE_MEMMOVE */
 
 #ifndef HAVE_STRDUP
 /****************************************************************************
@@ -265,7 +310,7 @@ duplicate a string
 ****************************************************************************/
  char *strdup(const char *s)
 {
-       int len;
+       size_t len;
        char *ret;
 
        if (!s) return(NULL);
@@ -276,20 +321,147 @@ duplicate a string
        memcpy(ret,s,len);
        return(ret);
 }
-#endif
+#endif /* HAVE_STRDUP */
 
 #ifdef REPLACE_INET_NTOA
 char *rep_inet_ntoa(struct in_addr ip)
 {
        unsigned char *p = (unsigned char *)&ip.s_addr;
        static char buf[18];
-#if WORDS_BIGENDIAN
        slprintf(buf, 17, "%d.%d.%d.%d", 
                 (int)p[0], (int)p[1], (int)p[2], (int)p[3]);
-#else
-       slprintf(buf, 17, "%d.%d.%d.%d", 
-                (int)p[3], (int)p[2], (int)p[1], (int)p[0]);
-#endif
        return buf;
 }
+#endif /* REPLACE_INET_NTOA */
+
+#ifndef HAVE_STRTOUL
+#ifndef ULONG_MAX
+#define        ULONG_MAX       ((unsigned long)(~0L))          /* 0xFFFFFFFF */
+#endif
+
+/*
+ * Convert a string to an unsigned long integer.
+ * Taken from libg++ - libiberty code.
+ *
+ * Ignores `locale' stuff.  Assumes that the upper and lower case
+ * alphabets and digits are each contiguous.
+ */
+ unsigned long strtoul(const char *nptr, char **endptr, int base)
+{
+       const char *s = nptr;
+       unsigned long acc;
+       int c;
+       unsigned long cutoff;
+       int neg = 0, any, cutlim;
+
+       /*
+        * See strtol for comments as to the logic used.
+        */
+       do {
+               c = *s++;
+       } while (isspace(c));
+       if (c == '-') {
+               neg = 1;
+               c = *s++;
+       } else if (c == '+')
+               c = *s++;
+       if ((base == 0 || base == 16) &&
+           c == '0' && (*s == 'x' || *s == 'X')) {
+               c = s[1];
+               s += 2;
+               base = 16;
+       }
+       if (base == 0)
+               base = c == '0' ? 8 : 10;
+       cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
+       cutlim = (int)((unsigned long)ULONG_MAX % (unsigned long)base);
+       for (acc = 0, any = 0;; c = *s++) {
+               if (isdigit(c))
+                       c -= '0';
+               else if (isalpha(c))
+                       c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+               else
+                       break;
+               if (c >= base)
+                       break;
+               if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
+                       any = -1;
+               else {
+                       any = 1;
+                       acc *= base;
+                       acc += c;
+               }
+       }
+       if (any < 0) {
+               acc = ULONG_MAX;
+               errno = ERANGE;
+       } else if (neg)
+               acc = -acc;
+       if (endptr != 0)
+               *endptr = (char *) (any ? s - 1 : nptr);
+       return (acc);
+}
+#endif /* HAVE_STRTOUL */
+
+#ifndef HAVE_SETLINEBUF
+ int setlinebuf(FILE *stream)
+{
+       return setvbuf(stream, (char *)NULL, _IOLBF, 0);
+}
+#endif /* HAVE_SETLINEBUF */
+
+#ifndef HAVE_VSYSLOG
+#ifdef HAVE_SYSLOG
+ void vsyslog (int facility_priority, char *format, va_list arglist)
+{
+       char *msg = NULL;
+       vasprintf(&msg, format, arglist);
+       if (!msg)
+               return;
+       syslog(facility_priority, "%s", msg);
+       SAFE_FREE(msg);
+}
+#endif /* HAVE_SYSLOG */
+#endif /* HAVE_VSYSLOG */
+
+
+#ifndef HAVE_TIMEGM
+/*
+  yes, I know this looks insane, but its really needed. The function in the 
+  Linux timegm() manpage does not work on solaris.
+*/
+ time_t timegm(struct tm *tm) 
+{
+       struct tm tm2, tm3;
+       time_t t;
+
+       tm2 = *tm;
+
+       t = mktime(&tm2);
+       tm3 = *localtime(&t);
+       tm2 = *tm;
+       tm2.tm_isdst = tm3.tm_isdst;
+       t = mktime(&tm2);
+       t -= TimeDiff(t);
+
+       return t;
+}
+#endif
+
+#ifndef HAVE_SETENV
+ int setenv(const char *name, const char *value, int overwrite) 
+{
+       char *p = NULL;
+       int ret = -1;
+
+       asprintf(&p, "%s=%s", name, value);
+
+       if (overwrite || getenv(name)) {
+               if (p) ret = putenv(p);
+       } else {
+               ret = 0;
+       }
+
+       return ret;     
+}
 #endif