ReactOS 0.4.16-dev-122-g325d74c
type.c File Reference
#include "precomp.h"
Include dependency graph for type.c:

Go to the source code of this file.

Functions

static BOOL FileGetString (IN HANDLE hFile, OUT LPTSTR lpBuffer, IN LONG nBufferLength)
 
static BOOL DoTypeFile (IN LPTSTR FileName, IN HANDLE hConsoleOut, IN BOOL bNoFileName, IN BOOL bPaging)
 
INT cmd_type (LPTSTR param)
 

Function Documentation

◆ cmd_type()

INT cmd_type ( LPTSTR  param)

Definition at line 221 of file type.c.

222{
223 INT argc, i;
224 LPTSTR* argv;
226 HANDLE hConsoleOut;
227 BOOL bNoFileName = FALSE;
228 BOOL bPaging = FALSE;
229 BOOL bFileFound;
230 DWORD dwLastError;
231 UINT nFileSpecs = 0;
232 HANDLE hFind;
233 WIN32_FIND_DATA FindData;
234
235 if (!_tcsncmp(param, _T("/?"), 2))
236 {
238 return 0;
239 }
240
241 if (!*param)
242 {
244 return 1;
245 }
246
247 /* Parse the command line. We will manually expand any file specification present. */
249
250 /* Loop through the options, count also the specified number of file specifications */
251 for (i = 0; i < argc; ++i)
252 {
253 if (argv[i][0] == _T('/'))
254 {
255 if (_tcslen(argv[i]) == 2)
256 {
257 switch (_totupper(argv[i][1]))
258 {
259 case _T('N'):
260 bNoFileName = TRUE;
261 continue;
262
263 case _T('P'):
264 bPaging = TRUE;
265 continue;
266 }
267 }
268
269 // error_invalid_switch(argv[i] + 1);
271 nErrorLevel = 1;
272 goto Quit;
273 }
274
275 /* This should be a file specification */
276 ++nFileSpecs;
277 }
278
279 nErrorLevel = 0;
280
281 hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
282
283 /* Reset paging state */
284 if (bPaging)
286
287 /* Now loop through the files */
288 for (i = 0; i < argc; ++i)
289 {
290 /* Skip the options */
291 if (argv[i][0] == _T('/'))
292 continue;
293
294 /* If wildcards are present in this file specification, perform a file enumeration */
295 if (_tcschr(argv[i], _T('*')) || _tcschr(argv[i], _T('?')))
296 {
297 dwLastError = ERROR_SUCCESS;
298 bFileFound = FALSE;
299
300 hFind = FindFirstFile(argv[i], &FindData);
301
302 if (hFind != INVALID_HANDLE_VALUE)
303 {
304 /* Loop through all the files */
305 do
306 {
307 /* Ignore any directory silently */
308 if (!_tcscmp(FindData.cFileName, _T(".")) ||
309 !_tcscmp(FindData.cFileName, _T("..")) ||
310 (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
311 {
312 continue;
313 }
314
315 bFileFound = TRUE;
316 if (!DoTypeFile(FindData.cFileName, hConsoleOut, bNoFileName, bPaging))
317 {
318 FindClose(hFind);
319 goto Quit;
320 }
321
322 } while (FindNextFile(hFind, &FindData));
323
324 FindClose(hFind);
325 }
326
327 /*
328 * Return an error if the file specification could not be resolved,
329 * or no actual files were encountered (but only directories).
330 */
331 if (hFind == INVALID_HANDLE_VALUE)
332 dwLastError = GetLastError();
333 else if (!bFileFound)
334 dwLastError = ERROR_FILE_NOT_FOUND;
335
336 if (dwLastError != ERROR_SUCCESS)
337 {
338 // FIXME: Use ErrorMessage() ?
342 NULL,
343 dwLastError,
345 (LPTSTR)&errmsg,
346 0,
347 NULL);
348 ConErrPrintf(_T("%s - %s"), argv[i], errmsg);
350 nErrorLevel = 1;
351 }
352 }
353 else
354 {
355 if (!DoTypeFile(argv[i], hConsoleOut, (bNoFileName || (nFileSpecs <= 1)), bPaging))
356 goto Quit;
357 }
358
359 /* Continue with the next file specification */
360 }
361
362Quit:
363 freep(argv);
364 return nErrorLevel;
365}
static int argc
Definition: ServiceArgs.c:12
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
INT nErrorLevel
Definition: cmd.c:158
VOID error_req_param_missing(VOID)
Definition: error.c:110
VOID ConOutResPaging(BOOL StartPaging, UINT resID)
Definition: console.c:178
BOOL __cdecl ConOutPrintfPaging(BOOL StartPaging, LPWSTR szFormat,...)
Definition: console.c:167
#define ConErrPrintf(szStr,...)
Definition: console.h:44
#define ConErrResPrintf(uID,...)
Definition: console.h:50
#define STRING_TYPE_HELP1
Definition: resource.h:186
#define STRING_TYPE_ERROR
Definition: resource.h:65
static BOOL DoTypeFile(IN LPTSTR FileName, IN HANDLE hConsoleOut, IN BOOL bNoFileName, IN BOOL bPaging)
Definition: type.c:78
static VOID freep(LPSTR *p)
Definition: cmdcons.c:98
static LPSTR * split(LPSTR s, LPINT args)
Definition: cmdcons.c:163
EXPORT int errmsg(char *msg, va_alist)
Definition: comerr.c:192
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
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
GLfloat param
Definition: glext.h:5796
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
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define _tcscmp
Definition: tchar.h:1424
#define _tcsncmp
Definition: tchar.h:1428
#define _totupper
Definition: tchar.h:1509
#define _tcschr
Definition: tchar.h:1406
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define argv
Definition: mplay32.c:18
unsigned int UINT
Definition: ndis.h:50
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
#define LANG_NEUTRAL
Definition: nls.h:22
#define MAKELANGID(p, s)
Definition: nls.h:15
#define SUBLANG_DEFAULT
Definition: nls.h:168
int32_t INT
Definition: typedefs.h:58
#define _T(x)
Definition: vfdio.h:22
#define FormatMessage
Definition: winbase.h:3795
#define STD_OUTPUT_HANDLE
Definition: winbase.h:268
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:420
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:423
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:419
#define FindNextFile
Definition: winbase.h:3788
#define FindFirstFile
Definition: winbase.h:3782
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define _tcslen
Definition: xmlstorage.h:198

Referenced by client_dde_callback().

◆ DoTypeFile()

static BOOL DoTypeFile ( IN LPTSTR  FileName,
IN HANDLE  hConsoleOut,
IN BOOL  bNoFileName,
IN BOOL  bPaging 
)
static

Definition at line 78 of file type.c.

83{
85 BOOL bIsFile;
87 DWORD dwFilePos;
88 DWORD dwRet;
90 TCHAR buff[256];
91
97
99 {
100 // FIXME: Use ErrorMessage() ?
104 NULL,
105 GetLastError(),
107 (LPTSTR)&errmsg,
108 0,
109 NULL);
110 ConErrPrintf(_T("%s - %s"), FileName, errmsg);
112 nErrorLevel = 1;
113 return TRUE;
114 }
115
116 /*
117 * When reading from a file, retrieve its original size, so that
118 * we can stop reading it once we are beyond its original ending.
119 * This allows avoiding an infinite read loop in case the output
120 * of the file is redirected back to it.
121 * If we read from somewhere else (device, ...) don't do anything;
122 * we will stop when ReadFile() fails (e.g. when Ctrl-Z is seen...).
123 */
124 bIsFile = ((GetFileType(hFile) & ~FILE_TYPE_REMOTE) == FILE_TYPE_DISK);
125 if (bIsFile)
126 {
128 if ((dwFileSize == INVALID_FILE_SIZE) &&
130 {
131 WARN("Error when retrieving file size, or size too large (%d)\n",
132 dwFileSize);
133 dwFileSize = 0;
134 }
135 dwFilePos = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
136 if ((dwFilePos == INVALID_SET_FILE_POINTER) &&
138 {
139 WARN("Error when setting file pointer\n");
140 dwFilePos = 0;
141 }
142 }
143 else
144 {
145 dwFileSize = dwFilePos = 0;
146 }
147
148 /*
149 * Display the file name on StdErr if required, so that if StdOut
150 * alone is redirected, we can obtain the file contents only.
151 */
152 if (!bNoFileName)
153 ConErrPrintf(_T("\n%s\n\n\n"), FileName);
154
155 if (bPaging)
156 {
158 {
159 if (!ConOutPrintfPaging(FALSE, _T("%s"), buff))
160 {
163 nErrorLevel = 1;
164 return FALSE;
165 }
166
167 /*
168 * If we read from a file, check where we are and stop
169 * once we are beyond the original end of the file.
170 */
171 if (bIsFile)
172 {
173 dwFilePos = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
174 if ((dwFilePos == INVALID_SET_FILE_POINTER) &&
176 {
177 WARN("Error when getting file pointer\n");
178 break;
179 }
180 if (dwFilePos >= dwFileSize)
181 break;
182 }
183 }
184 }
185 else
186 {
187 while (ReadFile(hFile, buff, sizeof(buff), &dwRet, NULL) && dwRet > 0)
188 {
189 WriteFile(hConsoleOut, buff, dwRet, &dwRet, NULL);
190 if (bCtrlBreak)
191 {
194 nErrorLevel = 1;
195 return FALSE;
196 }
197
198 /*
199 * If we read from a file, check where we are and stop
200 * once we are beyond the original end of the file.
201 */
202 if (bIsFile)
203 {
204 dwFilePos = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
205 if ((dwFilePos == INVALID_SET_FILE_POINTER) &&
207 {
208 WARN("Error when getting file pointer\n");
209 break;
210 }
211 if (dwFilePos >= dwFileSize)
212 break;
213 }
214 }
215 }
216
218 return TRUE;
219}
#define WARN(fmt,...)
Definition: precomp.h:61
static BOOL FileGetString(IN HANDLE hFile, OUT LPTSTR lpBuffer, IN LONG nBufferLength)
Definition: type.c:34
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define CloseHandle
Definition: compat.h:739
#define FILE_BEGIN
Definition: compat.h:761
#define INVALID_SET_FILE_POINTER
Definition: compat.h:732
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetFilePointer
Definition: compat.h:743
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:269
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
static unsigned char buff[32768]
Definition: fatten.c:17
DWORD dwFileSize
Definition: more.c:40
_In_ HANDLE hFile
Definition: mswsock.h:90
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
BOOL bCtrlBreak
Definition: util.c:11
#define FILE_CURRENT
Definition: winbase.h:113
#define INVALID_FILE_SIZE
Definition: winbase.h:548
#define CreateFile
Definition: winbase.h:3749
#define FILE_TYPE_DISK
Definition: winbase.h:259
char TCHAR
Definition: xmlstorage.h:189

Referenced by cmd_type().

◆ FileGetString()

static BOOL FileGetString ( IN HANDLE  hFile,
OUT LPTSTR  lpBuffer,
IN LONG  nBufferLength 
)
static

Definition at line 34 of file type.c.

38{
40 DWORD dwRead;
41 LONG len = 0;
42
43#ifdef _UNICODE
45#else
47#endif
48
49 if (ReadFile(hFile, pString, nBufferLength - 1, &dwRead, NULL))
50 {
51 /* Break at new line*/
52 PCHAR end = memchr(pString, '\n', dwRead);
53 len = dwRead;
54 if (end)
55 {
56 len = (LONG)(end - pString) + 1;
58 }
59 }
60
61 if (!len)
62 {
63#ifdef _UNICODE
65#endif
66 return FALSE;
67 }
68
69 pString[len++] = '\0';
70#ifdef _UNICODE
73#endif
74 return TRUE;
75}
UINT OutputCodePage
Definition: console.c:26
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define cmd_alloc(size)
Definition: cmddbg.h:29
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
#define MultiByteToWideChar
Definition: compat.h:110
FxString * pString
GLuint GLuint end
Definition: gl.h:1545
GLenum GLsizei len
Definition: glext.h:6722
#define memchr(s, c, n)
Definition: mkisofs.h:875
long LONG
Definition: pedump.c:60
char * PCHAR
Definition: typedefs.h:51
_In_ LPCSTR _In_opt_ LPCSTR _In_ DWORD nBufferLength
Definition: winbase.h:3073

Referenced by DoTypeFile().