diff options
Diffstat (limited to 'source/lib/util.c')
-rw-r--r-- | source/lib/util.c | 283 |
1 files changed, 126 insertions, 157 deletions
diff --git a/source/lib/util.c b/source/lib/util.c index d3f07dd0a8c..26ca621fde2 100644 --- a/source/lib/util.c +++ b/source/lib/util.c @@ -59,6 +59,8 @@ extern SSL *ssl; extern int sslFd; #endif /* WITH_SSL */ +extern int DEBUGLEVEL; + int Protocol = PROTOCOL_COREPLUS; /* a default finfo structure to ensure all fields are sensible */ @@ -91,27 +93,28 @@ char **my_netbios_names; /**************************************************************************** - find a suitable temporary directory. The result should be copied immediately - as it may be overwritten by a subsequent call - ****************************************************************************/ + Find a suitable temporary directory. The result should be copied immediately + as it may be overwritten by a subsequent call. +****************************************************************************/ + char *tmpdir(void) { - char *p; - if ((p = getenv("TMPDIR"))) { - return p; - } - return "/tmp"; + char *p; + if ((p = getenv("TMPDIR"))) + return p; + return "/tmp"; } /**************************************************************************** -determine whether we are in the specified group + Determine whether we are in the specified group. ****************************************************************************/ BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups) { int i; - if (group == current_gid) return(True); + if (group == current_gid) + return(True); for (i=0;i<ngroups;i++) if (group == groups[i]) @@ -120,14 +123,13 @@ BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups) return(False); } - /**************************************************************************** -like atoi but gets the value up to the separater character + Like atoi but gets the value up to the separater character. ****************************************************************************/ + char *Atoic(char *p, int *n, char *c) { - if (!isdigit((int)*p)) - { + if (!isdigit((int)*p)) { DEBUG(5, ("Atoic: malformed number\n")); return NULL; } @@ -135,12 +137,9 @@ char *Atoic(char *p, int *n, char *c) (*n) = atoi(p); while ((*p) && isdigit((int)*p)) - { p++; - } - if (strchr_m(c, *p) == NULL) - { + if (strchr(c, *p) == NULL) { DEBUG(5, ("Atoic: no separator characters (%s) not found\n", c)); return NULL; } @@ -149,31 +148,29 @@ char *Atoic(char *p, int *n, char *c) } /************************************************************************* - reads a list of numbers + Reads a list of numbers. *************************************************************************/ + char *get_numlist(char *p, uint32 **num, int *count) { int val; if (num == NULL || count == NULL) - { return NULL; - } (*count) = 0; (*num ) = NULL; - while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') - { + while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') { uint32 *tn; - + tn = Realloc((*num), ((*count)+1) * sizeof(uint32)); - if (tn == NULL) - { - SAFE_FREE(*num); + if (tn == NULL) { + if (*num) + free(*num); return NULL; - } - else (*num) = tn; + } else + (*num) = tn; (*num)[(*count)] = val; (*count)++; p++; @@ -182,37 +179,40 @@ char *get_numlist(char *p, uint32 **num, int *count) return p; } - /******************************************************************* - check if a file exists - call vfs_file_exist for samba files + Check if a file exists - call vfs_file_exist for samba files. ********************************************************************/ + BOOL file_exist(char *fname,SMB_STRUCT_STAT *sbuf) { - SMB_STRUCT_STAT st; - if (!sbuf) sbuf = &st; + SMB_STRUCT_STAT st; + if (!sbuf) + sbuf = &st; - if (sys_stat(fname,sbuf) != 0) - return(False); + if (sys_stat(fname,sbuf) != 0) + return(False); - return(S_ISREG(sbuf->st_mode)); + return(S_ISREG(sbuf->st_mode)); } /******************************************************************* -check a files mod time + Check a files mod time. ********************************************************************/ + time_t file_modtime(char *fname) { - SMB_STRUCT_STAT st; + SMB_STRUCT_STAT st; - if (sys_stat(fname,&st) != 0) - return(0); + if (sys_stat(fname,&st) != 0) + return(0); - return(st.st_mtime); + return(st.st_mtime); } /******************************************************************* - check if a directory exists + Check if a directory exists. ********************************************************************/ + BOOL directory_exist(char *dname,SMB_STRUCT_STAT *st) { SMB_STRUCT_STAT st2; @@ -334,21 +334,20 @@ int set_message(char *buf,int num_words,int num_bytes,BOOL zero) /******************************************************************* setup only the byte count for a smb message ********************************************************************/ -int set_message_bcc(char *buf,int num_bytes) +void set_message_bcc(char *buf,int num_bytes) { int num_words = CVAL(buf,smb_wct); SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes); smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4); - return (smb_size + num_words*2 + num_bytes); } /******************************************************************* setup only the byte count for a smb message, using the end of the message as a marker ********************************************************************/ -int set_message_end(void *outbuf,void *end_ptr) +void set_message_end(void *outbuf,void *end_ptr) { - return set_message_bcc((char *)outbuf,PTR_DIFF(end_ptr,smb_buf((char *)outbuf))); + set_message_bcc((char *)outbuf,PTR_DIFF(end_ptr,smb_buf((char *)outbuf))); } /******************************************************************* @@ -370,7 +369,7 @@ void dos_clean_name(char *s) *p = 0; pstrcpy(s1,p+3); - if ((p=strrchr_m(s,'\\')) != NULL) + if ((p=strrchr(s,'\\')) != NULL) *p = 0; else *s = 0; @@ -408,7 +407,7 @@ void unix_clean_name(char *s) *p = 0; pstrcpy(s1,p+3); - if ((p=strrchr_m(s,'/')) != NULL) + if ((p=strrchr(s,'/')) != NULL) *p = 0; else *s = 0; @@ -432,25 +431,25 @@ void make_dir_struct(char *buf,char *mask,char *fname,SMB_OFF_T size,int mode,ti size = 0; memset(buf+1,' ',11); - if ((p = strchr_m(mask2,'.')) != NULL) + if ((p = strchr(mask2,'.')) != NULL) { *p = 0; - push_ascii(buf+1,mask2,8, 0); - push_ascii(buf+9,p+1,3, 0); + memcpy(buf+1,mask2,MIN(strlen(mask2),8)); + memcpy(buf+9,p+1,MIN(strlen(p+1),3)); *p = '.'; } else - push_ascii(buf+1,mask2,11, 0); + memcpy(buf+1,mask2,MIN(strlen(mask2),11)); memset(buf+21,'\0',DIR_STRUCT_SIZE-21); CVAL(buf,21) = mode; put_dos_date(buf,22,date); SSVAL(buf,26,size & 0xFFFF); SSVAL(buf,28,(size >> 16)&0xFFFF); - push_ascii(buf+30,fname,12, 0); + StrnCpy(buf+30,fname,12); if (!case_sensitive) strupper(buf+30); - DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname)); + DEBUG(8,("put name [%s] into dir struct\n",buf+30)); } @@ -561,34 +560,35 @@ SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n) } /******************************************************************* -sleep for a specified number of milliseconds + Sleep for a specified number of milliseconds. ********************************************************************/ + void msleep(int t) { - int tdiff=0; - struct timeval tval,t1,t2; - fd_set fds; + int tdiff=0; + struct timeval tval,t1,t2; + fd_set fds; - GetTimeOfDay(&t1); - GetTimeOfDay(&t2); + GetTimeOfDay(&t1); + GetTimeOfDay(&t2); - while (tdiff < t) { - tval.tv_sec = (t-tdiff)/1000; - tval.tv_usec = 1000*((t-tdiff)%1000); + while (tdiff < t) { + tval.tv_sec = (t-tdiff)/1000; + tval.tv_usec = 1000*((t-tdiff)%1000); - FD_ZERO(&fds); - errno = 0; - sys_select_intr(0,&fds,&tval); + FD_ZERO(&fds); + errno = 0; + sys_select_intr(0,&fds,&tval); - GetTimeOfDay(&t2); - tdiff = TvalDiff(&t1,&t2); - } + GetTimeOfDay(&t2); + tdiff = TvalDiff(&t1,&t2); + } } - /**************************************************************************** -become a daemon, discarding the controlling terminal + Become a daemon, discarding the controlling terminal. ****************************************************************************/ + void become_daemon(void) { if (sys_fork()) { @@ -612,62 +612,64 @@ void become_daemon(void) close_low_fds(); } - /**************************************************************************** -put up a yes/no prompt + Put up a yes/no prompt ****************************************************************************/ + BOOL yesno(char *p) { - pstring ans; - printf("%s",p); + pstring ans; + printf("%s",p); - if (!fgets(ans,sizeof(ans)-1,stdin)) - return(False); + if (!fgets(ans,sizeof(ans)-1,stdin)) + return(False); - if (*ans == 'y' || *ans == 'Y') - return(True); + if (*ans == 'y' || *ans == 'Y') + return(True); - return(False); + return(False); } /**************************************************************************** -expand a pointer to be a particular size + Expand a pointer to be a particular size. ****************************************************************************/ + void *Realloc(void *p,size_t size) { - void *ret=NULL; + void *ret=NULL; - if (size == 0) { - SAFE_FREE(p); - DEBUG(5,("Realloc asked for 0 bytes\n")); - return NULL; - } + if (size == 0) { + if (p) + free(p); + DEBUG(5,("Realloc asked for 0 bytes\n")); + return NULL; + } - if (!p) - ret = (void *)malloc(size); - else - ret = (void *)realloc(p,size); + if (!p) + ret = (void *)malloc(size); + else + ret = (void *)realloc(p,size); - if (!ret) - DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size)); + if (!ret) + DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size)); - return(ret); + return(ret); } - /**************************************************************************** -free memory, checks for NULL and set to NULL -use directly SAFE_FREE() -exist only because we need to pass a function pointer somewhere --SSS + Free memory, checks for NULL. ****************************************************************************/ + void safe_free(void *p) { - SAFE_FREE(p); + if (p != NULL) + free(p); } /**************************************************************************** -get my own name and IP + Get my own name and IP. ****************************************************************************/ + BOOL get_myname(char *my_name) { pstring hostname; @@ -685,8 +687,9 @@ BOOL get_myname(char *my_name) if (my_name) { /* split off any parts after an initial . */ - char *p = strchr_m(hostname,'.'); - if (p) *p = 0; + char *p = strchr(hostname,'.'); + if (p) + *p = 0; fstrcpy(my_name,hostname); } @@ -695,8 +698,9 @@ BOOL get_myname(char *my_name) } /**************************************************************************** -interpret a protocol description string, with a default + Interpret a protocol description string, with a default. ****************************************************************************/ + int interpret_protocol(char *str,int def) { if (strequal(str,"NT1")) @@ -731,7 +735,7 @@ BOOL is_ipaddress(const char *str) pure_address = False; /* Check that a pure number is not misinterpreted as an IP */ - pure_address = pure_address && (strchr_m(str, '.') != NULL); + pure_address = pure_address && (strchr(str, '.') != NULL); return pure_address; } @@ -740,7 +744,7 @@ BOOL is_ipaddress(const char *str) interpret an internet address or name into an IP address in 4 byte form ****************************************************************************/ -uint32 interpret_addr(const char *str) +uint32 interpret_addr(char *str) { struct hostent *hp; uint32 res; @@ -773,7 +777,7 @@ uint32 interpret_addr(const char *str) /******************************************************************* a convenient addition to interpret_addr() ******************************************************************/ -struct in_addr *interpret_addr2(const char *str) +struct in_addr *interpret_addr2(char *str) { static struct in_addr ret; uint32 a = interpret_addr(str); @@ -1109,7 +1113,7 @@ BOOL is_in_path(char *name, name_compare_entry *namelist) } /* Get the last component of the unix name. */ - p = strrchr_m(name, '/'); + p = strrchr(name, '/'); strncpy(last_component, p ? ++p : name, sizeof(last_component)-1); last_component[sizeof(last_component)-1] = '\0'; @@ -1176,7 +1180,7 @@ void set_namearray(name_compare_entry **ppname_array, char *namelist) continue; } /* find the next / */ - name_end = strchr_m(nameptr, '/'); + name_end = strchr(nameptr, '/'); /* oops - the last check for a / didn't find one. */ if (name_end == NULL) @@ -1209,7 +1213,7 @@ void set_namearray(name_compare_entry **ppname_array, char *namelist) continue; } /* find the next / */ - if ((name_end = strchr_m(nameptr, '/')) != NULL) + if ((name_end = strchr(nameptr, '/')) != NULL) { *name_end = 0; } @@ -1241,11 +1245,13 @@ routine to free a namearray. void free_namearray(name_compare_entry *name_array) { - if(name_array == NULL) + if(name_array == 0) return; - SAFE_FREE(name_array->name); - SAFE_FREE(name_array); + if(name_array->name != NULL) + free(name_array->name); + + free((char *)name_array); } /**************************************************************************** @@ -1413,16 +1419,16 @@ void out_data(FILE *f,char *buf1,int len, int per_line) } } -void print_asc(int level, const unsigned char *buf,int len) +void print_asc(int level, unsigned char *buf,int len) { int i; for (i=0;i<len;i++) DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.')); } -void dump_data(int level, const char *buf1,int len) +void dump_data(int level,char *buf1,int len) { - const unsigned char *buf = (const unsigned char *)buf1; + unsigned char *buf = (unsigned char *)buf1; int i=0; if (len<=0) return; @@ -1494,7 +1500,7 @@ zero a memory area then free it. Used to catch bugs faster void zero_free(void *p, size_t size) { memset(p, 0, size); - SAFE_FREE(p); + free(p); } @@ -1610,7 +1616,7 @@ BOOL reg_split_key(char *full_keyname, uint32 *reg_type, char *key_name) return False; } - if (next_token(&full_keyname, tmp, "\n\r", sizeof(tmp))) + if (next_token(NULL, tmp, "\n\r", sizeof(tmp))) { fstrcpy(key_name, tmp); } @@ -1687,16 +1693,6 @@ char *lock_path(char *name) return fname; } -/***************************************************************** -a useful function for returning a path in the Samba lib directory - *****************************************************************/ -char *lib_path(char *name) -{ - static pstring fname; - snprintf(fname, sizeof(fname), "%s/%s", LIBDIR, name); - return fname; -} - /******************************************************************* Given a filename - get its directory name NB: Returned in static storage. Caveats: @@ -1714,7 +1710,7 @@ char *parent_dirname(const char *path) return(NULL); pstrcpy(dirpath, path); - p = strrchr_m(dirpath, '/'); /* Find final '/', if any */ + p = strrchr(dirpath, '/'); /* Find final '/', if any */ if (!p) { pstrcpy(dirpath, "."); /* No final "/", so dir is "." */ } else { @@ -1752,19 +1748,18 @@ BOOL ms_has_wild(char *s) BOOL mask_match(char *string, char *pattern, BOOL is_case_sensitive) { fstring p2, s2; - if (strcmp(string,"..") == 0) string = "."; if (strcmp(pattern,".") == 0) return False; if (is_case_sensitive) { - return ms_fnmatch(pattern, string, Protocol) == 0; + return ms_fnmatch(pattern, string) == 0; } fstrcpy(p2, pattern); fstrcpy(s2, string); strlower(p2); strlower(s2); - return ms_fnmatch(p2, s2, Protocol) == 0; + return ms_fnmatch(p2, s2) == 0; } /********************************************************* @@ -1893,32 +1888,6 @@ BOOL unix_wild_match(char *pattern, char *string) return unix_do_match(p2, s2) == 0; } -/******************************************************************* - construct a data blob, must be freed with data_blob_free() -*******************************************************************/ -DATA_BLOB data_blob(void *p, size_t length) -{ - DATA_BLOB ret; - - if (!p) { - ZERO_STRUCT(ret); - return ret; - } - - ret.data = memdup(p, length); - ret.length = length; - return ret; -} - -/******************************************************************* -free a data blob -*******************************************************************/ -void data_blob_free(DATA_BLOB *d) -{ - SAFE_FREE(d->data); -} - - #ifdef __INSURE__ /******************************************************************* |