#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
void swap (char **p,char **q)
{
Char *tmp=*p;
*p=*q;
*q=tmp;
}
int main (int argc,char *argv[])
{
pid_t ID;
while (1) {
printf ("[[email protected] fangfang]#");
Fflush (stdout);
Char cmd_str[1024];
memset (Cmd_str, ' n ', sizeof (CMD_STR));
ssize_t Size=read (0,cmd_str,sizeof (CMD_STR)-1);
if (size>=0) {
cmd_str[size-1]= ' + ';
}
Char *my_argv[32];
int index=0;
int Len=strlen (CMD_STR)-1;
while (len-->0) {
if (cmd_str[len]== ' && (cmd_str[len+1] = = "' | | cmd_str[len+1]== ')") {
len--;
}else if (cmd_str[len]== ") {
my_argv[index++]=&cmd_str[len+1];
cmd_str[len]= ' + ';
}
}
if (cmd_str[0]!= ")
{
MY_ARGV[INDEX++]=CMD_STR;
my_argv[index]= ' + ';
}
int j=index-1;
int i=0;
while (I<J) {
Swap (&my_argv[i++],&my_argv[j--]);
}
for (i=0;i<index;++i)
//{
printf ("%s\n", My_argv[i]);
//}
if (strcmp ("CD", My_argv[0]==0)) {
ChDir (my_argv[1]);
Continue
}
Id=fork ();
if (id<0) {
Perror ("fork");
Exit (1);
}else if (id==0) {
EXECVP (MY_ARGV[0],MY_ARGV);
Exit (1);
}else{
pid_t _wait_pid=waitpid (id,null,0);
if (_wait_pid==id) {
Do Nohang
}
}
}
return 0;
}
This is just a simple shell, and if you need to better use it, you need to improve the functionality of bash.
This article is from the "fringe" blog, so be sure to keep this source http://ab6107.blog.51cto.com/10538332/1761904
Write a simple myshell using the Replace function (the EXEC function family) under Linux