Fri Sep 25 19:28:45 2009

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

Referenced by process_sdp(), and strtoq().

#define LONG_MIN   (-9223372036854775807L-1L)

Definition at line 211 of file strcompat.c.

Referenced by process_sdp(), and strtoq().


Function Documentation

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

Definition at line 195 of file strcompat.c.

References vasprintf.

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

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

Referenced by ast_readconfig(), and increase_call_count().

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

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

Definition at line 63 of file strcompat.c.

Referenced by env_write(), launch_script(), load_odbc_config(), 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 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(), reqprep(), respprep(), and sip_sipredirect().

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

References s.

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

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

Definition at line 448 of file strcompat.c.

References s.

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

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

Definition at line 142 of file 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 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 strcompat.c.

Referenced by __ast_play_and_record(), __login_exec(), _build_port_config(), _macro_exec(), _parse(), acf_vmcount_exec(), add_realm_authentication(), adsi_load(), adsi_message(), agi_exec_full(), aji_send_exec(), aji_status_exec(), append_history_va(), append_mailbox(), apply_options(), apply_outgoing(), ast_autoanswer_login(), 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(), autoanswer_exec(), 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(), connect_link(), console_dial(), console_dial_deprecated(), cut_internal(), decrypt_frame(), del_exec(), deltree_exec(), dial_trunk(), do_directory(), do_say(), 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(), gosubif_exec(), gtalk_alloc(), gtalk_new(), 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(), orig_app(), orig_exten(), page_exec(), parkandannounce_exec(), parse_dial_string(), 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_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(), sendurl_exec(), set(), set_address_from_contact(), set_config_flags(), set_insecure_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(), ss_thread(), stat_read(), transmit_fake_auth_response(), transmit_state_notify(), tryexec_exec(), verbose_exec(), vmauthenticate(), zapateller_exec(), zt_devicestate(), and zt_request().

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

References LONG_MAX, LONG_MIN, and s.

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

int unsetenv ( const char *  name  ) 

Definition at line 81 of file 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 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 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 Fri Sep 25 19:28:45 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.5