How to send EOF from the command line * without a new line *?

Of course, to send EOF from the command line, Enterfollowed by Ctrl-Zdoes the trick.

C:\> type con > file.txt
line1
line2
^Z

It works, but file.txtcontains line1\r\nline2\r\n. But how can you do the same without the last line of a new line, so what file.txtdoes it contain line1\r\nline2?

On Linux, the solution is to Ctrl-Ddouble click 1 . But what is equivalent on Windows? The command line will happily print ^Zat the end of the line without sending an EOF. (And if you press Enter, then any ^Zthat you type is written to the file as literal escape characters!)

If there is no way on Windows to do this, then why?


1 https://askubuntu.com/questions/118548/how-do-i-end-standard-input-without-a-newline-character

+4
source share
1 answer

The command type con > file.txthas no special handling for ^Zthe cmd shell, since the target file is not con, and the command was typenot run in Unicode (UTF-16LE). In this case, processing ^Zis performed only in ReadFilethat, for the console input buffer, has undocumented behavior for return 0 bytes read if the line starts with ^Z.

, , (lpNumberOfBytesRead) - 4- ( r9 x64), .

C:\Temp>type con > file.txt
Breakpoint 1 hit
KERNELBASE!ReadFile:
00007ffc`fb573cc0 48895c2410      mov     qword ptr [rsp+10h],rbx
                                          ss:00000068`c5d1dfa8=000001e3000001e7
0:000> r r9
r9=00000068c5d1dfd0

0:000> pt
line1
KERNELBASE!ReadFile+0xa9:
00007ffc`fb573d69 c3              ret

0:000> dd 68c5d1dfd0 l1
00000068`c5d1dfd0  00000007

, "line1\r\n" 7 , . "\x1aline2\r\n" , ReadFile, , :

0:000> g
Breakpoint 1 hit
KERNELBASE!ReadFile:
00007ffc`fb573cc0 48895c2410      mov     qword ptr [rsp+10h],rbx
                                          ss:00000068`c5d1dfa8=0000000000000000
0:000> r r9
r9=00000068c5d1dfd0

0:000> pt
^Zline2
KERNELBASE!ReadFile+0xa9:
00007ffc`fb573d69 c3              ret

0:000> dd 68c5d1dfd0 l1
00000068`c5d1dfd0  00000000

, 0 , EOF. ^Z .

, , ^Z . type , Unicode, .. cmd /u /c type con > file.txt. cmd ^Z. , UTF-16LE, , cmd , UTF.

, , copy con file.txt , . cmd!ZScanA ^Z. , . , 3- ( r8 x64) , in-out.

, 7- "line1\r\n":

C:\Temp>copy con file.txt
line1
Breakpoint 0 hit
cmd!ZScanA:
00007ff7`cf4c26d0 48895c2408      mov     qword ptr [rsp+8],rbx
                                          ss:00000068`c5d1e9d0=0000000000000000
0:000> r r8; dd @r8 l1
r8=00000068c5d1ea64
00000068`c5d1ea64  00000007

7 :

0:000> pt
cmd!ZScanA+0x4f:
00007ff7`cf4c271f c3              ret
0:000> dd 68c5d1ea64 l1
00000068`c5d1ea64  00000007
0:000> g

23 (0x17) "line2\x1a Ignore this...\r\n":

line2^Z Ignore this...
Breakpoint 0 hit
cmd!ZScanA:
00007ff7`cf4c26d0 48895c2408      mov     qword ptr [rsp+8],rbx
                                          ss:00000068`c5d1e9d0=0000000000000000
0:000> r r8; dd @r8 l1
r8=00000068c5d1ea64
00000068`c5d1ea64  00000017

5 , ^Z:

0:000> pt
cmd!ZScanA+0x4f:
00007ff7`cf4c271f c3              ret
0:000> dd 68c5d1ea64 l1
00000068`c5d1ea64  00000005

, file.txt 12 :

C:\Temp>for %a in (file.txt) do @echo %~za
12

, Windows Ctrl + D, Unix, ReadConsoleW, CONSOLE_READCONSOLE_CONTROL struct by reference pInputControl. struct dwCtrlWakeupMask - -, , . , 4 Ctrl + D. , :

C:\Temp>.\test
Enter some text: line1
You entered: line1\x04

, Ctrl + D, enter. ^D (.. '\x04') , , .

+4

Source: https://habr.com/ru/post/1676715/


All Articles