How can I write commands to the vxworks shell with a c program

Jason picture Jason · Oct 14, 2010 · Viewed 9.9k times · Source

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);  
}  

Answer

Jason picture Jason · Oct 18, 2010

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);
}