summaryrefslogtreecommitdiff
path: root/source3/lib
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2002-08-17 17:00:51 +0000
committerJelmer Vernooij <jelmer@samba.org>2002-08-17 17:00:51 +0000
commitb2edf254eda92f775e7d3d9b6793b4d77f9000b6 (patch)
tree18eb2564a769678c774a19bb07c00fc4aa7b2758 /source3/lib
parent669a39fae36f8bc60753c9b352556ef8ffaeb568 (diff)
downloadsamba-b2edf254eda92f775e7d3d9b6793b4d77f9000b6.tar.gz
samba-b2edf254eda92f775e7d3d9b6793b4d77f9000b6.tar.bz2
samba-b2edf254eda92f775e7d3d9b6793b4d77f9000b6.zip
sync 3.0 branch with head
(This used to be commit 3928578b52cfc949be5e0ef444fce1558d75f290)
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/account_pol.c88
-rw-r--r--source3/lib/charcnv.c19
-rw-r--r--source3/lib/debug.c8
-rw-r--r--source3/lib/genrand.c2
-rw-r--r--source3/lib/replace.c2
-rw-r--r--source3/lib/smbrun.c2
-rw-r--r--source3/lib/substitute.c53
-rw-r--r--source3/lib/system.c10
-rw-r--r--source3/lib/username.c2
-rw-r--r--source3/lib/util.c38
-rw-r--r--source3/lib/util_getent.c21
-rw-r--r--source3/lib/util_sid.c3
-rw-r--r--source3/lib/util_sock.c2
-rw-r--r--source3/lib/util_str.c165
-rw-r--r--source3/lib/wins_srv.c2
-rw-r--r--source3/lib/xfile.c2
16 files changed, 259 insertions, 160 deletions
diff --git a/source3/lib/account_pol.c b/source3/lib/account_pol.c
index 07676e2202..07b5e2ecfc 100644
--- a/source3/lib/account_pol.c
+++ b/source3/lib/account_pol.c
@@ -2,6 +2,7 @@
* Unix SMB/CIFS implementation.
* account policy storage
* Copyright (C) Jean François Micouleau 1998-2001.
+ * Copyright (C) Andrew Bartlett 2002
*
* 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
@@ -31,6 +32,7 @@ BOOL init_account_policy(void)
{
static pid_t local_pid;
char *vstring = "INFO/version";
+ uint32 version;
if (tdb && local_pid == sys_getpid())
return True;
@@ -44,9 +46,9 @@ BOOL init_account_policy(void)
/* handle a Samba upgrade */
tdb_lock_bystring(tdb, vstring);
- if (tdb_fetch_int32(tdb, vstring) != DATABASE_VERSION) {
+ if (!tdb_fetch_uint32(tdb, vstring, &version) || version != DATABASE_VERSION) {
tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
- tdb_store_int32(tdb, vstring, DATABASE_VERSION);
+ tdb_store_uint32(tdb, vstring, DATABASE_VERSION);
account_policy_set(AP_MIN_PASSWORD_LEN, MINPASSWDLENGTH); /* 5 chars minimum */
account_policy_set(AP_PASSWORD_HISTORY, 0); /* don't keep any old password */
@@ -63,33 +65,50 @@ BOOL init_account_policy(void)
return True;
}
+static const struct {
+ int field;
+ char *string;
+} account_policy_names[] = {
+ {AP_MIN_PASSWORD_LEN, "min password length"},
+ {AP_PASSWORD_HISTORY, "password history"},
+ {AP_USER_MUST_LOGON_TO_CHG_PASS, "user must logon to change password"},
+ {AP_MAX_PASSWORD_AGE, "maximum password age"},
+ {AP_MIN_PASSWORD_AGE,"minimum password age"},
+ {AP_LOCK_ACCOUNT_DURATION, "lockout duration"},
+ {AP_RESET_COUNT_TIME, "reset count minutes"},
+ {AP_BAD_ATTEMPT_LOCKOUT, "bad lockout attempt"},
+ {AP_TIME_TO_LOGOUT, "disconnect time"},
+ {0, NULL}
+};
+
+/****************************************************************************
+Get the account policy name as a string from its #define'ed number
+****************************************************************************/
+
+static const char *decode_account_policy_name(int field)
+{
+ int i;
+ for (i=0; account_policy_names[i].string; i++) {
+ if (field == account_policy_names[i].field)
+ return account_policy_names[i].string;
+ }
+ return NULL;
+
+}
+
/****************************************************************************
+Get the account policy name as a string from its #define'ed number
****************************************************************************/
-static char *decode_account_policy_name(int field)
+int account_policy_name_to_fieldnum(const char *name)
{
- switch (field) {
- case AP_MIN_PASSWORD_LEN:
- return "min password length";
- case AP_PASSWORD_HISTORY:
- return "password history";
- case AP_USER_MUST_LOGON_TO_CHG_PASS:
- return "user must logon to change password";
- case AP_MAX_PASSWORD_AGE:
- return "maximum password age";
- case AP_MIN_PASSWORD_AGE:
- return "minimum password age";
- case AP_LOCK_ACCOUNT_DURATION:
- return "lockout duration";
- case AP_RESET_COUNT_TIME:
- return "reset count minutes";
- case AP_BAD_ATTEMPT_LOCKOUT:
- return "bad lockout attempt";
- case AP_TIME_TO_LOGOUT:
- return "disconnect time";
- default:
- return "undefined value";
+ int i;
+ for (i=0; account_policy_names[i].string; i++) {
+ if (strcmp(name, account_policy_names[i].string) == 0)
+ return account_policy_names[i].field;
}
+ return 0;
+
}
@@ -101,8 +120,17 @@ BOOL account_policy_get(int field, uint32 *value)
init_account_policy();
+ *value = 0;
+
fstrcpy(name, decode_account_policy_name(field));
- *value=tdb_fetch_int32(tdb, name);
+ if (!*name) {
+ DEBUG(1, ("account_policy_get: Field %d is not a valid account policy type! Cannot get, returning 0.\n", field));
+ return False;
+ }
+ if (!tdb_fetch_uint32(tdb, name, value)) {
+ DEBUG(1, ("account_policy_get: tdb_fetch_uint32 failed for feild %d (%s), returning 0", field, name));
+ return False;
+ }
DEBUG(10,("account_policy_get: %s:%d\n", name, *value));
return True;
}
@@ -117,8 +145,16 @@ BOOL account_policy_set(int field, uint32 value)
init_account_policy();
fstrcpy(name, decode_account_policy_name(field));
- if ( tdb_store_int32(tdb, name, value)== -1)
+ if (!*name) {
+ DEBUG(1, ("Field %d is not a valid account policy type! Cannot set.\n", field));
return False;
+ }
+
+ if (!tdb_store_uint32(tdb, name, value)) {
+ DEBUG(1, ("tdb_store_uint32 failed for feild %d (%s) on value %u", field, name, value));
+ return False;
+ }
+
DEBUG(10,("account_policy_set: %s:%d\n", name, value));
return True;
diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c
index d42dc994b0..6e96136643 100644
--- a/source3/lib/charcnv.c
+++ b/source3/lib/charcnv.c
@@ -249,15 +249,15 @@ convert:
size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
void const *src, size_t srclen, void **dest)
{
- void *ob;
+ void *alloced_string;
size_t dest_len;
*dest = NULL;
- dest_len=convert_string_allocate(from, to, src, srclen, (void **)&ob);
+ dest_len=convert_string_allocate(from, to, src, srclen, &alloced_string);
if (dest_len == -1)
return -1;
- *dest = talloc_strdup(ctx, (char *)ob);
- SAFE_FREE(ob);
+ *dest = talloc_memdup(ctx, alloced_string, dest_len);
+ SAFE_FREE(alloced_string);
if (*dest == NULL)
return -1;
return dest_len;
@@ -505,12 +505,12 @@ int push_utf8_pstring(void *dest, const char *src)
*
* @retval The number of bytes occupied by the string in the destination
**/
-int push_utf8_talloc(TALLOC_CTX *ctx, void **dest, const char *src)
+int push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
{
int src_len = strlen(src)+1;
*dest = NULL;
- return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, dest);
+ return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void**)dest);
}
/**
@@ -562,7 +562,8 @@ int pull_ucs2(const void *base_ptr, char *dest, const void *src, int dest_len, i
}
/* ucs2 is always a multiple of 2 bytes */
- src_len &= ~1;
+ if (src_len != -1)
+ src_len &= ~1;
ret = convert_string(CH_UCS2, CH_UNIX, src, src_len, dest, dest_len);
if (dest_len) dest[MIN(ret, dest_len-1)] = 0;
@@ -658,11 +659,11 @@ int pull_utf8_fstring(char *dest, const void *src)
*
* @retval The number of bytes occupied by the string in the destination
**/
-int pull_utf8_talloc(TALLOC_CTX *ctx, void **dest, const char *src)
+int pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
{
int src_len = strlen(src)+1;
*dest = NULL;
- return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, dest);
+ return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest);
}
/**
diff --git a/source3/lib/debug.c b/source3/lib/debug.c
index f41c3b6497..842d2dac1d 100644
--- a/source3/lib/debug.c
+++ b/source3/lib/debug.c
@@ -423,7 +423,7 @@ BOOL debug_parse_levels(const char *params_str)
if (AllowDebugChange == False)
return True;
- params = str_list_make(params_str);
+ params = str_list_make(params_str, NULL);
if (debug_parse_params(params, DEBUGLEVEL_CLASS,
DEBUGLEVEL_CLASS_ISSET))
@@ -602,6 +602,12 @@ BOOL reopen_logs( void )
force_check_log_size();
(void)umask(oldumask);
+ /* Take over stderr to catch ouput into logs */
+ if (dbf && sys_dup2(dbf->fd, 2) == -1) {
+ close_low_fds(True); /* Close stderr too, if dup2 can't point it
+ at the logfile */
+ }
+
return ret;
}
diff --git a/source3/lib/genrand.c b/source3/lib/genrand.c
index ee8bc0b1d5..fe756169a6 100644
--- a/source3/lib/genrand.c
+++ b/source3/lib/genrand.c
@@ -259,7 +259,7 @@ char *generate_random_str(size_t len)
len = sizeof(retstr) -1;
generate_random_buffer( retstr, len, False);
for (i = 0; i < len; i++)
- retstr[i] = c_list[ retstr[i] % sizeof(c_list) ];
+ retstr[i] = c_list[ retstr[i] % (sizeof(c_list)-1) ];
retstr[i] = '\0';
diff --git a/source3/lib/replace.c b/source3/lib/replace.c
index 2cc7d48adb..fd7b2cf7f0 100644
--- a/source3/lib/replace.c
+++ b/source3/lib/replace.c
@@ -428,3 +428,5 @@ char *rep_inet_ntoa(struct in_addr ip)
}
#endif /* HAVE_SYSLOG */
#endif /* HAVE_VSYSLOG */
+
+
diff --git a/source3/lib/smbrun.c b/source3/lib/smbrun.c
index 67f82ed0ad..592543bc43 100644
--- a/source3/lib/smbrun.c
+++ b/source3/lib/smbrun.c
@@ -143,7 +143,7 @@ int smbrun(char *cmd, int *outfd)
/* point our stdout at the file we want output to go into */
if (outfd) {
close(1);
- if (dup2(*outfd,1) != 1) {
+ if (sys_dup2(*outfd,1) != 1) {
DEBUG(2,("Failed to create stdout file descriptor\n"));
close(*outfd);
exit(80);
diff --git a/source3/lib/substitute.c b/source3/lib/substitute.c
index 6d96a1820f..026df0f67f 100644
--- a/source3/lib/substitute.c
+++ b/source3/lib/substitute.c
@@ -25,9 +25,41 @@ fstring local_machine="";
fstring remote_arch="UNKNOWN";
userdom_struct current_user_info;
fstring remote_proto="UNKNOWN";
-fstring remote_machine="";
extern pstring global_myname;
+static fstring remote_machine="";
+
+
+void set_local_machine_name(const char* local_name)
+{
+ fstring tmp_local_machine;
+
+ fstrcpy(tmp_local_machine,local_name);
+ trim_string(tmp_local_machine," "," ");
+ strlower(tmp_local_machine);
+ alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1);
+}
+
+void set_remote_machine_name(const char* remote_name)
+{
+ fstring tmp_remote_machine;
+
+ fstrcpy(tmp_remote_machine,remote_name);
+ trim_string(tmp_remote_machine," "," ");
+ strlower(tmp_remote_machine);
+ alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1);
+}
+
+const char* get_remote_machine_name(void)
+{
+ return remote_machine;
+}
+
+const char* get_local_machine_name(void)
+{
+ return local_machine;
+}
+
/*******************************************************************
Given a pointer to a %$(NAME) expand it as an environment variable.
Return the number of characters by which the pointer should be advanced.
@@ -188,14 +220,15 @@ static char *automount_path(const char *user_name)
moved out to a separate function.
*******************************************************************/
-static char *automount_server(const char *user_name)
+static const char *automount_server(const char *user_name)
{
static pstring server_name;
+ const char *local_machine_name = get_local_machine_name();
/* use the local machine name as the default */
/* this will be the default if WITH_AUTOMOUNT is not used or fails */
- if (*local_machine)
- pstrcpy(server_name, local_machine);
+ if (local_machine_name && *local_machine_name)
+ pstrcpy(server_name, local_machine_name);
else
pstrcpy(server_name, global_myname);
@@ -229,6 +262,7 @@ void standard_sub_basic(const char *smb_name, char *str,size_t len)
char *p, *s;
fstring pidstr;
struct passwd *pass;
+ const char *local_machine_name = get_local_machine_name();
for (s=str; (p=strchr_m(s, '%'));s=p) {
fstring tmp_str;
@@ -261,8 +295,8 @@ void standard_sub_basic(const char *smb_name, char *str,size_t len)
string_sub(p,"%I", client_addr(),l);
break;
case 'L' :
- if (*local_machine)
- string_sub(p,"%L", local_machine,l);
+ if (local_machine_name && *local_machine_name)
+ string_sub(p,"%L", local_machine_name,l);
else
string_sub(p,"%L", global_myname,l);
break;
@@ -286,7 +320,7 @@ void standard_sub_basic(const char *smb_name, char *str,size_t len)
string_sub(p,"%h", myhostname(),l);
break;
case 'm' :
- string_sub(p,"%m", remote_machine,l);
+ string_sub(p,"%m", get_remote_machine_name(),l);
break;
case 'v' :
string_sub(p,"%v", VERSION,l);
@@ -381,6 +415,7 @@ char *alloc_sub_basic(const char *smb_name, const char *str)
char *b, *p, *s, *t, *r, *a_string;
fstring pidstr;
struct passwd *pass;
+ const char *local_machine_name = get_local_machine_name();
a_string = strdup(str);
if (a_string == NULL) {
@@ -415,8 +450,8 @@ char *alloc_sub_basic(const char *smb_name, const char *str)
t = realloc_string_sub(t, "%I", client_addr());
break;
case 'L' :
- if (*local_machine)
- t = realloc_string_sub(t, "%L", local_machine);
+ if (local_machine_name && *local_machine_name)
+ t = realloc_string_sub(t, "%L", local_machine_name);
else
t = realloc_string_sub(t, "%L", global_myname);
break;
diff --git a/source3/lib/system.c b/source3/lib/system.c
index 8b2ba800f5..edda54a78d 100644
--- a/source3/lib/system.c
+++ b/source3/lib/system.c
@@ -1219,6 +1219,16 @@ const char *sys_dlerror(void)
#endif
}
+int sys_dup2(int oldfd, int newfd)
+{
+#if defined(HAVE_DUP2)
+ return dup2(oldfd, newfd);
+#else
+ errno = ENOSYS;
+ return -1;
+#endif
+}
+
/**************************************************************************
Wrapper for Admin Logs.
****************************************************************************/
diff --git a/source3/lib/username.c b/source3/lib/username.c
index 4813c8fd19..5db7f58b1e 100644
--- a/source3/lib/username.c
+++ b/source3/lib/username.c
@@ -163,7 +163,7 @@ BOOL map_username(char *user)
}
}
- dosuserlist = str_list_make(dosname);
+ dosuserlist = str_list_make(dosname, NULL);
if (!dosuserlist) {
DEBUG(0,("Unable to build user list\n"));
return False;
diff --git a/source3/lib/util.c b/source3/lib/util.c
index be108aa405..ae94b710b2 100644
--- a/source3/lib/util.c
+++ b/source3/lib/util.c
@@ -100,7 +100,7 @@ char *tmpdir(void)
Determine whether we are in the specified group.
****************************************************************************/
-BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups)
+BOOL in_group(gid_t group, gid_t current_gid, int ngroups, const gid_t *groups)
{
int i;
@@ -503,27 +503,32 @@ void make_dir_struct(char *buf,char *mask,char *fname,SMB_OFF_T size,int mode,ti
/*******************************************************************
close the low 3 fd's and open dev/null in their place
********************************************************************/
-void close_low_fds(void)
+void close_low_fds(BOOL stderr_too)
{
int fd;
int i;
close(0); close(1);
-#ifndef __INSURE__
- close(2);
-#endif
+
+ if (stderr_too) {
+ close(2);
+ }
+
/* try and use up these file descriptors, so silly
library routines writing to stdout etc won't cause havoc */
for (i=0;i<3;i++) {
- fd = sys_open("/dev/null",O_RDWR,0);
- if (fd < 0) fd = sys_open("/dev/null",O_WRONLY,0);
- if (fd < 0) {
- DEBUG(0,("Can't open /dev/null\n"));
- return;
- }
- if (fd != i) {
- DEBUG(0,("Didn't get file descriptor %d\n",i));
- return;
- }
+ if (i == 2 && !stderr_too)
+ continue;
+
+ fd = sys_open("/dev/null",O_RDWR,0);
+ if (fd < 0) fd = sys_open("/dev/null",O_WRONLY,0);
+ if (fd < 0) {
+ DEBUG(0,("Can't open /dev/null\n"));
+ return;
+ }
+ if (fd != i) {
+ DEBUG(0,("Didn't get file descriptor %d\n",i));
+ return;
+ }
}
}
@@ -678,7 +683,8 @@ void become_daemon(void)
#endif /* HAVE_SETSID */
/* Close fd's 0,1,2. Needed if started by rsh */
- close_low_fds();
+ close_low_fds(False); /* Don't close stderr, let the debug system
+ attach it to the logfile */
}
diff --git a/source3/lib/util_getent.c b/source3/lib/util_getent.c
index 2e76121aae..6699ce3e92 100644
--- a/source3/lib/util_getent.c
+++ b/source3/lib/util_getent.c
@@ -21,27 +21,6 @@
#include "includes.h"
-#if 0
-static void print_grent_list(struct sys_grent *glist)
-{
- DEBUG(100, ("print_grent_list: %x\n", glist ));
- while (glist) {
- DEBUG(100,("glist: %x ", glist));
- if (glist->gr_name)
- DEBUG(100,(": gr_name = (%x) %s ", glist->gr_name, glist->gr_name));
- if (glist->gr_passwd)
- DEBUG(100,(": gr_passwd = (%x) %s ", glist->gr_passwd, glist->gr_passwd));
- if (glist->gr_mem) {
- int i;
- for (i = 0; glist->gr_mem[i]; i++)
- DEBUG(100,(" : gr_mem[%d] = (%x) %s ", i, glist->gr_mem[i], glist->gr_mem[i]));
- }
- DEBUG(100,(": gr_next = %x\n", glist->next ));
- glist = glist->next;
- }
- DEBUG(100,("FINISHED !\n\n"));
-}
-#endif
/****************************************************************
Returns a single linked list of group entries.
diff --git a/source3/lib/util_sid.c b/source3/lib/util_sid.c
index 5dd1d75c70..ad09f91234 100644
--- a/source3/lib/util_sid.c
+++ b/source3/lib/util_sid.c
@@ -365,6 +365,9 @@ BOOL sid_parse(char *inbuf, size_t len, DOM_SID *sid)
{
int i;
if (len < 8) return False;
+
+ ZERO_STRUCTP(sid);
+
sid->sid_rev_num = CVAL(inbuf, 0);
sid->num_auths = CVAL(inbuf, 1);
memcpy(sid->id_auth, inbuf+2, 6);
diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c
index 4f1f2a1470..5e2b7c5ed9 100644
--- a/source3/lib/util_sock.c
+++ b/source3/lib/util_sock.c
@@ -708,7 +708,7 @@ int open_socket_in( int type, int port, int dlevel, uint32 socket_addr, BOOL reb
/* now we've got a socket - we need to bind it */
if( bind( res, (struct sockaddr *)&sock, sizeof(sock) ) == -1 ) {
- if( DEBUGLVL(dlevel) && (port == SMB_PORT || port == NMB_PORT) ) {
+ if( DEBUGLVL(dlevel) && (port == SMB_PORT1 || port == SMB_PORT2 || port == NMB_PORT) ) {
dbgtext( "bind failed on port %d ", port );
dbgtext( "socket_addr = %s.\n", inet_ntoa( sock.sin_addr ) );
dbgtext( "Error = %s\n", strerror(errno) );
diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c
index 88a72f1536..19d92eec8f 100644
--- a/source3/lib/util_str.c
+++ b/source3/lib/util_str.c
@@ -212,6 +212,18 @@ int strwicmp(const char *psz1, const char *psz2)
}
+/* Convert a string to upper case, but don't modify it */
+
+char *strupper_static(const char *s)
+{
+ static pstring str;
+
+ pstrcpy(str, s);
+ strupper(str);
+
+ return str;
+}
+
/*******************************************************************
convert a string to "normal" form
********************************************************************/
@@ -299,7 +311,7 @@ BOOL trim_string(char *s,const char *front,const char *back)
}
if (back_len) {
- while (strncmp(s+len-back_len,back,back_len)==0) {
+ while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
s[len-back_len]='\0';
len -= back_len;
ret=True;
@@ -667,7 +679,7 @@ void string_sub(char *s,const char *pattern, const char *insert, size_t len)
li = (ssize_t)strlen(insert);
if (len == 0)
- len = ls;
+ len = ls + 1; /* len is number of *bytes* */
while (lp <= ls && (p = strstr(s,pattern))) {
if (ls + (li-lp) >= len) {
@@ -798,7 +810,7 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
return;
if (len == 0)
- len = ls;
+ len = ls + 1; /* len is number of *bytes* */
while (lp <= ls && (p = strstr(s,pattern))) {
if (ls + (li-lp) >= len) {
@@ -826,7 +838,8 @@ return a new allocate unicode string.
smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern,
const smb_ucs2_t *insert)
{
- smb_ucs2_t *r, *rp, *sp;
+ smb_ucs2_t *r, *rp;
+ const smb_ucs2_t *sp;
size_t lr, lp, li, lt;
if (!insert || !pattern || !*pattern || !s) return NULL;
@@ -836,7 +849,7 @@ smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern,
li = (size_t)strlen_w(insert);
if (li > lp) {
- smb_ucs2_t *st = s;
+ const smb_ucs2_t *st = s;
int ld = li - lp;
while ((sp = strstr_w(st, pattern))) {
st = sp + lp;
@@ -879,68 +892,59 @@ smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern,
}
/****************************************************************************
- splits out the front and back at a separator.
+ Splits out the front and back at a separator.
****************************************************************************/
+
void split_at_last_component(char *path, char *front, char sep, char *back)
{
char *p = strrchr_m(path, sep);
if (p != NULL)
- {
*p = 0;
- }
+
if (front != NULL)
- {
pstrcpy(front, path);
- }
- if (p != NULL)
- {
+
+ if (p != NULL) {
if (back != NULL)
- {
pstrcpy(back, p+1);
- }
*p = '\\';
- }
- else
- {
+ } else {
if (back != NULL)
- {
back[0] = 0;
- }
}
}
-
/****************************************************************************
-write an octal as a string
+ Write an octal as a string.
****************************************************************************/
+
char *octal_string(int i)
{
static char ret[64];
- if (i == -1) {
+ if (i == -1)
return "-1";
- }
slprintf(ret, sizeof(ret)-1, "0%o", i);
return ret;
}
/****************************************************************************
-truncate a string at a specified length
+ Truncate a string at a specified length.
****************************************************************************/
+
char *string_truncate(char *s, int length)
{
- if (s && strlen(s) > length) {
+ if (s && strlen(s) > length)
s[length] = 0;
- }
return s;
}
-
/****************************************************************************
-strchr and strrchr_m are very hard to do on general multi-byte strings.
-we convert via ucs2 for now
+ Strchr and strrchr_m are very hard to do on general multi-byte strings.
+ We convert via ucs2 for now.
****************************************************************************/
+
char *strchr_m(const char *s, char c)
{
wpstring ws;
@@ -949,7 +953,8 @@ char *strchr_m(const char *s, char c)
push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
p = strchr_w(ws, UCS2_CHAR(c));
- if (!p) return NULL;
+ if (!p)
+ return NULL;
*p = 0;
pull_ucs2_pstring(s2, ws);
return (char *)(s+strlen(s2));
@@ -963,26 +968,29 @@ char *strrchr_m(const char *s, char c)
push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
p = strrchr_w(ws, UCS2_CHAR(c));
- if (!p) return NULL;
+ if (!p)
+ return NULL;
*p = 0;
pull_ucs2_pstring(s2, ws);
return (char *)(s+strlen(s2));
}
/*******************************************************************
- convert a string to lower case
+ Convert a string to lower case.
********************************************************************/
+
void strlower_m(char *s)
{
/* this is quite a common operation, so we want it to be
fast. We optimise for the ascii case, knowing that all our
supported multi-byte character sets are ascii-compatible
(ie. they match for the first 128 chars) */
- while (*s && !(((unsigned char)s[0]) & 0x7F)) {
+
+ while (*s && !(((unsigned char)s[0]) & 0x7F))
*s++ = tolower((unsigned char)*s);
- }
- if (!*s) return;
+ if (!*s)
+ return;
/* I assume that lowercased string takes the same number of bytes
* as source string even in UTF-8 encoding. (VIV) */
@@ -990,8 +998,9 @@ void strlower_m(char *s)
}
/*******************************************************************
- duplicate convert a string to lower case
+ Duplicate convert a string to lower case.
********************************************************************/
+
char *strdup_lower(const char *s)
{
char *t = strdup(s);
@@ -1004,19 +1013,21 @@ char *strdup_lower(const char *s)
}
/*******************************************************************
- convert a string to upper case
+ Convert a string to upper case.
********************************************************************/
+
void strupper_m(char *s)
{
/* this is quite a common operation, so we want it to be
fast. We optimise for the ascii case, knowing that all our
supported multi-byte character sets are ascii-compatible
(ie. they match for the first 128 chars) */
- while (*s && !(((unsigned char)s[0]) & 0x7F)) {
+
+ while (*s && !(((unsigned char)s[0]) & 0x7F))
*s++ = toupper((unsigned char)*s);
- }
- if (!*s) return;
+ if (!*s)
+ return;
/* I assume that lowercased string takes the same number of bytes
* as source string even in multibyte encoding. (VIV) */
@@ -1024,8 +1035,9 @@ void strupper_m(char *s)
}
/*******************************************************************
- convert a string to upper case
+ Convert a string to upper case.
********************************************************************/
+
char *strdup_upper(const char *s)
{
char *t = strdup(s);
@@ -1048,7 +1060,8 @@ char *binary_string(char *buf, int len)
int i, j;
const char *hex = "0123456789ABCDEF";
s = malloc(len * 3 + 1);
- if (!s) return NULL;
+ if (!s)
+ return NULL;
for (j=i=0;i<len;i++) {
s[j] = '\\';
s[j+1] = hex[((unsigned char)buf[i]) >> 4];
@@ -1059,8 +1072,8 @@ char *binary_string(char *buf, int len)
return s;
}
-
/* Just a typesafety wrapper for snprintf into a pstring */
+
int pstr_sprintf(pstring s, const char *fmt, ...)
{
va_list ap;
@@ -1072,8 +1085,8 @@ int pstr_sprintf(pstring s, const char *fmt, ...)
return ret;
}
-
/* Just a typesafety wrapper for snprintf into a fstring */
+
int fstr_sprintf(fstring s, const char *fmt, ...)
{
va_list ap;
@@ -1085,18 +1098,19 @@ int fstr_sprintf(fstring s, const char *fmt, ...)
return ret;
}
-
#ifndef HAVE_STRNDUP
/*******************************************************************
-some platforms don't have strndup
+ Some platforms don't have strndup.
********************************************************************/
+
char *strndup(const char *s, size_t n)
{
char *ret;
n = strnlen(s, n);
ret = malloc(n+1);
- if (!ret) return NULL;
+ if (!ret)
+ return NULL;
memcpy(ret, s, n);
ret[n] = 0;
@@ -1111,39 +1125,39 @@ some platforms don't have strnlen
size_t strnlen(const char *s, size_t n)
{
int i;
- for (i=0; s[i] && i<n; i++) /* noop */ ;
+ for (i=0; s[i] && i<n; i++)
+ /* noop */ ;
return i;
}
#endif
-
-
/***********************************************************
List of Strings manipulation functions
***********************************************************/
#define S_LIST_ABS 16 /* List Allocation Block Size */
-char **str_list_make(const char *string)
+char **str_list_make(const char *string, const char *sep)
{
char **list, **rlist;
char *str, *s;
int num, lsize;
pstring tok;
- if (!string || !*string) return NULL;
+ if (!string || !*string)
+ return NULL;
s = strdup(string);
if (!s) {
DEBUG(0,("str_list_make: Unable to allocate memory"));
return NULL;
}
+ if (!sep) sep = LIST_SEP;
num = lsize = 0;
list = NULL;
str = s;
- while (next_token(&str, tok, LIST_SEP, sizeof(tok)))
- {
+ while (next_token(&str, tok, sep, sizeof(tok))) {
if (num == lsize) {
lsize += S_LIST_ABS;
rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
@@ -1178,13 +1192,13 @@ BOOL str_list_copy(char ***dest, char **src)
int num, lsize;
*dest = NULL;
- if (!src) return False;
+ if (!src)
+ return False;
num = lsize = 0;
list = NULL;
- while (src[num])
- {
+ while (src[num]) {
if (num == lsize) {
lsize += S_LIST_ABS;
rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
@@ -1212,17 +1226,22 @@ BOOL str_list_copy(char ***dest, char **src)
}
/* return true if all the elemnts of the list matches exactly */
+
BOOL str_list_compare(char **list1, char **list2)
{
int num;
- if (!list1 || !list2) return (list1 == list2);
+ if (!list1 || !list2)
+ return (list1 == list2);
for (num = 0; list1[num]; num++) {
- if (!list2[num]) return False;
- if (!strcsequal(list1[num], list2[num])) return False;
+ if (!list2[num])
+ return False;
+ if (!strcsequal(list1[num], list2[num]))
+ return False;
}
- if (list2[num]) return False; /* if list2 has more elements than list1 fail */
+ if (list2[num])
+ return False; /* if list2 has more elements than list1 fail */
return True;
}
@@ -1231,9 +1250,11 @@ void str_list_free(char ***list)
{
char **tlist;
- if (!list || !*list) return;
+ if (!list || !*list)
+ return;
tlist = *list;
- for(; *tlist; tlist++) SAFE_FREE(*tlist);
+ for(; *tlist; tlist++)
+ SAFE_FREE(*tlist);
SAFE_FREE(*list);
}
@@ -1242,25 +1263,25 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert)
char *p, *s, *t;
ssize_t ls, lp, li, ld, i, d;
- if (!list) return False;
- if (!pattern) return False;
- if (!insert) return False;
+ if (!list)
+ return False;
+ if (!pattern)
+ return False;
+ if (!insert)
+ return False;
lp = (ssize_t)strlen(pattern);
li = (ssize_t)strlen(insert);
ld = li -lp;
- while (*list)
- {
+ while (*list) {
s = *list;
ls = (ssize_t)strlen(s);
- while ((p = strstr(s, pattern)))
- {
+ while ((p = strstr(s, pattern))) {
t = *list;
d = p -t;
- if (ld)
- {
+ if (ld) {
t = (char *) malloc(ls +ld +1);
if (!t) {
DEBUG(0,("str_list_substitute: Unable to allocate memory"));
diff --git a/source3/lib/wins_srv.c b/source3/lib/wins_srv.c
index adf405ae7e..61e77aca58 100644
--- a/source3/lib/wins_srv.c
+++ b/source3/lib/wins_srv.c
@@ -236,7 +236,7 @@ char **wins_srv_tags(void)
}
/* add it to the list */
- ret = (char **)Realloc(ret, (count+1) * sizeof(char *));
+ ret = (char **)Realloc(ret, (count+2) * sizeof(char *));
ret[count] = strdup(t_ip.tag);
if (!ret[count]) break;
count++;
diff --git a/source3/lib/xfile.c b/source3/lib/xfile.c
index 903dfb1ae0..b5710f3a39 100644
--- a/source3/lib/xfile.c
+++ b/source3/lib/xfile.c
@@ -171,7 +171,7 @@ int x_fwrite(const void *p, size_t size, size_t nmemb, XFILE *f)
flush a bit more than necessary, but that is harmless */
if (f->buftype == X_IOLBF && f->bufused) {
int i;
- for (i=size-1; i>=0; i--) {
+ for (i=(size*nmemb)-1; i>=0; i--) {
if (*(i+(const char *)p) == '\n') {
x_fflush(f);
break;