Programming a shell..

Discussion in 'Technical Discussion' started by Alriightyman, Jul 20, 2013.

Thread Status:
Not open for further replies.
  1. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    354
    3
    18
    Largo, FL
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    First, this is a homework assignment.
    I am to program a shell in C or C++. It has a variety of commands that I have working, but I am unsure about this one in particular.

    This is from the assignment:
    Now I am getting better at using Ubuntu, and I have looked up the more filter, but how do I use it in a programmatic way? Also, we are NOT to use the system() command.
    The "user manual" is a readme text file.

    Thanks!
     
  2. TmEE

    TmEE

    Master of OPL3-SA2/3 Tech Member
    1,726
    0
    16
    Estonia, Rapla City
    T-04YBSC-A !
    Load in your text, show segments of it as the user presses some key...?
     
  3. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    354
    3
    18
    Largo, FL
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    I am starting to think I can run it through an exec() command. Here is what I have, but it is not working....
    Code (Text):
    1.  
    2. void Help()
    3. {
    4.     pid_t pid;
    5.    
    6.     pid = fork();                               // create a new process
    7.     if (pid < 0)
    8.             cout << "Error creating child\n";
    9.     else if (pid == 0)
    10.     {
    11.         char file[1024];
    12.         getcwd(file,1024);
    13.         strcat(file,"/readme");
    14.  
    15.         // child process
    16.             if(execl("/usr/bin/more",file,(char* const*)NULL))
    17.             {
    18.                     cout << "Error associating process with program.\n";
    19.                         _exit(-1);
    20.             }
    21.     }
    22.     else // parent process
    23.     {
    24.  
    25.         wait(NULL);  // wait for child process
    26.     }
    27. }
    28.  
     
  4. EXOGGEDDEN

    EXOGGEDDEN

    orgasmic Member
    411
    0
    16
    I'd say that after loading the contents of the readme file into memory, write a function that divides the contents into a vector of "chunks", with each chunk having enough text in it to fill up the screen. Then, when displaying the contents of the file, just have the program iterate through the contents of this vector, while sticking it into an endless loop that waits for a certain key press (like the Enter key) in order to proceed into displaying the next item in the vector. Of course, you can also have it look for another key that'd make it go backwards and display the contents of the previous item in the vector =P
     
  5. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    354
    3
    18
    Largo, FL
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    Ok, I managed to fix my problem, sort of.
    I'll post my fix:
    Code (Text):
    1.  
    2. void Help(char* readme)
    3. {
    4.     pid_t pid;
    5.     pid = fork();
    6.     if(pid == -1)
    7.             cout << "Error Creating child\n";
    8.     else if ( pid == 0)
    9.     {
    10.             char* arg[4];
    11.             arg[0] = (char*)"/usr/bin/more";
    12.             arg[1] = (char*)"-d";
    13.             arg[2] = readme;
    14.             arg[3] = NULL;
    15.        
    16.             if(execv(arg[0],arg))
    17.             {
    18.                     cout << "Error: Cannot open readme file\n";
    19.                     _exit(-1);
    20.             }
    21.     }
    22.     else if ( pid != 0)
    23.     {
    24.             wait(NULL);
    25.             cout << endl;
    26.     }
    27. }
    28.  

    Now, the above code works on my Mac, but not under Ubuntu. It seems that either they moved the more filter somewhere else, or they just got rid of it in favor or the less filter.
    If I replace:
    Code (Text):
    1.  
    2. arg[0] = (char*)"/usr/bin/more";
    3.  
    with:
    Code (Text):
    1.  
    2. arg[0] = (char*)"/usr/bin/less";
    3.  
    Then it works under Ubuntu and Mac.

    Thanks for the suggestions, however, he wanted me to use the more filter, not recreate it.


    EDIT: Fixed! I was looking in the wrong directory. more is in /bin not /usr/bin. Hope this helps someone in the future!
     
Thread Status:
Not open for further replies.