13b3dad0bf70e9048877deb460746ac10714c7d6
[samba.git] / source / libcli / util / cliutil.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client utility routines
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) James Myers 2003 <myersjj@samba.org>
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23 /*******************************************************************
24  Functions nicked from lib/util.c needed by client.
25 *******************************************************************/
26
27 /* a default finfo structure to ensure all fields are sensible */
28 file_info def_finfo = {-1,0,0,0,0,0,0,"",""};
29
30 /*******************************************************************
31  A wrapper that handles case sensitivity and the special handling
32  of the ".." name.
33 *******************************************************************/
34
35 BOOL mask_match(struct cli_state *cli, const char *string, char *pattern, BOOL is_case_sensitive)
36 {
37         fstring p2, s2;
38
39         if (strcmp(string,"..") == 0)
40                 string = ".";
41         if (strcmp(pattern,".") == 0)
42                 return False;
43         
44         if (is_case_sensitive)
45                 return ms_fnmatch(pattern, string, 
46                                   cli->transport->negotiate.protocol) == 0;
47
48         fstrcpy(p2, pattern);
49         fstrcpy(s2, string);
50         strlower(p2); 
51         strlower(s2);
52         return ms_fnmatch(p2, s2, cli->transport->negotiate.protocol) == 0;
53 }
54
55 /****************************************************************************
56  Put up a yes/no prompt.
57 ****************************************************************************/
58
59 BOOL yesno(char *p)
60 {
61         pstring ans;
62         printf("%s",p);
63
64         if (!fgets(ans,sizeof(ans)-1,stdin))
65                 return(False);
66
67         if (*ans == 'y' || *ans == 'Y')
68                 return(True);
69
70         return(False);
71 }
72
73 /*******************************************************************
74   A readdir wrapper which just returns the file name.
75  ********************************************************************/
76
77 const char *readdirname(DIR *p)
78 {
79         struct smb_dirent *ptr;
80         char *dname;
81
82         if (!p)
83                 return(NULL);
84   
85         ptr = (struct smb_dirent *)sys_readdir(p);
86         if (!ptr)
87                 return(NULL);
88
89         dname = ptr->d_name;
90
91 #ifdef NEXT2
92         if (telldir(p) < 0)
93                 return(NULL);
94 #endif
95
96 #ifdef HAVE_BROKEN_READDIR
97         /* using /usr/ucb/cc is BAD */
98         dname = dname - 2;
99 #endif
100
101         {
102                 static pstring buf;
103                 int len = NAMLEN(ptr);
104                 memcpy(buf, dname, len);
105                 buf[len] = 0;
106                 dname = buf;
107         }
108
109         return(dname);
110 }