librb: add versions of rb_strcasecmp, rb_strncasecmp, and rb_strcasestr.

The first two are POSIX but not standard (Windows has functions that act
like it, so use them), but the latter is non-standard, full stop.
This commit is contained in:
Elizabeth Myers 2016-04-05 05:33:43 -05:00
parent 731d128990
commit 508e25a85b
4 changed files with 108 additions and 9 deletions

View file

@ -211,7 +211,7 @@ AC_CHECK_TYPE([sa_family_t], [],
dnl check for various functions...
AC_CHECK_FUNCS([snprintf vsnprintf socketpair gettimeofday writev sendmsg gmtime_r strtok_r usleep posix_spawn getexecname strlcpy strlcat strnlen fstat signalfd select poll kevent port_create epoll_ctl arc4random getrusage timerfd_create])
AC_CHECK_FUNCS([snprintf vsnprintf socketpair gettimeofday writev sendmsg gmtime_r strtok_r usleep posix_spawn getexecname strlcpy strlcat strnlen strcasestr strcasecmp strncasecmp fstat signalfd select poll kevent port_create epoll_ctl arc4random getrusage timerfd_create])
AC_SEARCH_LIBS(dlinfo, dl, AC_DEFINE(HAVE_DLINFO, 1, [Define if you have dlinfo]))
AC_SEARCH_LIBS(nanosleep, rt posix4, AC_DEFINE(HAVE_NANOSLEEP, 1, [Define if you have nanosleep]))

View file

@ -30,6 +30,9 @@
#ifndef __TOOLS_H__
#define __TOOLS_H__
int rb_strcasecmp(const char *s1, const char *s2);
int rb_strncasecmp(const char *s1, const char *s2, size_t n);
char *rb_strcasestr(const char *s, const char *find);
size_t rb_strlcpy(char *dst, const char *src, size_t siz);
size_t rb_strlcat(char *dst, const char *src, size_t siz);
size_t rb_strnlen(const char *s, size_t count);

View file

@ -49,21 +49,21 @@ rb_event_init
rb_event_next
rb_event_run
rb_event_update
rb_fdlist_init
rb_fd_ssl
rb_fdlist_init
rb_free_rawbuffer
rb_free_rb_dlink_node
rb_get_fd
rb_get_fde
rb_get_iotype
rb_getmaxconnect
rb_getpid
rb_get_random
rb_get_sockerr
rb_get_ssl_certfp
rb_get_ssl_strerror
rb_gettimeofday
rb_get_type
rb_getmaxconnect
rb_getpid
rb_gettimeofday
rb_helper_child
rb_helper_close
rb_helper_loop
@ -113,6 +113,7 @@ rb_new_rawbuffer
rb_note
rb_open
rb_outofmemory
rb_path_to_self
rb_patricia_lookup
rb_patricia_process
rb_patricia_remove
@ -147,12 +148,12 @@ rb_run_one_event
rb_select
rb_send_fd_buf
rb_set_buffers
rb_setenv
rb_set_nb
rb_setselect
rb_set_time
rb_settimeout
rb_set_type
rb_setenv
rb_setselect
rb_settimeout
rb_setup_fd
rb_setup_ssl_server
rb_sleep
@ -166,10 +167,13 @@ rb_ssl_handshake_count
rb_ssl_listen
rb_ssl_start_accepted
rb_ssl_start_connected
rb_strcasecmp
rb_strcasestr
rb_strerror
rb_string_to_array
rb_strlcat
rb_strlcpy
rb_strncasecmp
rb_strnlen
rb_strtok_r
rb_supports_ssl
@ -188,4 +192,3 @@ rb_zstring_serialized
rb_zstring_to_c
rb_zstring_to_c_alloc
rb_zstring_to_ptr
rb_path_to_self

View file

@ -138,6 +138,99 @@ rb_string_to_array(char *string, char **parv, int maxpara)
return x;
}
#ifndef HAVE_STRCASECMP
#ifndef _WIN32
/* Crummy fallback impl by me. --Elizafox */
int
rb_strcasecmp(const char *s1, const char *s2)
{
for(; *s1 != '\0' || *s2 != '\0'; s1++, s2++)
{
if(tolower(*s1) != tolower(*s2))
return tolower(*s1) - tolower(*s2);
}
if(*s1 == '\0' && *s2 == '\0')
return 0;
return tolower(*(--s1)) - tolower(*(--s2));
}
#else /* _WIN32 */
int
rb_strcasecmp(const char *s1, const char *s2)
{
return stricmp(s1, s2);
}
#endif /* _WIN32 */
#else /* HAVE_STRCASECMP */
int
rb_strcasecmp(const char *s1, const char *s2)
{
return strcasecmp(s1, s2);
}
#endif
#ifndef HAVE_STRNCASECMP
#ifndef _WIN32
/* Crummy fallback impl by me. --Elizafox */
int
rb_strncasecmp(const char *s1, const char *s2, size_t n)
{
if(n == 0)
return 0;
for(; *s1 != '\0' || *s2 != '\0' || n; s1++, s2++, n--)
{
if(tolower(*s1) != tolower(*s2))
return tolower(*s1) - tolower(*s2);
}
if(*s1 == '\0' && *s2 == '\0')
return 0;
return tolower(*(--s1)) - tolower(*(--s2));
}
#else /* _WIN32 */
int
rb_strncasecmp(const char *s1, const char *s2, size_t n)
{
return strnicmp(s1, s2, n);
}
#endif /* _WIN32 */
#else /* HAVE_STRNCASECMP */
int
rb_strncasecmp(const char *s1, const char *s2, size_t n)
{
return strncasecmp(s1, s2, n);
}
#endif
#ifndef HAVE_STRCASESTR
/* Crummy fallback impl by me. --Elizafox */
char *
rb_strcasestr(const char *s, const char *find)
{
size_t len_f = strlen(find);
if(*s == '\0')
return s;
for(char *c = s; *c != '\0'; c++)
{
if(*c == *find && strncasecmp(c, find, len_f) == 0)
return c;
}
return NULL;
}
#else
char *
rb_strcasestr(const char *s, const char *find)
{
return strcasestr(s, find);
}
#endif
#ifndef HAVE_STRLCAT
size_t
rb_strlcat(char *dest, const char *src, size_t count)