SYSTEM_TIME_ZONE_INFORMATION added.
[reactos.git] / rosapps / cmdutils / more.c
1 /*
2 * MORE.C - external command.
3 *
4 * clone from 4nt more command
5 *
6 * 26 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
7 * started
8 */
9
10 #include <windows.h>
11 #include <malloc.h>
12 #include <tchar.h>
13
14
15 DWORD len;
16 LPTSTR msg = "--- continue ---";
17
18
19 /*handle for file and console*/
20 HANDLE hStdIn;
21 HANDLE hStdOut;
22 HANDLE hStdErr;
23 HANDLE hKeyboard;
24
25
26 static VOID
27 GetScreenSize (PSHORT maxx, PSHORT maxy)
28 {
29 CONSOLE_SCREEN_BUFFER_INFO csbi;
30
31 GetConsoleScreenBufferInfo (hStdOut, &csbi);
32
33 if (maxx)
34 *maxx = csbi.dwSize.X;
35 if (maxy)
36 *maxy = csbi.dwSize.Y;
37 }
38
39
40 static
41 VOID ConOutPuts (LPTSTR szText)
42 {
43 DWORD dwWritten;
44
45 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), szText, _tcslen(szText), &dwWritten, NULL);
46 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), "\n", 1, &dwWritten, NULL);
47 }
48
49
50 static VOID
51 ConInKey (VOID)
52 {
53 INPUT_RECORD ir;
54 DWORD dwRead;
55
56 do
57 {
58 ReadConsoleInput (hKeyboard, &ir, 1, &dwRead);
59 if ((ir.EventType == KEY_EVENT) &&
60 (ir.Event.KeyEvent.bKeyDown == TRUE))
61 return;
62 }
63 while (TRUE);
64 }
65
66
67 static VOID
68 WaitForKey (VOID)
69 {
70 DWORD dwWritten;
71
72 WriteFile (hStdErr,msg , len, &dwWritten, NULL);
73
74 ConInKey();
75
76 WriteFile (hStdErr, _T("\n"), 1, &dwWritten, NULL);
77
78 // FlushConsoleInputBuffer (hConsoleIn);
79 }
80
81
82 //INT CommandMore (LPTSTR cmd, LPTSTR param)
83 int main (int argc, char **argv)
84 {
85 SHORT maxx,maxy;
86 SHORT line_count=0,ch_count=0;
87 INT i;
88
89 /*reading/writing buffer*/
90 TCHAR *buff;
91
92 /*bytes written by WriteFile and ReadFile*/
93 DWORD dwRead,dwWritten;
94
95 /*ReadFile() return value*/
96 BOOL bRet;
97
98 len = _tcslen (msg);
99 hStdIn = GetStdHandle(STD_INPUT_HANDLE);
100 hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
101 hStdErr = GetStdHandle(STD_ERROR_HANDLE);
102
103 if (_tcsncmp (argv[1], _T("/?"), 2) == 0)
104 {
105 ConOutPuts(_T("Help text still missing!!"));
106 return 0;
107 }
108
109 hKeyboard = CreateFile ("CONIN$", GENERIC_READ,
110 0,NULL,OPEN_ALWAYS,0,0);
111
112 GetScreenSize(&maxx,&maxy);
113
114 buff=malloc(maxx);
115
116 FlushConsoleInputBuffer (hKeyboard);
117
118 do
119 {
120 bRet = ReadFile(hStdIn,buff,1,&dwRead,NULL);
121
122 if (dwRead>0 && bRet)
123 WriteFile(hStdOut,buff,dwRead,&dwWritten,NULL);
124
125 for(i=0;i<dwRead;i++)
126 {
127 ch_count++;
128 if(buff[i] == _T('\x0a') || ch_count == maxx)
129 {
130 ch_count=0;
131 line_count++;
132 if (line_count == maxy-1)
133 {
134 line_count = 0;
135 FlushFileBuffers (hStdOut);
136 WaitForKey ();
137 }
138 }
139 }
140 }
141 while(dwRead>0 && bRet);
142
143 free (buff);
144 CloseHandle (hKeyboard);
145
146 return 0;
147 }
148
149 /* EOF */