ReactOS 0.4.16-dev-2610-ge2c92c0
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
60 _tcscpy(TempPath, _T(""));
61 _tcscpy(OutPath, _T(""));
62
63 for (i = 0; i < _tcslen(Path); i++)
64 {
65 if (Path[i] != _T('\\'))
66 {
67 _tcsncat(TempPath, &Path[i], 1);
68 if (i != _tcslen(Path) - 1)
69 continue;
70 }
71 /* Handle the base part of the path different.
72 Because if you put it into findfirstfile, it will
73 return your current folder */
74 if (_tcslen(TempPath) == 2 && TempPath[1] == _T(':'))
75 {
76 _tcscat(OutPath, TempPath);
77 _tcscat(OutPath, _T("\\"));
78 _tcscat(TempPath, _T("\\"));
79 }
80 else
81 {
82 hFind = FindFirstFile(TempPath,&FindFileData);
83 if (hFind == INVALID_HANDLE_VALUE)
84 {
85 _tcscpy(OutPath, Path);
86 return;
87 }
88 _tcscat(TempPath, _T("\\"));
89 _tcscat(OutPath, FindFileData.cFileName);
90 _tcscat(OutPath, _T("\\"));
91 FindClose(hFind);
92 }
93 }
94}
95
96/*
97 * Checks if a file exists (is accessible)
98 */
100{
101 DWORD attr = GetFileAttributes(pszPath);
103}
104
106{
107 DWORD attr = GetFileAttributes(pszPath);
109}
110
112{
113 TCHAR szMsg[4];
114// TCHAR cKey = 0;
115// LPTSTR szKeys = _T("yna");
116
117 TCHAR szIn[10];
118 LPTSTR p;
119
120 if (resID != 0)
121 ConOutResPrintf(resID);
122
123 /* preliminary fix */
124 ConInString(szIn, 10);
125
126 _tcsupr(szIn);
127 for (p = szIn; _istspace(*p); p++)
128 ;
129
131
132 if (_tcsncmp(p, &szMsg[0], 1) == 0)
133 return PROMPT_YES;
134 else if (_tcsncmp(p, &szMsg[1], 1) == 0)
135 return PROMPT_NO;
136 else if (_tcsncmp(p, &szMsg[2], 1) == 0)
137 return PROMPT_ALL;
138#if 0
139 else if (*p == _T('\03'))
140 return PROMPT_BREAK;
141#endif
142
143 return PROMPT_NO;
144
145 /* unfinished solution */
146#if 0
148 ConInDisable();
149
150 do
151 {
152 ConInKey(&ir);
153 cKey = _totlower(ir.Event.KeyEvent.uChar.AsciiChar);
154 if (_tcschr(szKeys, cKey[0]) == NULL)
155 cKey = 0;
156 }
157 while ((ir.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT) ||
158 (ir.Event.KeyEvent.wVirtualKeyCode == VK_MENU) ||
159 (ir.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL));
160
162 ConInEnable();
163
164 if ((ir.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) ||
165 ((ir.Event.KeyEvent.wVirtualKeyCode == _T('C')) &&
166 (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))))
167 return PROMPT_BREAK;
168
169 return PROMPT_YES;
170#endif
171}
172
174{
175 DWORD dwOldMode;
176 DWORD dwRead = 0;
178
179 LPTSTR p;
180 PCHAR pBuf;
181
182#ifdef _UNICODE
183 pBuf = (PCHAR)malloc(dwLength - 1);
184#else
185 pBuf = lpInput;
186#endif
187 ZeroMemory(lpInput, dwLength * sizeof(TCHAR));
189 GetConsoleMode(hFile, &dwOldMode);
190
192
193 ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
194
195#ifdef _UNICODE
196 MultiByteToWideChar(GetConsoleCP(), 0, pBuf, dwRead, lpInput, dwLength - 1);
197 free(pBuf);
198#endif
199 for (p = lpInput; *p; p++)
200 {
201 if (*p == _T('\r')) // Terminate at the carriage-return.
202 {
203 *p = _T('\0');
204 break;
205 }
206 }
207
208 SetConsoleMode(hFile, dwOldMode);
209}
210
212{
214}
215
217{
218 INT Len;
219 va_list arg_ptr;
220
221 va_start(arg_ptr, MessageId);
224 NULL,
225 MessageId,
227 &arg_ptr);
228 va_end(arg_ptr);
229
230 if (Len <= 0)
232}
233
235{
236 ConWrite(StdOut, &c, 1);
237}
238
239/*
240 * get a character out-of-band and honor Ctrl-Break characters
241 */
242TCHAR
244{
246 INPUT_RECORD irBuffer;
247 DWORD dwRead;
248
249 do
250 {
251 ReadConsoleInput(hInput, &irBuffer, 1, &dwRead);
252 if ((irBuffer.EventType == KEY_EVENT) &&
253 (irBuffer.Event.KeyEvent.bKeyDown != FALSE))
254 {
255 if (irBuffer.Event.KeyEvent.dwControlKeyState &
257 {
258 if (irBuffer.Event.KeyEvent.wVirtualKeyCode == 'C')
259 {
261 break;
262 }
263 }
264 else if ((irBuffer.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT) ||
265 (irBuffer.Event.KeyEvent.wVirtualKeyCode == VK_MENU) ||
267 {
268 // Nothing to do
269 }
270 else
271 {
272 break;
273 }
274 }
275 }
276 while (TRUE);
277
278#ifndef _UNICODE
279 return irBuffer.Event.KeyEvent.uChar.AsciiChar;
280#else
281 return irBuffer.Event.KeyEvent.uChar.UnicodeChar;
282#endif /* _UNICODE */
283}
PRTL_UNICODE_STRING_BUFFER Path
#define STRING_CONSOLE_ERROR
Definition: resource.h:6
#define STRING_COPY_OPTION
Definition: resource.h:24
#define STRING_ERROR_D_PAUSEMSG
Definition: resource.h:29
VOID ConOutChar(TCHAR c)
Definition: util.c:234
VOID __cdecl ConFormatMessage(PCON_STREAM Stream, DWORD MessageId,...)
Definition: util.c:216
BOOL IsExistingDirectory(IN LPCTSTR pszPath)
Definition: util.c:105
BOOL IsExistingFile(IN LPCTSTR pszPath)
Definition: util.c:99
INT GetRootPath(IN LPCTSTR InPath, OUT LPTSTR OutPath, IN INT size)
Definition: util.c:21
TCHAR cgetchar(VOID)
Definition: util.c:243
VOID msg_pause(VOID)
Definition: util.c:211
VOID ConInString(LPTSTR lpInput, DWORD dwLength)
Definition: util.c:173
INT FilePromptYNA(UINT resID)
Definition: util.c:111
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 StdOut
Definition: conutils_noros.h:6
void ConResPrintf(FILE *fp, UINT nID,...)
#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:1571
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1608
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(void)
Definition: console.c:821
#define __cdecl
Definition: corecrt.h:121
#define va_end(v)
Definition: stdarg.h:28
#define va_start(v, l)
Definition: stdarg.h:26
char * va_list
Definition: vadefs.h:50
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
char TCHAR
Definition: tchar.h:1402
#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 _tcslen
Definition: tchar.h:626
#define _tcschr
Definition: tchar.h:1406
#define _totlower
Definition: tchar.h:1511
#define ZeroMemory
Definition: minwinbase.h:31
#define PCHAR
Definition: match.c:90
static IStream Stream
Definition: htmldoc.c:1115
LPSTR LPTSTR
Definition: ms-dtyp.idl:131
LPCSTR LPCTSTR
Definition: ms-dtyp.idl:130
_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:84
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:1028
#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
#define LoadStringW
Definition: utils.h:64
union _INPUT_RECORD::@3521 Event
WORD EventType
Definition: wincon.h:294
KEY_EVENT_RECORD KeyEvent
Definition: wincon.h:296
union _KEY_EVENT_RECORD::@3520 uChar
DWORD dwControlKeyState
Definition: wincon.h:269
WORD wVirtualKeyCode
Definition: wincon.h:263
WCHAR UnicodeChar
Definition: wincon.h:266
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 STD_INPUT_HANDLE
Definition: winbase.h:291
#define GetFileAttributes
Definition: winbase.h:3564
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:400
#define FindFirstFile
Definition: winbase.h:3531
#define GetCurrentDirectory
Definition: winbase.h:3554
#define LEFT_CTRL_PRESSED
Definition: wincon.h:168
#define ReadConsoleInput
Definition: wincon.h:1024
#define ENABLE_ECHO_INPUT
Definition: wincon.h:109
#define KEY_EVENT
Definition: wincon.h:156
#define RIGHT_CTRL_PRESSED
Definition: wincon.h:167
#define ENABLE_LINE_INPUT
Definition: wincon.h:108
#define ENABLE_PROCESSED_INPUT
Definition: wincon.h:107
#define VK_CONTROL
Definition: winuser.h:2239
#define VK_SHIFT
Definition: winuser.h:2238
#define VK_ESCAPE
Definition: winuser.h:2250
#define VK_MENU
Definition: winuser.h:2240