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