>From 948e7d590a2cf4c222219d7eb8bcb269b4953cdc Mon Sep 17 00:00:00 2001 From: Noel Power Date: Mon, 21 Oct 2013 19:45:50 +0100 Subject: [PATCH] add source file to share 'basic' (trivial dependency) utility functions --- lib/util/common.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/util/common.h | 28 +++++++++++++++++++++++++ lib/util/util.c | 39 ---------------------------------- lib/util/util.h | 5 +---- nsswitch/pam_winbind.c | 13 ++++++++++++ source3/Makefile.in | 5 +++-- 6 files changed, 102 insertions(+), 45 deletions(-) create mode 100644 lib/util/common.c create mode 100644 lib/util/common.h diff --git a/lib/util/common.c b/lib/util/common.c new file mode 100644 index 0000000..5118ea9 --- /dev/null +++ b/lib/util/common.c @@ -0,0 +1,57 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "common.h" +/** + Trim the specified elements off the front and back of a string. +**/ +_PUBLIC_ bool trim_string(char *s, const char *front, const char *back) +{ + bool ret = false; + size_t front_len; + size_t back_len; + size_t len; + + /* Ignore null or empty strings. */ + if (!s || (s[0] == '\0')) + return false; + + front_len = front? strlen(front) : 0; + back_len = back? strlen(back) : 0; + + len = strlen(s); + + if (front_len) { + while (len && strncmp(s, front, front_len)==0) { + /* Must use memmove here as src & dest can + * easily overlap. Found by valgrind. JRA. */ + memmove(s, s+front_len, (len-front_len)+1); + len -= front_len; + ret=true; + } + } + + if (back_len) { + while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) { + s[len-back_len]='\0'; + len -= back_len; + ret=true; + } + } + return ret; +} diff --git a/lib/util/common.h b/lib/util/common.h new file mode 100644 index 0000000..cc14594 --- /dev/null +++ b/lib/util/common.h @@ -0,0 +1,28 @@ +/* + Unix SMB/CIFS implementation. + Utility functions for Samba + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _SAMBA_COMMON_H_ +#define _SAMBA_COMMON_H_ + +#include + +/** + Trim the specified elements off the front and back of a string. +**/ +_PUBLIC_ bool trim_string(char *s, const char *front, const char *back); +#endif /* _SAMBA_COMMON_H_ */ diff --git a/lib/util/util.c b/lib/util/util.c index d4a936f..e78d81b 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -597,45 +597,6 @@ void *malloc_array(size_t el_size, unsigned int count) } /** - Trim the specified elements off the front and back of a string. -**/ -_PUBLIC_ bool trim_string(char *s, const char *front, const char *back) -{ - bool ret = false; - size_t front_len; - size_t back_len; - size_t len; - - /* Ignore null or empty strings. */ - if (!s || (s[0] == '\0')) - return false; - - front_len = front? strlen(front) : 0; - back_len = back? strlen(back) : 0; - - len = strlen(s); - - if (front_len) { - while (len && strncmp(s, front, front_len)==0) { - /* Must use memmove here as src & dest can - * easily overlap. Found by valgrind. JRA. */ - memmove(s, s+front_len, (len-front_len)+1); - len -= front_len; - ret=true; - } - } - - if (back_len) { - while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) { - s[len-back_len]='\0'; - len -= back_len; - ret=true; - } - } - return ret; -} - -/** Find the number of 'c' chars in a string **/ _PUBLIC_ _PURE_ size_t count_chars(const char *s, char c) diff --git a/lib/util/util.h b/lib/util/util.h index 81289b8..88d7d53 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -23,6 +23,7 @@ #include "lib/util/charset/charset.h" #include "lib/util/attr.h" +#include "lib/util/common.h" /* for TALLOC_CTX */ #include @@ -236,10 +237,6 @@ bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx, const char *sep); -/** - Trim the specified elements off the front and back of a string. -**/ -_PUBLIC_ bool trim_string(char *s, const char *front, const char *back); /** Find the number of 'c' chars in a string diff --git a/nsswitch/pam_winbind.c b/nsswitch/pam_winbind.c index 322b569..7e03ac7d 100644 --- a/nsswitch/pam_winbind.c +++ b/nsswitch/pam_winbind.c @@ -11,6 +11,19 @@ */ #include "pam_winbind.h" +#include "lib/util/common.h" + +/* + * Some dummy placeholder function requiring 'trim_string' + */ + +static void useTrim(const char *someString) +{ + char *tmp = strdup(someString); + trim_string(tmp," ", " "); + SAFE_FREE(tmp); +} + #define CONST_DISCARD(type,ptr) ((type)(void *)ptr) diff --git a/source3/Makefile.in b/source3/Makefile.in index 9e8e03d..0d7c413 100644 --- a/source3/Makefile.in +++ b/source3/Makefile.in @@ -427,7 +427,7 @@ UTIL_OBJ = ../lib/util/rbtree.o ../lib/util/signal.o ../lib/util/time.o \ ../lib/util/genrand.o ../lib/util/util_net.o \ ../lib/util/become_daemon.o ../lib/util/system.o \ ../lib/util/tevent_unix.o ../lib/util/tevent_ntstatus.o \ - ../lib/util/tevent_werror.o \ + ../lib/util/tevent_werror.o ../lib/util/common.o \ ../lib/util/smb_threads.o ../lib/util/util_id.o \ ../lib/util/blocking.o ../lib/util/rfc1738.o \ ../lib/util/select.o ../lib/util/util_pw.o @@ -1071,7 +1071,8 @@ RPCCLIENT_OBJ = $(RPCCLIENT_OBJ1) \ rpc_client/init_netlogon.o \ rpc_client/init_samr.o -PAM_WINBIND_OBJ = ../nsswitch/pam_winbind.o $(WBCOMMON_OBJ) \ +PAM_WINBIND_OBJ = ../nsswitch/pam_winbind.o ../lib/util/common.o \ + $(WBCOMMON_OBJ) \ $(LIBREPLACE_OBJ) @BUILD_INIPARSER@ LIBSMBCLIENT_THREAD_OBJ = \ -- 1.8.1.4