-const char miscutil_rcs[] = "$Id: miscutil.c,v 1.73 2012/03/04 11:52:45 fabiankeil Exp $";
+const char miscutil_rcs[] = "$Id: miscutil.c,v 1.80 2016/01/16 12:33:36 fabiankeil Exp $";
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $
* to deserve their own file but don't really fit in
* any other file.
*
- * Copyright : Written by and Copyright (C) 2001-2011 the
+ * Copyright : Written by and Copyright (C) 2001-2016 the
* Privoxy team. http://www.privoxy.org/
*
* Based on the Internet Junkbuster originally written
}
+/*********************************************************************
+ *
+ * Function : zalloc_or_die
+ *
+ * Description : zalloc wrapper that either succeeds or causes
+ * program termination.
+ *
+ * Useful in situations were the string length is
+ * "small" and zalloc() failures couldn't be handled
+ * better anyway. In case of debug builds, failures
+ * trigger an assert().
+ *
+ * Parameters :
+ * 1 : size = Size of memory chunk to return.
+ *
+ * Returns : Pointer to newly malloc'd memory chunk.
+ *
+ *********************************************************************/
+void *zalloc_or_die(size_t size)
+{
+ void *buffer;
+
+ buffer = zalloc(size);
+ if (buffer == NULL)
+ {
+ assert(buffer != NULL);
+ log_error(LOG_LEVEL_FATAL, "Out of memory in zalloc_or_die().");
+ exit(1);
+ }
+
+ return(buffer);
+
+}
+
/*********************************************************************
*
* Function : strdup_or_die
}
+/*********************************************************************
+ *
+ * Function : malloc_or_die
+ *
+ * Description : malloc wrapper that either succeeds or causes
+ * program termination.
+ *
+ * Useful in situations were the buffer size is "small"
+ * and malloc() failures couldn't be handled better
+ * anyway. In case of debug builds, failures trigger
+ * an assert().
+ *
+ * Parameters :
+ * 1 : buffer_size = Size of the space to allocate
+ *
+ * Returns : Pointer to newly malloc'd memory
+ *
+ *********************************************************************/
+void *malloc_or_die(size_t buffer_size)
+{
+ char *new_buf;
+
+ if (buffer_size == 0)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "malloc_or_die() called with buffer size 0");
+ assert(buffer_size != 0);
+ buffer_size = 4096;
+ }
+
+ new_buf = malloc(buffer_size);
+
+ if (new_buf == NULL)
+ {
+ assert(new_buf != NULL);
+ log_error(LOG_LEVEL_FATAL, "Out of memory in malloc_or_die().");
+ exit(1);
+ }
+
+ return(new_buf);
+
+}
+
+
#if defined(unix)
/*********************************************************************
*
while (*s1 && *s2)
{
- if (( *s1 != *s2) && ( privoxy_tolower(*s1) != privoxy_tolower(*s2) ) )
+ if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2)))
{
break;
}
while (*s1 && *s2)
{
- if (( *s1 != *s2) && ( privoxy_tolower(*s1) != privoxy_tolower(*s2) ) )
+ if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2)))
{
break;
}
}
+/*********************************************************************
+ *
+ * Function : string_move
+ *
+ * Description : memmove wrapper to move the last part of a string
+ * towards the beginning, overwriting the part in
+ * the middle. strlcpy() can't be used here as the
+ * strings overlap.
+ *
+ * Parameters :
+ * 1 : dst = Destination to overwrite
+ * 2 : src = Source to move.
+ *
+ * Returns : N/A
+ *
+ *********************************************************************/
+void string_move(char *dst, char *src)
+{
+ assert(dst < src);
+
+ /* +1 to copy the terminating nul as well. */
+ memmove(dst, src, strlen(src)+1);
+}
+
+
/*********************************************************************
*
* Function : bindup
#ifdef AMIGA
char path[512];
- if(dir)
+ if (dir)
{
- if(dir[0] == '.')
+ if (dir[0] == '.')
{
- if(dir[1] == '/')
+ if (dir[1] == '/')
{
strncpy(path,dir+2,512);
}
{
path[0]=0;
}
- if(AddPart(path,file,512))
+ if (AddPart(path,file,512))
{
return strdup(path);
}
#else /* ifndef _WIN32 || __OS2__ */
|| (*file == '/') /* Absolute path (U*ix) */
#endif /* ifndef _WIN32 || __OS2__ */
- )
+ )
{
return strdup(file);
}
assert(NULL != path);
#if defined(_WIN32) || defined(__OS2__)
- if(path[strlen(path)-1] != '\\')
+ if (path[strlen(path)-1] != '\\')
{
strlcat(path, "\\", path_size);
}
#else /* ifndef _WIN32 || __OS2__ */
- if(path[strlen(path)-1] != '/')
+ if (path[strlen(path)-1] != '/')
{
strlcat(path, "/", path_size);
}
tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
#endif
} else if (alternate_form) {
- if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X'))
+ if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') )
{ tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; }
/* alternate form should have no effect for p conversion, but ... */
#ifdef HPUX_COMPATIBLE
/* Actually it uses 0x prefix even for a zero value. */
&& arg_sign != 0
#endif
- ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
+ ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
#endif
}
zero_padding_insertion_ind = str_arg_l;
* converting a zero value with a precision of zero is a null string.
* Actually HP returns all zeroes, and Linux returns "(nil)". */
#endif
- ) {
+ ) {
/* converted to null string */
/* When zero value is formatted with an explicit precision 0,
the resulting formatted string is empty (d, i, u, o, x, X, p). */
if (zero_padding_insertion_ind+1 < str_arg_l &&
tmp[zero_padding_insertion_ind] == '0' &&
(tmp[zero_padding_insertion_ind+1] == 'x' ||
- tmp[zero_padding_insertion_ind+1] == 'X')) {
+ tmp[zero_padding_insertion_ind+1] == 'X') ) {
zero_padding_insertion_ind += 2;
}
}
&& !(zero_padding_insertion_ind < str_arg_l
&& tmp[zero_padding_insertion_ind] == '0')
#endif
- ) { /* assure leading zero for alternate-form octal numbers */
+ ) { /* assure leading zero for alternate-form octal numbers */
if (!precision_specified || precision < num_of_digits+1) {
/* precision is increased to force the first character to be zero,
except if a zero value is formatted with an explicit precision