changes to make cmd compile (not link)
[reactos.git] / reactos / apps / utils / cmd / filecomp.c
1 /*
2 * FILECOMP.C - handles filename completion.
3 *
4 *
5 * Comments:
6 *
7 * 30-Jul-1998 (John P Price <linux-guru@gcfl.net>)
8 * moved from command.c file
9 * made second TAB display list of filename matches
10 * made filename be lower case if last character typed is lower case
11 *
12 * 25-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
13 * Cleanup. Unicode safe!
14 */
15
16 #include "config.h"
17
18 #include <windows.h>
19 #include <tchar.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23
24 #include "cmd.h"
25
26
27 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
28
29 VOID CompleteFilename (LPTSTR str, INT charcount)
30 {
31 WIN32_FIND_DATA file;
32 HANDLE hFile;
33 INT curplace = 0;
34 INT start;
35 INT count;
36 BOOL found_dot = FALSE;
37 BOOL perfectmatch = TRUE;
38 TCHAR path[MAX_PATH];
39 TCHAR fname[MAX_PATH];
40 TCHAR maxmatch[MAX_PATH] = _T("");
41 TCHAR directory[MAX_PATH];
42
43 /* expand current file name */
44 count = charcount - 1;
45 if (count < 0)
46 count = 0;
47
48 /* find front of word */
49 while (count > 0 && str[count] != _T(' '))
50 count--;
51
52 /* if not at beginning, go forward 1 */
53 if (str[count] == _T(' '))
54 count++;
55
56 start = count;
57
58 /* extract directory from word */
59 _tcscpy (directory, &str[start]);
60 curplace = _tcslen (directory) - 1;
61 while (curplace >= 0 && directory[curplace] != _T('\\') &&
62 directory[curplace] != _T(':'))
63 {
64 directory[curplace] = 0;
65 curplace--;
66 }
67
68 _tcscpy (path, &str[start]);
69
70 /* look for a '.' in the filename */
71 for (count = _tcslen (directory); path[count] != _T('\0'); count++)
72 {
73 if (path[count] == _T('.'))
74 {
75 found_dot = TRUE;
76 break;
77 }
78 }
79
80 if (found_dot)
81 _tcscat (path, _T("*"));
82 else
83 _tcscat (path, _T("*.*"));
84
85 /* current fname */
86 curplace = 0;
87
88 hFile = FindFirstFile (path, &file);
89 if (hFile != INVALID_HANDLE_VALUE)
90 {
91 /* find anything */
92 do
93 {
94 /* ignore "." and ".." */
95 if (!_tcscmp (file.cFileName, _T(".")) ||
96 !_tcscmp (file.cFileName, _T("..")))
97 continue;
98
99 _tcscpy (fname, file.cFileName);
100
101 if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
102 _tcscat (fname, _T("\\"));
103 else
104 _tcscat (fname, _T(" "));
105
106 if (!maxmatch[0] && perfectmatch)
107 {
108 _tcscpy(maxmatch, fname);
109 }
110 else
111 {
112 for (count = 0; maxmatch[count] && fname[count]; count++)
113 {
114 if (tolower(maxmatch[count]) != tolower(fname[count]))
115 {
116 perfectmatch = FALSE;
117 maxmatch[count] = 0;
118 break;
119 }
120 }
121 }
122 }
123 while (FindNextFile (hFile, &file));
124
125 FindClose (hFile);
126
127 _tcscpy (&str[start], directory);
128 _tcscat (&str[start], maxmatch);
129
130 if (!perfectmatch)
131 #ifdef __REACTOS__
132 Beep (440, 50);
133 #else
134 MessageBeep (-1);
135 #endif
136 }
137 else
138 {
139 /* no match found */
140 #ifdef __REACTOS__
141 Beep (440, 50);
142 #else
143 MessageBeep (-1);
144 #endif
145 }
146 }
147
148
149 /*
150 * returns 1 if at least one match, else returns 0
151 */
152
153 BOOL ShowCompletionMatches (LPTSTR str, INT charcount)
154 {
155 WIN32_FIND_DATA file;
156 HANDLE hFile;
157 BOOL found_dot = FALSE;
158 INT curplace = 0;
159 INT start;
160 INT count;
161 TCHAR path[MAX_PATH];
162 TCHAR fname[MAX_PATH];
163 TCHAR directory[MAX_PATH];
164
165 /* expand current file name */
166 count = charcount - 1;
167 if (count < 0)
168 count = 0;
169
170 /* find front of word */
171 while (count > 0 && str[count] != _T(' '))
172 count--;
173
174 /* if not at beginning, go forward 1 */
175 if (str[count] == _T(' '))
176 count++;
177
178 start = count;
179
180 /* extract directory from word */
181 _tcscpy (directory, &str[start]);
182 curplace = _tcslen (directory) - 1;
183 while (curplace >= 0 &&
184 directory[curplace] != _T('\\') &&
185 directory[curplace] != _T(':'))
186 {
187 directory[curplace] = 0;
188 curplace--;
189 }
190
191 _tcscpy (path, &str[start]);
192
193 /* look for a . in the filename */
194 for (count = _tcslen (directory); path[count] != _T('\0'); count++)
195 {
196 if (path[count] == _T('.'))
197 {
198 found_dot = TRUE;
199 break;
200 }
201 }
202
203 if (found_dot)
204 _tcscat (path, _T("*"));
205 else
206 _tcscat (path, _T("*.*"));
207
208 /* current fname */
209 curplace = 0;
210
211 hFile = FindFirstFile (path, &file);
212 if (hFile != INVALID_HANDLE_VALUE)
213 {
214 /* find anything */
215 ConOutChar (_T('\n'));
216 count = 0;
217 do
218 {
219 /* ignore . and .. */
220 if (!_tcscmp (file.cFileName, _T(".")) ||
221 !_tcscmp (file.cFileName, _T("..")))
222 continue;
223
224 if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
225 wsprintf (fname,"[%s]", file.cFileName);
226 else
227 _tcscpy (fname, file.cFileName);
228
229 ConOutPrintf (_T("%-14s"), fname);
230 if (++count == 5)
231 {
232 ConOutChar (_T('\n'));
233 count = 0;
234 }
235 }
236 while (FindNextFile (hFile, &file));
237
238 FindClose (hFile);
239
240 if (count)
241 ConOutChar (_T('\n'));
242 }
243 else
244 {
245 /* no match found */
246 #ifdef __REACTOS__
247 Beep (440, 50);
248 #else
249 MessageBeep (-1);
250 #endif
251 return FALSE;
252 }
253
254 return TRUE;
255 }
256 #endif
257
258 #ifdef FEATURE_4NT_FILENAME_COMPLETION
259
260 //static VOID BuildFilenameMatchList (...)
261
262 // VOID CompleteFilenameNext (LPTSTR, INT)
263 // VOID CompleteFilenamePrev (LPTSTR, INT)
264
265 // VOID RemoveFilenameMatchList (VOID)
266
267 #endif