Fix #3719 : mixing -c, -o and --rm
`-c` disables `--rm`, but only if it's selected.
In situations where `-o` is in the same command and happens to be present after `-c`, `-o` ends up being the selected one, and the different rules of `-o` become applicable (essentially `-o` respects `--rm` if there is only 1 input file).

fix #3719
**Describe the bug**
zstd won't remove the file after compression when `--rm` argument is passed.

**To Reproduce**
Steps to reproduce the behavior:
1. `zstd --rm -f -T0 -8qc dir/file -o dir/file.zst`
2. `ls dir/file`
3. `file` is still present

If `--rm` is placed as a last argument, for example `zstd -T0 -8qc --rm dir/file -o dir/file.zst`, removal works as expected.

**Expected behavior**
File is removed even when `--rm` is not last argument.

**Desktop (please complete the following information):**
 - OS: Debian 12 (bookworm) and Debian 13 (trixie)
 - Version `1.5.4+dfsg2-5` and `1.5.5+dfsg2-1`

zstd won't remove the file after compression unless `--rm` is last argument
**Describe the bug**
zstd won't remove the file after compression when `--rm` argument is passed.

**To Reproduce**
Steps to reproduce the behavior:
1. `zstd --rm -f -T0 -8qc dir/file -o dir/file.zst`
2. `ls dir/file`
3. `file` is still present

If `--rm` is placed as a last argument, for example `zstd -T0 -8qc --rm dir/file -o dir/file.zst`, removal works as expected.

**Expected behavior**
File is removed even when `--rm` is not last argument.

**Desktop (please complete the following information):**
 - OS: Debian 12 (bookworm) and Debian 13 (trixie)
 - Version `1.5.4+dfsg2-5` and `1.5.5+dfsg2-1`

zstd won't remove the file after compression unless `--rm` is last argument
diff --git a/programs/zstd.1.md b/programs/zstd.1.md
index c5d0ef70a36..646e3cf28eb 100644
--- a/programs/zstd.1.md
+++ b/programs/zstd.1.md
@@ -225,15 +225,17 @@ the last one takes effect.
     This parameter defines a loose target: compressed blocks will target this size "on average", but individual blocks can still be larger or smaller.
     Enabling this feature can decrease compression speed by up to ~10% at level 1.
     Higher levels will see smaller relative speed regression, becoming invisible at higher settings.
-* `-o FILE`:
-    save result into `FILE`.
 * `-f`, `--force`:
     disable input and output checks. Allows overwriting existing files, input
     from console, output to stdout, operating on links, block devices, etc.
     During decompression and when the output destination is stdout, pass-through
     unrecognized formats as-is.
 * `-c`, `--stdout`:
-    write to standard output (even if it is the console); keep original files unchanged.
+    write to standard output (even if it is the console); keep original files (disable `--rm`).
+* `-o FILE`:
+    save result into `FILE`.
+    Note that this operation is in conflict with `-c`.
+    If both operations are present on the command line, the last expressed one wins.
 * `--[no-]sparse`:
     enable / disable sparse FS support,
     to make files with many zeroes smaller on disk.
diff --git a/programs/zstdcli.c b/programs/zstdcli.c
index 1cb16ef50e7..9dd6b051a7b 100644
--- a/programs/zstdcli.c
+++ b/programs/zstdcli.c
@@ -962,7 +962,7 @@ int main(int argCount, const char* argv[])
                 if (!strcmp(argument, "--help")) { usageAdvanced(programName); CLEAN_RETURN(0); }
                 if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
                 if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
-                if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; removeSrcFile=0; continue; }
+                if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; continue; }
                 if (!strcmp(argument, "--ultra")) { ultra=1; continue; }
                 if (!strcmp(argument, "--check")) { FIO_setChecksumFlag(prefs, 2); continue; }
                 if (!strcmp(argument, "--no-check")) { FIO_setChecksumFlag(prefs, 0); continue; }
@@ -1176,7 +1176,10 @@ int main(int argCount, const char* argv[])
                         operation=zom_decompress; argument++; break;
 
                     /* Force stdout, even if stdout==console */
-                case 'c': forceStdout=1; outFileName=stdoutmark; removeSrcFile=0; argument++; break;
+                case 'c': forceStdout=1; outFileName=stdoutmark; argument++; break;
+
+                    /* destination file name */
+                case 'o': argument++; NEXT_FIELD(outFileName); break;
 
                     /* do not store filename - gzip compatibility - nothing to do */
                 case 'n': argument++; break;
@@ -1202,9 +1205,6 @@ int main(int argCount, const char* argv[])
                     /* test compressed file */
                 case 't': operation=zom_test; argument++; break;
 
-                    /* destination file name */
-                case 'o': argument++; NEXT_FIELD(outFileName); break;
-
                     /* limit memory */
                 case 'M':
                     argument++;

diff --git a/tests/playTests.sh b/tests/playTests.sh
index bf5fba89b35..dc7794654aa 100755
--- a/tests/playTests.sh
+++ b/tests/playTests.sh
@@ -234,12 +234,23 @@ unset ZSTD_CLEVEL
 println "test : compress to stdout"
 zstd tmp -c > tmpCompressed
 zstd tmp --stdout > tmpCompressed       # long command format
-println "test : compress to named file"
+
+println "test : compress to named file (-o)"
 rm -f tmpCompressed
 zstd tmp -o tmpCompressed
 test -f tmpCompressed   # file must be created
+
 println "test : force write, correct order"
 zstd tmp -fo tmpCompressed
+
+println "test : -c + -o : last one wins"
+rm -f tmpOut
+zstd tmp -c > tmpCompressed -o tmpOut
+test -f tmpOut   # file must be created
+rm -f tmpCompressed
+zstd tmp -o tmpOut -c > tmpCompressed
+test -f tmpCompressed   # file must be created
+
 println "test : forgotten argument"
 cp tmp tmp2
 zstd tmp2 -fo && die "-o must be followed by filename "
@@ -394,6 +405,8 @@ println "test: --rm is disabled when output is stdout"
 test -f tmp
 zstd --rm tmp -c > $INTOVOID
 test -f tmp # tmp shall still be there
+zstd --rm tmp --stdout > $INTOVOID
+test -f tmp # tmp shall still be there
 zstd -f --rm tmp -c > $INTOVOID
 test -f tmp # tmp shall still be there
 zstd -f tmp -c > $INTOVOID --rm
@@ -411,7 +424,22 @@ zstd -f tmp tmp2 -o tmp3.zst --rm # just warns, no prompt
 test -f tmp
 test -f tmp2
 zstd -q tmp tmp2 -o tmp3.zst --rm && die "should refuse to concatenate"
-
+println "test: --rm is active with -o when single input"
+rm -f tmp2.zst
+zstd --rm tmp2 -o tmp2.zst
+test -f tmp2.zst
+test ! -f tmp2
+println "test: -c followed by -o => -o wins, so --rm remains active" # (#3719)
+rm tmp2.zst
+cp tmp tmp2
+zstd --rm tmp2 -c > $INTOVOID -o tmp2.zst
+test ! -f tmp2
+println "test: -o followed by -c => -c wins, so --rm is disabled" # (#3719)
+rm tmp3.zst
+cp tmp tmp2
+zstd -v --rm tmp2 -o tmp2.zst -c > tmp3.zst
+test -f tmp2
+test -f tmp3.zst
 println "test : should quietly not remove non-regular file"
 println hello > tmp
 zstd tmp -f -o "$DEVDEVICE" 2>tmplog > "$INTOVOID"

{"cltools/zstdgrep.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/verbose-wlog.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/levels.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/multi-threaded.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/compress-stdin-to-file.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/decompress-file-to-file.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/adapt.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "decompression/detectErrors.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/stream-size.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/decompress-stdin-to-file.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "dict-builder/no-inputs.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "dict-builder/empty-input.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/format.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/row-match-finder.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/basic.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/compress-file-to-file.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "zstd-symlinks/zstdcat.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/golden.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "progress/no-progress.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "dictionaries/dictionary-mismatch.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/multiple-files.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/decompress-file-to-stdout.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/window-resize.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/version.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/output_dir.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cltools/zstdless.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "decompression/golden.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "progress/progress.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/compress-literals.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "decompression/pass-through.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/long-distance-matcher.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/memlimit.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/decompress-stdin-to-stdout.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/compress-file-to-stdout.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/args.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/gzip-compat.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "dictionaries/golden.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/help.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/compress-stdin-to-stdout.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}}
{"cltools/zstdgrep.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/verbose-wlog.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/levels.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/multi-threaded.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/compress-stdin-to-file.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/decompress-file-to-file.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/adapt.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "decompression/detectErrors.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/stream-size.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/decompress-stdin-to-file.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "dict-builder/no-inputs.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "dict-builder/empty-input.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/format.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/row-match-finder.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/basic.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/compress-file-to-file.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "zstd-symlinks/zstdcat.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/golden.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "progress/no-progress.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "dictionaries/dictionary-mismatch.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/multiple-files.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/decompress-file-to-stdout.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/window-resize.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/version.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/output_dir.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cltools/zstdless.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "decompression/golden.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "progress/progress.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/compress-literals.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "decompression/pass-through.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/long-distance-matcher.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/memlimit.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/decompress-stdin-to-stdout.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/compress-file-to-stdout.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/args.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "compression/gzip-compat.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "dictionaries/golden.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "basic/help.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "file-stat/compress-stdin-to-stdout.sh": {"run": "PASS", "test": "NONE", "fix": "PASS"}}
{"passed_count": 39, "failed_count": 2, "skipped_count": 0, "passed_tests": ["cltools/zstdgrep.sh", "compression/verbose-wlog.sh", "compression/levels.sh", "compression/multi-threaded.sh", "file-stat/compress-stdin-to-file.sh", "file-stat/decompress-file-to-file.sh", "compression/adapt.sh", "decompression/detectErrors.sh", "compression/stream-size.sh", "file-stat/decompress-stdin-to-file.sh", "dict-builder/no-inputs.sh", "dict-builder/empty-input.sh", "compression/format.sh", "compression/row-match-finder.sh", "compression/basic.sh", "file-stat/compress-file-to-file.sh", "zstd-symlinks/zstdcat.sh", "compression/golden.sh", "progress/no-progress.sh", "dictionaries/dictionary-mismatch.sh", "compression/multiple-files.sh", "file-stat/decompress-file-to-stdout.sh", "compression/window-resize.sh", "basic/version.sh", "basic/output_dir.sh", "cltools/zstdless.sh", "decompression/golden.sh", "progress/progress.sh", "compression/compress-literals.sh", "decompression/pass-through.sh", "compression/long-distance-matcher.sh", "basic/memlimit.sh", "file-stat/decompress-stdin-to-stdout.sh", "file-stat/compress-file-to-stdout.sh", "basic/args.sh", "compression/gzip-compat.sh", "dictionaries/golden.sh", "basic/help.sh", "file-stat/compress-stdin-to-stdout.sh"], "failed_tests": ["file-stat/compress-file-to-dir-without-write-perm.sh.check_stderr", "file-stat/compress-file-to-dir-without-write-perm.sh"], "skipped_tests": []}
{"passed_count": 0, "failed_count": 0, "skipped_count": 0, "passed_tests": [], "failed_tests": [], "skipped_tests": []}
{"passed_count": 39, "failed_count": 2, "skipped_count": 0, "passed_tests": ["cltools/zstdgrep.sh", "compression/verbose-wlog.sh", "compression/levels.sh", "compression/multi-threaded.sh", "file-stat/compress-stdin-to-file.sh", "file-stat/decompress-file-to-file.sh", "compression/adapt.sh", "decompression/detectErrors.sh", "compression/stream-size.sh", "file-stat/decompress-stdin-to-file.sh", "dict-builder/no-inputs.sh", "dict-builder/empty-input.sh", "compression/format.sh", "compression/row-match-finder.sh", "compression/basic.sh", "file-stat/compress-file-to-file.sh", "zstd-symlinks/zstdcat.sh", "compression/golden.sh", "progress/no-progress.sh", "dictionaries/dictionary-mismatch.sh", "compression/multiple-files.sh", "file-stat/decompress-file-to-stdout.sh", "compression/window-resize.sh", "basic/version.sh", "basic/output_dir.sh", "cltools/zstdless.sh", "decompression/golden.sh", "progress/progress.sh", "compression/compress-literals.sh", "decompression/pass-through.sh", "compression/long-distance-matcher.sh", "basic/memlimit.sh", "file-stat/decompress-stdin-to-stdout.sh", "file-stat/compress-file-to-stdout.sh", "basic/args.sh", "compression/gzip-compat.sh", "dictionaries/golden.sh", "basic/help.sh", "file-stat/compress-stdin-to-stdout.sh"], "failed_tests": ["file-stat/compress-file-to-dir-without-write-perm.sh.check_stderr", "file-stat/compress-file-to-dir-without-write-perm.sh"], "skipped_tests": []}