WHAT is the PROBLEM??

This is a discussion on WHAT is the PROBLEM?? within the Linux Programming forums, part of the Platform Specific Boards category; so I have this mini-shell running....and it's forking correctly but It's not executing the commands that I want it to ...

  1. #1
    Unregistered
    Guest

    WHAT is the PROBLEM??

    so I have this mini-shell running....and it's forking correctly but It's not executing the commands that I want it to run. It only executes ls no matter what command i type in (date, more, etc). I've modified the execve statement as many ways as I can think of, but to no avail. Here is my code:

    mars:$ cat modshell1.cpp
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <string>
    #include <iomanip.h>
    #include <sys/wait.h>

    main()
    {
    cout<<"You have entered my 1814 shell"<<endl;
    string command;
    char *line[20];
    int n, pid, parentpid, childpids[0];


    while (command !="exit")
    {

    cout<<"GIMME:#";
    cin>>command;
    cout<<"The command you typed was: " <<command<<endl;
    if (command == "exit")
    return 0;

    //end of while

    parentpid=getpid();
    printf("I am the parent process and my pid is %d\n", getpid());
    if (pid=fork())
    {
    int status;
    waitpid(-1, &status, 0);
    childpids[n]=pid;
    }
    else
    {
    printf("I am a child process and my pid is %d\n", getpid());
    line[0] = "ls";
    line[1] = "-la";
    line[2] = NULL;
    execve("/bin/ls", line, NULL);
    exit(0);
    }
    printf("I am still the parent process and my pid is %d\n",
    getpid());

    // line[0] = "ls";
    // line[1] = "-la";
    // line[2] = NULL;

    // execve("/bin/ls", line, NULL);
    // kill(childpids[n]);
    }//end of while
    if (command =="exit")
    return 0;

    printf("I am still the parent process and my pid is %d\n", getpid());
    }//end of main

  2. #2
    Unregistered
    Guest

    here it is cleaned up

    I guess I could have cleaned that up, huh? here it is:

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <string>
    #include <iomanip.h>
    #include <sys/wait.h>

    main()
    {
    cout<<"You have entered my 1814 shell"<<endl;
    string command;
    char *line[20];
    int n, pid, parentpid, childpids[0];


    while (command !="exit")
    {

    cout<<"GIMME:#";
    cin>>command;
    cout<<"The command you typed was: " <<command<<endl;
    if (command == "exit")
    return 0;

    parentpid=getpid();
    printf("I am the parent process and my pid is %d\n", getpid());
    if (pid=fork())
    {
    int status;
    waitpid(-1, &status, 0);
    childpids[n]=pid;
    }
    else
    {
    printf("I am a child process and my pid is %d\n", getpid());
    line[0] = "ls";
    line[1] = "-la";
    line[2] = NULL;
    execve("/bin/ls", line, NULL);
    exit(0);
    }
    printf("I am still the parent process and my pid is %d\n",
    getpid());
    }//end of while
    if (command =="exit")
    return 0;

    printf("I am still the parent process and my pid is %d\n", getpid());
    }//end of main

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > cin>>command;
    Well you have to break the command up into words, like "ls", "-la"

    Ok, so there's another problem, which is cin only reads one word at a time, so how about

    cin.getline( command );

    Right, having got a string of space separated words, you need to split them up into words

    For example
    char *str = command.c_str(); // is this how you get at the string within a c++ string object?

    Then to actually break out the words
    Code:
    char *p;
    int i = 0;
    for ( p = strtok( str, " " ); p != NULL ; p = strtok( NULL, " " ) ) {
      line[i++] = p;
    }
    line[i] = NULL;
    Then it should be
    execve("/bin/ls", line, NULL);

    Or perhaps
    execve(line[0], line, NULL);

    One more thing
    if (pid=fork())

    Should be
    if ( (pid=fork()) != 0 )

    In this example, it doesn't much matter, but you will be badly burnt in future if you type = when you meant ==

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21