xref: /reactos/base/shell/cmd/type.c (revision 50cf16b3)
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)
16  *        Added support for quoted arguments (type "test file.dat").
17  *        Cleaned up.
18  *
19  *    19-Jan-1999 (Eric Kohl)
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 hardcoded strings in En.rc
27  */
28 
29 #include "precomp.h"
30 
31 #ifdef INCLUDE_CMD_TYPE
32 
33 
34 INT cmd_type(LPTSTR param)
35 {
36     TCHAR  buff[256];
37     HANDLE hFile, hConsoleOut;
38     DWORD  dwRet;
39     INT    argc,i;
40     LPTSTR *argv;
41     LPTSTR errmsg;
42     BOOL bPaging = FALSE;
43     BOOL bFirstTime = TRUE;
44 
45     hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
46 
47     if (!_tcsncmp(param, _T("/?"), 2))
48     {
49         ConOutResPaging(TRUE,STRING_TYPE_HELP1);
50         return 0;
51     }
52 
53     if (!*param)
54     {
55         error_req_param_missing();
56         return 1;
57     }
58 
59     argv = split(param, &argc, TRUE, FALSE);
60 
61     for (i = 0; i < argc; i++)
62     {
63         if (argv[i][0] == _T('/') && _tcslen(argv[i]) == 2 && _totupper(argv[i][1]) == _T('P'))
64         {
65             bPaging = TRUE;
66         }
67     }
68 
69     for (i = 0; i < argc; i++)
70     {
71         if (argv[i][0] == _T('/') && _totupper(argv[i][1]) != _T('P'))
72         {
73             ConErrResPrintf(STRING_TYPE_ERROR1, argv[i] + 1);
74             continue;
75         }
76         if (argv[i][0] == _T('/') && _tcslen(argv[i]) == 2 && _totupper(argv[i][1]) == _T('P'))
77         {
78             continue;
79         }
80 
81         nErrorLevel = 0;
82 
83         hFile = CreateFile(argv[i],
84                            GENERIC_READ,
85                            FILE_SHARE_READ | FILE_SHARE_WRITE, 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         if (bPaging)
107         {
108             while (FileGetString(hFile, buff, ARRAYSIZE(buff)))
109             {
110                 if (!ConOutPrintfPaging(bFirstTime, _T("%s"), buff))
111                 {
112                     bCtrlBreak = FALSE;
113                     CloseHandle(hFile);
114                     freep(argv);
115                     return 0;
116                 }
117                 bFirstTime = FALSE;
118             }
119         }
120         else
121         {
122             while (ReadFile(hFile, buff, sizeof(buff), &dwRet, NULL) && dwRet > 0)
123             {
124                 WriteFile(hConsoleOut, buff, dwRet, &dwRet, NULL);
125                 if (bCtrlBreak)
126                 {
127                     bCtrlBreak = FALSE;
128                     CloseHandle(hFile);
129                     freep(argv);
130                     return 0;
131                 }
132             }
133         }
134 
135         CloseHandle(hFile);
136     }
137 
138     freep(argv);
139     return 0;
140 }
141 
142 #endif
143