Mon Jan 30 13:29:28 2012

Asterisk developer's documentation


main/strcompat.c File Reference

Compatibility functions for strsep and strtoq missing on Solaris. More...

#include "asterisk.h"
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

Go to the source code of this file.

Defines

#define LONG_MAX   9223372036854775807L
#define LONG_MIN   (-9223372036854775807L-1L)

Functions

int asprintf (char **str, const char *fmt,...)
int getloadavg (double *list, int nelem)
 Return something that won't cancel the call, but still return -1, in case we correct the implementation to check return value.
int setenv (const char *name, const char *value, int overwrite)
char * strcasestr (const char *haystack, const char *needle)
size_t strlcat (char *dst, const char *src, size_t siz)
size_t strlcpy (char *dst, const char *src, size_t siz)
char * strndup (const char *s, size_t n)
size_t strnlen (const char *s, size_t n)
char * strsep (char **str, const char *delims)
uint64_t strtoq (const char *nptr, char **endptr, int base)
 Convert a string to a quad integer.
void timersub (struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
int unsetenv (const char *name)
static char * upper (const char *orig, char *buf, int bufsize)
int vasprintf (char **strp, const char *fmt, va_list ap)

Detailed Description

Compatibility functions for strsep and strtoq missing on Solaris.

Definition in file main/strcompat.c.


Define Documentation

#define LONG_MAX   9223372036854775807L

Definition at line 228 of file main/strcompat.c.

Referenced by process_sdp_a_audio(), and strtoq().

#define LONG_MIN   (-9223372036854775807L-1L)

Definition at line 224 of file main/strcompat.c.

Referenced by process_sdp_a_audio(), and strtoq().


Function Documentation

int asprintf ( char **  str,
const char *  fmt,
  ... 
)

Definition at line 208 of file main/strcompat.c.

References vasprintf.

00209 {
00210         va_list ap;
00211         int ret;
00212 
00213         *str = NULL;
00214         va_start(ap, fmt);
00215         ret = vasprintf(str, fmt, ap);
00216         va_end(ap);
00217 
00218         return ret;
00219 }

int getloadavg ( double *  list,
int  nelem 
)

Return something that won't cancel the call, but still return -1, in case we correct the implementation to check return value.

Definition at line 347 of file main/strcompat.c.

Referenced by ast_readconfig(), and increase_call_count().

00348 {
00349    int i;
00350 
00351    for (i = 0; i < nelem; i++) {
00352       list[i] = 0.1;
00353    }
00354    return -1;
00355 }

int setenv ( const char *  name,
const char *  value,
int  overwrite 
)

Definition at line 63 of file main/strcompat.c.

Referenced by env_write(), launch_script(), load_odbc_config(), read_environment(), reload(), and unsetenv().

00064 {
00065    unsigned char *buf;
00066    int buflen;
00067 
00068    buflen = strlen(name) + strlen(value) + 2;
00069    buf = alloca(buflen);
00070 
00071    if (!overwrite && getenv(name))
00072       return 0;
00073 
00074    snprintf(buf, buflen, "%s=%s", name, value);
00075 
00076    return putenv(buf);
00077 }

char* strcasestr ( const char *  haystack,
const char *  needle 
)

Definition at line 102 of file main/strcompat.c.

References offset, and upper().

Referenced by anti_injection(), do_directory(), find_sdp(), get_refer_info(), gettag(), handle_request_invite(), handle_response_register(), handle_show_applications(), handle_show_applications_deprecated(), modlist_modentry(), parse_register_contact(), playback_exec(), realtime_multi_odbc(), realtime_odbc(), register_verify(), reqprep(), respprep(), sip_sipredirect(), and sip_uri_headers_cmp().

00103 {
00104    char *u1, *u2;
00105    int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
00106 
00107    u1 = alloca(u1len);
00108    u2 = alloca(u2len);
00109    if (u1 && u2) {
00110       char *offset;
00111       if (u2len > u1len) {
00112          /* Needle bigger than haystack */
00113          return NULL;
00114       }
00115       offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
00116       if (offset) {
00117          /* Return the offset into the original string */
00118          return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
00119       } else {
00120          return NULL;
00121       }
00122    } else {
00123       return NULL;
00124    }
00125 }

size_t strlcat ( char *  dst,
const char *  src,
size_t  siz 
)

Definition at line 397 of file main/strcompat.c.

00398 {
00399    register char *d = dst;
00400    register const char *s = src;
00401    register size_t n = siz;
00402    size_t dlen;
00403 
00404    /* Find the end of dst and adjust bytes left but don't go past end */
00405    while (n-- != 0 && *d != '\0')
00406       d++;
00407    dlen = d - dst;
00408    n = siz - dlen;
00409 
00410    if (n == 0)
00411       return dlen + strlen(s);
00412 
00413    while (*s != '\0') {
00414       if (n != 1) {
00415          *d++ = *s;
00416          n--;
00417       }
00418       s++;
00419    }
00420    *d = '\0';
00421 
00422    return dlen + (s - src);   /* count does not include NUL */
00423 }

size_t strlcpy ( char *  dst,
const char *  src,
size_t  siz 
)

Definition at line 461 of file main/strcompat.c.

00462 {
00463    register char *d = dst;
00464    register const char *s = src;
00465    register size_t n = siz;
00466 
00467    /* Copy as many bytes as will fit */
00468    if (n != 0 && --n != 0) {
00469       do {
00470          if ((*d++ = *s++) == 0)
00471             break;
00472       } while (--n != 0);
00473    }
00474 
00475    /* Not enough room in dst, add NUL and traverse rest of src */
00476    if (n == 0) {
00477       if (siz != 0)
00478          *d = '\0';     /* NUL-terminate dst */
00479       while (*s++)
00480          ;
00481    }
00482 
00483    return s - src - 1;  /* count does not include NUL */
00484 }

char* strndup ( const char *  s,
size_t  n 
)

Definition at line 142 of file main/strcompat.c.

References len(), malloc, and strnlen().

00143 {
00144    size_t len = strnlen(s, n);
00145    char *new = malloc(len + 1);
00146 
00147    if (!new)
00148       return NULL;
00149 
00150    new[len] = '\0';
00151    return memcpy(new, s, len);
00152 }

size_t strnlen ( const char *  s,
size_t  n 
)

Definition at line 129 of file main/strcompat.c.

References len().

Referenced by strndup().

00130 {
00131    size_t len;
00132 
00133    for (len = 0; len < n; len++)
00134       if (s[len] == '\0')
00135          break;
00136 
00137    return len;
00138 }

char* strsep ( char **  str,
const char *  delims 
)

Definition at line 36 of file main/strcompat.c.

Referenced by __ast_play_and_record(), __login_exec(), __set_address_from_contact(), _build_port_config(), _macro_exec(), _parse(), acf_vmcount_exec(), adsi_load(), adsi_message(), agi_exec_full(), aji_send_exec(), aji_status_exec(), append_history_va(), append_mailbox(), append_mailbox_mapping(), apply_options(), apply_outgoing(), ast_aji_get_client(), ast_build_timing(), ast_device_state(), ast_el_strtoarr(), ast_extension_state2(), ast_filehelper(), ast_format_str_reduce(), ast_get_group(), ast_hint_state_changed(), ast_netsock_bind(), ast_parse_allow_disallow(), ast_parseable_goto(), ast_playtones_start(), ast_read_image(), ast_remotecontrol(), ast_utils_which(), astman_get_variables(), asyncgoto_exec(), attempt_reconnect(), authenticate_verify(), background_detect_exec(), build_channels(), build_device(), callerid_read(), check_auth(), check_blacklist(), check_user_full(), cleanup_stale_contexts(), collect_function_digits(), complete_context_add_ignorepat(), complete_context_add_ignorepat_deprecated(), complete_context_add_include(), complete_context_add_include_deprecated(), complete_context_dont_include_deprecated(), complete_context_remove_ignorepat(), complete_context_remove_ignorepat_deprecated(), complete_context_remove_include(), complete_meetmecmd(), conf_exec(), connect_link(), console_dial(), console_dial_deprecated(), cut_internal(), dahdi_request(), decrypt_frame(), del_exec(), deltree_exec(), dial_trunk(), do_directory(), do_say(), exec_exec(), extenspy_exec(), extract_uri(), exts_compare(), feature_interpret_helper(), fileexists_core(), find_gtalk(), forward_message(), function_fieldqty(), function_ilink(), function_remote(), function_sippeer(), get_destination(), get_rdnis(), gettag(), gosubif_exec(), gtalk_alloc(), gtalk_request(), handle_agidumphtml(), handle_common_options(), handle_context_add_extension(), handle_context_add_extension_deprecated(), handle_request_invite(), handle_show_dialplan(), handle_uri(), has_voicemail(), hasvoicemail_exec(), iax2_register(), iftime(), inboxcount(), ind_load_module(), ivr_dispatch(), leave_voicemail(), load_config(), log_exec(), make_components(), metermaidstate(), misdn_call(), misdn_request(), misdn_set_opt_exec(), notify_new_message(), orig_app(), orig_exten(), page_exec(), parkandannounce_exec(), parse_cookies(), parse_dial_string(), parse_register_contact(), parse_via(), pbx_builtin_background(), pbx_builtin_execiftime(), pbx_builtin_gotoif(), pbx_builtin_gotoiftime(), pbx_builtin_importvar(), pbx_builtin_saynumber(), pbx_builtin_setglobalvar(), pbx_load_config(), pbx_load_users(), peer_set_srcaddr(), pickup_exec(), playback_exec(), process_dahdi(), process_text_line(), queue_set_param(), random_exec(), read_config_maps(), readfile_exec(), realtime_multi_odbc(), realtime_multi_pgsql(), realtime_odbc(), realtime_pgsql(), record_exec(), reg_source_db(), register_peer_exten(), register_verify(), reload_agents(), reload_config(), reload_queue_members(), reply_digest(), reqprep(), rpt_exec(), rpt_tele_thread(), send_tone_telemetry(), sendmail(), sendurl_exec(), set(), set_config_flags(), set_insecure_flags(), sip_do_debug_ip(), sip_sipredirect(), sip_uri_cmp(), sip_uri_headers_cmp(), sip_uri_params_cmp(), sla_add_trunk_to_station(), sla_check_device(), sla_queue_event_conf(), sla_ring_station(), sla_state(), sla_station_exec(), softhangup_exec(), sort_internal(), spawn_mp3(), spawn_ras(), speech_background(), ss_thread(), stat_read(), transmit_fake_auth_response(), transmit_state_notify(), tryexec_exec(), verbose_exec(), vmauthenticate(), and zapateller_exec().

00037 {
00038     char *token;
00039 
00040     if (!*str) {
00041         /* No more tokens */
00042         return NULL;
00043     }
00044 
00045     token = *str;
00046     while (**str != '\0') {
00047         if (strchr(delims, **str)) {
00048             **str = '\0';
00049             (*str)++;
00050             return token;
00051         }
00052         (*str)++;
00053     }
00054 
00055     /* There is no other token */
00056     *str = NULL;
00057 
00058     return token;
00059 }

uint64_t strtoq ( const char *  nptr,
char **  endptr,
int  base 
)

Convert a string to a quad integer.

Note:
Ignores `locale' stuff. Assumes that the upper and lower case alphabets and digits are each contiguous.

Definition at line 238 of file main/strcompat.c.

References LONG_MAX, LONG_MIN, and s.

00239 {
00240     const char *s;
00241     uint64_t acc;
00242     unsigned char c;
00243     uint64_t qbase, cutoff;
00244     int neg, any, cutlim;
00245 
00246     /*
00247      * Skip white space and pick up leading +/- sign if any.
00248      * If base is 0, allow 0x for hex and 0 for octal, else
00249      * assume decimal; if base is already 16, allow 0x.
00250      */
00251     s = nptr;
00252     do {
00253             c = *s++;
00254     } while (isspace(c));
00255     if (c == '-') {
00256             neg = 1;
00257             c = *s++;
00258     } else {
00259             neg = 0;
00260             if (c == '+')
00261                     c = *s++;
00262     }
00263     if ((base == 0 || base == 16) &&
00264         c == '\0' && (*s == 'x' || *s == 'X')) {
00265             c = s[1];
00266             s += 2;
00267             base = 16;
00268     }
00269     if (base == 0)
00270             base = c == '\0' ? 8 : 10;
00271 
00272     /*
00273      * Compute the cutoff value between legal numbers and illegal
00274      * numbers.  That is the largest legal value, divided by the
00275      * base.  An input number that is greater than this value, if
00276      * followed by a legal input character, is too big.  One that
00277      * is equal to this value may be valid or not; the limit
00278      * between valid and invalid numbers is then based on the last
00279      * digit.  For instance, if the range for quads is
00280      * [-9223372036854775808..9223372036854775807] and the input base
00281      * is 10, cutoff will be set to 922337203685477580 and cutlim to
00282      * either 7 (neg==0) or 8 (neg==1), meaning that if we have
00283      * accumulated a value > 922337203685477580, or equal but the
00284      * next digit is > 7 (or 8), the number is too big, and we will
00285      * return a range error.
00286      *
00287      * Set any if any `digits' consumed; make it negative to indicate
00288      * overflow.
00289      */
00290     qbase = (unsigned)base;
00291     cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
00292     cutlim = cutoff % qbase;
00293     cutoff /= qbase;
00294     for (acc = 0, any = 0;; c = *s++) {
00295             if (!isascii(c))
00296                     break;
00297             if (isdigit(c))
00298                     c -= '\0';
00299             else if (isalpha(c))
00300                     c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00301             else
00302                     break;
00303             if (c >= base)
00304                     break;
00305             if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00306                     any = -1;
00307             else {
00308                     any = 1;
00309                     acc *= qbase;
00310                     acc += c;
00311             }
00312     }
00313     if (any < 0) {
00314             acc = neg ? LONG_MIN : LONG_MAX;
00315     } else if (neg)
00316             acc = -acc;
00317     if (endptr != 0)
00318             *((const char **)endptr) = any ? s - 1 : nptr;
00319     return acc;
00320 }

void timersub ( struct timeval *  tvend,
struct timeval *  tvstart,
struct timeval *  tvdiff 
)

Definition at line 176 of file main/strcompat.c.

Referenced by ast_rtcp_write_rr(), ast_rtcp_write_sr(), and ast_select().

00177 {
00178    tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
00179    tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
00180    if (tvdiff->tv_usec < 0) {
00181       tvdiff->tv_sec --;
00182       tvdiff->tv_usec += 1000000;
00183    }
00184 
00185 }

int unsetenv ( const char *  name  ) 

Definition at line 81 of file main/strcompat.c.

References setenv().

Referenced by env_write().

00082 {
00083    return setenv(name, "", 0);
00084 }

static char* upper ( const char *  orig,
char *  buf,
int  bufsize 
) [static]

Definition at line 88 of file main/strcompat.c.

Referenced by strcasestr().

00089 {
00090    int i = 0;
00091 
00092    while (i < (bufsize - 1) && orig[i]) {
00093       buf[i] = toupper(orig[i]);
00094       i++;
00095    }
00096 
00097    buf[i] = '\0';
00098 
00099    return buf;
00100 }

int vasprintf ( char **  strp,
const char *  fmt,
va_list  ap 
)

Definition at line 156 of file main/strcompat.c.

References malloc, and s.

00157 {
00158    int size;
00159    va_list ap2;
00160    char s;
00161 
00162    *strp = NULL;
00163    va_copy(ap2, ap);
00164    size = vsnprintf(&s, 1, fmt, ap2);
00165    va_end(ap2);
00166    *strp = malloc(size + 1);
00167    if (!*strp)
00168       return -1;
00169    vsnprintf(*strp, size + 1, fmt, ap);
00170 
00171    return size;
00172 }


Generated on 30 Jan 2012 for Asterisk - the Open Source PBX by  doxygen 1.6.1