update for HEAD-2003050101
[reactos.git] / 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
26 #include "config.h"
27
28 #ifdef INCLUDE_CMD_TYPE
29
30 #include <windows.h>
31 #include <tchar.h>
32 #include <string.h>
33
34 #include "cmd.h"
35
36
37 INT cmd_type (LPTSTR cmd, LPTSTR param)
38 {
39         TCHAR  buff[256];
40         HANDLE hFile, hConsoleOut;
41         DWORD  dwRead;
42         DWORD  dwWritten;
43         BOOL   bRet;
44         INT    argc,i;
45         LPTSTR *argv;
46         LPTSTR errmsg;
47         
48         hConsoleOut=GetStdHandle (STD_OUTPUT_HANDLE);
49
50         if (!_tcsncmp (param, _T("/?"), 2))
51         {
52                 ConOutPuts (_T("Displays the contents of text files.\n\n"
53                                            "TYPE [drive:][path]filename"));
54                 return 0;
55         }
56
57         if (!*param)
58         {
59                 error_req_param_missing ();
60                 return 1;
61         }
62
63         argv = split (param, &argc, TRUE);
64         
65         for (i = 0; i < argc; i++)
66         {
67                 if (_T('/') == argv[i][0])
68                 {
69                         ConErrPrintf("Invalid option \"%s\"\n", argv[i] + 1);
70                         continue;
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 ("%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