The MASM Forum

General => The Workshop => Topic started by: felipe on February 25, 2019, 04:59:43 AM

Title: filecopy
Post by: felipe on February 25, 2019, 04:59:43 AM
a very simple program to copy a file from the command line (cmd.exe). syntax usage: filecopy filenametocopy filenameofcopy. The syntax for this little program is strict, so if you add more than one space between the program name and the first argument, or between the first argument and the second argument, the program will not do the copy of the file succesfully. yeah, it's not a too friendly program, but anyway, who really need it... :P

.asm and .exe attached in .zip.  :icon14:
Title: Re: filecopy
Post by: aw27 on March 01, 2019, 02:48:42 AM
What happens if the file names have spaces?  :(
Title: Re: filecopy
Post by: felipe on March 01, 2019, 03:52:43 AM
it doesn't work?  :idea:
Title: Re: filecopy
Post by: aw27 on March 01, 2019, 04:06:56 AM
Quote from: felipe on March 01, 2019, 03:52:43 AM
it doesn't work?  :idea:
It is your turn to answer my question, not ask me to test your software. :idea:
Title: Re: filecopy
Post by: felipe on March 01, 2019, 04:27:05 AM
Quote from: AW on March 01, 2019, 04:06:56 AM
It is your turn to answer my question, not ask me to test your software. :idea:

Really? and what license you paid for?  :P

The code it's not that hard, i don't think it requires any explanation to your question... :idea:
Title: Re: filecopy
Post by: aw27 on March 01, 2019, 04:31:57 AM
Quote from: felipe on March 01, 2019, 04:27:05 AM
Quote from: AW on March 01, 2019, 04:06:56 AM
It is your turn to answer my question, not ask me to test your software. :idea:
Really? and what license you paid for?  :P
It appears that you are making a favor to everybody by dropping careless pieces of crap.  :idea:
I am dropping out, have fun.
Title: Re: filecopy
Post by: felipe on March 01, 2019, 05:02:02 AM
Quote from: AW on March 01, 2019, 04:31:57 AM
It appears that you are making a favor to everybody by dropping careless pieces of crap.  :idea:

It's more and academic issue (not a commercial issue as you seem to think... ::)). The program states as how it is, very simple, with an strict syntax. Just a simple exercise. :idea:

Quote from: AW on March 01, 2019, 04:31:57 AM
I am dropping out, have fun.

Sure, no problem. This simple academic exercise it's too much of a piece of crap to such a smart professional programmer like you. So keep having fun!  ;)
Title: Re: filecopy
Post by: felipe on March 01, 2019, 09:04:03 AM
 I have been considering the request of a "demanding client"  :P of this software called "filecopy" (a.k.a "careless piece of crap"  :shock:). So in this version that i upload to the forum with the .asm file and the .exe file in the attached .zip file  :icon14:, allows for file names with spaces.  :idea:
Title: Re: filecopy
Post by: jj2007 on March 01, 2019, 10:03:40 AM
Are you sure this is a good way to design a loop?

search_arg_1:
          cmp     byte ptr[esi],20h                   ; Look up the first space.
          je      ready_to_arg_1

          inc     esi                                 ; Skip the program name to search for first argument.
          jmp     search_arg_1


Btw lodsb and stosb are fantastic instructions for such tasks. They are a bit slow as other members will not hesitate to remark, so don't use them if your filenames are much longer than a Million characters 8)
Title: Re: filecopy
Post by: felipe on March 01, 2019, 10:31:05 AM
Quote from: jj2007 on March 01, 2019, 10:03:40 AM
Are you sure this is a good way to design a loop?

I like it and it works. If you have any other suggestion, please post it, don't be shy... :idea:
Title: Re: filecopy
Post by: jj2007 on March 01, 2019, 10:48:59 AM
Point is, if there is no space, this loop will run very far and eventually crash. In fact, if you launch the program without arguments, it will throw an exception, or, depending on the mood of the OS, it will show nothing, or it will say "ERROR: The operation was NOT performed succesfully"

A well behaved program should never throw an exception into the user's face. It should show an error message explaining, for example, that the program expects two arguments.
Title: Re: filecopy
Post by: hutch-- on March 01, 2019, 11:22:13 AM
Come on folks, be reasonable, the original post was a SIMPLE file copy, not an atom cracking wickedly crafted AVX4 world record holder. Anyone old enough to remember the DOS era would know how to use the old style simple file names which had no spaces. The parser to differentiate between a wide variety of inputs is more complicated than the file copy itself.

It is important for people making the effort to write assembler to try out all sorts of things and play with the code design to make it more efficient. I have never seen an algorithm that cannot be tweaked somewhere so it not like there will ever be an "ultimate" version.
Title: Re: filecopy
Post by: felipe on March 01, 2019, 11:47:24 AM
Hutch is right jj, i have never talked about this program like it was an error-free program. I have make some error checkings, but that's all. It can be crashed if there are no arguments, i suppose (i think didn't check that out, as far as i can recall... :idea:). But of course a serious program, aimed to some expected end users must be error-free, at least to some extend... :idea:
Title: Re: filecopy
Post by: aw27 on March 01, 2019, 05:00:55 PM
Quote from: hutch-- on March 01, 2019, 11:22:13 AM
The parser to differentiate between a wide variety of inputs is more complicated than the file copy itself.
The OP is expected to be aware of the limitations and not ask users to check if something works or not when they can do it themselves or should have done it themselves beforehand.
I am not going to ask what happens if I use Russian filenames because he may answer that he is not a Russian.
I am not going to ask the reason filecopy origfile.txt "2"file.txt" produces a file named 2 when it should have produced a file named 2file.txt

Note:
All this parsing activity, even if were not crappy, is a waste of time.
For some reason Microsoft invented the function CommandLineToArgvW more than 20 years ago but never invented a function CommandLineToArgvA.
Title: Re: filecopy
Post by: jj2007 on March 01, 2019, 06:35:35 PM
Quote from: AW on March 01, 2019, 05:00:55 PMI am not going to ask the reason filecopy origfile.txt "2"file.txt" produces a file named 2 when it should have produced a file named 2file.txt

Where is it documented that a malformed "2"file.txt" should produce a well-formed 2file.txt? Otherwise, strangely enough, I agree with José :bgrin:
Title: Re: filecopy
Post by: aw27 on March 01, 2019, 07:49:08 PM
Quote from: jj2007 on March 01, 2019, 06:35:35 PM
Where is it documented that a malformed "2"file.txt" should produce a well-formed 2file.txt?
Where is it documented that a malformed "2"file.txt" should produce a 2 file?  :biggrin:
And why filecopy "original file.txt" "2"""""""""""file.txt" produces a 2 file?  :icon_eek:

The expected behaviour should be an error or ignoring the double quote(s) as Microsoft does in copy.exe.
Title: Re: filecopy
Post by: TimoVJL on March 01, 2019, 08:14:17 PM
The program actually owns it's command line format ?
Example to conform MS C++, Parsing C++ Command-Line Arguments (https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017)
Naming Files, Paths, and Namespaces (https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file)

EDIT: an interesting shell function PathGetArgsA function (https://docs.microsoft.com/en-us/windows/desktop/api/shlwapi/nf-shlwapi-pathgetargsa).#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//#include <shlwapi.h>

#pragma comment(lib, "msvcrt.lib")
LPCSTR __stdcall PathGetArgsA(LPCSTR pszPath);
#pragma comment(lib, "shlwapi.lib")

void __cdecl mainCRTStartup(void)
{
char d, *p, *p1, *p2, *p3;
p = GetCommandLineA();
p1 = (char*)PathGetArgsA(p);
puts(p);
puts(p1);
p2 = (char*)PathGetArgsA(p1);
*(p2-1) = 0; // user should avoid this
p3 = (char*)PathGetArgsA(p2);
*(p3-1) = 0; // user should avoid this
puts(p1);
puts(p2);
puts(p3);
printf("%p,%p,%p,%p\n", p, p1, p2, p3);
_getch();
ExitProcess(0);
}
Title: Re: filecopy
Post by: jj2007 on March 01, 2019, 08:19:10 PM
Quote from: AW on March 01, 2019, 07:49:08 PMWhere is it documented that a malformed "2"file.txt" should produce a 2 file?  :biggrin:

Nowhere, of course - it's malformed. But you suggested that it should produce 2file.txt:
Quote from: AW on March 01, 2019, 05:00:55 PMI am not going to ask the reason filecopy origfile.txt "2"file.txt" produces a file named 2 when it should have produced a file named 2file.txt
Title: Re: filecopy
Post by: hutch-- on March 01, 2019, 08:48:23 PM
I vaguely remember the very old "argv" "argc" notation of long ago that didn't handle names with spaces either, prior to I think it was NT4, there were no file names with spaces as the OS versions would not handle it. Try a file name in the DOS era, "my.file.name with spaces.whatever" and you are in for a less than pleasant surprise.

Attached is a 64 bit scruffy that seems to work OK. NOTE that it uses the hard coded delimiter #
Title: Re: filecopy
Post by: aw27 on March 01, 2019, 09:22:23 PM
That's it @Timo:
(https://www.dropbox.com/s/fmg7bngclso69h9/parsing.jpg?dl=1)
Title: Re: filecopy
Post by: felipe on March 02, 2019, 12:41:00 AM
Quote from: AW on March 01, 2019, 05:00:55 PM
The OP is expected to be aware of the limitations and not ask users to check if something works or not when they can do it themselves or should have done it themselves beforehand.
I knew that this was never an error-free program from the beginning... :idea: Since we are talking about "standards" here,  i have one for you:
when i reply to your first question with this:
Quote from: felipe on March 01, 2019, 03:52:43 AM
it doesn't work?  :idea:
i was expecting that my answer wasn't so difficult to you to understand.  ::)
Of course your "standard" (and maybe jj'"standard" as well) has been somenthing like: let's ignore simple reallity, lets use this "case" to stretch the programming horizonts of the world, yeah, lets be better programmers...  :icon14:

Title: Re: filecopy
Post by: felipe on March 02, 2019, 01:38:53 AM
Quote from: TimoVJL on March 01, 2019, 08:14:17 PM
The program actually owns it's command line format ?
Yes timovjl, that was the idea from the beginning, thanks for understand that.  :t
But i repeat for those that didn't get it: the program never intended to be an error free program. It was just an excercise.  :idea:
I agree with the discussions about good programming practices and all that, what i dislike is to take over my shoulders the way others abuse of my good intentions of collaborate with the forum, like if i was working for them or something like that... :eusa_naughty:
Why they should treat me like an example of bad practices? can't just they talk about the program, maybe using brain abstraction capabilities...?  :idea:
Title: Re: filecopy
Post by: hutch-- on March 02, 2019, 04:15:09 AM
I still don't understand why the criticism for what is effectively a test piece, what it is was was  explained in the first post and with some explanation that it was not a perfect program. Most people post bits and pieces in here without the expectation that they are perfect things, I wonder why it is happening here.

With a test piece there is little reason to make it idiot proof, the quality of idiot is so high that you can never succeed, best you can do is document the behaviour of the code and call alternative attempts at using it the same as Microsoft do with API functions, the results are UNDEFINED.  :P
Title: Re: filecopy
Post by: guga on March 02, 2019, 04:54:36 AM
Quote from: hutch-- on March 02, 2019, 04:15:09 AM
I still don't understand why the criticism for what is effectively a test piece, what it is was was  explained in the first post and with some explanation that it was not a perfect program. Most people post bits and pieces in here without the expectation that they are perfect things, I wonder why it is happening here.

Didn´t wanted to put more fire on the discussion, but I share the same thoughts. For me, it was only a small test app. Felipe also make fun in the end " but anyway, who really need it... :P"  So, from what i understood it was a test file. Probably a misunderstanding on the way he answered to AW (And vice-versa).

Felipe/AW, it was clearly a misunderstanding. Come on guys, let´s calm down and take a beer. The bar is open. :greensml: :greensml: :greensml:

(http://i64.tinypic.com/2e5nqxk.jpg)
:t :t :t
Title: Re: filecopy
Post by: aw27 on March 02, 2019, 04:58:26 AM
There was a program sitting here that after 6 days not a single soul bothered to download.
Apparently, I shouldn't have touched it.

When I asked Felipe what would happen if there were file names with spaces he replied with a question not with an answer as expected. Probably I said something foolish and impossible of happening from his master piece.

When I called his attention that I was expecting a reply he said that for what I pay I don't deserve any better.

I don't understand this victimization.
Title: Re: filecopy
Post by: felipe on March 02, 2019, 05:21:47 AM
Quote from: AW on March 02, 2019, 04:58:26 AM
There was a program sitting here that after 6 days not a single soul bothered to download.
Apparently, I shouldn't have touched it.
Nice openning...  ;) Let's see what else comes with this:  :idea:

Quote from: AW on March 02, 2019, 04:58:26 AM
When I asked Felipe what would happen if there were file names with spaces he replied with a question not with an answer as expected. Probably I said something foolish and impossible of happening from his master piece.
The program was so simple and you have been always so nice, so i decided to answer with a little of sarcasm... :idea:

Quote from: AW on March 02, 2019, 04:58:26 AM
When I called his attention that I was expecting a reply he said that for what I pay I don't deserve any better.
I think it was more like:  :idea:
Quote from: AW on March 01, 2019, 04:06:56 AM
It is your turn to answer my question, not ask me to test your software. :idea:

I haven't ask you to test my software...so:  :idea:
Quote from: AW on March 02, 2019, 04:58:26 AM
I don't understand this victimization.
Me neither... :idea:
Title: Re: filecopy
Post by: felipe on March 02, 2019, 05:33:34 AM
Anyway, it seems like i was a little rude. Maybe it was a bad day. I'm sorry, next time i will try to be more gentle with the questions about programs that i upload.  :idea:
Title: Re: filecopy
Post by: Vortex on March 02, 2019, 05:46:00 AM
Hi felipe,

Everything will go fine for you. I am sure you will have new ideas. Step by step felipe, it's OK.
Title: Re: filecopy
Post by: Vortex on March 02, 2019, 06:01:11 AM
https://stackoverflow.com/questions/291424/canonical-way-to-parse-the-command-line-into-arguments-in-plain-c-windows-api

Why the developers of the shell restricted themselves with the UNICODE version of CommandLineToArgv is a mystery.

Implementation of CommandLineToArgv for Win32 (ANSI and alternative Unicode versions) :

http://alter.org.ua/docs/win/args/

CommandLineToArgvA :

https://github.com/futurist/CommandLineToArgvA

Title: Re: filecopy
Post by: felipe on March 02, 2019, 06:42:27 AM
Thanks vortex, you are very kind, really  :icon14:
Title: Re: filecopy
Post by: jj2007 on March 02, 2019, 06:44:08 AM
Quote from: Vortex on March 02, 2019, 06:01:11 AMWhy the developers of the shell restricted themselves with the UNICODE version of CommandLineToArgv is a mystery.

For some time, M$ was convinced that UTF-16 was the future. The rest of the World was more convinced of UTF-8. Anyway, translating CommandLineToArgvW to UTF-8 (or any other charset, e.g. Norwegian) is very easy.
Title: Re: filecopy
Post by: Vortex on March 02, 2019, 06:59:56 AM
Hi Jochen,

One can understand the point of view of M$ but coding the ANSI version of CommandLineToArgv should not be a big effort for them. Probably, they didn't bother to do so.
Title: Re: filecopy
Post by: TimoVJL on March 02, 2019, 07:04:19 AM
Windows NT is internally UNICODE, so only CommandLineToArgvW was needed to support most filenames ?
Title: Re: filecopy
Post by: Vortex on March 02, 2019, 07:29:19 AM
Hi Timo,

M$ is providing both of the ANSI and UNICODE forms of the API functions. This is mostly the case but CommandLineToArgvW is missing it's companion when there are a lot of useless API functions.
Title: Re: filecopy
Post by: nidud on March 02, 2019, 07:49:56 AM
deleted
Title: Re: filecopy
Post by: aw27 on March 02, 2019, 01:44:34 PM
Simple filecopy example with full verbose error reporting.


H:\Masm32 Forum\getmainarguments> filecopy testfile.txt "my file 222"
Number of arguments: 3
Source File: testfile.txt
Destination File: my file 222
File Successfully Copied

H:\Masm32 Forum\getmainarguments> filecopy testfile.txt "my file 2"22"
Number of arguments: 3
Source File: testfile.txt
Destination File: my file 222
Error: The file exists.

H:\Masm32 Forum\getmainarguments> filecopy testfile.txt "my" file 222"
Number of arguments: 5
Syntax: Filecopy source destination

H:\Masm32 Forum\getmainarguments> filecopy testfile.txt "my file""222
Number of arguments: 3
Source File: testfile.txt
Destination File: my file"222
Error: The filename, directory name, or volume label syntax is incorrect.

H:\Masm32 Forum\getmainarguments> filecopy testfile.txt "my file"2"222
Number of arguments: 3
Source File: testfile.txt
Destination File: my file2222
File Successfully Copied

H:\Masm32 Forum\getmainarguments> filecopy "my file2222" "my file 3333"
Number of arguments: 3
Source File: my file2222
Destination File: my file 3333
File Successfully Copied