- Merge the remaining portion of the wlan-bringup branch
[reactos.git] / rosapps / applications / cmdutils / tee / tee.c
1 /*
2 * TEE.C - external command.
3 *
4 * clone from 4nt tee command
5 *
6 * 01 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
7 * started
8 *
9 *
10 */
11
12
13 #include <windows.h>
14 #include <tchar.h>
15 #include <stdio.h>
16 #include <malloc.h>
17
18
19
20 #define TEE_BUFFER_SIZE 8192
21
22 /*these are function that emulate the ones used in cmd*/
23
24 /*many of them are just copied in this file from their
25 original location*/
26
27 VOID ConOutPuts (LPTSTR szText)
28 {
29 DWORD dwWritten;
30
31 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), szText, _tcslen(szText), &dwWritten, NULL);
32 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), _T("\n"), 1, &dwWritten, NULL);
33 }
34
35
36 VOID ConErrPrintf (LPTSTR szFormat, ...)
37 {
38 DWORD dwWritten;
39 TCHAR szOut[4096];
40 va_list arg_ptr;
41
42 va_start (arg_ptr, szFormat);
43 _vstprintf (szOut, szFormat, arg_ptr);
44 va_end (arg_ptr);
45
46 WriteFile (GetStdHandle (STD_ERROR_HANDLE), szOut, _tcslen(szOut), &dwWritten, NULL);
47 }
48
49
50
51 VOID error_sfile_not_found (LPTSTR f)
52 {
53 ConErrPrintf (_T("Error opening file") _T(" - %s\n"), f);
54 }
55
56
57
58
59 VOID ConErrPuts (LPTSTR szText)
60 {
61 ConErrPrintf(_T("%s\n"),szText );
62 }
63
64
65 INT main (int argc,char **p)
66 {
67 /*reading/writing buffer*/
68 TCHAR buff[TEE_BUFFER_SIZE];
69
70 /*handle for file and console*/
71 HANDLE hConsoleIn,hConsoleOut;
72
73 /*bytes written by WriteFile and ReadFile*/
74 DWORD dwRead,dwWritten;
75
76
77 BOOL bRet,bAppend=FALSE;
78
79
80 /*command line parsing stuff*/
81 LPTSTR tmp;
82 INT i;
83 BOOL bQuote;
84
85 /*file list implementation*/
86 LPTSTR *files;
87 INT iFileCounter=0;
88 HANDLE *hFile;
89
90 /*used to remove '"' (if any)*/
91 INT add;
92
93 DWORD dw;
94
95
96 if (argc < 2)
97 return 1;
98
99 if (_tcsncmp (p[1], _T("/?"), 2) == 0)
100 {
101 ConOutPuts (_T("Copy standard input to both standard output and a file.\n"
102 "\n"
103 "TEE [/A] file...\n"
104 "\n"
105 " file One or more files that will receive output.\n"
106 " /A Append output to files.\n"));
107 return 0;
108 }
109
110 files = malloc(sizeof(LPTSTR)*argc);
111 hFile = malloc(sizeof(HANDLE)*argc);
112
113 hConsoleIn=GetStdHandle(STD_INPUT_HANDLE);
114 hConsoleOut=GetStdHandle(STD_OUTPUT_HANDLE);
115
116 /*parse command line for /a and file name(s)*/
117 for(i=1;i <argc;i++)
118 {
119 bQuote=FALSE;
120 add=0;
121
122 if(_tcsnicmp(p[i],_T("/a"),2) == 0)
123 {
124 bAppend = TRUE;
125 continue;
126 }
127
128 /*remove quote if any*/
129 if (p[i][0] == _T('"'))
130 {
131 tmp = _tcschr (p[i]+1, _T('"'));
132 if (tmp != 0)
133 {
134 add = 1;
135 *tmp= _T('\0');
136 }
137 }
138
139 /*add filename to array of filename*/
140 /*
141 if( iFileCounter >= sizeof(files) / sizeof(*files) )
142 {
143 ConErrPrintf("too many files, maximum is %d\n",sizeof(files) / sizeof(*files));
144 return 1;
145 }
146 */
147
148 files[iFileCounter++]= p[i]+add;
149 }
150
151 /*open file(s)*/
152 for(i=0;i<iFileCounter;i++)
153 {
154 //l=0;
155 hFile[i] = CreateFile(files[i],GENERIC_WRITE,
156 0,NULL,
157 CREATE_ALWAYS,
158 FILE_ATTRIBUTE_NORMAL,NULL);
159
160 if (hFile[i] == INVALID_HANDLE_VALUE)
161 {
162 error_sfile_not_found (files[i]);
163
164 for(i=0;i<iFileCounter;i++)
165 CloseHandle (hFile[i]);
166
167 free (files);
168 free (hFile);
169
170 return 1;
171 }
172
173 /*set append mode*/
174 if (bAppend)
175 {
176 if (GetFileType (hFile[i]) == FILE_TYPE_DISK)
177 {
178 dw = SetFilePointer (hFile[i],0,NULL,FILE_END);
179 if (dw == 0xFFFFFFFF)
180 {
181 ConErrPrintf(_T("error moving to end of file %s"),files[i]);
182
183 for(i=0;i<iFileCounter;i++)
184 CloseHandle (hFile[i]);
185
186 free (files);
187 free (hFile);
188
189 return 1;
190 }
191
192 ConErrPrintf(_T("SetFilePointer() = %d\n"),dw);
193 }
194 }
195 }
196
197 /*read and write*/
198 do
199 {
200 bRet = ReadFile(hConsoleIn,buff,sizeof(buff),&dwRead,NULL);
201
202 if (dwRead>0 && bRet)
203 {
204 for(i=0;i<iFileCounter;i++)
205 WriteFile(hFile[i],buff,dwRead,&dwWritten,NULL);
206
207 WriteFile(hConsoleOut,buff,dwRead,&dwWritten,NULL);
208 }
209 } while(dwRead>0 && bRet);
210
211 for(i=0;i<iFileCounter;i++)
212 CloseHandle (hFile[i]);
213
214 free (files);
215 free (hFile);
216
217 return 0;
218 }
219
220 /* EOF */