tags:

views:

86

answers:

4

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

I think that the ExecCmd function is what you are looking for.

http://www.dholloway.com/vxworks/6.5/man/cat2/ExecCmd.shtml

nategoose
I will investigate this one also, thank you.
Jason
+1  A: 

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

Benoit
This is true. I can think of few reasons to inject a command string into the shell unless the command or expression were not known at compile time (maybe user entered for from a script file). However the shell is capable of performing runtime C expression evaluation which may be useful (infrequently perhaps).
Clifford
This is very helpful information.
Jason
A: 

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).

Clifford
Thanks for the feedback. I am a complete vxworks novice which is perhaps why I was having trouble finding the api. (I know sounds ridiculous) Anyhow, thanks for the link, that should help a lot.
Jason
@Jason: I just happend to find that copy of VxWorks documentation by Google. Rather rely on some university hosted copy of probably old documentation, you should use the documentation provided with your VxWorks development tools and license. You can also access the official documentation on-line at http://www.windriver.com/support/
Clifford
A: 

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

}

Jason