Posted 19 July 2013 - 09:45 PM
- !!!!!!!!!!!!!!!!!
-
-
Posts:
350
-
Joined:
27-November 07
-
Gender:Male
-
Location:Largo, Fl
-
Project:0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
-
Wiki edits:5

00
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:
Quote
The shell must support the following internal commands:
...
- help - Display the user manual using the more filter.
...
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!
Posted 19 July 2013 - 10:15 PM
- Hot music ~~~~
-
-
Posts:
1716
-
Joined:
06-January 08
-
Gender:Male
-
Location:Estonia, Rapla City
-
Project:Big Neighbor Disturber, Laser Raster Scan Projector
-
Wiki edits:11

00
Load in your text, show segments of it as the user presses some key...?
Posted 20 July 2013 - 12:00 AM
- !!!!!!!!!!!!!!!!!
-
-
Posts:
350
-
Joined:
27-November 07
-
Gender:Male
-
Location:Largo, Fl
-
Project:0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
-
Wiki edits:5

00
I am starting to think I can run it through an exec() command. Here is what I have, but it is not working....
void Help()
{
pid_t pid;
pid = fork(); // create a new process
if (pid < 0)
cout << "Error creating child\n";
else if (pid == 0)
{
char file[1024];
getcwd(file,1024);
strcat(file,"/readme");
// child process
if(execl("/usr/bin/more",file,(char* const*)NULL))
{
cout << "Error associating process with program.\n";
_exit(-1);
}
}
else // parent process
{
wait(NULL); // wait for child process
}
}
This post has been edited by Alriightyman: 20 July 2013 - 12:02 AM
Posted 20 July 2013 - 01:10 AM
- orgasmic
-
-
Posts:
410
-
Joined:
24-July 03

00
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
Posted 20 July 2013 - 10:49 AM
- !!!!!!!!!!!!!!!!!
-
-
Posts:
350
-
Joined:
27-November 07
-
Gender:Male
-
Location:Largo, Fl
-
Project:0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
-
Wiki edits:5

00
Ok, I managed to fix my problem, sort of.
I'll post my fix:
void Help(char* readme)
{
pid_t pid;
pid = fork();
if(pid == -1)
cout << "Error Creating child\n";
else if ( pid == 0)
{
char* arg[4];
arg[0] = (char*)"/usr/bin/more";
arg[1] = (char*)"-d";
arg[2] = readme;
arg[3] = NULL;
if(execv(arg[0],arg))
{
cout << "Error: Cannot open readme file\n";
_exit(-1);
}
}
else if ( pid != 0)
{
wait(NULL);
cout << endl;
}
}
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:
arg[0] = (char*)"/usr/bin/more";
with:
arg[0] = (char*)"/usr/bin/less";
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!
This post has been edited by Alriightyman: 20 July 2013 - 11:13 AM