Skip to content

copy: fix file concatenation when invoked via COMMAND /C#117

Merged
stsp merged 1 commit intodosemu2:masterfrom
tgies:fix-expand-pluses-trailing-space
Feb 5, 2026
Merged

copy: fix file concatenation when invoked via COMMAND /C#117
stsp merged 1 commit intodosemu2:masterfrom
tgies:fix-expand-pluses-trailing-space

Conversation

@tgies
Copy link
Contributor

@tgies tgies commented Feb 4, 2026

The COPY command with file concatenation (e.g., copy /b a+b dest) fails with "Source and destination cannot match" when invoked via COMMAND /C, such as when nmake executes makefile commands.

Root cause: The /C handler builds cmd_line by concatenating argv elements with spaces, always adding a trailing space after the last argument. In expand_pluses(), strrchr(cmd_args, ' ') then finds this trailing space instead of the space before the destination filename, causing the destination to be effectively empty.

Example with trailing space from /C:

Input: "/b msload.com+msbio.cl1 io.sys "
last_arg = strrchr(..., ' ') -> points to trailing " "
Output: "/b msload.com  ;msbio.cl1 io.sys "
Result: first file group has no destination, defaults to source

This bug was introduced in commit 234e221 ("copy: support any amount of pluses [fixes #94]"). The previous implementation was immune to trailing spaces because it used forward-searching (strchr from after the +) rather than backward-searching (strrchr from end of string).

Fix:

  1. Return early if no + in cmd_args (restores old behavior for simple copy commands)
  2. Strip trailing spaces from cmd_args_bkp before finding last_arg

The early exit is necessary because stripping trailing spaces from a single-argument copy like copy file (which becomes copy file via COMMAND /C) would leave no spaces, causing a spurious "syntax error".

tgies added a commit to tgies/MS-DOS that referenced this pull request Feb 4, 2026
Workaround for dosemu2/comcom64#117 - the copy command fails with
'Source and destination cannot match' when using file concatenation
(copy a+b dest) via COMMAND /C, which breaks the IO.SYS build step.
The COPY command with file concatenation (e.g., `copy /b a+b dest`)
fails with "Source and destination cannot match" when invoked via
`COMMAND /C`, such as when nmake executes makefile commands.

Root cause: The /C handler builds cmd_line by concatenating argv
elements with spaces, always adding a trailing space after the last
argument. In expand_pluses(), `strrchr(cmd_args, ' ')` then finds
this trailing space instead of the space before the destination
filename, causing the destination to be effectively empty.

Example with trailing space from /C:
  Input: "/b msload.com+msbio.cl1 io.sys "
  last_arg = strrchr(..., ' ') -> points to trailing " "
  Output: "/b msload.com  ;msbio.cl1 io.sys "
  Result: first file group has no destination, defaults to source

This bug was introduced in commit 234e221 ("copy: support any amount
of pluses [fixes dosemu2#94]"). The previous implementation was immune to
trailing spaces because it used forward-searching (strchr from after
the +) rather than backward-searching (strrchr from end of string).

Fix: Strip trailing spaces from cmd_args_bkp before finding last_arg.
@tgies tgies force-pushed the fix-expand-pluses-trailing-space branch from 66f44d9 to b4c6759 Compare February 4, 2026 12:30
@stsp
Copy link
Member

stsp commented Feb 4, 2026

I believe the CI patch went
in by mistake. Please remove
it and I'll apply.

@tgies
Copy link
Contributor Author

tgies commented Feb 5, 2026

The CI patch isn't part of this PR -- GitHub is just showing it in the summary because I referenced this PR from a separate commit in my tgies/MS-DOS repo. "tgies added a commit to tgies/MS-DOS that referenced this pull request"

@stsp stsp merged commit dda293d into dosemu2:master Feb 5, 2026
2 checks passed
@stsp
Copy link
Member

stsp commented Feb 5, 2026

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Too many parameters error

2 participants