Skip to content
Snippets Groups Projects
utils.c 29.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • int getloadavg(double *list, int nelem)
    {
    	int i;
    
    	for (i = 0; i < nelem; i++) {
    		list[i] = 0.1;
    	}
    	return -1;
    }
    #endif /* linux */
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    #endif /* !defined(_BSD_SOURCE) */
    
    Olle Johansson's avatar
    Olle Johansson 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
    
    
    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;
    }
    
    
    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,
    
    						ast_string_field *fields, int num_fields)
    
    	if (__builtin_expect(needed > mgr->space, 0)) {
    		size_t new_size = mgr->size * 2;
    
    		while (new_size < needed)
    
    		if (add_string_pool(mgr, new_size))
    
    	result = mgr->pool->base + mgr->used;
    	mgr->used += needed;
    	mgr->space -= needed;
    
    void __ast_string_field_index_build(struct ast_string_field_mgr *mgr,
    
    				    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 */
    
    	needed = vsnprintf(mgr->pool->base + mgr->used, mgr->space, format, ap1) + 1;
    
    	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;
    
    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;
    }
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief
    
    int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
    
    
    	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 */
    
    	if (sscanf(src, "%ld%n", &t, &scanned) == 1) {