Newer
Older
Kevin P. Fleming
committed
int getloadavg(double *list, int nelem)
{
int i;
for (i = 0; i < nelem; i++) {
list[i] = 0.1;
}
return -1;
}
#endif /* linux */
Russell Bryant
committed
/*! \brief glibc puts a lock inside random(3), so that the results are thread-safe.
* BSD libc (and others) do not. */
#ifndef linux
AST_MUTEX_DEFINE_STATIC(randomlock);
long int ast_random(void)
{
long int res;
ast_mutex_lock(&randomlock);
res = random();
ast_mutex_unlock(&randomlock);
return res;
}
#endif
Russell Bryant
committed
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
{
char *dataPut = start;
int inEscape = 0;
int inQuotes = 0;
for (; *start; start++) {
if (inEscape) {
*dataPut++ = *start; /* Always goes verbatim */
inEscape = 0;
} else {
if (*start == '\\') {
inEscape = 1; /* Do not copy \ into the data */
} else if (*start == '\'') {
inQuotes = 1-inQuotes; /* Do not copy ' into the data */
} else {
/* Replace , with |, unless in quotes */
*dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start);
}
}
}
if (start != dataPut)
*dataPut = 0;
return dataPut;
}
Russell Bryant
committed
void ast_join(char *s, size_t len, char * const w[])
{
int x, ofs = 0;
const char *src;
/* Join words into a string */
if (!s)
return;
for (x=0; ofs < len && w[x]; x++) {
if (x > 0)
s[ofs++] = ' ';
for (src = w[x]; *src && ofs < len; src++)
s[ofs++] = *src;
}
if (ofs == len)
ofs--;
s[ofs] = '\0';
}
const char __ast_string_field_empty[] = "";
static int add_string_pool(struct ast_string_field_mgr *mgr, size_t size)
{
struct ast_string_field_pool *pool;
if (!(pool = ast_calloc(1, sizeof(*pool) + size)))
return -1;
pool->prev = mgr->pool;
mgr->pool = pool;
mgr->size = size;
mgr->space = size;
mgr->used = 0;
return 0;
}
int __ast_string_field_init(struct ast_string_field_mgr *mgr, size_t size,
ast_string_field *fields, int num_fields)
{
int index;
if (add_string_pool(mgr, size))
return -1;
for (index = 0; index < num_fields; index++)
fields[index] = __ast_string_field_empty;
return 0;
ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr, size_t needed,
Kevin P. Fleming
committed
ast_string_field *fields, int num_fields)
{
char *result = NULL;
if (__builtin_expect(needed > mgr->space, 0)) {
size_t new_size = mgr->size * 2;
new_size *= 2;
if (add_string_pool(mgr, new_size))
return NULL;
}
result = mgr->pool->base + mgr->used;
mgr->used += needed;
mgr->space -= needed;
return result;
}
Kevin P. Fleming
committed
void __ast_string_field_index_build(struct ast_string_field_mgr *mgr,
Kevin P. Fleming
committed
ast_string_field *fields, int num_fields,
int index, const char *format, ...)
{
size_t needed;
va_list ap1, ap2;
va_start(ap1, format);
va_start(ap2, format); /* va_copy does not exist on FreeBSD */
Kevin P. Fleming
committed
needed = vsnprintf(mgr->pool->base + mgr->used, mgr->space, format, ap1) + 1;
Kevin P. Fleming
committed
va_end(ap1);
if (needed > mgr->space) {
size_t new_size = mgr->size * 2;
while (new_size < needed)
new_size *= 2;
if (add_string_pool(mgr, new_size))
return;
vsprintf(mgr->pool->base + mgr->used, format, ap2);
}
fields[index] = mgr->pool->base + mgr->used;
mgr->used += needed;
mgr->space -= needed;
Kevin P. Fleming
committed
va_end(ap2);
}
AST_MUTEX_DEFINE_STATIC(fetchadd_m); /* used for all fetc&add ops */
int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
{
int ret;
ast_mutex_lock(&fetchadd_m);
ret = *p;
*p += v;
ast_mutex_unlock(&fetchadd_m);
return ret;
}
* get values from config variables.
*/
Kevin P. Fleming
committed
int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
{
long t;
Kevin P. Fleming
committed
int scanned;
if (dst == NULL)
return -1;
*dst = _default;
if (ast_strlen_zero(src))
return -1;
/* only integer at the moment, but one day we could accept more formats */
Kevin P. Fleming
committed
if (sscanf(src, "%ld%n", &t, &scanned) == 1) {
*dst = t;
Kevin P. Fleming
committed
if (consumed)
*consumed = scanned;
return 0;
} else
return -1;
}