ReactOS 0.4.16-dev-197-g92996da
util.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Replace Command
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Internal helpers. See cmd/internal.c
5 * COPYRIGHT: Copyright Samuel Erdtman (samuel@erdtman.se)
6 * COPYRIGHT: Copyright 2024 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
7 */
8
9#include "replace.h"
10
11BOOL bCtrlBreak = FALSE; /* Ctrl-Break or Ctrl-C hit */
12
13/*
14 * Helper function for getting the current path from drive
15 * without changing the drive. Return code: 0 = ok, 1 = fail.
16 * 'InPath' can have any size; if the two first letters are
17 * not a drive with ':' it will get the current path on
18 * the current drive exactly as GetCurrentDirectory() does.
19 */
20INT
22 IN LPCTSTR InPath,
23 OUT LPTSTR OutPath,
24 IN INT size)
25{
26 if (InPath[0] && InPath[1] == _T(':'))
27 {
28 INT t = 0;
29
30 if ((InPath[0] >= _T('0')) && (InPath[0] <= _T('9')))
31 {
32 t = (InPath[0] - _T('0')) + 28;
33 }
34 else if ((InPath[0] >= _T('a')) && (InPath[0] <= _T('z')))
35 {
36 t = (InPath[0] - _T('a')) + 1;
37 }
38 else if ((InPath[0] >= _T('A')) && (InPath[0] <= _T('Z')))
39 {
40 t = (InPath[0] - _T('A')) + 1;
41 }
42
43 return (_tgetdcwd(t, OutPath, size) == NULL);
44 }
45
46 /* Get current directory */
47 return !GetCurrentDirectory(size, OutPath);
48}
49
50/*
51 * Takes a path in and returns it with the correct case of the letters
52 */
54{
55 UINT i = 0;
56 TCHAR TempPath[MAX_PATH];
57 WIN32_FIND_DATA FindFileData;
58 HANDLE hFind;
59 _tcscpy(TempPath, _T(""));
60 _tcscpy(OutPath, _T(""));
61
62 for(i = 0; i < _tcslen(Path); i++)
63 {
64 if (Path[i] != _T('\\'))
65 {
66 _tcsncat(TempPath, &Path[i], 1);
67 if (i != _tcslen(Path) - 1)
68 continue;
69 }
70 /* Handle the base part of the path different.
71 Because if you put it into findfirstfile, it will
72 return your current folder */
73 if (_tcslen(TempPath) == 2 && TempPath[1] == _T(':'))
74 {
75 _tcscat(OutPath, TempPath);
76 _tcscat(OutPath, _T("\\"));
77 _tcscat(TempPath, _T("\\"));
78 }
79 else
80 {
81 hFind = FindFirstFile(TempPath,&FindFileData);
82 if (hFind == INVALID_HANDLE_VALUE)
83 {
84 _tcscpy(OutPath, Path);
85 return;
86 }
87 _tcscat(TempPath, _T("\\"));
88 _tcscat(OutPath, FindFileData.cFileName);
89 _tcscat(OutPath, _T("\\"));
90 FindClose(hFind);
91 }
92 }
93}
94
95/*
96 * Checks if a file exists (is accessible)
97 */
99{
100 DWORD attr = GetFileAttributes(pszPath);
102}
103
105{
106 DWORD attr = GetFileAttributes(pszPath);
108}
109
111{
113// TCHAR cKey = 0;
114// LPTSTR szKeys = _T("yna");
115
116 TCHAR szIn[10];
117 LPTSTR p;
118
119 if (resID != 0)
120 ConOutResPrintf (resID);
121
122 /* preliminary fix */
123 ConInString(szIn, 10);
124
125 _tcsupr (szIn);
126 for (p = szIn; _istspace (*p); p++)
127 ;
128
130
131 if (_tcsncmp(p, &szMsg[0], 1) == 0)
132 return PROMPT_YES;
133 else if (_tcsncmp(p, &szMsg[1], 1) == 0)
134 return PROMPT_NO;
135 else if (_tcsncmp(p, &szMsg[2], 1) == 0)
136 return PROMPT_ALL;
137#if 0
138 else if (*p == _T('\03'))
139 return PROMPT_BREAK;
140#endif
141
142 return PROMPT_NO;
143
144 /* unfinished solution */
145#if 0
147 ConInDisable();
148
149 do
150 {
151 ConInKey (&ir);
152 cKey = _totlower (ir.Event.KeyEvent.uChar.AsciiChar);
153 if (_tcschr (szKeys, cKey[0]) == NULL)
154 cKey = 0;
155 }
156 while ((ir.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT) ||
157 (ir.Event.KeyEvent.wVirtualKeyCode == VK_MENU) ||
158 (ir.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL));
159
161 ConInEnable();
162
163 if ((ir.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) ||
164 ((ir.Event.KeyEvent.wVirtualKeyCode == _T('C')) &&
165 (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))))
166 return PROMPT_BREAK;
167
168 return PROMPT_YES;
169#endif
170}
171
173{
174 DWORD dwOldMode;
175 DWORD dwRead = 0;
177
178 LPTSTR p;
179 PCHAR pBuf;
180
181#ifdef _UNICODE
182 pBuf = (PCHAR)malloc(dwLength - 1);
183#else
184 pBuf = lpInput;
185#endif
186 ZeroMemory(lpInput, dwLength * sizeof(TCHAR));
188 GetConsoleMode(hFile, &dwOldMode);
189
191
192 ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
193
194#ifdef _UNICODE
195 MultiByteToWideChar(GetConsoleCP(), 0, pBuf, dwRead, lpInput, dwLength - 1);
196 free(pBuf);
197#endif
198 for (p = lpInput; *p; p++)
199 {
200 if (*p == _T('\r')) // Terminate at the carriage-return.
201 {
202 *p = _T('\0');
203 break;
204 }
205 }
206
207 SetConsoleMode(hFile, dwOldMode);
208}
209
211{
213}
214
216{
217 INT Len;
218 va_list arg_ptr;
219
220 va_start(arg_ptr, MessageId);
223 NULL,
224 MessageId,
226 &arg_ptr);
227 va_end(arg_ptr);
228
229 if (Len <= 0)
231}
232
234{
235 ConWrite(StdOut, &c, 1);
236}
237
238/*
239 * get a character out-of-band and honor Ctrl-Break characters
240 */
241TCHAR
243{
245 INPUT_RECORD irBuffer;
246 DWORD dwRead;
247
248 do
249 {
250 ReadConsoleInput (hInput, &irBuffer, 1, &dwRead);
251 if ((irBuffer.EventType == KEY_EVENT) &&
252 (irBuffer.Event.KeyEvent.bKeyDown != FALSE))
253 {
254 if (irBuffer.Event.KeyEvent.dwControlKeyState &
256 {
257 if (irBuffer.Event.KeyEvent.wVirtualKeyCode == 'C')
258 {
260 break;
261 }
262 }
263 else if ((irBuffer.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT) ||
264 (irBuffer.Event.KeyEvent.wVirtualKeyCode == VK_MENU) ||
266 {
267 // Nothing to do
268 }
269 else
270 {
271 break;
272 }
273 }
274 }
275 while (TRUE);
276
277#ifndef _UNICODE
278 return irBuffer.Event.KeyEvent.uChar.AsciiChar;
279#else
280 return irBuffer.Event.KeyEvent.uChar.UnicodeChar;
281#endif /* _UNICODE */
282}
PRTL_UNICODE_STRING_BUFFER Path
#define __cdecl
Definition: accygwin.h:79
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define RC_STRING_MAX_SIZE
Definition: resource.h:3
#define STRING_CONSOLE_ERROR
Definition: resource.h:6
#define StdOut
Definition: fc.c:14
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
#define STRING_COPY_OPTION
Definition: resource.h:22
#define STRING_ERROR_D_PAUSEMSG
Definition: resource.h:27
VOID ConOutChar(TCHAR c)
Definition: util.c:233
VOID __cdecl ConFormatMessage(PCON_STREAM Stream, DWORD MessageId,...)
Definition: util.c:215
BOOL IsExistingDirectory(IN LPCTSTR pszPath)
Definition: util.c:104
BOOL IsExistingFile(IN LPCTSTR pszPath)
Definition: util.c:98
INT GetRootPath(IN LPCTSTR InPath, OUT LPTSTR OutPath, IN INT size)
Definition: util.c:21
TCHAR cgetchar(VOID)
Definition: util.c:242
VOID msg_pause(VOID)
Definition: util.c:210
VOID ConInString(LPTSTR lpInput, DWORD dwLength)
Definition: util.c:172
INT FilePromptYNA(UINT resID)
Definition: util.c:110
BOOL bCtrlBreak
Definition: util.c:11
VOID GetPathCase(TCHAR *Path, TCHAR *OutPath)
Definition: util.c:53
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
VOID AddBreakHandler(VOID)
Definition: cmd.c:1844
VOID RemoveBreakHandler(VOID)
Definition: cmd.c:1850
VOID ConInDisable(VOID)
Definition: console.c:36
VOID ConInEnable(VOID)
Definition: console.c:46
VOID ConInKey(PINPUT_RECORD lpBuffer)
Definition: console.c:61
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define Len
Definition: deflate.h:82
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
#define MultiByteToWideChar
Definition: compat.h:110
static DWORD DWORD * dwLength
Definition: fusion.c:86
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1569
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1606
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(VOID)
Definition: console.c:2391
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLdouble GLdouble t
Definition: gl.h:2047
GLsizeiptr size
Definition: glext.h:5919
const GLubyte * c
Definition: glext.h:8905
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define _istspace
Definition: tchar.h:1504
#define _tcscat
Definition: tchar.h:622
#define _tcscpy
Definition: tchar.h:623
#define _tcsupr
Definition: tchar.h:1467
#define _tcsncmp
Definition: tchar.h:1428
#define _tgetdcwd
Definition: tchar.h:674
#define _tcsncat
Definition: tchar.h:1408
#define _tcschr
Definition: tchar.h:1406
#define _totlower
Definition: tchar.h:1511
#define PCHAR
Definition: match.c:90
static IStream Stream
Definition: htmldoc.c:1115
_In_ HANDLE hFile
Definition: mswsock.h:90
unsigned int UINT
Definition: ndis.h:50
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
INT __stdcall ConWrite(IN PCON_STREAM Stream, IN PCTCH szStr, IN DWORD len)
Definition: outstream.c:85
INT ConMsgPrintfV(IN PCON_STREAM Stream, IN DWORD dwFlags, IN LPCVOID lpSource OPTIONAL, IN DWORD dwMessageId, IN DWORD dwLanguageId, IN va_list *Arguments OPTIONAL)
Definition: outstream.c:1030
#define ConOutResPrintf(uID,...)
Definition: replace.h:31
#define PROMPT_YES
Definition: replace.h:21
#define ConOutResPuts(uID)
Definition: replace.h:28
#define PROMPT_NO
Definition: replace.h:20
#define PROMPT_ALL
Definition: replace.h:22
#define PROMPT_BREAK
Definition: replace.h:23
union _INPUT_RECORD::@3282 Event
WORD EventType
Definition: wincon.h:273
KEY_EVENT_RECORD KeyEvent
Definition: wincon.h:275
DWORD dwControlKeyState
Definition: wincon.h:248
WORD wVirtualKeyCode
Definition: wincon.h:242
union _KEY_EVENT_RECORD::@3281 uChar
WCHAR UnicodeChar
Definition: wincon.h:245
Definition: cookie.c:202
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50
int32_t INT
Definition: typedefs.h:58
#define IN
Definition: typedefs.h:39
#define OUT
Definition: typedefs.h:40
char * PCHAR
Definition: typedefs.h:51
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
#define _T(x)
Definition: vfdio.h:22
#define ZeroMemory
Definition: winbase.h:1736
#define STD_INPUT_HANDLE
Definition: winbase.h:292
#define GetFileAttributes
Definition: winbase.h:3839
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:448
#define FindFirstFile
Definition: winbase.h:3806
#define GetCurrentDirectory
Definition: winbase.h:3829
#define LEFT_CTRL_PRESSED
Definition: wincon.h:140
#define ReadConsoleInput
Definition: wincon.h:778
#define ENABLE_ECHO_INPUT
Definition: wincon.h:80
#define KEY_EVENT
Definition: wincon.h:128
#define RIGHT_CTRL_PRESSED
Definition: wincon.h:139
#define ENABLE_LINE_INPUT
Definition: wincon.h:79
#define ENABLE_PROCESSED_INPUT
Definition: wincon.h:78
#define VK_CONTROL
Definition: winuser.h:2206
#define LoadString
Definition: winuser.h:5831
#define VK_SHIFT
Definition: winuser.h:2205
#define VK_ESCAPE
Definition: winuser.h:2217
#define VK_MENU
Definition: winuser.h:2207
char TCHAR
Definition: xmlstorage.h:189
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define _tcslen
Definition: xmlstorage.h:198