Mon May 14 04:53:03 2007

Asterisk developer's documentation


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 dependency graph for strcompat.c:

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.
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 strcompat.c.


Define Documentation

#define LONG_MAX   9223372036854775807L

Definition at line 214 of file strcompat.c.

Referenced by process_sdp(), and strtoq().

#define LONG_MIN   (-9223372036854775807L-1L)

Definition at line 210 of file strcompat.c.

Referenced by process_sdp(), and strtoq().


Function Documentation

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

Definition at line 194 of file strcompat.c.

References vasprintf.

00195 {
00196         va_list ap;
00197         int ret;
00198 
00199         *str = NULL;
00200         va_start(ap, fmt);
00201         ret = vasprintf(str, fmt, ap);
00202         va_end(ap);
00203 
00204         return ret;
00205 }

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 333 of file strcompat.c.

Referenced by increase_call_count().

00334 {
00335    int i;
00336 
00337    for (i = 0; i < nelem; i++) {
00338       list[i] = 0.1;
00339    }
00340    return -1;
00341 }

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

Definition at line 62 of file strcompat.c.

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

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

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

Definition at line 101 of file 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(), reqprep(), and respprep().

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

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

Definition at line 383 of file strcompat.c.

References s.

00384 {
00385    register char *d = dst;
00386    register const char *s = src;
00387    register size_t n = siz;
00388    size_t dlen;
00389 
00390    /* Find the end of dst and adjust bytes left but don't go past end */
00391    while (n-- != 0 && *d != '\0')
00392       d++;
00393    dlen = d - dst;
00394    n = siz - dlen;
00395 
00396    if (n == 0)
00397       return dlen + strlen(s);
00398 
00399    while (*s != '\0') {
00400       if (n != 1) {
00401          *d++ = *s;
00402          n--;
00403       }
00404       s++;
00405    }
00406    *d = '\0';
00407 
00408    return dlen + (s - src);   /* count does not include NUL */
00409 }

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

Definition at line 447 of file strcompat.c.

References s.

00448 {
00449    register char *d = dst;
00450    register const char *s = src;
00451    register size_t n = siz;
00452 
00453    /* Copy as many bytes as will fit */
00454    if (n != 0 && --n != 0) {
00455       do {
00456          if ((*d++ = *s++) == 0)
00457             break;
00458       } while (--n != 0);
00459    }
00460 
00461    /* Not enough room in dst, add NUL and traverse rest of src */
00462    if (n == 0) {
00463       if (siz != 0)
00464          *d = '\0';     /* NUL-terminate dst */
00465       while (*s++)
00466          ;
00467    }
00468 
00469    return s - src - 1;  /* count does not include NUL */
00470 }

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

Definition at line 141 of file strcompat.c.

References len, malloc, and strnlen().

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

size_t strnlen ( const char *  s,
size_t  n 
)

Definition at line 128 of file strcompat.c.

References len.

Referenced by strndup().

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

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

Definition at line 35 of file strcompat.c.

Referenced by __ast_play_and_record(), __login_exec(), _build_port_config(), _macro_exec(), _parse(), acf_if(), acf_vmcount_exec(), add_realm_authentication(), adsi_load(), adsi_message(), agi_exec_full(), aji_send_exec(), aji_status_exec(), aMYSQL_clear(), aMYSQL_connect(), aMYSQL_disconnect(), aMYSQL_fetch(), aMYSQL_query(), append_history_va(), append_mailbox(), apply_options(), apply_outgoing(), ast_app_getdata(), ast_build_timing(), ast_device_state(), ast_el_strtoarr(), ast_extension_state2(), ast_feature_interpret(), ast_filehelper(), 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(), astman_get_variables(), asyncgoto_exec(), attempt_reconnect(), authenticate_verify(), background_detect_exec(), build_channels(), build_device(), callerid_read(), check_auth(), 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(), console_dial(), console_dial_deprecated(), cut_internal(), decrypt_frame(), del_exec(), deltree_exec(), dial_trunk(), do_directory(), exec_exec(), extenspy_exec(), extract_uri(), exts_compare(), find_gtalk(), fix_complete_args(), forward_message(), function_fieldqty(), function_ilink(), function_remote(), function_sippeer(), get_destination(), get_rdnis(), getSearchPath(), gettag(), gosub_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_set_opt_exec(), notify_new_message(), nv_background_detect_exec(), nv_detectfax_exec(), orig_app(), orig_exten(), page_exec(), parkandannounce_exec(), parse_dial_string(), parse_moved_contact(), parse_register_contact(), 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_text_line(), process_zap(), queue_set_param(), random_exec(), read_config_maps(), readfile_exec(), realtime_multi_mysql(), realtime_multi_odbc(), realtime_multi_pgsql(), realtime_mysql(), realtime_odbc(), realtime_pgsql(), record_exec(), reg_source_db(), register_peer_exten(), register_verify(), reload_agents(), reload_queue_members(), reply_digest(), reqprep(), rpt_exec(), rpt_tele_thread(), safe_scan_int(), send_tone_telemetry(), sendurl_exec(), set(), set_address_from_contact(), set_config_flags(), sip_do_debug_ip(), sip_sipredirect(), 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(), split_ext(), ss_thread(), stat_read(), testclient_exec(), transmit_state_notify(), tryexec_exec(), verbose_exec(), vmauthenticate(), zapateller_exec(), and zt_request().

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

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 224 of file strcompat.c.

References LONG_MAX, LONG_MIN, and s.

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

int unsetenv ( const char *  name  ) 

Definition at line 80 of file strcompat.c.

References setenv().

Referenced by env_write().

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

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

Definition at line 87 of file strcompat.c.

Referenced by strcasestr().

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

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

Definition at line 155 of file strcompat.c.

References malloc, and s.

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


Generated on Mon May 14 04:53:03 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1