gpt4 book ai didi

c - 向程序中添加一个函数,并从函数中的命令行调用该函数

转载 作者:太空狗 更新时间:2023-10-29 15:41:27 24 4
gpt4 key购买 nike

我必须添加一个自定义函数来显示当前运行的前台和后台进程,该进程由该 shell 启动。如何从 shell 的命令行定义和调用该函数?

#include "smallsh.h" /*include file for example*/

/*program buffers and work pointers*/
static char inpbuf[MAXBUF], tokbuf[2*MAXBUF],
*ptr = inpbuf, *tok = tokbuf;

userin(p) /*print prompt and read a line*/
char *p;
{

int c, count;

/*initialization for later routines*/
ptr = inpbuf;
tok = tokbuf;

/*display prompt*/
printf("%s ",p);

for(count = 0;;)
{
if((c = getchar()) == EOF)
return(EOF);

if(count<MAXBUF)
inpbuf[count++] = c;

if(c == '\n' && count <MAXBUF)
{
inpbuf[count] = '\0';
return(count);
}

/*if line too long restart*/
if(c == '\n')
{
printf("smallsh:input line too long\n");
count = 0;
printf("%s",p);
}
}
}

gettok(outptr) /*get token and place into tokbuf*/
char **outptr;
{
int type;

*outptr = tok;

/*strip white space*/
for(;*ptr == ' ' || *ptr == '\t'; ptr++)
;

*tok++ = *ptr;

switch(*ptr++)
{
case '\n':
type = EOL; break;
case '&':
type = AMPERSAND; break;
case ';':
type = SEMICOLON; break;
case '#':
type = POUND; break;
default:
type = ARG;
while(inarg(*ptr))
*tok++ = *ptr++;
}

*tok++ = '\0';
return(type);
}

static char special[]=
{' ', '\t', '&', ':', '\n', '\0'};

inarg(c) /*are we in an ordinary argument*/
char c;
{
char *wrk;

for(wrk = special;*wrk != '\0';wrk++)
if(c == *wrk)
return(0);

return(1);
}

#include "smallsh.h"

procline() /*process input line*/
{
char *arg[MAXARG+1]; /*pointer array for runcommand*/
int toktype; /*type of token in command*/
int narg; /*number of arguments so far*/
int type; /*FOREGROUND or BACKGROUND*/

for(narg = 0;;)
{
/*loop FOREVER*/

/*take action according to token type*/

switch(toktype = gettok(&arg[narg]))
{
case ARG:

if(narg<MAXARG)
narg++;
break;

case EOL:
case SEMICOLON:
case AMPERSAND:
case POUND:

type = (toktype == AMPERSAND) ?
BACKGROUND : FOREGROUND;

if(narg!=0)
{
arg[narg] = NULL;
runcommand(arg, type);
}

if((toktype == EOL)||(toktype=POUND))
return;

narg = 0;
break;
}
}
}


#include "smallsh.h"

/*execute a command with optional wait*/
runcommand(cline,where)
char **cline;
int where;
{
int pid, exitstat, ret;

if((pid = fork()) <0)
{
perror("smallsh");
return(-1);
}

if(pid == 0)
{
/*child*/
execvp(*cline, cline);
perror(*cline);
exit(127);
}

/*code for parent*/
/*if background process print pid and exit*/

if(where == BACKGROUND)
{
printf("[Process id %d]\n", pid);
return(0);
}

/*wait until process pid exists*/

while( (ret=wait(&exitstat)) != pid && ret != -1)
;

return(ret == -1 ? -1 : exitstat);
}

#include "smallsh.h"

char *prompt = "Command>"; /*prompt*/

main()
{
while(userin(prompt) != EOF)
procline();
}

为了尝试调用函数,在 runco​​mmand 内部,在子进程的代码之前,我尝试添加另一个 if 语句说 if(cline == "dowork"){dowork();} , 并尝试在其他地方放置类似的行,但没有类似的工作。

最佳答案

因为这是 C,cline == "dowork" 不会做你想做的事。您需要将其更改为:

if (strcmp(cline, "dowork") == 0)
{
...
}

如果您希望您的 shell 也处理“DOWORK”、“DoWork”等,您可以将 strcmp 替换为 strcasecmp

更新:由于您有char **cline,您需要将其更改为:

if (strcmp(*cline, "dowork") == 0)

此外,您应该在编译时发出警告 - 如果您这样做,编译会告诉您此处存在类型不匹配。

关于c - 向程序中添加一个函数,并从函数中的命令行调用该函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2794973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com