Merge 14981:15268 from trunk
[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 DWORD dwWritten;
42 BOOL bRet;
43 INT argc,i;
44 LPTSTR *argv;
45 LPTSTR errmsg;
46
47 hConsoleOut=GetStdHandle (STD_OUTPUT_HANDLE);
48
49 if (!_tcsncmp (param, _T("/?"), 2))
50 {
51 ConOutResPuts(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 (_T('/') == argv[i][0])
66 {
67 LoadString(CMD_ModuleHandle, STRING_TYPE_ERROR1, szMsg, RC_STRING_MAX_SIZE);
68 ConErrPrintf(szMsg, argv[i] + 1);
69 continue;
70 }
71
72 hFile = CreateFile(argv[i],
73 GENERIC_READ,
74 FILE_SHARE_READ,NULL,
75 OPEN_EXISTING,
76 FILE_ATTRIBUTE_NORMAL,NULL);
77
78 if(hFile == INVALID_HANDLE_VALUE)
79 {
80 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
81 FORMAT_MESSAGE_IGNORE_INSERTS |
82 FORMAT_MESSAGE_FROM_SYSTEM,
83 NULL,
84 GetLastError(),
85 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
86 (LPTSTR) &errmsg,
87 0,
88 NULL);
89 ConErrPrintf (_T("%s - %s"), argv[i], errmsg);
90 LocalFree (errmsg);
91 continue;
92 }
93
94 do
95 {
96 bRet = ReadFile(hFile,buff,sizeof(buff),&dwRead,NULL);
97
98 if (dwRead>0 && bRet)
99 WriteFile(hConsoleOut,buff,dwRead,&dwWritten,NULL);
100
101 } while(dwRead>0 && bRet);
102
103 CloseHandle(hFile);
104 }
105
106 freep (argv);
107
108 return 0;
109 }
110
111 #endif