always compile before commit :-)
[Samba.git] / source / lib / util.c
blobdb87cfefa595219e0f61b208f593d59229b40d3f
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
25 #ifdef WITH_NISPLUS_HOME
26 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
28 * The following lines are needed due to buggy include files
29 * in Solaris 2.6 which define GROUP in both /usr/include/sys/acl.h and
30 * also in /usr/include/rpcsvc/nis.h. The definitions conflict. JRA.
31 * Also GROUP_OBJ is defined as 0x4 in /usr/include/sys/acl.h and as
32 * an enum in /usr/include/rpcsvc/nis.h.
35 #if defined(GROUP)
36 #undef GROUP
37 #endif
39 #if defined(GROUP_OBJ)
40 #undef GROUP_OBJ
41 #endif
43 #endif /* BROKEN_NISPLUS_INCLUDE_FILES */
45 #include <rpcsvc/nis.h>
47 #else /* !WITH_NISPLUS_HOME */
49 #include "rpcsvc/ypclnt.h"
51 #endif /* WITH_NISPLUS_HOME */
52 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
54 #ifdef WITH_SSL
55 #include <ssl.h>
56 #undef Realloc /* SSLeay defines this and samba has a function of this name */
57 extern SSL *ssl;
58 extern int sslFd;
59 #endif /* WITH_SSL */
61 extern int DEBUGLEVEL;
63 int Protocol = PROTOCOL_COREPLUS;
65 /* a default finfo structure to ensure all fields are sensible */
66 file_info def_finfo = {-1,0,0,0,0,0,0,"",""};
68 /* this is used by the chaining code */
69 int chain_size = 0;
71 int trans_num = 0;
74 case handling on filenames
76 int case_default = CASE_LOWER;
78 /* the following control case operations - they are put here so the
79 client can link easily */
80 BOOL case_sensitive;
81 BOOL case_preserve;
82 BOOL use_mangled_map = False;
83 BOOL short_case_preserve;
84 BOOL case_mangle;
86 static enum remote_arch_types ra_type = RA_UNKNOWN;
87 pstring user_socket_options=DEFAULT_SOCKET_OPTIONS;
89 pstring global_myname = "";
90 fstring global_myworkgroup = "";
91 char **my_netbios_names;
94 /****************************************************************************
95 find a suitable temporary directory. The result should be copied immediately
96 as it may be overwritten by a subsequent call
97 ****************************************************************************/
98 char *tmpdir(void)
100 char *p;
101 if ((p = getenv("TMPDIR"))) {
102 return p;
104 return "/tmp";
107 /****************************************************************************
108 determine whether we are in the specified group
109 ****************************************************************************/
111 BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups)
113 int i;
115 if (group == current_gid) return(True);
117 for (i=0;i<ngroups;i++)
118 if (group == groups[i])
119 return(True);
121 return(False);
125 /****************************************************************************
126 like atoi but gets the value up to the separater character
127 ****************************************************************************/
128 char *Atoic(char *p, int *n, char *c)
130 if (!isdigit((int)*p))
132 DEBUG(5, ("Atoic: malformed number\n"));
133 return NULL;
136 (*n) = atoi(p);
138 while ((*p) && isdigit((int)*p))
140 p++;
143 if (strchr(c, *p) == NULL)
145 DEBUG(5, ("Atoic: no separator characters (%s) not found\n", c));
146 return NULL;
149 return p;
152 /*************************************************************************
153 reads a list of numbers
154 *************************************************************************/
155 char *get_numlist(char *p, uint32 **num, int *count)
157 int val;
159 if (num == NULL || count == NULL)
160 return NULL;
162 (*count) = 0;
163 (*num ) = NULL;
165 while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') {
166 uint32 *tn;
168 tn = Realloc((*num), ((*count)+1) * sizeof(uint32));
169 if (tn == NULL) {
170 if (*num)
171 free(*num);
172 return NULL;
173 } else
174 (*num) = tn;
175 (*num)[(*count)] = val;
176 (*count)++;
177 p++;
180 return p;
184 /*******************************************************************
185 check if a file exists - call vfs_file_exist for samba files
186 ********************************************************************/
187 BOOL file_exist(char *fname,SMB_STRUCT_STAT *sbuf)
189 SMB_STRUCT_STAT st;
190 if (!sbuf) sbuf = &st;
192 if (sys_stat(fname,sbuf) != 0)
193 return(False);
195 return(S_ISREG(sbuf->st_mode));
198 /*******************************************************************
199 rename a unix file
200 ********************************************************************/
201 int file_rename(char *from, char *to)
203 int rcode = rename (from, to);
205 if (errno == EXDEV)
207 /* Rename across filesystems needed. */
208 rcode = copy_reg (from, to);
210 return rcode;
213 /*******************************************************************
214 check a files mod time
215 ********************************************************************/
216 time_t file_modtime(char *fname)
218 SMB_STRUCT_STAT st;
220 if (sys_stat(fname,&st) != 0)
221 return(0);
223 return(st.st_mtime);
226 /*******************************************************************
227 check if a directory exists
228 ********************************************************************/
229 BOOL directory_exist(char *dname,SMB_STRUCT_STAT *st)
231 SMB_STRUCT_STAT st2;
232 BOOL ret;
234 if (!st) st = &st2;
236 if (sys_stat(dname,st) != 0)
237 return(False);
239 ret = S_ISDIR(st->st_mode);
240 if(!ret)
241 errno = ENOTDIR;
242 return ret;
245 /*******************************************************************
246 returns the size in bytes of the named file
247 ********************************************************************/
248 SMB_OFF_T get_file_size(char *file_name)
250 SMB_STRUCT_STAT buf;
251 buf.st_size = 0;
252 if(sys_stat(file_name,&buf) != 0)
253 return (SMB_OFF_T)-1;
254 return(buf.st_size);
257 /*******************************************************************
258 return a string representing an attribute for a file
259 ********************************************************************/
260 char *attrib_string(uint16 mode)
262 static fstring attrstr;
264 attrstr[0] = 0;
266 if (mode & aVOLID) fstrcat(attrstr,"V");
267 if (mode & aDIR) fstrcat(attrstr,"D");
268 if (mode & aARCH) fstrcat(attrstr,"A");
269 if (mode & aHIDDEN) fstrcat(attrstr,"H");
270 if (mode & aSYSTEM) fstrcat(attrstr,"S");
271 if (mode & aRONLY) fstrcat(attrstr,"R");
273 return(attrstr);
276 /*******************************************************************
277 show a smb message structure
278 ********************************************************************/
279 void show_msg(char *buf)
281 int i;
282 int bcc=0;
284 if (DEBUGLEVEL < 5) return;
286 DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
287 smb_len(buf),
288 (int)CVAL(buf,smb_com),
289 (int)CVAL(buf,smb_rcls),
290 (int)CVAL(buf,smb_reh),
291 (int)SVAL(buf,smb_err),
292 (int)CVAL(buf,smb_flg),
293 (int)SVAL(buf,smb_flg2)));
294 DEBUG(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\nsmt_wct=%d\n",
295 (int)SVAL(buf,smb_tid),
296 (int)SVAL(buf,smb_pid),
297 (int)SVAL(buf,smb_uid),
298 (int)SVAL(buf,smb_mid),
299 (int)CVAL(buf,smb_wct)));
301 for (i=0;i<(int)CVAL(buf,smb_wct);i++)
303 DEBUG(5,("smb_vwv[%d]=%d (0x%X)\n",i,
304 SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
307 bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
309 DEBUG(5,("smb_bcc=%d\n",bcc));
311 if (DEBUGLEVEL < 10) return;
313 if (DEBUGLEVEL < 50)
315 bcc = MIN(bcc, 512);
318 dump_data(10, smb_buf(buf), bcc);
321 /*******************************************************************
322 set the length and marker of an smb packet
323 ********************************************************************/
324 void smb_setlen(char *buf,int len)
326 _smb_setlen(buf,len);
328 CVAL(buf,4) = 0xFF;
329 CVAL(buf,5) = 'S';
330 CVAL(buf,6) = 'M';
331 CVAL(buf,7) = 'B';
334 /*******************************************************************
335 setup the word count and byte count for a smb message
336 ********************************************************************/
337 int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
339 if (zero)
340 memset(buf + smb_size,'\0',num_words*2 + num_bytes);
341 CVAL(buf,smb_wct) = num_words;
342 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
343 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
344 return (smb_size + num_words*2 + num_bytes);
347 /*******************************************************************
348 setup only the byte count for a smb message
349 ********************************************************************/
350 void set_message_bcc(char *buf,int num_bytes)
352 int num_words = CVAL(buf,smb_wct);
353 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
354 smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
357 /*******************************************************************
358 setup only the byte count for a smb message, using the end of the
359 message as a marker
360 ********************************************************************/
361 void set_message_end(void *outbuf,void *end_ptr)
363 set_message_bcc((char *)outbuf,PTR_DIFF(end_ptr,smb_buf((char *)outbuf)));
366 /*******************************************************************
367 reduce a file name, removing .. elements.
368 ********************************************************************/
369 void dos_clean_name(char *s)
371 char *p=NULL;
373 DEBUG(3,("dos_clean_name [%s]\n",s));
375 /* remove any double slashes */
376 all_string_sub(s, "\\\\", "\\", 0);
378 while ((p = strstr(s,"\\..\\")) != NULL)
380 pstring s1;
382 *p = 0;
383 pstrcpy(s1,p+3);
385 if ((p=strrchr(s,'\\')) != NULL)
386 *p = 0;
387 else
388 *s = 0;
389 pstrcat(s,s1);
392 trim_string(s,NULL,"\\..");
394 all_string_sub(s, "\\.\\", "\\", 0);
397 /*******************************************************************
398 reduce a file name, removing .. elements.
399 ********************************************************************/
400 void unix_clean_name(char *s)
402 char *p=NULL;
404 DEBUG(3,("unix_clean_name [%s]\n",s));
406 /* remove any double slashes */
407 all_string_sub(s, "//","/", 0);
409 /* Remove leading ./ characters */
410 if(strncmp(s, "./", 2) == 0) {
411 trim_string(s, "./", NULL);
412 if(*s == 0)
413 pstrcpy(s,"./");
416 while ((p = strstr(s,"/../")) != NULL)
418 pstring s1;
420 *p = 0;
421 pstrcpy(s1,p+3);
423 if ((p=strrchr(s,'/')) != NULL)
424 *p = 0;
425 else
426 *s = 0;
427 pstrcat(s,s1);
430 trim_string(s,NULL,"/..");
433 /****************************************************************************
434 make a dir struct
435 ****************************************************************************/
436 void make_dir_struct(char *buf,char *mask,char *fname,SMB_OFF_T size,int mode,time_t date)
438 char *p;
439 pstring mask2;
441 pstrcpy(mask2,mask);
443 if ((mode & aDIR) != 0)
444 size = 0;
446 memset(buf+1,' ',11);
447 if ((p = strchr(mask2,'.')) != NULL)
449 *p = 0;
450 memcpy(buf+1,mask2,MIN(strlen(mask2),8));
451 memcpy(buf+9,p+1,MIN(strlen(p+1),3));
452 *p = '.';
454 else
455 memcpy(buf+1,mask2,MIN(strlen(mask2),11));
457 memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
458 CVAL(buf,21) = mode;
459 put_dos_date(buf,22,date);
460 SSVAL(buf,26,size & 0xFFFF);
461 SSVAL(buf,28,(size >> 16)&0xFFFF);
462 StrnCpy(buf+30,fname,12);
463 if (!case_sensitive)
464 strupper(buf+30);
465 DEBUG(8,("put name [%s] into dir struct\n",buf+30));
469 /*******************************************************************
470 close the low 3 fd's and open dev/null in their place
471 ********************************************************************/
472 void close_low_fds(void)
474 int fd;
475 int i;
476 close(0); close(1);
477 #ifndef __INSURE__
478 close(2);
479 #endif
480 /* try and use up these file descriptors, so silly
481 library routines writing to stdout etc won't cause havoc */
482 for (i=0;i<3;i++) {
483 fd = sys_open("/dev/null",O_RDWR,0);
484 if (fd < 0) fd = sys_open("/dev/null",O_WRONLY,0);
485 if (fd < 0) {
486 DEBUG(0,("Can't open /dev/null\n"));
487 return;
489 if (fd != i) {
490 DEBUG(0,("Didn't get file descriptor %d\n",i));
491 return;
496 /****************************************************************************
497 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
498 else
499 if SYSV use O_NDELAY
500 if BSD use FNDELAY
501 ****************************************************************************/
502 int set_blocking(int fd, BOOL set)
504 int val;
505 #ifdef O_NONBLOCK
506 #define FLAG_TO_SET O_NONBLOCK
507 #else
508 #ifdef SYSV
509 #define FLAG_TO_SET O_NDELAY
510 #else /* BSD */
511 #define FLAG_TO_SET FNDELAY
512 #endif
513 #endif
515 if((val = fcntl(fd, F_GETFL, 0)) == -1)
516 return -1;
517 if(set) /* Turn blocking on - ie. clear nonblock flag */
518 val &= ~FLAG_TO_SET;
519 else
520 val |= FLAG_TO_SET;
521 return fcntl( fd, F_SETFL, val);
522 #undef FLAG_TO_SET
525 /****************************************************************************
526 transfer some data between two fd's
527 ****************************************************************************/
528 SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n,char *header,int headlen,int align)
530 static char *buf=NULL;
531 static int size=0;
532 char *buf1,*abuf;
533 SMB_OFF_T total = 0;
535 DEBUG(4,("transfer_file n=%.0f (head=%d) called\n",(double)n,headlen));
537 if (size == 0) {
538 size = lp_readsize();
539 size = MAX(size,1024);
542 while (!buf && size>0) {
543 buf = (char *)malloc(size+8);
544 if (!buf) size /= 2;
547 if (!buf) {
548 DEBUG(0,("Can't allocate transfer buffer!\n"));
549 exit(1);
552 abuf = buf + (align%8);
554 if (header)
555 n += headlen;
557 while (n > 0)
559 int s = (int)MIN(n,(SMB_OFF_T)size);
560 int ret,ret2=0;
562 ret = 0;
564 if (header && (headlen >= MIN(s,1024))) {
565 buf1 = header;
566 s = headlen;
567 ret = headlen;
568 headlen = 0;
569 header = NULL;
570 } else {
571 buf1 = abuf;
574 if (header && headlen > 0)
576 ret = MIN(headlen,size);
577 memcpy(buf1,header,ret);
578 headlen -= ret;
579 header += ret;
580 if (headlen <= 0) header = NULL;
583 if (s > ret)
584 ret += read(infd,buf1+ret,s-ret);
586 if (ret > 0)
588 ret2 = (outfd>=0?write_data(outfd,buf1,ret):ret);
589 if (ret2 > 0) total += ret2;
590 /* if we can't write then dump excess data */
591 if (ret2 != ret)
592 transfer_file(infd,-1,n-(ret+headlen),NULL,0,0);
594 if (ret <= 0 || ret2 != ret)
595 return(total);
596 n -= ret;
598 return(total);
602 /*******************************************************************
603 sleep for a specified number of milliseconds
604 ********************************************************************/
605 void msleep(int t)
607 int tdiff=0;
608 struct timeval tval,t1,t2;
609 fd_set fds;
611 GetTimeOfDay(&t1);
612 GetTimeOfDay(&t2);
614 while (tdiff < t) {
615 tval.tv_sec = (t-tdiff)/1000;
616 tval.tv_usec = 1000*((t-tdiff)%1000);
618 FD_ZERO(&fds);
619 errno = 0;
620 sys_select_intr(0,&fds,&tval);
622 GetTimeOfDay(&t2);
623 tdiff = TvalDiff(&t1,&t2);
628 /****************************************************************************
629 become a daemon, discarding the controlling terminal
630 ****************************************************************************/
631 void become_daemon(void)
633 if (sys_fork()) {
634 _exit(0);
637 /* detach from the terminal */
638 #ifdef HAVE_SETSID
639 setsid();
640 #elif defined(TIOCNOTTY)
642 int i = sys_open("/dev/tty", O_RDWR, 0);
643 if (i != -1) {
644 ioctl(i, (int) TIOCNOTTY, (char *)0);
645 close(i);
648 #endif /* HAVE_SETSID */
650 /* Close fd's 0,1,2. Needed if started by rsh */
651 close_low_fds();
655 /****************************************************************************
656 put up a yes/no prompt
657 ****************************************************************************/
658 BOOL yesno(char *p)
660 pstring ans;
661 printf("%s",p);
663 if (!fgets(ans,sizeof(ans)-1,stdin))
664 return(False);
666 if (*ans == 'y' || *ans == 'Y')
667 return(True);
669 return(False);
672 #ifdef HPUX
673 /****************************************************************************
674 this is a version of setbuffer() for those machines that only have setvbuf
675 ****************************************************************************/
676 void setbuffer(FILE *f,char *buf,int bufsize)
678 setvbuf(f,buf,_IOFBF,bufsize);
680 #endif
682 /****************************************************************************
683 expand a pointer to be a particular size
684 ****************************************************************************/
685 void *Realloc(void *p,size_t size)
687 void *ret=NULL;
689 if (size == 0) {
690 if (p) free(p);
691 DEBUG(5,("Realloc asked for 0 bytes\n"));
692 return NULL;
695 if (!p)
696 ret = (void *)malloc(size);
697 else
698 ret = (void *)realloc(p,size);
700 if (!ret)
701 DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
703 return(ret);
707 /****************************************************************************
708 free memory, checks for NULL
709 ****************************************************************************/
710 void safe_free(void *p)
712 if (p != NULL)
714 free(p);
719 /****************************************************************************
720 get my own name and IP
721 ****************************************************************************/
722 BOOL get_myname(char *my_name)
724 pstring hostname;
726 *hostname = 0;
728 /* get my host name */
729 if (gethostname(hostname, sizeof(hostname)) == -1) {
730 DEBUG(0,("gethostname failed\n"));
731 return False;
734 /* Ensure null termination. */
735 hostname[sizeof(hostname)-1] = '\0';
737 if (my_name) {
738 /* split off any parts after an initial . */
739 char *p = strchr(hostname,'.');
740 if (p) *p = 0;
742 fstrcpy(my_name,hostname);
745 return(True);
748 /****************************************************************************
749 interpret a protocol description string, with a default
750 ****************************************************************************/
751 int interpret_protocol(char *str,int def)
753 if (strequal(str,"NT1"))
754 return(PROTOCOL_NT1);
755 if (strequal(str,"LANMAN2"))
756 return(PROTOCOL_LANMAN2);
757 if (strequal(str,"LANMAN1"))
758 return(PROTOCOL_LANMAN1);
759 if (strequal(str,"CORE"))
760 return(PROTOCOL_CORE);
761 if (strequal(str,"COREPLUS"))
762 return(PROTOCOL_COREPLUS);
763 if (strequal(str,"CORE+"))
764 return(PROTOCOL_COREPLUS);
766 DEBUG(0,("Unrecognised protocol level %s\n",str));
768 return(def);
771 /****************************************************************************
772 Return true if a string could be a pure IP address.
773 ****************************************************************************/
775 BOOL is_ipaddress(const char *str)
777 BOOL pure_address = True;
778 int i;
780 for (i=0; pure_address && str[i]; i++)
781 if (!(isdigit((int)str[i]) || str[i] == '.'))
782 pure_address = False;
784 /* Check that a pure number is not misinterpreted as an IP */
785 pure_address = pure_address && (strchr(str, '.') != NULL);
787 return pure_address;
790 /****************************************************************************
791 interpret an internet address or name into an IP address in 4 byte form
792 ****************************************************************************/
794 uint32 interpret_addr(char *str)
796 struct hostent *hp;
797 uint32 res;
799 if (strcmp(str,"0.0.0.0") == 0) return(0);
800 if (strcmp(str,"255.255.255.255") == 0) return(0xFFFFFFFF);
802 /* if it's in the form of an IP address then get the lib to interpret it */
803 if (is_ipaddress(str)) {
804 res = inet_addr(str);
805 } else {
806 /* otherwise assume it's a network name of some sort and use
807 sys_gethostbyname */
808 if ((hp = sys_gethostbyname(str)) == 0) {
809 DEBUG(3,("sys_gethostbyname: Unknown host. %s\n",str));
810 return 0;
812 if(hp->h_addr == NULL) {
813 DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
814 return 0;
816 putip((char *)&res,(char *)hp->h_addr);
819 if (res == (uint32)-1) return(0);
821 return(res);
824 /*******************************************************************
825 a convenient addition to interpret_addr()
826 ******************************************************************/
827 struct in_addr *interpret_addr2(char *str)
829 static struct in_addr ret;
830 uint32 a = interpret_addr(str);
831 ret.s_addr = a;
832 return(&ret);
835 /*******************************************************************
836 check if an IP is the 0.0.0.0
837 ******************************************************************/
838 BOOL zero_ip(struct in_addr ip)
840 uint32 a;
841 putip((char *)&a,(char *)&ip);
842 return(a == 0);
846 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
847 /******************************************************************
848 Remove any mount options such as -rsize=2048,wsize=2048 etc.
849 Based on a fix from <Thomas.Hepper@icem.de>.
850 *******************************************************************/
852 static void strip_mount_options( pstring *str)
854 if (**str == '-')
856 char *p = *str;
857 while(*p && !isspace(*p))
858 p++;
859 while(*p && isspace(*p))
860 p++;
861 if(*p) {
862 pstring tmp_str;
864 pstrcpy(tmp_str, p);
865 pstrcpy(*str, tmp_str);
870 /*******************************************************************
871 Patch from jkf@soton.ac.uk
872 Split Luke's automount_server into YP lookup and string splitter
873 so can easily implement automount_path().
874 As we may end up doing both, cache the last YP result.
875 *******************************************************************/
877 #ifdef WITH_NISPLUS_HOME
878 char *automount_lookup(char *user_name)
880 static fstring last_key = "";
881 static pstring last_value = "";
883 char *nis_map = (char *)lp_nis_home_map_name();
885 char buffer[NIS_MAXATTRVAL + 1];
886 nis_result *result;
887 nis_object *object;
888 entry_obj *entry;
890 if (strcmp(user_name, last_key))
892 slprintf(buffer, sizeof(buffer)-1, "[key=%s],%s", user_name, nis_map);
893 DEBUG(5, ("NIS+ querystring: %s\n", buffer));
895 if (result = nis_list(buffer, FOLLOW_PATH|EXPAND_NAME|HARD_LOOKUP, NULL, NULL))
897 if (result->status != NIS_SUCCESS)
899 DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result->status)));
900 fstrcpy(last_key, ""); pstrcpy(last_value, "");
902 else
904 object = result->objects.objects_val;
905 if (object->zo_data.zo_type == ENTRY_OBJ)
907 entry = &object->zo_data.objdata_u.en_data;
908 DEBUG(5, ("NIS+ entry type: %s\n", entry->en_type));
909 DEBUG(3, ("NIS+ result: %s\n", entry->en_cols.en_cols_val[1].ec_value.ec_value_val));
911 pstrcpy(last_value, entry->en_cols.en_cols_val[1].ec_value.ec_value_val);
912 pstring_sub(last_value, "&", user_name);
913 fstrcpy(last_key, user_name);
917 nis_freeresult(result);
920 strip_mount_options(&last_value);
922 DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n", user_name, last_value));
923 return last_value;
925 #else /* WITH_NISPLUS_HOME */
926 char *automount_lookup(char *user_name)
928 static fstring last_key = "";
929 static pstring last_value = "";
931 int nis_error; /* returned by yp all functions */
932 char *nis_result; /* yp_match inits this */
933 int nis_result_len; /* and set this */
934 char *nis_domain; /* yp_get_default_domain inits this */
935 char *nis_map = (char *)lp_nis_home_map_name();
937 if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) {
938 DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
939 return last_value;
942 DEBUG(5, ("NIS Domain: %s\n", nis_domain));
944 if (!strcmp(user_name, last_key)) {
945 nis_result = last_value;
946 nis_result_len = strlen(last_value);
947 nis_error = 0;
949 } else {
951 if ((nis_error = yp_match(nis_domain, nis_map,
952 user_name, strlen(user_name),
953 &nis_result, &nis_result_len)) == 0) {
954 if (!nis_error && nis_result_len >= sizeof(pstring)) {
955 nis_result_len = sizeof(pstring)-1;
957 fstrcpy(last_key, user_name);
958 strncpy(last_value, nis_result, nis_result_len);
959 last_value[nis_result_len] = '\0';
960 strip_mount_options(&last_value);
962 } else if(nis_error == YPERR_KEY) {
964 /* If Key lookup fails user home server is not in nis_map
965 use default information for server, and home directory */
966 last_value[0] = 0;
967 DEBUG(3, ("YP Key not found: while looking up \"%s\" in map \"%s\"\n",
968 user_name, nis_map));
969 DEBUG(3, ("using defaults for server and home directory\n"));
970 } else {
971 DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n",
972 yperr_string(nis_error), user_name, nis_map));
977 DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name, last_value));
978 return last_value;
980 #endif /* WITH_NISPLUS_HOME */
981 #endif
984 /*******************************************************************
985 are two IPs on the same subnet?
986 ********************************************************************/
987 BOOL same_net(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
989 uint32 net1,net2,nmask;
991 nmask = ntohl(mask.s_addr);
992 net1 = ntohl(ip1.s_addr);
993 net2 = ntohl(ip2.s_addr);
995 return((net1 & nmask) == (net2 & nmask));
999 /****************************************************************************
1000 check if a process exists. Does this work on all unixes?
1001 ****************************************************************************/
1003 BOOL process_exists(pid_t pid)
1005 return(kill(pid,0) == 0 || errno != ESRCH);
1009 /*******************************************************************
1010 Convert a uid into a user name.
1011 ********************************************************************/
1013 char *uidtoname(uid_t uid)
1015 static fstring name;
1016 struct passwd *pass;
1018 if (winbind_uidtoname(name, uid))
1019 return name;
1021 pass = sys_getpwuid(uid);
1022 if (pass) return(pass->pw_name);
1023 slprintf(name, sizeof(name) - 1, "%d",(int)uid);
1024 return(name);
1028 /*******************************************************************
1029 Convert a gid into a group name.
1030 ********************************************************************/
1032 char *gidtoname(gid_t gid)
1034 static fstring name;
1035 struct group *grp;
1037 if (winbind_gidtoname(name, gid))
1038 return name;
1040 grp = getgrgid(gid);
1041 if (grp) return(grp->gr_name);
1042 slprintf(name,sizeof(name) - 1, "%d",(int)gid);
1043 return(name);
1046 /*******************************************************************
1047 Convert a user name into a uid. If winbindd is present uses this.
1048 ********************************************************************/
1050 uid_t nametouid(char *name)
1052 struct passwd *pass;
1053 char *p;
1054 uid_t u;
1056 u = (uid_t)strtol(name, &p, 0);
1057 if ((p != name) && (*p == '\0'))
1058 return u;
1060 if (winbind_nametouid(&u, name))
1061 return u;
1063 pass = sys_getpwnam(name);
1064 if (pass)
1065 return(pass->pw_uid);
1066 return (uid_t)-1;
1069 /*******************************************************************
1070 Convert a name to a gid_t if possible. Return -1 if not a group. If winbindd
1071 is present does a shortcut lookup...
1072 ********************************************************************/
1074 gid_t nametogid(char *name)
1076 struct group *grp;
1077 char *p;
1078 gid_t g;
1080 g = (gid_t)strtol(name, &p, 0);
1081 if ((p != name) && (*p == '\0'))
1082 return g;
1084 if (winbind_nametogid(&g, name))
1085 return g;
1087 grp = getgrnam(name);
1088 if (grp)
1089 return(grp->gr_gid);
1090 return (gid_t)-1;
1093 /*******************************************************************
1094 something really nasty happened - panic!
1095 ********************************************************************/
1096 void smb_panic(char *why)
1098 char *cmd = lp_panic_action();
1099 if (cmd && *cmd) {
1100 system(cmd);
1102 DEBUG(0,("PANIC: %s\n", why));
1103 dbgflush();
1104 abort();
1108 /*******************************************************************
1109 a readdir wrapper which just returns the file name
1110 ********************************************************************/
1111 char *readdirname(DIR *p)
1113 SMB_STRUCT_DIRENT *ptr;
1114 char *dname;
1116 if (!p) return(NULL);
1118 ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p);
1119 if (!ptr) return(NULL);
1121 dname = ptr->d_name;
1123 #ifdef NEXT2
1124 if (telldir(p) < 0) return(NULL);
1125 #endif
1127 #ifdef HAVE_BROKEN_READDIR
1128 /* using /usr/ucb/cc is BAD */
1129 dname = dname - 2;
1130 #endif
1133 static pstring buf;
1134 int len = NAMLEN(ptr);
1135 memcpy(buf, dname, len);
1136 buf[len] = 0;
1137 dname = buf;
1140 return(dname);
1143 /*******************************************************************
1144 Utility function used to decide if the last component
1145 of a path matches a (possibly wildcarded) entry in a namelist.
1146 ********************************************************************/
1148 BOOL is_in_path(char *name, name_compare_entry *namelist)
1150 pstring last_component;
1151 char *p;
1153 DEBUG(8, ("is_in_path: %s\n", name));
1155 /* if we have no list it's obviously not in the path */
1156 if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL)))
1158 DEBUG(8,("is_in_path: no name list.\n"));
1159 return False;
1162 /* Get the last component of the unix name. */
1163 p = strrchr(name, '/');
1164 strncpy(last_component, p ? ++p : name, sizeof(last_component)-1);
1165 last_component[sizeof(last_component)-1] = '\0';
1167 for(; namelist->name != NULL; namelist++)
1169 if(namelist->is_wild)
1171 if (mask_match(last_component, namelist->name, case_sensitive))
1173 DEBUG(8,("is_in_path: mask match succeeded\n"));
1174 return True;
1177 else
1179 if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
1180 (!case_sensitive && (StrCaseCmp(last_component, namelist->name) == 0)))
1182 DEBUG(8,("is_in_path: match succeeded\n"));
1183 return True;
1187 DEBUG(8,("is_in_path: match not found\n"));
1189 return False;
1192 /*******************************************************************
1193 Strip a '/' separated list into an array of
1194 name_compare_enties structures suitable for
1195 passing to is_in_path(). We do this for
1196 speed so we can pre-parse all the names in the list
1197 and don't do it for each call to is_in_path().
1198 namelist is modified here and is assumed to be
1199 a copy owned by the caller.
1200 We also check if the entry contains a wildcard to
1201 remove a potentially expensive call to mask_match
1202 if possible.
1203 ********************************************************************/
1205 void set_namearray(name_compare_entry **ppname_array, char *namelist)
1207 char *name_end;
1208 char *nameptr = namelist;
1209 int num_entries = 0;
1210 int i;
1212 (*ppname_array) = NULL;
1214 if((nameptr == NULL ) || ((nameptr != NULL) && (*nameptr == '\0')))
1215 return;
1217 /* We need to make two passes over the string. The
1218 first to count the number of elements, the second
1219 to split it.
1221 while(*nameptr)
1223 if ( *nameptr == '/' )
1225 /* cope with multiple (useless) /s) */
1226 nameptr++;
1227 continue;
1229 /* find the next / */
1230 name_end = strchr(nameptr, '/');
1232 /* oops - the last check for a / didn't find one. */
1233 if (name_end == NULL)
1234 break;
1236 /* next segment please */
1237 nameptr = name_end + 1;
1238 num_entries++;
1241 if(num_entries == 0)
1242 return;
1244 if(( (*ppname_array) = (name_compare_entry *)malloc(
1245 (num_entries + 1) * sizeof(name_compare_entry))) == NULL)
1247 DEBUG(0,("set_namearray: malloc fail\n"));
1248 return;
1251 /* Now copy out the names */
1252 nameptr = namelist;
1253 i = 0;
1254 while(*nameptr)
1256 if ( *nameptr == '/' )
1258 /* cope with multiple (useless) /s) */
1259 nameptr++;
1260 continue;
1262 /* find the next / */
1263 if ((name_end = strchr(nameptr, '/')) != NULL)
1265 *name_end = 0;
1268 /* oops - the last check for a / didn't find one. */
1269 if(name_end == NULL)
1270 break;
1272 (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
1273 if(((*ppname_array)[i].name = strdup(nameptr)) == NULL)
1275 DEBUG(0,("set_namearray: malloc fail (1)\n"));
1276 return;
1279 /* next segment please */
1280 nameptr = name_end + 1;
1281 i++;
1284 (*ppname_array)[i].name = NULL;
1286 return;
1289 /****************************************************************************
1290 routine to free a namearray.
1291 ****************************************************************************/
1293 void free_namearray(name_compare_entry *name_array)
1295 if(name_array == 0)
1296 return;
1298 if(name_array->name != NULL)
1299 free(name_array->name);
1301 free((char *)name_array);
1304 /****************************************************************************
1305 Simple routine to do POSIX file locking. Cruft in NFS and 64->32 bit mapping
1306 is dealt with in posix.c
1307 ****************************************************************************/
1309 BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
1311 SMB_STRUCT_FLOCK lock;
1312 int ret;
1314 DEBUG(8,("fcntl_lock %d %d %.0f %.0f %d\n",fd,op,(double)offset,(double)count,type));
1316 lock.l_type = type;
1317 lock.l_whence = SEEK_SET;
1318 lock.l_start = offset;
1319 lock.l_len = count;
1320 lock.l_pid = 0;
1322 errno = 0;
1324 ret = fcntl(fd,op,&lock);
1326 if (errno != 0)
1327 DEBUG(3,("fcntl_lock: fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
1329 /* a lock query */
1330 if (op == SMB_F_GETLK)
1332 if ((ret != -1) &&
1333 (lock.l_type != F_UNLCK) &&
1334 (lock.l_pid != 0) &&
1335 (lock.l_pid != sys_getpid()))
1337 DEBUG(3,("fcntl_lock: fd %d is locked by pid %d\n",fd,(int)lock.l_pid));
1338 return(True);
1341 /* it must be not locked or locked by me */
1342 return(False);
1345 /* a lock set or unset */
1346 if (ret == -1)
1348 DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
1349 (double)offset,(double)count,op,type,strerror(errno)));
1350 return(False);
1353 /* everything went OK */
1354 DEBUG(8,("fcntl_lock: Lock call successful\n"));
1356 return(True);
1359 /*******************************************************************
1360 is the name specified one of my netbios names
1361 returns true is it is equal, false otherwise
1362 ********************************************************************/
1363 BOOL is_myname(char *s)
1365 int n;
1366 BOOL ret = False;
1368 for (n=0; my_netbios_names[n]; n++) {
1369 if (strequal(my_netbios_names[n], s))
1370 ret=True;
1372 DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
1373 return(ret);
1376 /*******************************************************************
1377 set the horrid remote_arch string based on an enum.
1378 ********************************************************************/
1379 void set_remote_arch(enum remote_arch_types type)
1381 extern fstring remote_arch;
1382 ra_type = type;
1383 switch( type )
1385 case RA_WFWG:
1386 fstrcpy(remote_arch, "WfWg");
1387 return;
1388 case RA_OS2:
1389 fstrcpy(remote_arch, "OS2");
1390 return;
1391 case RA_WIN95:
1392 fstrcpy(remote_arch, "Win95");
1393 return;
1394 case RA_WINNT:
1395 fstrcpy(remote_arch, "WinNT");
1396 return;
1397 case RA_WIN2K:
1398 fstrcpy(remote_arch, "Win2K");
1399 return;
1400 case RA_SAMBA:
1401 fstrcpy(remote_arch,"Samba");
1402 return;
1403 default:
1404 ra_type = RA_UNKNOWN;
1405 fstrcpy(remote_arch, "UNKNOWN");
1406 break;
1410 /*******************************************************************
1411 Get the remote_arch type.
1412 ********************************************************************/
1413 enum remote_arch_types get_remote_arch(void)
1415 return ra_type;
1419 void out_ascii(FILE *f, unsigned char *buf,int len)
1421 int i;
1422 for (i=0;i<len;i++)
1424 fprintf(f, "%c", isprint(buf[i])?buf[i]:'.');
1428 void out_data(FILE *f,char *buf1,int len, int per_line)
1430 unsigned char *buf = (unsigned char *)buf1;
1431 int i=0;
1432 if (len<=0)
1434 return;
1437 fprintf(f, "[%03X] ",i);
1438 for (i=0;i<len;)
1440 fprintf(f, "%02X ",(int)buf[i]);
1441 i++;
1442 if (i%(per_line/2) == 0) fprintf(f, " ");
1443 if (i%per_line == 0)
1445 out_ascii(f,&buf[i-per_line ],per_line/2); fprintf(f, " ");
1446 out_ascii(f,&buf[i-per_line/2],per_line/2); fprintf(f, "\n");
1447 if (i<len) fprintf(f, "[%03X] ",i);
1450 if ((i%per_line) != 0)
1452 int n;
1454 n = per_line - (i%per_line);
1455 fprintf(f, " ");
1456 if (n>(per_line/2)) fprintf(f, " ");
1457 while (n--)
1459 fprintf(f, " ");
1461 n = MIN(per_line/2,i%per_line);
1462 out_ascii(f,&buf[i-(i%per_line)],n); fprintf(f, " ");
1463 n = (i%per_line) - n;
1464 if (n>0) out_ascii(f,&buf[i-n],n);
1465 fprintf(f, "\n");
1469 void print_asc(int level, unsigned char *buf,int len)
1471 int i;
1472 for (i=0;i<len;i++)
1473 DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
1476 void dump_data(int level,char *buf1,int len)
1478 unsigned char *buf = (unsigned char *)buf1;
1479 int i=0;
1480 if (len<=0) return;
1482 DEBUG(level,("[%03X] ",i));
1483 for (i=0;i<len;) {
1484 DEBUG(level,("%02X ",(int)buf[i]));
1485 i++;
1486 if (i%8 == 0) DEBUG(level,(" "));
1487 if (i%16 == 0) {
1488 print_asc(level,&buf[i-16],8); DEBUG(level,(" "));
1489 print_asc(level,&buf[i-8],8); DEBUG(level,("\n"));
1490 if (i<len) DEBUG(level,("[%03X] ",i));
1493 if (i%16) {
1494 int n;
1496 n = 16 - (i%16);
1497 DEBUG(level,(" "));
1498 if (n>8) DEBUG(level,(" "));
1499 while (n--) DEBUG(level,(" "));
1501 n = MIN(8,i%16);
1502 print_asc(level,&buf[i-(i%16)],n); DEBUG(level,(" "));
1503 n = (i%16) - n;
1504 if (n>0) print_asc(level,&buf[i-n],n);
1505 DEBUG(level,("\n"));
1509 char *tab_depth(int depth)
1511 static pstring spaces;
1512 memset(spaces, ' ', depth * 4);
1513 spaces[depth * 4] = 0;
1514 return spaces;
1517 /*****************************************************************************
1518 * Provide a checksum on a string
1520 * Input: s - the null-terminated character string for which the checksum
1521 * will be calculated.
1523 * Output: The checksum value calculated for s.
1525 * ****************************************************************************
1527 int str_checksum(const char *s)
1529 int res = 0;
1530 int c;
1531 int i=0;
1533 while(*s) {
1534 c = *s;
1535 res ^= (c << (i % 15)) ^ (c >> (15-(i%15)));
1536 s++;
1537 i++;
1539 return(res);
1540 } /* str_checksum */
1544 /*****************************************************************
1545 zero a memory area then free it. Used to catch bugs faster
1546 *****************************************************************/
1547 void zero_free(void *p, size_t size)
1549 memset(p, 0, size);
1550 free(p);
1554 /*****************************************************************
1555 set our open file limit to a requested max and return the limit
1556 *****************************************************************/
1557 int set_maxfiles(int requested_max)
1559 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
1560 struct rlimit rlp;
1561 int saved_current_limit;
1563 if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1564 DEBUG(0,("set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s\n",
1565 strerror(errno) ));
1566 /* just guess... */
1567 return requested_max;
1571 * Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
1572 * account for the extra fd we need
1573 * as well as the log files and standard
1574 * handles etc. Save the limit we want to set in case
1575 * we are running on an OS that doesn't support this limit (AIX)
1576 * which always returns RLIM_INFINITY for rlp.rlim_max.
1579 /* Try raising the hard (max) limit to the requested amount. */
1581 #if defined(RLIM_INFINITY)
1582 if (rlp.rlim_max != RLIM_INFINITY) {
1583 int orig_max = rlp.rlim_max;
1585 if ( rlp.rlim_max < requested_max )
1586 rlp.rlim_max = requested_max;
1588 /* This failing is not an error - many systems (Linux) don't
1589 support our default request of 10,000 open files. JRA. */
1591 if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1592 DEBUG(3,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d max files failed with error %s\n",
1593 (int)rlp.rlim_max, strerror(errno) ));
1595 /* Set failed - restore original value from get. */
1596 rlp.rlim_max = orig_max;
1599 #endif
1601 /* Now try setting the soft (current) limit. */
1603 saved_current_limit = rlp.rlim_cur = MIN(requested_max,rlp.rlim_max);
1605 if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1606 DEBUG(0,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s\n",
1607 (int)rlp.rlim_cur, strerror(errno) ));
1608 /* just guess... */
1609 return saved_current_limit;
1612 if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1613 DEBUG(0,("set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s\n",
1614 strerror(errno) ));
1615 /* just guess... */
1616 return saved_current_limit;
1619 #if defined(RLIM_INFINITY)
1620 if(rlp.rlim_cur == RLIM_INFINITY)
1621 return saved_current_limit;
1622 #endif
1624 if((int)rlp.rlim_cur > saved_current_limit)
1625 return saved_current_limit;
1627 return rlp.rlim_cur;
1628 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
1630 * No way to know - just guess...
1632 return requested_max;
1633 #endif
1636 /*****************************************************************
1637 splits out the start of the key (HKLM or HKU) and the rest of the key
1638 *****************************************************************/
1639 BOOL reg_split_key(char *full_keyname, uint32 *reg_type, char *key_name)
1641 pstring tmp;
1643 if (!next_token(&full_keyname, tmp, "\\", sizeof(tmp)))
1645 return False;
1648 (*reg_type) = 0;
1650 DEBUG(10, ("reg_split_key: hive %s\n", tmp));
1652 if (strequal(tmp, "HKLM") || strequal(tmp, "HKEY_LOCAL_MACHINE"))
1654 (*reg_type) = HKEY_LOCAL_MACHINE;
1656 else if (strequal(tmp, "HKU") || strequal(tmp, "HKEY_USERS"))
1658 (*reg_type) = HKEY_USERS;
1660 else
1662 DEBUG(10,("reg_split_key: unrecognised hive key %s\n", tmp));
1663 return False;
1666 if (next_token(NULL, tmp, "\n\r", sizeof(tmp)))
1668 fstrcpy(key_name, tmp);
1670 else
1672 key_name[0] = 0;
1675 DEBUG(10, ("reg_split_key: name %s\n", key_name));
1677 return True;
1681 /*****************************************************************
1682 possibly replace mkstemp if it is broken
1683 *****************************************************************/
1684 int smb_mkstemp(char *template)
1686 #if HAVE_SECURE_MKSTEMP
1687 return mkstemp(template);
1688 #else
1689 /* have a reasonable go at emulating it. Hope that
1690 the system mktemp() isn't completly hopeless */
1691 char *p = mktemp(template);
1692 if (!p) return -1;
1693 return open(p, O_CREAT|O_EXCL|O_RDWR, 0600);
1694 #endif
1697 /*****************************************************************
1698 like strdup but for memory
1699 *****************************************************************/
1700 void *memdup(void *p, size_t size)
1702 void *p2;
1703 if (size == 0) return NULL;
1704 p2 = malloc(size);
1705 if (!p2) return NULL;
1706 memcpy(p2, p, size);
1707 return p2;
1710 /*****************************************************************
1711 get local hostname and cache result
1712 *****************************************************************/
1713 char *myhostname(void)
1715 static pstring ret;
1716 if (ret[0] == 0) {
1717 get_myname(ret);
1719 return ret;
1723 /*****************************************************************
1724 a useful function for returning a path in the Samba lock directory
1725 *****************************************************************/
1726 char *lock_path(char *name)
1728 static pstring fname;
1730 pstrcpy(fname,lp_lockdir());
1731 trim_string(fname,"","/");
1733 if (!directory_exist(fname,NULL)) {
1734 mkdir(fname,0755);
1737 pstrcat(fname,"/");
1738 pstrcat(fname,name);
1740 return fname;
1743 /*******************************************************************
1744 Given a filename - get its directory name
1745 NB: Returned in static storage. Caveats:
1746 o Not safe in thread environment.
1747 o Caller must not free.
1748 o If caller wishes to preserve, they should copy.
1749 ********************************************************************/
1751 char *parent_dirname(const char *path)
1753 static pstring dirpath;
1754 char *p;
1756 if (!path)
1757 return(NULL);
1759 pstrcpy(dirpath, path);
1760 p = strrchr(dirpath, '/'); /* Find final '/', if any */
1761 if (!p) {
1762 pstrcpy(dirpath, "."); /* No final "/", so dir is "." */
1763 } else {
1764 if (p == dirpath)
1765 ++p; /* For root "/", leave "/" in place */
1766 *p = '\0';
1768 return dirpath;
1772 /*******************************************************************
1773 determine if a pattern contains any Microsoft wildcard characters
1774 *******************************************************************/
1775 BOOL ms_has_wild(char *s)
1777 char c;
1778 while ((c = *s++)) {
1779 switch (c) {
1780 case '*':
1781 case '?':
1782 case '<':
1783 case '>':
1784 case '"':
1785 return True;
1788 return False;
1791 /*******************************************************************
1792 a wrapper that handles case sensitivity and the special handling
1793 of the ".." name
1794 *******************************************************************/
1795 BOOL mask_match(char *string, char *pattern, BOOL is_case_sensitive)
1797 fstring p2, s2;
1798 if (strcmp(string,"..") == 0) string = ".";
1799 if (strcmp(pattern,".") == 0) return False;
1801 if (is_case_sensitive) {
1802 return ms_fnmatch(pattern, string) == 0;
1805 fstrcpy(p2, pattern);
1806 fstrcpy(s2, string);
1807 strlower(p2);
1808 strlower(s2);
1809 return ms_fnmatch(p2, s2) == 0;
1812 /*******************************************************************
1813 Simple case insensitive interface to ms_fnmatch.
1814 *******************************************************************/
1816 BOOL wild_match(char *string, char *pattern)
1818 pstring p2, s2;
1820 pstrcpy(p2, pattern);
1821 pstrcpy(s2, string);
1822 strlower(p2);
1823 strlower(s2);
1824 return ms_fnmatch(p2, s2) == 0;
1827 #ifdef __INSURE__
1829 /*******************************************************************
1830 This routine is a trick to immediately catch errors when debugging
1831 with insure. A xterm with a gdb is popped up when insure catches
1832 a error. It is Linux specific.
1833 ********************************************************************/
1834 int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6)
1836 static int (*fn)();
1837 int ret;
1838 char pidstr[10];
1839 pstring cmd = "/usr/X11R6/bin/xterm -display :0 -T Panic -n Panic -e /bin/sh -c 'cat /tmp/ierrs.*.%d ; gdb /proc/%d/exe %d'";
1841 slprintf(pidstr, sizeof(pidstr)-1, "%d", sys_getpid());
1842 pstring_sub(cmd, "%d", pidstr);
1844 if (!fn) {
1845 static void *h;
1846 h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY);
1847 fn = dlsym(h, "_Insure_trap_error");
1850 ret = fn(a1, a2, a3, a4, a5, a6);
1852 system(cmd);
1854 return ret;
1856 #endif