Merge branch 'master' of ssh://git.exim.org/home/git/exim
[exim.git] / src / src / string.c
index 3fea7c048393e4006d2cf1b8adc2414438f46dc6..914390255f43f59e55a919f00a3fb3d0d55d8689 100644 (file)
@@ -2,7 +2,7 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) University of Cambridge 1995 - 2014 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 /* Miscellaneous string-handling functions. Some are not required for
@@ -34,7 +34,7 @@ Returns:    0 if the string is not a textual representation of an IP address
 */
 
 int
-string_is_ip_address(uschar *s, int *maskptr)
+string_is_ip_address(const uschar *s, int *maskptr)
 {
 int i;
 int yield = 4;
@@ -44,7 +44,7 @@ offset. */
 
 if (maskptr != NULL)
   {
-  uschar *ss = s + Ustrlen(s);
+  const uschar *ss = s + Ustrlen(s);
   *maskptr = 0;
   if (s != ss && isdigit(*(--ss)))
     {
@@ -374,7 +374,8 @@ while (*p)
   {
   if (*p == '\\')
     {
-    *q = string_interpret_escape(&p);
+    *q++ = string_interpret_escape(&p);
+    p++;
     }
   else
     {
@@ -716,7 +717,8 @@ uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
 va_start(ap, format);
 if (!string_vformat(buffer, sizeof(buffer), format, ap))
   log_write(0, LOG_MAIN|LOG_PANIC_DIE,
-    "string_sprintf expansion was longer than %d", sizeof(buffer));
+    "string_sprintf expansion was longer than " SIZE_T_FMT " (%s)",
+    sizeof(buffer), format);
 va_end(ap);
 return string_copy(buffer);
 }
@@ -965,6 +967,50 @@ return buffer;
 #endif  /* COMPILE_UTILITY */
 
 
+#ifndef COMPILE_UTILITY
+/************************************************
+*      Add element to seperated list           *
+************************************************/
+/* This function is used to build a list, returning
+an allocated null-terminated growable string. The
+given element has any embedded seperator characters
+doubled.
+
+Arguments:
+  list points to the start of the list that is being built, or NULL
+       if this is a new list that has no contents yet
+  sep  list seperator charactoer
+  ele  new lement to be appended to the list
+
+Returns:  pointer to the start of the list, changed if copied for expansion.
+*/
+
+uschar *
+string_append_listele(uschar * list, uschar sep, const uschar * ele)
+{
+uschar * new = NULL;
+int sz = 0, off = 0;
+uschar * sp;
+
+if (list)
+  {
+  new = string_cat(new, &sz, &off, list, Ustrlen(list));
+  new = string_cat(new, &sz, &off, &sep, 1);
+  }
+
+while((sp = Ustrchr(ele, sep)))
+  {
+  new = string_cat(new, &sz, &off, ele, sp-ele+1);
+  new = string_cat(new, &sz, &off, &sep, 1);
+  ele = sp+1;
+  }
+new = string_cat(new, &sz, &off, ele, Ustrlen(ele));
+new[off] = '\0';
+return new;
+}
+#endif  /* COMPILE_UTILITY */
+
+
 
 #ifndef COMPILE_UTILITY
 /*************************************************
@@ -1134,7 +1180,8 @@ return yield;
 BOOL
 string_vformat(uschar *buffer, int buflen, const char *format, va_list ap)
 {
-enum { L_NORMAL, L_SHORT, L_LONG, L_LONGLONG, L_LONGDOUBLE };
+/* We assume numbered ascending order, C does not guarantee that */
+enum { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 };
 
 BOOL yield = TRUE;
 int width, precision;
@@ -1204,7 +1251,7 @@ while (*fp != 0)
       }
     }
 
-  /* Skip over 'h', 'L', 'l', and 'll', remembering the item length */
+  /* Skip over 'h', 'L', 'l', 'll' and 'z', remembering the item length */
 
   if (*fp == 'h')
     { fp++; length = L_SHORT; }
@@ -1223,6 +1270,8 @@ while (*fp != 0)
       length = L_LONG;
       }
     }
+  else if (*fp == 'z')
+    { fp++; length = L_SIZE; }
 
   /* Handle each specific format type. */
 
@@ -1252,6 +1301,7 @@ while (*fp != 0)
       case L_NORMAL:   sprintf(CS p, newformat, va_arg(ap, int)); break;
       case L_LONG:     sprintf(CS p, newformat, va_arg(ap, long int)); break;
       case L_LONGLONG: sprintf(CS p, newformat, va_arg(ap, LONGLONG_T)); break;
+      case L_SIZE:     sprintf(CS p, newformat, va_arg(ap, size_t)); break;
       }
     while (*p) p++;
     break;