如何使用 ac 程序向 vxworks shell 写入命令
如果我想在 linux 中使用 ac 程序运行 shell 命令,我会使用
system("ls");
Is there a way I can Complete this in Wind River vxworks?
我找到了下面的示例,但我想知道我是否需要包含 vxworks 头文件才能使其工作?我想是的,但是我怎么知道是哪一个呢?
例子:
// This function runs a shell command and captures the output to the
// specified file
//
extern int consoleFd;
typedef unsigned int (*UINTFUNCPTR) ();
extern "C" int shellToFile(char * shellCmd, char * outputFile)
{
int rtn;
int STDFd;
int outFileFd;
outFileFd = creat( outputFile, O_RDWR);
printf("creat returned %x as a file desc\n",outFileFd);
if (outFileFd != -1)
{
STDFd=ioGlobalStdGet(STD_OUT);
ioGlobalStdSet(STD_OUT,outFileFd);
rtn=execute(shellCmd);
if (rtn !=0)
printf("execute returned %d \n",outFileFd);
ioGlobalStdSet(STD_OUT,STDFd);
}
close(outFileFd);
return (rtn);
}
If I wanted to run a shell command in linux with a c program, I would use
system("ls");
Is there a way I can accomplish this in Wind River vxworks?
I found the below example but I'm wondering do I need to include vxworks header files for this to work? I assume I do, but how do I figure out which one?
Example:
// This function runs a shell command and captures the output to the
// specified file
//
extern int consoleFd;
typedef unsigned int (*UINTFUNCPTR) ();
extern "C" int shellToFile(char * shellCmd, char * outputFile)
{
int rtn;
int STDFd;
int outFileFd;
outFileFd = creat( outputFile, O_RDWR);
printf("creat returned %x as a file desc\n",outFileFd);
if (outFileFd != -1)
{
STDFd=ioGlobalStdGet(STD_OUT);
ioGlobalStdSet(STD_OUT,outFileFd);
rtn=execute(shellCmd);
if (rtn !=0)
printf("execute returned %d \n",outFileFd);
ioGlobalStdSet(STD_OUT,STDFd);
}
close(outFileFd);
return (rtn);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现下面的代码段对我有用。由于某种原因,更改 globalStdOut 不起作用。另外,执行功能对我不起作用。但是我将特定任务设置到我的文件中,我能够获得我需要的数据。
I found the code segment below worked for me. For some reason changing the globalStdOut didn't work. Also the execute function did not work for me. But my setting the specific task out to my file, I was able to obtain the data I needed.
如果这是目标/内核 shell(即在目标本身上运行),请记住所有 shell 命令都只是转换为函数调用。
因此“ls”实际上是对 ls() 的调用,我相信它是在 dirLib.h 中声明的
If this is a target/kernel shell (i.e. running on the target itself), then remember that all the shell commands are simply translated to function calls.
Thus "ls" really is a call to ls(), which I believe is declared in dirLib.h
我认为
ExecCmd
函数就是您正在寻找的。http://www.dholloway.com/vxworks/6.5/man/cat2 /ExecCmd.shtml
I think that the
ExecCmd
function is what you are looking for.http://www.dholloway.com/vxworks/6.5/man/cat2/ExecCmd.shtml
与以往一样,请阅读文档。该示例中使用的大多数函数都需要 ioLib.h,当然 printf() 也需要 stdio.h。
至于是否需要包含任何特定标头来编译任何代码的一般问题,您确实需要声明使用的所有符号,通常这意味着包含适当的标头。编译器很快就会通过警告或错误的方式告诉您任何未定义的符号(在 C89/90 中,未定义的函数不是错误,只是一个坏主意)。
As ever, read the documentation. ioLib.h is required for most of the functions used in that example, and stdio.h of course for printf().
As to the general question of whether you need to include any particular headers for any code to compile, you do need to declare all symbols used, and generally that means including appropriate headers. The compiler will soon tell you about any undefined symbols, either by warning or error (in C89/90 undefined functions are not an error, just a bad idea).