summaryrefslogtreecommitdiff
path: root/mysys
diff options
context:
space:
mode:
authorunknown <svoj@april.(none)>2006-05-03 16:37:42 +0500
committerunknown <svoj@april.(none)>2006-05-03 16:37:42 +0500
commit7f1cc13802ab9e3abea375057564f1a524fd5f17 (patch)
treebd7529f9479191b5ef0ae49f73ed9d9f28bb6e82 /mysys
parent8cec2f3e20e26d3edfd3650d65ffc694e0be66e5 (diff)
parentb06e6127f6a07f6a06f7607aab52816216d30ae6 (diff)
downloadmariadb-git-7f1cc13802ab9e3abea375057564f1a524fd5f17.tar.gz
Merge svojtovich@bk-internal.mysql.com:/home/bk/mysql-5.0
into april.(none):/home/svoj/devel/mysql/BUG17810/mysql-5.0
Diffstat (limited to 'mysys')
-rw-r--r--mysys/Makefile.am1
-rw-r--r--mysys/mf_iocache2.c92
-rw-r--r--mysys/my_memmem.c66
3 files changed, 139 insertions, 20 deletions
diff --git a/mysys/Makefile.am b/mysys/Makefile.am
index 2de8d7820f5..d046b2fa3f8 100644
--- a/mysys/Makefile.am
+++ b/mysys/Makefile.am
@@ -55,6 +55,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c my_mmap.c \
charset.c charset-def.c my_bitmap.c my_bit.c md5.c \
my_gethostbyname.c rijndael.c my_aes.c sha1.c \
my_handler.c my_netware.c my_largepage.c \
+ my_memmem.c \
my_windac.c my_access.c base64.c
EXTRA_DIST = thr_alarm.c thr_lock.c my_pthread.c my_thr_init.c \
thr_mutex.c thr_rwlock.c
diff --git a/mysys/mf_iocache2.c b/mysys/mf_iocache2.c
index e181ccfb88d..f1ea21c2a47 100644
--- a/mysys/mf_iocache2.c
+++ b/mysys/mf_iocache2.c
@@ -252,37 +252,89 @@ uint my_b_printf(IO_CACHE *info, const char* fmt, ...)
uint my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
{
uint out_length=0;
+ uint minimum_width; /* as yet unimplemented */
+ uint minimum_width_sign;
+ uint precision; /* as yet unimplemented for anything but %b */
- for (; *fmt ; fmt++)
+ /*
+ Store the location of the beginning of a format directive, for the
+ case where we learn we shouldn't have been parsing a format string
+ at all, and we don't want to lose the flag/precision/width/size
+ information.
+ */
+ const char* backtrack;
+
+ for (; *fmt != '\0'; fmt++)
{
- if (*fmt++ != '%')
+ /* Copy everything until '%' or end of string */
+ const char *start=fmt;
+ uint length;
+
+ for (; (*fmt != '\0') && (*fmt != '%'); fmt++) ;
+
+ length= (uint) (fmt - start);
+ out_length+=length;
+ if (my_b_write(info, start, length))
+ goto err;
+
+ if (*fmt == '\0') /* End of format */
{
- /* Copy everything until '%' or end of string */
- const char *start=fmt-1;
- uint length;
- for (; *fmt && *fmt != '%' ; fmt++ ) ;
- length= (uint) (fmt - start);
- out_length+=length;
- if (my_b_write(info, start, length))
- goto err;
- if (!*fmt) /* End of format */
- {
- return out_length;
- }
- fmt++;
- /* Found one '%' */
+ return out_length;
}
+
+ /*
+ By this point, *fmt must be a percent; Keep track of this location and
+ skip over the percent character.
+ */
+ DBUG_ASSERT(*fmt == '%');
+ backtrack= fmt;
+ fmt++;
+
+ minimum_width= 0;
+ precision= 0;
+ minimum_width_sign= 1;
/* Skip if max size is used (to be compatible with printf) */
- while (my_isdigit(&my_charset_latin1, *fmt) || *fmt == '.' || *fmt == '-')
+ while (*fmt == '-') { fmt++; minimum_width_sign= -1; }
+ if (*fmt == '*') {
+ precision= (int) va_arg(args, int);
+ fmt++;
+ } else {
+ while (my_isdigit(&my_charset_latin1, *fmt)) {
+ minimum_width=(minimum_width * 10) + (*fmt - '0');
+ fmt++;
+ }
+ }
+ minimum_width*= minimum_width_sign;
+
+ if (*fmt == '.') {
fmt++;
+ if (*fmt == '*') {
+ precision= (int) va_arg(args, int);
+ fmt++;
+ } else {
+ while (my_isdigit(&my_charset_latin1, *fmt)) {
+ precision=(precision * 10) + (*fmt - '0');
+ fmt++;
+ }
+ }
+ }
+
if (*fmt == 's') /* String parameter */
{
reg2 char *par = va_arg(args, char *);
uint length = (uint) strlen(par);
+ /* TODO: implement minimum width and precision */
out_length+=length;
if (my_b_write(info, par, length))
goto err;
}
+ else if (*fmt == 'b') /* Sized buffer parameter, only precision makes sense */
+ {
+ char *par = va_arg(args, char *);
+ out_length+= precision;
+ if (my_b_write(info, par, precision))
+ goto err;
+ }
else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
{
register int iarg;
@@ -317,9 +369,9 @@ uint my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
else
{
/* %% or unknown code */
- if (my_b_write(info, "%", 1))
- goto err;
- out_length++;
+ if (my_b_write(info, backtrack, fmt-backtrack))
+ goto err;
+ out_length+= fmt-backtrack;
}
}
return out_length;
diff --git a/mysys/my_memmem.c b/mysys/my_memmem.c
new file mode 100644
index 00000000000..682a1314f09
--- /dev/null
+++ b/mysys/my_memmem.c
@@ -0,0 +1,66 @@
+#include "my_base.h"
+
+/*
+ my_memmem, port of a GNU extension.
+
+ Returns a pointer to the beginning of the substring, needle, or NULL if the
+ substring is not found in haystack.
+*/
+void *my_memmem(const void *haystack, size_t haystacklen,
+ const void *needle, size_t needlelen)
+{
+ const unsigned char *cursor;
+ const unsigned char *last_possible_needle_location =
+ (unsigned char *)haystack + haystacklen - needlelen;
+
+ /* Easy answers */
+ if (needlelen > haystacklen) return(NULL);
+ if (needle == NULL) return(NULL);
+ if (haystack == NULL) return(NULL);
+ if (needlelen == 0) return(NULL);
+ if (haystacklen == 0) return(NULL);
+
+ for (cursor = haystack; cursor <= last_possible_needle_location; cursor++) {
+ if (memcmp(needle, cursor, needlelen) == 0) {
+ return((void *) cursor);
+ }
+ }
+ return(NULL);
+}
+
+
+
+#ifdef MAIN
+#include <assert.h>
+
+int main(int argc, char *argv[]) {
+ char haystack[10], needle[3];
+
+ memmove(haystack, "0123456789", 10);
+
+ memmove(needle, "no", 2);
+ assert(my_memmem(haystack, 10, needle, 2) == NULL);
+
+ memmove(needle, "345", 3);
+ assert(my_memmem(haystack, 10, needle, 3) != NULL);
+
+ memmove(needle, "789", 3);
+ assert(my_memmem(haystack, 10, needle, 3) != NULL);
+ assert(my_memmem(haystack, 9, needle, 3) == NULL);
+
+ memmove(needle, "012", 3);
+ assert(my_memmem(haystack, 10, needle, 3) != NULL);
+ assert(my_memmem(NULL, 10, needle, 3) == NULL);
+
+ assert(my_memmem(NULL, 10, needle, 3) == NULL);
+ assert(my_memmem(haystack, 0, needle, 3) == NULL);
+ assert(my_memmem(haystack, 10, NULL, 3) == NULL);
+ assert(my_memmem(haystack, 10, needle, 0) == NULL);
+
+ assert(my_memmem(haystack, 1, needle, 3) == NULL);
+
+ printf("success\n");
+ return(0);
+}
+
+#endif