2 * TEE.C - external command.
4 * clone from 4nt tee command
6 * 01 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
20 #define TEE_BUFFER_SIZE 8192
22 /*these are function that emulate the ones used in cmd*/
24 /*many of them are just copied in this file from their
27 VOID
ConOutPuts (LPTSTR szText
)
31 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), szText
, _tcslen(szText
), &dwWritten
, NULL
);
32 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), _T("\n"), 1, &dwWritten
, NULL
);
36 VOID
ConErrPrintf (LPTSTR szFormat
, ...)
42 va_start (arg_ptr
, szFormat
);
43 _vstprintf (szOut
, szFormat
, arg_ptr
);
46 WriteFile (GetStdHandle (STD_ERROR_HANDLE
), szOut
, _tcslen(szOut
), &dwWritten
, NULL
);
51 VOID
error_sfile_not_found (LPTSTR f
)
53 ConErrPrintf (_T("Error opening file") _T(" - %s\n"), f
);
59 VOID
ConErrPuts (LPTSTR szText
)
61 ConErrPrintf(_T("%s\n"),szText
);
65 INT
main (int argc
,char **p
)
67 /*reading/writing buffer*/
68 TCHAR buff
[TEE_BUFFER_SIZE
];
70 /*handle for file and console*/
71 HANDLE hConsoleIn
,hConsoleOut
;
73 /*bytes written by WriteFile and ReadFile*/
74 DWORD dwRead
,dwWritten
;
77 BOOL bRet
,bAppend
=FALSE
;
80 /*command line parsing stuff*/
85 /*file list implementation*/
90 /*used to remove '"' (if any)*/
99 if (_tcsncmp (p
[1], _T("/?"), 2) == 0)
101 ConOutPuts (_T("Copy standard input to both standard output and a file.\n"
105 " file One or more files that will receive output.\n"
106 " /A Append output to files.\n"));
110 files
= malloc(sizeof(LPTSTR
)*argc
);
111 hFile
= malloc(sizeof(HANDLE
)*argc
);
113 hConsoleIn
=GetStdHandle(STD_INPUT_HANDLE
);
114 hConsoleOut
=GetStdHandle(STD_OUTPUT_HANDLE
);
116 /*parse command line for /a and file name(s)*/
122 if(_tcsnicmp(p
[i
],_T("/a"),2) == 0)
128 /*remove quote if any*/
129 if (p
[i
][0] == _T('"'))
131 tmp
= _tcschr (p
[i
]+1, _T('"'));
139 /*add filename to array of filename*/
141 if( iFileCounter >= sizeof(files) / sizeof(*files) )
143 ConErrPrintf("too many files, maximum is %d\n",sizeof(files) / sizeof(*files));
148 files
[iFileCounter
++]= p
[i
]+add
;
152 for(i
=0;i
<iFileCounter
;i
++)
155 hFile
[i
] = CreateFile(files
[i
],GENERIC_WRITE
,
158 FILE_ATTRIBUTE_NORMAL
,NULL
);
160 if (hFile
[i
] == INVALID_HANDLE_VALUE
)
162 error_sfile_not_found (files
[i
]);
164 for(i
=0;i
<iFileCounter
;i
++)
165 CloseHandle (hFile
[i
]);
176 if (GetFileType (hFile
[i
]) == FILE_TYPE_DISK
)
178 dw
= SetFilePointer (hFile
[i
],0,NULL
,FILE_END
);
179 if (dw
== 0xFFFFFFFF)
181 ConErrPrintf(_T("error moving to end of file %s"),files
[i
]);
183 for(i
=0;i
<iFileCounter
;i
++)
184 CloseHandle (hFile
[i
]);
192 ConErrPrintf(_T("SetFilePointer() = %d\n"),dw
);
200 bRet
= ReadFile(hConsoleIn
,buff
,sizeof(buff
),&dwRead
,NULL
);
202 if (dwRead
>0 && bRet
)
204 for(i
=0;i
<iFileCounter
;i
++)
205 WriteFile(hFile
[i
],buff
,dwRead
,&dwWritten
,NULL
);
207 WriteFile(hConsoleOut
,buff
,dwRead
,&dwWritten
,NULL
);
209 } while(dwRead
>0 && bRet
);
211 for(i
=0;i
<iFileCounter
;i
++)
212 CloseHandle (hFile
[i
]);