83f67999997aad1a203985a9a819f4bc07251048
[reactos.git] / reactos / subsys / system / cmd / type.c
1 /*
2 * TYPE.C - type internal command.
3 *
4 * History:
5 *
6 * 07/08/1998 (John P. Price)
7 * started.
8 *
9 * 07/12/98 (Rob Lake)
10 * Changed error messages
11 *
12 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
13 * added config.h include
14 *
15 * 07-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
16 * Added support for quoted arguments (type "test file.dat").
17 * Cleaned up.
18 *
19 * 19-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
20 * Unicode and redirection ready!
21 *
22 * 19-Jan-1999 (Paolo Pantaleo <paolopan@freemail.it>)
23 * Added multiple file support (copied from y.c)
24 *
25 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
26 * Remove all hardcode string to En.rc
27 */
28
29 #include <precomp.h>
30 #include "resource.h"
31
32 #ifdef INCLUDE_CMD_TYPE
33
34
35 INT cmd_type (LPTSTR cmd, LPTSTR param)
36 {
37 TCHAR szMsg[RC_STRING_MAX_SIZE];
38 TCHAR buff[256];
39 HANDLE hFile, hConsoleOut;
40 DWORD dwRead;
41 BOOL bRet;
42 INT argc,i;
43 LPTSTR *argv;
44 LPTSTR errmsg;
45 BOOL bPaging = FALSE;
46 BOOL bFirstTime = TRUE;
47
48 hConsoleOut=GetStdHandle (STD_OUTPUT_HANDLE);
49
50 if (!_tcsncmp (param, _T("/?"), 2))
51 {
52 ConOutResPaging(TRUE,STRING_TYPE_HELP1);
53 return 0;
54 }
55
56 if (!*param)
57 {
58 error_req_param_missing ();
59 return 1;
60 }
61
62 argv = split (param, &argc, TRUE);
63
64 for(i = 0; i < argc; i++)
65 {
66 if(*argv[i] == _T('/') && _tcslen(argv[i]) >= 2 && _totupper(argv[i][1]) == _T('P'))
67 {
68 bPaging = TRUE;
69 }
70 }
71
72 for (i = 0; i < argc; i++)
73 {
74 if (_T('/') == argv[i][0] && _totupper(argv[i][1]) != _T('P'))
75 {
76 LoadString(CMD_ModuleHandle, STRING_TYPE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
77 ConErrPrintf(szMsg, argv[i] + 1);
78 continue;
79 }
80
81 nErrorLevel = 0;
82
83 hFile = CreateFile(argv[i],
84 GENERIC_READ,
85 FILE_SHARE_READ,NULL,
86 OPEN_EXISTING,
87 FILE_ATTRIBUTE_NORMAL,NULL);
88
89 if(hFile == INVALID_HANDLE_VALUE)
90 {
91 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
92 FORMAT_MESSAGE_IGNORE_INSERTS |
93 FORMAT_MESSAGE_FROM_SYSTEM,
94 NULL,
95 GetLastError(),
96 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
97 (LPTSTR) &errmsg,
98 0,
99 NULL);
100 ConErrPrintf (_T("%s - %s"), argv[i], errmsg);
101 LocalFree (errmsg);
102 nErrorLevel = 1;
103 continue;
104 }
105
106 do
107 {
108 bRet = ReadFile(hFile,buff,sizeof(buff),&dwRead,NULL);
109
110 if(bPaging)
111 {
112 if(dwRead>0 && bRet)
113 ConOutPrintfPaging(bFirstTime, buff);
114 }
115 else
116 {
117 if(dwRead>0 && bRet)
118 ConOutPrintf(buff);
119 }
120 bFirstTime = FALSE;
121
122 } while(dwRead>0 && bRet);
123
124 CloseHandle(hFile);
125 }
126
127 freep (argv);
128
129 return 0;
130 }
131
132 #endif