从 C++ 调用 cURL 命令 returns 意外错误代码,如 1792 和 6656

Calling cURL command from C++ returns unexpected error codes like 1792 and 6656

我在嵌入式 Linux 上有一个 C++ 应用程序 运行,它构建并调用 cURL 命令以将文件复制到 FTP 服务器。

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

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

根据构建命令的变量,此 returns 意外错误代码。例如,当我尝试复制一个不存在的文件时,retVal 是 6656 而不是预期的 26 ("local file not found"),而当我关闭服务器时,retVal 是1792 而不是预期的 7 ("could not connect to server").

查看这些值,我很确定这与字节顺序有关,但我想了解根本原因。该设备具有 ARMv7 处理器并使用小端格式。

这与字节序无关,但记录在 system 联机帮助页中:

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).

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 (WIFEXITED(retVal)) {
   int retCode = WIFEXITSTATUS(retVal);
   ...
} else if (WIFSIGNALED(retVal) {
   int signal = WTERMSIG(retVal);
   ...
} else {
   /* process was stopped by a signal */
}