f2981c96d7db2d9ff45b4fa9db3f4e3559c7932c
2 * TEE.C - external command.
4 * clone from 4nt tee command
6 * 01 Sep 1999 - Dr.F <dfaustus@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
);
33 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), "\x0a\x0d", 2, &dwWritten
, NULL
);
35 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), "\n", 1, &dwWritten
, NULL
);
39 VOID
ConErrPrintf (LPTSTR szFormat
, ...)
45 va_start (arg_ptr
, szFormat
);
46 _vstprintf (szOut
, szFormat
, arg_ptr
);
49 WriteFile (GetStdHandle (STD_ERROR_HANDLE
), szOut
, _tcslen(szOut
), &dwWritten
, NULL
);
54 VOID
error_sfile_not_found (LPTSTR f
)
56 ConErrPrintf (_T("Error opening file") _T(" - %s\n"), f
);
62 VOID
ConErrPuts (LPTSTR szText
)
64 ConErrPrintf("%s\n",szText
);
68 INT
main (int argc
,char **p
)
70 /*reading/writing buffer*/
71 TCHAR buff
[TEE_BUFFER_SIZE
];
73 /*handle for file and console*/
74 HANDLE hConsoleIn
,hConsoleOut
;
76 /*bytes written by WriteFile and ReadFile*/
77 DWORD dwRead
,dwWritten
;
80 BOOL bRet
,bAppend
=FALSE
;
83 /*command line parsing stuff*/
88 /*file list implementation*/
93 /*used to remove '"' (if any)*/
102 if (_tcsncmp (p
[1], _T("/?"), 2) == 0)
104 ConOutPuts (_T("Copy standard input to both standard output and a file.\n"
108 " file One or more files that will receive output.\n"
109 " /A Append output to files.\n"));
113 files
= malloc(sizeof(LPTSTR
)*argc
);
114 hFile
= malloc(sizeof(HANDLE
)*argc
);
116 hConsoleIn
=GetStdHandle(STD_INPUT_HANDLE
);
117 hConsoleOut
=GetStdHandle(STD_OUTPUT_HANDLE
);
119 /*parse command line for /a and file name(s)*/
125 if(_tcsnicmp(p
[i
],_T("/a"),2) == 0)
131 /*remove quote if any*/
132 if (p
[i
][0] == _T('"'))
134 tmp
= _tcschr (p
[i
]+1, _T('"'));
142 /*add filename to array of filename*/
144 if( iFileCounter >= sizeof(files) / sizeof(*files) )
146 ConErrPrintf("too many files, maximum is %d\n",sizeof(files) / sizeof(*files));
151 files
[iFileCounter
++]= p
[i
]+add
;
155 for(i
=0;i
<iFileCounter
;i
++)
158 hFile
[i
] = CreateFile(files
[i
],GENERIC_WRITE
,
161 FILE_ATTRIBUTE_NORMAL
,NULL
);
163 if (hFile
[i
] == INVALID_HANDLE_VALUE
)
165 error_sfile_not_found (files
[i
]);
167 for(i
=0;i
<iFileCounter
;i
++)
168 CloseHandle (hFile
[i
]);
179 if (GetFileType (hFile
[i
]) == FILE_TYPE_DISK
)
181 dw
= SetFilePointer (hFile
[i
],0,NULL
,FILE_END
);
182 if (dw
== 0xFFFFFFFF)
184 ConErrPrintf(_T("error moving to end of file %s"),files
[i
]);
186 for(i
=0;i
<iFileCounter
;i
++)
187 CloseHandle (hFile
[i
]);
195 ConErrPrintf(_T("SetFilePointer() = %d\n"),dw
);
203 bRet
= ReadFile(hConsoleIn
,buff
,sizeof(buff
),&dwRead
,NULL
);
205 if (dwRead
>0 && bRet
)
207 for(i
=0;i
<iFileCounter
;i
++)
208 WriteFile(hFile
[i
],buff
,dwRead
,&dwWritten
,NULL
);
210 WriteFile(hConsoleOut
,buff
,dwRead
,&dwWritten
,NULL
);
212 } while(dwRead
>0 && bRet
);
214 for(i
=0;i
<iFileCounter
;i
++)
215 CloseHandle (hFile
[i
]);