move network tools
[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 BOOL bRet;
41 INT argc,i;
42 LPTSTR *argv;
43 LPTSTR errmsg;
44 BOOL bPaging = FALSE;
45 BOOL bFirstTime = TRUE;
46
47 hConsoleOut=GetStdHandle (STD_OUTPUT_HANDLE);
48
49 if (!_tcsncmp (param, _T("/?"), 2))
50 {
51 ConOutResPaging(TRUE,STRING_TYPE_HELP1);
52 return 0;
53 }
54
55 if (!*param)
56 {
57 error_req_param_missing ();
58 return 1;
59 }
60
61 argv = split (param, &argc, TRUE);
62
63 for(i = 0; i < argc; i++)
64 {
65 if(*argv[i] == _T('/') && _tcslen(argv[i]) >= 2 && _totupper(argv[i][1]) == _T('P'))
66 {
67 bPaging = TRUE;
68 }
69 }
70
71 for (i = 0; i < argc; i++)
72 {
73 if (_T('/') == argv[i][0] && _totupper(argv[i][1]) != _T('P'))
74 {
75 LoadString(CMD_ModuleHandle, STRING_TYPE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
76 ConErrPrintf(szMsg, argv[i] + 1);
77 continue;
78 }
79
80 nErrorLevel = 0;
81
82 hFile = CreateFile(argv[i],
83 GENERIC_READ,
84 FILE_SHARE_READ,NULL,
85 OPEN_EXISTING,
86 FILE_ATTRIBUTE_NORMAL,NULL);
87
88 if(hFile == INVALID_HANDLE_VALUE)
89 {
90 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
91 FORMAT_MESSAGE_IGNORE_INSERTS |
92 FORMAT_MESSAGE_FROM_SYSTEM,
93 NULL,
94 GetLastError(),
95 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
96 (LPTSTR) &errmsg,
97 0,
98 NULL);
99 ConErrPrintf (_T("%s - %s"), argv[i], errmsg);
100 LocalFree (errmsg);
101 nErrorLevel = 1;
102 continue;
103 }
104
105 do
106 {
107 bRet = FileGetString (hFile, buff, sizeof(buff) / sizeof(TCHAR));
108 if(bPaging)
109 {
110 if(bRet)
111 {
112 if (ConOutPrintfPaging(bFirstTime, buff) == 1)
113 {
114 bCtrlBreak = FALSE;
115 return 0;
116 }
117 }
118 }
119 else
120 {
121 if(bRet)
122 ConOutPrintf(buff);
123 }
124 bFirstTime = FALSE;
125
126 } while(bRet);
127
128 CloseHandle(hFile);
129 }
130
131 freep (argv);
132
133 return 0;
134 }
135
136 #endif