flat assembler
Message board for the users of flat assembler.

Index > Windows > why windows add a new extra spacebar before argument?

Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 9132
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
found a weird bit,
why windows return back 2 space bar when initially there is only 1 space bar from our command prompt?

Code:
.code
start:
     invoke  GetCommandLine
      invoke  lstrlen,eax
         pusha
       cinvoke printf,<'len = %d',CR,LF>,eax
               popa
    

let say our application name is A.EXE

result would be
D:\>A.EXE
len = 5
[65] = [A]
[46] = [.]
[69] = [E]
[88] = [X]
[69] = [E]
[0] = [ ]

now if you do A.EXE 1 2 3
D:\>A.EXE 1 2 3
len = 12
[65] = [A]
[46] = [.]
[69] = [E]
[88] = [X]
[69] = [E]
[32] = [ ]
[32] = [ ]
[49] = [1]
[32] = [ ]
[50] = [2]
[32] = [ ]
[51] = [3]
[0] = [ ]

see, windows add an extra space bar,
Post 16 Apr 2012, 19:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17667
Location: In your JS exploiting you and your system
revolution
Why do birds sing?
Why is rain wet?
Post 16 Apr 2012, 21:39
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Parser of a command line must not rely on fact that every piece will be
separated by only one blank - it can be 10 blanks or 7 or whatever.
Also, blanks can be enclosed into double quotes and then they considered a part
of the command line token:

A.EXE "C:\Program Files\Microsoft" -del
Post 16 Apr 2012, 22:00
View user's profile Send private message Send e-mail Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
cmd allows several characters other than space to terminate a program name and begin its arguments (most usefully, the slash), but the C library's rules for parsing command lines requires a space, so cmd adds one:
program/foo → CreateProcess("C:\program.exe", "program /foo", ...)
program;foo → CreateProcess("C:\program.exe", "program ;foo", ...)
program(foo → CreateProcess("C:\program.exe", "program (foo", ...)
If the character *is* a space, this means there ends up being two spaces in the command line passed to CreateProcess.

Edit: It seems this behavior changed between XP and Vista. XP's cmd removes the extra space, Vista's does not.
Post 19 Apr 2012, 01:38
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.