0

I have a C++ application running on an embedded Linux that builds and calls cURL commands to copy files to an FTP server.

std::string cmd = "curl --connect-timeout 10 --ftp-create-dirs "
                  + localPath + filename + " "
                  + ftpPath + filename + " "
                  + userAuth;

int retVal = system(cmd.c_str());

Depending on the variables to build the command, this returns unexpected error codes. For example, when I try to copy a file that doesn't exist, retVal is 6656 instead of the expected 26 ("local file not found"), and when I turn off the server, retVal is 1792 instead of the expected 7 ("could not connect to server").

Looking at the values, I am pretty sure that this has something to do with endianness, but I would like to understand the root cause. The device has an ARMv7 processor and uses little endian format.

1 Answers1

1

This has nothing to do with endianness, but is documented in the system manpage:

In the last two cases, the return value is a "wait status" that can be examined using the macros described in waitpid(2). (i.e., WIFEXITED(), WEXITSTATUS(), and so on).

and WEXITSTATUS:

WEXITSTATUS(wstatus) returns the exit status of the child.
This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main().
This macro should be employed only if WIFEXITED returned true.

If you just want to know if the command succeeded, you should use:

if (WIFEXITED(retVal)) {
   int retCode = WIFEXITSTATUS(retVal);
   ...
} else if (WIFSIGNALED(retVal) {
   int signal = WTERMSIG(retVal);
   ...
} else {
   /* process was stopped by a signal */
}
Botje
  • 26,269
  • 3
  • 31
  • 41
  • Interesting, thank you! Seems like I looked into the wrong direction and it's not actually a curl problem. So if I want to check for specific error codes, I could do something like `switch (WEXITSTATUS(retVal)) { case 7: ... }` ? – monsterbacke Dec 03 '19 at 09:25
  • Yes. Assuming you check for `WIFEXITED(retVal)` first. – Botje Dec 03 '19 at 09:25