For a project in C, we need to build a shell over a Unix server.
It needs to be able to execute functionality native to Unix, and not bash (or any other Unix shell).
I am writing a method in the hopes to generalize a call to a command. into a general function:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> #include <string.h> #include <limits.h> void execCmd(char* cmd,char* options) { char directory[MAX_CANON] = '/bin/'; //currently is just set to /bin char* toExec = strcat(directory,cmd);
I would like to set the variable ‘directory’ to the proper directory for the Unix command I’m calling.
So, in essence, I would like to be able to use something like char directory[MAX_CANON] = which(cmd);
so that the which function will return me the directory of the command I’m trying to call.
If you feel this is not a good solution, please recommend some other way.
Thanks
EDIT: I guess at worst I could do a massive if-else statement, setting the ‘directory’ vbl based on the cmd parameter.
What you’re asking for is the command ‘which’, ie
On linux it’s a shell script, so take a look a gander.
However, I don’t think this is required to make a UNIX shell. If you type
you’ll find that execlp() and execvp() take care of the details of searching PATH for the given command and executing it.