Execvp 不执行带参数的 ping 命令
Execvp not executing ping command with arguments
我正在使用 exevcp
系统调用来执行 "ping www.google.com"
。但是,当我执行下面的代码时:
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
int main(int argc, char *argv[]){
vector<char*> pingArgs;
pingArgs.push_back("www.google.com");
pingArgs.push_back(NULL);
execvp("ping", &pingArgs[0]);
return 0;
}
显示以下输出,这意味着我没有提供 link 作为 ping 命令的参数。这看起来很奇怪,考虑到在存储参数的向量中,我清楚地添加了 "www.google.com"
:
Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-l preload] [-m mark] [-M pmtudisc_option]
[-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
[-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
[-W timeout] destination
vector<char*> pingArgs;
pingArgs.push_back("www.google.com");
pingArgs.push_back(NULL);
程序的第一个参数 argv[0]
是程序本身的名称。
在这里,您只是通知 ping
程序它的名称是 www.google.com
,它没有其他参数。
vector<char*> pingArgs;
pingArgs.push_back("ping");
pingArgs.push_back("www.google.com");
pingArgs.push_back(NULL);
execvp
的第一个参数是要执行的可执行文件,但您仍然必须单独提供所有参数。
然而,出于切线原因,以上所有内容都是完全错误的。在现代 C++ 中,字符串文字是 const char *
,而不是 char *
。您必须使用一个古老的 C++ 编译器,它在涉及字符串文字时放宽了 const
-correctness 或未能正确实现它,我希望每个现代 C++ 编译器都因为这个不相关的原因而无法编译显示的代码。
正确地做到这一点需要在 C++ 中做更多的工作,但这与您提出的问题没有直接关系,将是一个单独的问题。
我正在使用 exevcp
系统调用来执行 "ping www.google.com"
。但是,当我执行下面的代码时:
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
int main(int argc, char *argv[]){
vector<char*> pingArgs;
pingArgs.push_back("www.google.com");
pingArgs.push_back(NULL);
execvp("ping", &pingArgs[0]);
return 0;
}
显示以下输出,这意味着我没有提供 link 作为 ping 命令的参数。这看起来很奇怪,考虑到在存储参数的向量中,我清楚地添加了 "www.google.com"
:
Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-l preload] [-m mark] [-M pmtudisc_option]
[-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
[-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
[-W timeout] destination
vector<char*> pingArgs;
pingArgs.push_back("www.google.com");
pingArgs.push_back(NULL);
程序的第一个参数 argv[0]
是程序本身的名称。
在这里,您只是通知 ping
程序它的名称是 www.google.com
,它没有其他参数。
vector<char*> pingArgs;
pingArgs.push_back("ping");
pingArgs.push_back("www.google.com");
pingArgs.push_back(NULL);
execvp
的第一个参数是要执行的可执行文件,但您仍然必须单独提供所有参数。
然而,出于切线原因,以上所有内容都是完全错误的。在现代 C++ 中,字符串文字是 const char *
,而不是 char *
。您必须使用一个古老的 C++ 编译器,它在涉及字符串文字时放宽了 const
-correctness 或未能正确实现它,我希望每个现代 C++ 编译器都因为这个不相关的原因而无法编译显示的代码。
正确地做到这一点需要在 C++ 中做更多的工作,但这与您提出的问题没有直接关系,将是一个单独的问题。