execvp不会打印echo命令

时间:2018-10-26 14:11:08

标签: c exec

我试图理解execvp命令,当我键入/ bin / ls时,它会打印出正确的输出,但是当我打印echo内容时,它不会输出。

char* argumentCommand[11];
char args[10][256]; // inside of this are arguments 
 //args[0] = echo args[1] = "some text"
  //point it to the argument so i can pass it as a vector
  for(int i = 0; i<10;i++)
  {
      argumentCommand[i] = args[i];
  }

  //wont print anything when i type echo something or wont take the second parameter when i use /bin/ls -l
  execvp(argumentCommand[0],argumentCommand);

有人知道错误在哪里吗?

2 个答案:

答案 0 :(得分:2)

总结一下:

您需要使用argumentCommand指针终止自变量向量NULL,并且需要在正确的位置进行操作! 因此,如果您有三个参数:

char* argumentCommand[11];
char args[10][256]; // inside of this are arguments
strcpy(args[0], "ls");
strcpy(args[1], "-l");
strcpy(args[2], "/");

就像您一样填充参数Command-vector:

for(int i = 0; i<10;i++)
{
    argumentCommand[i] = args[i];
}

您仍然必须在 third 自变量之后对它进行NULL终止:

argumentCommand[3] = NULL;

执行前:

execvp(argumentCommand[0],argumentCommand);

然后它将按预期工作。

否则,argumentsVector要么根本不为NULL终止,从而导致-EFAULT上的execvp(),要么偶然地NULL终止于内存中的某个位置,从而导致该命令接收到许多伪随机参数,从而导致意外行为。

答案 1 :(得分:0)

替换此

name: vertretungsplan
description: DSB Interpreter

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# Read more about versioning at semver.org.
version: 1.0.0+1

environment:
  sdk: ">=2.0.0-dev.68.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  html: any
  http: any
  uuid: ^1.0.0
  intl: ^0.15.7
  archive: ^2.0.4
  path_provider: ^0.4.1

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

使用

  execvp(argument[0],argumentCommand);

如果 execvp(argumentCommand[0],argumentCommand + 1); 持有正确的可执行文件(带有有效选项且终止为null)。对于例如

argumentCommand