cp and rm return non zero value

rm and cp return non zero values sometimes.

sprintf(tmp, "rm -rf %s/* 2>/dev/null\n", dest);
rc = system( tmp );
rc = 1 and errno =0

sprintf(tmp, "cp -r %s/* %s 2>/dev/null", source, dest);
rc = system( tmp );
rc = -1 and errno = 2.

The files exist and the paths are correct. I can do a manual copy but the application fails. If I restart the application, this works fine.
Can anyone tell why this could happen?

Check what the current directory is when the program is running?

The program is running from a location different from the source or destination directories.

Does the application change the current directory or use fully qualified paths?

The application does not change the current directory and source and destination directories are FQPs.

  1. You don't need the "\n" at the end of the string for system()

  2. Try without the "2>/dev/null" and it might tell you why.

Thanks!
I can try that, but I really don't know when it would fail, because the program works 99.9% of the times.

also try using absolute path names for the standard utilities like rm and cp when using within a system function call !