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