2 * MORE.C - external command.
4 * clone from 4nt more command
6 * 26 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
8 * Oct 2003 - Timothy Schepens <tischepe at fastmail dot fm>
9 * use window size instead of buffer size.
23 static TCHAR szCont
[128];
24 static DWORD szContLength
;
25 static HINSTANCE hApp
;
27 /*handle for file and console*/
35 GetScreenSize (PSHORT maxx
, PSHORT maxy
)
37 CONSOLE_SCREEN_BUFFER_INFO csbi
;
39 GetConsoleScreenBufferInfo (hStdOut
, &csbi
);
40 *maxx
= (csbi
.srWindow
.Right
- csbi
.srWindow
.Left
) + 1;
41 *maxy
= (csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
) - 4;
47 VOID
ConOutPuts (LPTSTR szText
)
51 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), szText
, _tcslen(szText
), &dwWritten
, NULL
);
52 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), "\n", 1, &dwWritten
, NULL
);
64 ReadConsoleInput (hKeyboard
, &ir
, 1, &dwRead
);
65 if ((ir
.EventType
== KEY_EVENT
) &&
66 (ir
.Event
.KeyEvent
.bKeyDown
== TRUE
))
78 WriteFile (hStdErr
, szCont
, szContLength
, &dwWritten
, NULL
);
82 WriteFile (hStdErr
, _T("\n"), 1, &dwWritten
, NULL
);
84 // FlushConsoleInputBuffer (hConsoleIn);
88 //INT CommandMore (LPTSTR cmd, LPTSTR param)
89 int main (int argc
, char **argv
)
92 SHORT line_count
=0,ch_count
=0;
94 HANDLE hFile
= INVALID_HANDLE_VALUE
;
95 TCHAR szFullPath
[MAX_PATH
];
97 /*reading/writing buffer*/
100 /*bytes written by WriteFile and ReadFile*/
101 DWORD dwRead
,dwWritten
;
103 /*ReadFile() return value*/
107 hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
108 hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
109 hStdErr
= GetStdHandle(STD_ERROR_HANDLE
);
110 hApp
= GetModuleHandle(NULL
);
115 ConOutPuts(_T("Error: no memory"));
119 if (argc
> 1 && _tcsncmp (argv
[1], _T("/?"), 2) == 0)
121 if (LoadString(hApp
, IDS_USAGE
, buff
, 4096 / sizeof(TCHAR
)) < (int)(4096 / sizeof(TCHAR
)))
123 CharToOem(buff
, buff
);
131 hKeyboard
= CreateFile (_T("CONIN$"), GENERIC_READ
|GENERIC_WRITE
,
132 0,NULL
,OPEN_ALWAYS
,0,0);
134 GetScreenSize(&maxx
,&maxy
);
138 FlushConsoleInputBuffer (hKeyboard
);
142 GetFullPathNameA(argv
[1], MAX_PATH
, szFullPath
, NULL
);
143 hFile
= CreateFile (szFullPath
,
150 if (hFile
== INVALID_HANDLE_VALUE
)
152 if (LoadString(hApp
, IDS_FILE_ACCESS
, szMsg
, sizeof(szMsg
) / sizeof(TCHAR
)) < (int)(sizeof(szMsg
) / sizeof(TCHAR
)))
154 _stprintf(buff
, szMsg
, szFullPath
);
155 CharToOem(buff
, buff
);
168 if (!LoadString(hApp
, IDS_CONTINUE
, szCont
, sizeof(szCont
) / sizeof(TCHAR
)))
170 /* Shouldn't happen, so exit */
174 szContLength
= _tcslen(szCont
);
180 bRet
= ReadFile(hFile
,buff
,4096,&dwRead
,NULL
);
182 for(last
=i
=0;i
<dwRead
&& bRet
;i
++)
185 if(buff
[i
] == _T('\n') || ch_count
== maxx
)
189 if (line_count
== maxy
)
192 WriteFile(hStdOut
,&buff
[last
], i
-last
+1, &dwWritten
, NULL
);
194 FlushFileBuffers (hStdOut
);
199 if (last
<dwRead
&& bRet
)
200 WriteFile(hStdOut
,&buff
[last
], dwRead
-last
, &dwWritten
, NULL
);
203 while(dwRead
>0 && bRet
);
206 CloseHandle (hKeyboard
);