当前位置: 技术问答>linux和unix
编写 的 minishell
来源: 互联网 发布时间:2016-07-16
本文导语: 大家好,我初学shell,这是我自己编写的minishell模拟shell的功能,有些地方缺了一些功能,大家能帮我补上并且修正一些错误吗? ////////////////////////////////////////////////////////////////////////////////////// #define MAX_ARGS 64 #def...
大家好,我初学shell,这是我自己编写的minishell模拟shell的功能,有些地方缺了一些功能,大家能帮我补上并且修正一些错误吗?
//////////////////////////////////////////////////////////////////////////////////////
#define MAX_ARGS 64
#define MAX_ARG_LEN 16
#define MAX_LINE_LEN 80
#define LINE_LEN 80
#define MAX_PATHS 64
#define MAX_PATH_LEN 96
#define WHITESPACE " .,tn"
struct command_t
{
char *name;
int argc;
char *argv[MAX_ARGS];
};
#include
#include
#include
char *lookupPath(char **, char **);
int parseCommand(char *, struct command_t *);
int parsePath(char **);
void printPromt();
void readCommand(char *);
int main()
{
int i;
int pid;
char cmdLine[MAX_LINE_LEN];
struct command_t command;
char pathv[];
parsePath(pathv);// get directory paths from PATH
while(true)
{
printPromt();
readCommand(commandLine);
//read the command line and parse it
parseCommand(commandLine,&command);
//get the full path name for the file
command.name = lookupPath(command.argv, pathv);
if(command.name == NULL)
{
printf("error getting the name n");
contiune;
}
//create a child and execute the command
if((pid= fork()) ==0)
execv(command.name, command.argv);
//wait for the child to termimate
if(wait(NULL) > 0)
printf("Process[%d]: Parent detects terminating child n", getpid());
}
printf("nn miniShell terminating succesfullyn");
return 0;
}
void printPrompt()
{
string promptString = ...;
printf("%s", promptString);
}
int parsePath(char *dirs[])
{
char *pathEnvVar;
char *thePath;
for(int i =0; iargv[argc] = strsep(clPtr, WHITESPACE)) != NULL) {
cmd->argv[++argc] = (char *) malloc(MAX_ARG_LEN);
}
cmd->argc = argc-1;
cmd->name = (char *) malloc(sizeof(cmd->argv[0]));
strcpy(cmd->name, cmd->argv[0]);
return 1;
}
//////////////////////////////////////////////////////////////////////////////////////
#define MAX_ARGS 64
#define MAX_ARG_LEN 16
#define MAX_LINE_LEN 80
#define LINE_LEN 80
#define MAX_PATHS 64
#define MAX_PATH_LEN 96
#define WHITESPACE " .,tn"
struct command_t
{
char *name;
int argc;
char *argv[MAX_ARGS];
};
#include
#include
#include
char *lookupPath(char **, char **);
int parseCommand(char *, struct command_t *);
int parsePath(char **);
void printPromt();
void readCommand(char *);
int main()
{
int i;
int pid;
char cmdLine[MAX_LINE_LEN];
struct command_t command;
char pathv[];
parsePath(pathv);// get directory paths from PATH
while(true)
{
printPromt();
readCommand(commandLine);
//read the command line and parse it
parseCommand(commandLine,&command);
//get the full path name for the file
command.name = lookupPath(command.argv, pathv);
if(command.name == NULL)
{
printf("error getting the name n");
contiune;
}
//create a child and execute the command
if((pid= fork()) ==0)
execv(command.name, command.argv);
//wait for the child to termimate
if(wait(NULL) > 0)
printf("Process[%d]: Parent detects terminating child n", getpid());
}
printf("nn miniShell terminating succesfullyn");
return 0;
}
void printPrompt()
{
string promptString = ...;
printf("%s", promptString);
}
int parsePath(char *dirs[])
{
char *pathEnvVar;
char *thePath;
for(int i =0; iargv[argc] = strsep(clPtr, WHITESPACE)) != NULL) {
cmd->argv[++argc] = (char *) malloc(MAX_ARG_LEN);
}
cmd->argc = argc-1;
cmd->name = (char *) malloc(sizeof(cmd->argv[0]));
strcpy(cmd->name, cmd->argv[0]);
return 1;
}
|
友情uP
|
先mark,等会来看
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。