当前位置: 技术问答>linux和unix
自己编写的的shell不能执行外部命令
来源: 互联网 发布时间:2017-02-26
本文导语: #include #include #include #include #include #include #define MAXLINE 128 #define MAXARGC 128 typedef struct PC { char pc_name[20]; pid_t t; //int state; struct PC *next; }Process; Process *head; extern char **environ; char p[][20] = {"cd","environ","echo","help",...
#include
#include
#include
#include
#include
#include
#define MAXLINE 128
#define MAXARGC 128
typedef struct PC
{
char pc_name[20];
pid_t t;
//int state;
struct PC *next;
}Process;
Process *head;
extern char **environ;
char p[][20] = {"cd","environ","echo","help","jobs"};
void cmdLine_Analyse(char *cmdLine,char **argv,char cmd[]);
int cmdRun(char **argv,char cmd[]);
int cd_fun(char **argv);
void environ_fun();
void echo_fun(char **argv);
int help_fun();
int jobs_fun(char **argv);
int Is_Outcmd(char **argv);
void CreateProcessNode();
void InsertProcessNode(char *cmd,pid_t t);
void DeleteProcessNode(pid_t pid);
static void wait_child(int sig_type);
void set_singnal();
int new;
///////////////////////////////////////////main//////////////////////////////
int main()
{
//process Pcb;
char *argv[MAXARGC];
char dir[MAXARGC];
char cmdLine[MAXLINE];
char cmd[MAXARGC];
int n = 0,i;
CreateProcessNode();
while(1)
{
printf("[%s]$PS1",get_current_dir_name());
gets(cmdLine);
//strcpy(cmdLine,"date");
if(cmdLine[strlen(cmdLine) - 2] == '&')
new = 1;
if(!strcmp(cmdLine,"quit") || !strcmp(cmdLine,"exit") || !strcmp(cmdLine,"bye"))
break;
cmdLine_Analyse(cmdLine,argv,cmd);
cmdRun(argv,cmd);
}
return 0;
}
///////////////////////////////////////////////signal///////////////////////////////////////////////////////////
static void wait_child(int sig_type)
{
int status;
pid_t pid = wait(&status);
DeleteProcessNode(pid);
}
void set_signal()
{
if (signal(SIGCHLD, wait_child) == SIG_ERR) {
perror("Signal");
exit(EXIT_FAILURE);
}
}
/////////////////////////////cmdLine_devide////////////////////////////////////////
void cmdLine_Analyse(char *cmdLine, char **argv,char cmd[] )
{
char *temp;
temp = strtok(cmdLine , " ");
int argc = 0;
while(temp != NULL)
{
if(argc == 0)
{
strcpy(cmd,temp);
}
argv[argc++] = temp;
temp = strtok(NULL," ");
}
if(!strcmp(argv[argc-1],"&"))
{
argv[argc - 1] = '';
new = 1;
}
else
argv[argc] = NULL;
}
///////////////////////////////cmdRun/////////////////////////////////////////
int cmdRun(char **argv,char cmd[])
{
int i;
for(i = 0;i pc_name,q->t);
q = q->next;
}
}
/////////////////////////////////Record///////////////////////////////////
void CreateProcessNode()
{
head = (Process *)malloc(sizeof(Process));
strcpy(head->pc_name,"shell");
head->t = 0;
head->next = NULL;
}
void InsertProcessNode(char *cmd,pid_t t)
{
Process *q;
q = (Process *)malloc (sizeof(Process));
strcpy(head->pc_name,cmd);
q->t = t;
q->next = head->next;
head->next = q;
}
void DeleteProcessNode(pid_t pid)
{
Process *q,*temp;
temp = q = head;
while(q->t == pid)
{
temp = q;
q = q->next;
}
temp->next = q->next;
free(q);
}
#include
#include
#include
#include
#include
#define MAXLINE 128
#define MAXARGC 128
typedef struct PC
{
char pc_name[20];
pid_t t;
//int state;
struct PC *next;
}Process;
Process *head;
extern char **environ;
char p[][20] = {"cd","environ","echo","help","jobs"};
void cmdLine_Analyse(char *cmdLine,char **argv,char cmd[]);
int cmdRun(char **argv,char cmd[]);
int cd_fun(char **argv);
void environ_fun();
void echo_fun(char **argv);
int help_fun();
int jobs_fun(char **argv);
int Is_Outcmd(char **argv);
void CreateProcessNode();
void InsertProcessNode(char *cmd,pid_t t);
void DeleteProcessNode(pid_t pid);
static void wait_child(int sig_type);
void set_singnal();
int new;
///////////////////////////////////////////main//////////////////////////////
int main()
{
//process Pcb;
char *argv[MAXARGC];
char dir[MAXARGC];
char cmdLine[MAXLINE];
char cmd[MAXARGC];
int n = 0,i;
CreateProcessNode();
while(1)
{
printf("[%s]$PS1",get_current_dir_name());
gets(cmdLine);
//strcpy(cmdLine,"date");
if(cmdLine[strlen(cmdLine) - 2] == '&')
new = 1;
if(!strcmp(cmdLine,"quit") || !strcmp(cmdLine,"exit") || !strcmp(cmdLine,"bye"))
break;
cmdLine_Analyse(cmdLine,argv,cmd);
cmdRun(argv,cmd);
}
return 0;
}
///////////////////////////////////////////////signal///////////////////////////////////////////////////////////
static void wait_child(int sig_type)
{
int status;
pid_t pid = wait(&status);
DeleteProcessNode(pid);
}
void set_signal()
{
if (signal(SIGCHLD, wait_child) == SIG_ERR) {
perror("Signal");
exit(EXIT_FAILURE);
}
}
/////////////////////////////cmdLine_devide////////////////////////////////////////
void cmdLine_Analyse(char *cmdLine, char **argv,char cmd[] )
{
char *temp;
temp = strtok(cmdLine , " ");
int argc = 0;
while(temp != NULL)
{
if(argc == 0)
{
strcpy(cmd,temp);
}
argv[argc++] = temp;
temp = strtok(NULL," ");
}
if(!strcmp(argv[argc-1],"&"))
{
argv[argc - 1] = '';
new = 1;
}
else
argv[argc] = NULL;
}
///////////////////////////////cmdRun/////////////////////////////////////////
int cmdRun(char **argv,char cmd[])
{
int i;
for(i = 0;i pc_name,q->t);
q = q->next;
}
}
/////////////////////////////////Record///////////////////////////////////
void CreateProcessNode()
{
head = (Process *)malloc(sizeof(Process));
strcpy(head->pc_name,"shell");
head->t = 0;
head->next = NULL;
}
void InsertProcessNode(char *cmd,pid_t t)
{
Process *q;
q = (Process *)malloc (sizeof(Process));
strcpy(head->pc_name,cmd);
q->t = t;
q->next = head->next;
head->next = q;
}
void DeleteProcessNode(pid_t pid)
{
Process *q,*temp;
temp = q = head;
while(q->t == pid)
{
temp = q;
q = q->next;
}
temp->next = q->next;
free(q);
}
|
怎么个不能执行法??
|
你这是C程序。你所谓的“不能执行”不会是指不能像shell脚本那样写完了就可以执行吧?