1 /*
2  * Y.C - Y external command.
3  *
4  * clone from 4nt y command
5  *
6  * 02 Oct 1999 (Paolo Pantaleo)
7  *     started
8  *
9  *
10  */
11 
12 #define BUFF_SIZE 4096
13 
14 #include <windows.h>
15 #include <stdio.h>
16 #include <tchar.h>
17 
18 static
19 VOID ConErrPrintf (LPTSTR szFormat, ...)
20 {
21 	DWORD dwWritten;
22 	TCHAR szOut[BUFF_SIZE];
23 	va_list arg_ptr;
24 
25 	va_start (arg_ptr, szFormat);
26 	_vstprintf (szOut, szFormat, arg_ptr);
27 	va_end (arg_ptr);
28 
29 	WriteFile (GetStdHandle (STD_ERROR_HANDLE), szOut, _tcslen(szOut), &dwWritten, NULL);
30 }
31 
32 
33 static
34 VOID ConOutPuts (LPTSTR szText)
35 {
36 	DWORD dwWritten;
37 
38 	WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), szText, _tcslen(szText), &dwWritten, NULL);
39 	WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), "\n", 1, &dwWritten, NULL);
40 }
41 
42 
43 int main (int argc, char **argv)
44 {
45 	INT i;
46 	HANDLE hFind;
47 	HANDLE hConsoleIn, hConsoleOut, hFile;
48 	char buff[BUFF_SIZE];
49 	DWORD dwRead,dwWritten;
50 	BOOL bRet;
51 	WIN32_FIND_DATA FindData;
52 
53 	hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
54 	hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
55 
56 	if (argc == 2 && _tcsncmp (argv[1], _T("/?"), 2) == 0)
57 	{
58 		ConOutPuts(_T("copy stdin to stdout and then files to stdout\n"
59 		              "\n"
60 		              "Y [files]\n"
61 		              "\n"
62 		              "files         files to copy to stdout"));
63 		return 0;
64 	}
65 
66 	/*stdin to stdout*/
67 	do
68 	{
69 		bRet = ReadFile(hConsoleIn,buff,sizeof(buff),&dwRead,NULL);
70 
71 		if (dwRead>0 && bRet)
72 			WriteFile(hConsoleOut,buff,dwRead,&dwWritten,NULL);
73 
74 	} while(dwRead>0 && bRet);
75 
76 
77 	/*files to stdout*/
78 	Sleep(0);
79 
80 	for (i = 1; i < argc; i++)
81 	{
82 		hFind=FindFirstFile(argv[i],&FindData);
83 
84 		if (hFind==INVALID_HANDLE_VALUE)
85 		{
86 			ConErrPrintf("File not found - %s\n",argv[i]);
87 			continue;
88 		}
89 
90 		do
91 		{
92 			hFile = CreateFile(FindData.cFileName,
93 				GENERIC_READ,
94 				FILE_SHARE_READ,NULL,
95 				OPEN_EXISTING,
96 				FILE_ATTRIBUTE_NORMAL,NULL);
97 
98 			if(hFile == INVALID_HANDLE_VALUE)
99 			{
100 				ConErrPrintf("File not found - %s\n",FindData.cFileName);
101 				continue;
102 			}
103 
104 			do
105 			{
106 				bRet = ReadFile(hFile,buff,sizeof(buff),&dwRead,NULL);
107 
108 				if (dwRead>0 && bRet)
109 					WriteFile(hConsoleOut,buff,dwRead,&dwWritten,NULL);
110 
111 			} while(dwRead>0 && bRet);
112 
113 			CloseHandle(hFile);
114 
115 		}
116 		while(FindNextFile(hFind,&FindData));
117 
118 		FindClose(hFind);
119 	}
120 
121 	return 0;
122 }
123