ReactOS  0.4.13-dev-100-gc8611ae
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
25 original location*/
26 
28 {
29  DWORD dwWritten;
30 
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 
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 
65 INT 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;
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,
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  {
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 */
int add
Definition: i386-dis.c:3122
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
static int argc
Definition: ServiceArgs.c:12
#define TEE_BUFFER_SIZE
Definition: tee.c:20
#define TRUE
Definition: types.h:120
#define FILE_TYPE_DISK
Definition: winbase.h:256
#define CloseHandle
Definition: compat.h:398
#define free
Definition: debug_ros.c:5
#define _vstprintf
Definition: tchar.h:540
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:419
int32_t INT
Definition: typedefs.h:56
CHAR * LPTSTR
Definition: xmlstorage.h:192
DWORD WINAPI DECLSPEC_HOTPATCH SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
Definition: fileinfo.c:204
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 va_end(ap)
Definition: acmsvcex.h:90
static const WCHAR szText[]
Definition: dialog.c:139
unsigned int BOOL
Definition: ntddk_ex.h:94
#define _tcsnicmp
Definition: xmlstorage.h:207
#define GENERIC_WRITE
Definition: nt_native.h:90
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
#define STD_INPUT_HANDLE
Definition: winbase.h:264
_TCHAR * _tcschr(const _TCHAR *s, _XINT c)
Definition: tcschr.h:4
VOID ConErrPrintf(LPTSTR szFormat,...)
Definition: tee.c:36
char TCHAR
Definition: xmlstorage.h:189
GLfloat f
Definition: glext.h:7540
#define _T(x)
Definition: vfdio.h:22
#define FILE_END
Definition: winbase.h:114
#define STD_ERROR_HANDLE
Definition: winbase.h:266
VOID ConErrPuts(LPTSTR szText)
Definition: tee.c:59
unsigned long DWORD
Definition: ntddk_ex.h:95
REFIID LPVOID DWORD dw
Definition: atlbase.h:40
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
VOID error_sfile_not_found(LPTSTR f)
Definition: tee.c:51
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
_In_ HANDLE hFile
Definition: mswsock.h:90
INT main(int argc, char **p)
Definition: tee.c:65
#define CREATE_ALWAYS
Definition: disk.h:72
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define malloc
Definition: debug_ros.c:4
GLfloat GLfloat p
Definition: glext.h:8902
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
static unsigned char buff[32768]
Definition: fatten.c:17
int _tcsncmp(const _TCHAR *s1, const _TCHAR *s2, size_t n)
Definition: tcsncmp.h:9
VOID ConOutPuts(LPTSTR szText)
Definition: tee.c:27