Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

command shell

jerryjerry Member Posts: 8
Hello, I'm trying to create a simple shell that will handle a command with any number of arguments. I think that this shell will work except that it can not handle any arguments. I know that I will probably need to pass the arguments into an array and use execv, but I'm not quite sure how to implement. Could anybody offer some guidance? Thanks.


#include
#include
#define MAXLINE 4096

int main()
{
char buf[MAXLINE];
pid_t pid;
int status;

printf ("%% ");

while (gets(buf) != NULL)
{
if ((pid = fork()) < 0)
printf("fork error");

else if (pid ==0)
{
execlp (buf, buf, (char *) 0);
printf("couldn't execute: %s
", buf);
exit(127);
}

if ((pid = waitpid (pid, &status, 0)) <0)
printf("waitpid error
");

printf ("%% ");
}
exit (0);
}

Comments

  • malloc_size_tmalloc_size_t Member Posts: 4
    : Hello, I'm trying to create a simple shell that will handle a command with any number of arguments. I think that this shell will work except that it can not handle any arguments. I know that I will probably need to pass the arguments into an array and use execv, but I'm not quite sure how to implement. Could anybody offer some guidance? Thanks.

    Here are some suggestions:
    :
    :
    : #include
    : #include
    #include

    : #define MAXLINE 4096
    :
    : int main()
    : {
    : char buf[MAXLINE];
    : pid_t pid;
    : int status;
    :
    : printf ("%% ");
    :
    /* gets unsafe, use fgets like so */
    while (fgets(buf, sizeof(buf), stdin) != 0)
    /*: while (gets(buf) != NULL) */
    : {
    : if ((pid = fork()) < 0)
    : printf("fork error");
    :
    : else if (pid ==0)
    : {
    /* you will have to parse buf to make sure you are
    separating the command from the arguments, for example
    if the user enters ls -la, then the call to execlp should
    look like execlp("/bin/ls", "/bin/ls", "-la", 0);
    (remember the first argument to any program (argv[0]) is
    the name of the program itself. */
    : execlp (buf, buf, (char *) 0);
    /* also you might want to check the status of execlp */
    : printf("couldn't execute: %s
    ", buf);
    : exit(127);
    : }
    :
    : if ((pid = waitpid (pid, &status, 0)) <0)
    : printf("waitpid error
    ");
    :
    : printf ("%% ");
    : }
    : exit (0);
    : }
    :

  • ishaiishai Member Posts: 5
    First use the strtok to parse your string (you have to prepare an array)

    Like this:

    Char command[256];
    Char *CommVec[32];
    gets(command) /* or fgets*/
    CommVec[0]=(char *)strtok(command," ");
    if (!CommVec[0])
    For i=1 ; CommVec[i]=(char *)strtok(NULL," ")) != NULL ; i++);
    .
    .
    .
    execvp(CommVec[0],CommVec);


    Good luke.

  • Justin BibJustin Bib USAMember Posts: 0

    _________ \ http://forcoder.org \ free ebooks and video tutorials about | Ruby Swift PL/SQL Go Java Assembly Visual Basic PHP Perl Python MATLAB Objective-C JavaScript C# C R C++ Visual Basic .NET Delphi Scratch Ada Prolog D F# Fortran Julia Logo Lua Awk Rust Transact-SQL SAS Apex Alice Clojure Dart Erlang Kotlin Scala Bash Lisp VBScript COBOL Scheme Crystal LabVIEW FoxPro ML ABAP Hack | _________

Sign In or Register to comment.