-
Creating shell in C
I'm trying to create a shell in c. One of the problems Ive ran into is, how to implement input and output redirection and pipes. How can I modify my code here in order to make it support those things?
I can execute single command without a problem but when I try to redirect the output it doesnt work.
Any advice is much appreciated.
Code:
#include <stdio.h>
main()
{
char *path, *argv[20], buf[80], n, *p;
int m, status, inword, continu;
while(1) {
inword = 0;
p = buf;
m = 0;
continu=0;
printf( "\nshhh> ");
while ( ( n = getchar() ) != '\n' || continu ) {
if ( n == ' ' ) {
if ( inword ) {
inword = 0;
*p++ = 0;
}
}
else if ( n == '\n' ) continu = 0;
ele if ( n == '\\' && !inword ) continu = 1;
else {
if ( !inword ) {
inword = 1;
argv[m++] = p;
*p++ = n;
}
else
*p++ = n;
}
}
*p++ = 0;
argv[m] = 0;
if ( strcmp(argv[0],"quit") == 0 ) exit (0);
if ( fork() == 0 )
{
execvp( argv[0], argv );
printf ( " didn't exec \n ");
}
wait(&status);
}
}
-
Please use code tags when posting code. Thanks :)
-
Code:
#include <stdio.h>
int main( void )
{
printf("
############
### \ / ###
## \ \/ / ##
### \ / ###
#### \/ ####
############
A Shell, in C.
");
return 0;
}
It's not a very good shell, but it's the best I could do in a pinch, on the phone, with limited ascii graphics. ;)
Quzah.
-
I apologize for my ignorance, I should have taken the time to read the faq.
-
where were you 8 months ago when I was writing a shell vVv? :)
-
I'm also trying to make a little history function for my shell.
Im new to C so please bear with my simple questions.
In order to print the lines im entering into my shell would i use a line like this?
Code:
while( argv[i] !=0)
{
open("history.dat");
fprintf("history.dat", "%s",argv[i]);
close("history.dat");
}
I know this doesnt work because ive tried it. Am I on the right path with the commands?
Thanks
-
>>In order to print the lines im entering into my shell would i use a line like this?
When you read in the commands, I presume you read them into a char array? If so, I doubt its called argv, as this is the convention used to command line args passed in when the program starts.
You might do something like this:
- Read input from user (fgets)
- Write output to history file (fopen/fprintf/fclose)
- Validate/Action command
Also, you would probably want to consider storing the history in memory as well, so that you can go back without needing to open/read the file. You could do this with an array of char arrays.
-
Im trying to read from the argv I already have that stores the commands the users enter into the shell. I was thinking I could just use that array and spit it out to a file. I didnt know it could be saved in memory. How would I go about doing that?
-
Well, first things first. I suggest you get the shell working properly, then worry about a history element.
But for when you get there... The history would be maintained by the parent process, which is acting as the shell. The child processes, as created by fork() would not log any history as they did not receive input directly from the user.
Now, lets say for example you have buf[80] that holds one command. To keep a history of commands you need something like this:
char history[MAX_NUM_COMMANDS][MAX_COMMAND_LENGTH];
int CurrentHistoryPoint = 0;
When you have read a command into buf, simply copy it to history[CurrentHistoryPoint], then increment CurrentHistoryPoint ready for the next command. This gives you an array of commands that you can then manipulate as you see fit.
There are other ways too, this is only to get you started. :)