remove trailing whitespace at end of lines
[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 * Oct 2003 - Timothy Schepens <tischepe at fastmail dot fm>
9 * use window size instead of buffer size.
10 */
11
12 #include <windows.h>
13 #include <malloc.h>
14 #include <tchar.h>
15
16
17 DWORD len;
18 LPTSTR msg = "--- continue ---";
19
20
21 /*handle for file and console*/
22 HANDLE hStdIn;
23 HANDLE hStdOut;
24 HANDLE hStdErr;
25 HANDLE hKeyboard;
26
27
28 static VOID
29 GetScreenSize (PSHORT maxx, PSHORT maxy)
30 {
31 CONSOLE_SCREEN_BUFFER_INFO csbi;
32
33 GetConsoleScreenBufferInfo (hStdOut, &csbi);
34 *maxx = csbi.srWindow.Right;
35 *maxy = csbi.srWindow.Bottom;
36
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, last;
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 (argc > 1 && _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(4096);
115
116 FlushConsoleInputBuffer (hKeyboard);
117
118 do
119 {
120 bRet = ReadFile(hStdIn,buff,4096,&dwRead,NULL);
121
122 for(last=i=0;i<dwRead && bRet;i++)
123 {
124 ch_count++;
125 if(buff[i] == _T('\n') || ch_count == maxx)
126 {
127 ch_count=0;
128 line_count++;
129 if (line_count == maxy)
130 {
131 line_count = 0;
132 WriteFile(hStdOut,&buff[last], i-last+1, &dwWritten, NULL);
133 last=i+1;
134 FlushFileBuffers (hStdOut);
135 WaitForKey ();
136 }
137 }
138 }
139 if (last<dwRead && bRet)
140 WriteFile(hStdOut,&buff[last], dwRead-last, &dwWritten, NULL);
141
142 }
143 while(dwRead>0 && bRet);
144
145 free (buff);
146 CloseHandle (hKeyboard);
147
148 return 0;
149 }
150
151 /* EOF */