ReactOS 0.4.16-dev-340-g0540c21
tee.c
Go to the documentation of this file.
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
25original location*/
26
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
36VOID 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
52{
53 ConErrPrintf (_T("Error opening file") _T(" - %s\n"), f);
54}
55
56
57
58
60{
61 ConErrPrintf(_T("%s\n"),szText );
62}
63
64
65INT main (int argc,char **p)
66{
67 /*reading/writing buffer*/
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;
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,
159
161 {
162 error_sfile_not_found (files[i]);
163
164 for(i=0;i<iFileCounter;i++)
166
167 free (files);
168 free (hFile);
169
170 return 1;
171 }
172
173 /*set append mode*/
174 if (bAppend)
175 {
177 {
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++)
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++)
213
214 free (files);
215 free (hFile);
216
217 return 0;
218}
219
220/* EOF */
static int argc
Definition: ServiceArgs.c:12
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define ConErrPrintf(szStr,...)
Definition: console.h:44
#define ConErrPuts(szStr)
Definition: console.h:32
#define ConOutPuts(szStr)
Definition: console.h:29
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetFilePointer
Definition: compat.h:743
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:269
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
int main()
Definition: test.c:6
static unsigned char buff[32768]
Definition: fatten.c:17
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLfloat f
Definition: glext.h:7540
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define _vstprintf
Definition: tchar.h:540
#define _tcsncmp
Definition: tchar.h:1428
#define _tcschr
Definition: tchar.h:1406
REFIID LPVOID DWORD_PTR dw
Definition: atlbase.h:40
#define CREATE_ALWAYS
Definition: disk.h:72
_In_ HANDLE hFile
Definition: mswsock.h:90
#define GENERIC_WRITE
Definition: nt_native.h:90
#define TEE_BUFFER_SIZE
Definition: tee.c:20
VOID error_sfile_not_found(LPTSTR f)
Definition: tee.c:51
int32_t INT
Definition: typedefs.h:58
#define _T(x)
Definition: vfdio.h:22
#define STD_OUTPUT_HANDLE
Definition: winbase.h:294
#define FILE_END
Definition: winbase.h:115
#define STD_INPUT_HANDLE
Definition: winbase.h:293
#define STD_ERROR_HANDLE
Definition: winbase.h:295
#define CreateFile
Definition: winbase.h:3774
#define FILE_TYPE_DISK
Definition: winbase.h:285
char TCHAR
Definition: xmlstorage.h:189
#define _tcsnicmp
Definition: xmlstorage.h:207
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define _tcslen
Definition: xmlstorage.h:198