如何使用 ac 程序向 vxworks shell 写入命令

发布于 2024-09-27 04:43:50 字数 936 浏览 5 评论 0原文

如果我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苄①跕圉湢 2024-10-04 04:43:50

我发现下面的代码段对我有用。由于某种原因,更改 globalStdOut 不起作用。另外,执行功能对我不起作用。但是我将特定任务设置到我的文件中,我能够获得我需要的数据。

/* This function directs the output from the devs command into a new file*/

int devsToFile(const char * outputFile)  
{  
    int stdTaskFd;  
    int outputFileFd;

    outputFileFd = creat( outputFile, O_RDWR);

    if (outputFileFd != ERROR)
    {
        stdTaskFd = ioTaskStdGet(0,1);
        ioTaskStdSet(0,1,outputFileFd);
        devs();
        ioTaskStdSet(0,1,stdTaskFd);
        close(outputFileFd);
        return (OK);
    }
    else
        return (ERROR);
}

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.

/* This function directs the output from the devs command into a new file*/

int devsToFile(const char * outputFile)  
{  
    int stdTaskFd;  
    int outputFileFd;

    outputFileFd = creat( outputFile, O_RDWR);

    if (outputFileFd != ERROR)
    {
        stdTaskFd = ioTaskStdGet(0,1);
        ioTaskStdSet(0,1,outputFileFd);
        devs();
        ioTaskStdSet(0,1,stdTaskFd);
        close(outputFileFd);
        return (OK);
    }
    else
        return (ERROR);
}
野鹿林 2024-10-04 04:43:50

如果这是目标/内核 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

浪漫人生路 2024-10-04 04:43:50

我认为 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

自由如风 2024-10-04 04:43:50

与以往一样,请阅读文档。该示例中使用的大多数函数都需要 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文