c - 在 C/Linux 中返回什么系统函数和 cp 命令

标签 c linux

这里我在 linux 上的代码中使用了下面的代码。 在系统函数中使用 cp 命令。

我知道系统函数,如果命令成功执行,它会返回 0。否则它会返回错误代码。

如果我在这里使用正确的源路径和目标路径,那么我会得到这样的输出

Number == 0

如果我给出错误的源路径和目标路径而不是我得到的

cp: cannot create regular file `/home/sam/test/test': No such file or directory
Number == 256

cp: cannot stat `/home/sam/main/test2/test': Not a directory
Number == 256

这里我想知道cp命令的错误码what cp command return here.

我的问题在这里

1 System function return error code of cp command?
2 Can i get error code of cp command from source code of cp command?
3 i want to handle all types of error in this cp command.

代码:

#include <stdlib.h>
#include <stdio.h>

void main()
{
    int a;
    a = system("cp /home/sam/main/test /home/sam");
    printf("Number == %d\n",a);
}

所以任何人都请向我解释这一切

最佳答案

使用系统返回值的正确方法是使用 wait-specific macros .

if (WIFEXITED(a)) {
    int rc;
    rc = WEXITSTATUS(a);
    printf("Exit with status: %d\n", rc);
} else {
    /* Killed by a signal. */
}

关于c - 在 C/Linux 中返回什么系统函数和 cp 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803371/

相关文章:

c - 如何编译这个 Linux 程序?

c++ - Valgrind 在 pthread_spin_lock 中挂起,消耗 100% CPU

java - 用 Java 聚合来自远程 Linux 服务器的日志文件?

c++ - 如何在64位x86环境下编译32位x86应用程序?

ios - 来自导入的 unsigned char 二维数组的 Swift 字符串

c - 正确发送 RST 数据包到 TCP 客户端和服务器作为网关

c++ - 嵌入式 Windows XP 中的网络接口(interface)设置

linux - 在 bash 中从 google 页面提取链接

linux - ChefDK Policyfile工作流程——如何上传第三方cookbooks?

linux - 控制台上的 SSH 谷歌云权限被拒绝(公钥),并出现 google-cloud-sdk 文件错误