问题
static char inpbuf[MAXBUF], tokbuf[2 * MAXBUF], *ptr = inpbuf, *tok = tokbuf;
The array inpbuf is intended to store the user typed command and
its arguments. The array tokbuf is intended to store each extracted tokens from inpbuf,
such that, each element in the array char *arg[MAXARG + 1]; (in proc_line.c) is properly
assigned using the information in tokbuf in order later on to invoke execvp system call in
runcommand.c.
I wanted to get rid of tokbuf and make the program still work. I compiled it like this: cc main_smallsh.c userin.c runcommand.c proc_line.c -o smallsh. The program basically shows the listing of the current directory.
//main_smallsh.c
#include "smallsh.h"
char *prompt = "Command> ";
int userin(char *p);
void procline(void);
int main()
{
while (userin(prompt) != EOF)
procline();
}
//proc_line.c
#include "smallsh.h"
#include <string.h>
#include <stdlib.h>
int gettok(char **outptr);
int runcommand(char **cline, int where);
void procline(void)
{
char *arg[MAXARG + 1];
int toktype;
int narg;
int type;
narg = 0;
for (;;)
{
switch (toktype = gettok(&arg[narg])) {
case ARG:
if (narg < MAXARG)
narg++;
break;
case EOL:
case SEMICOLON:
case AMPERSAND:
if (toktype == AMPERSAND)
type = BACKGROUND;
else
type = FOREGROUND;
if (narg != 0)
{
arg[narg] = NULL;
runcommand(arg, type);
}
if (toktype == EOL)
return;
narg = 0;
break;
}
//added for assignment 3
// if (!strcmp(arg[narg-1],"exit"))
// exit(5);
}
}
//runcommand.c
#include "smallsh.h"
#include <unistd.h>
#include <stdlib.h>
int runcommand(char **cline, int where)
{
pid_t pid;
int status;
switch (pid = fork()) {
case 1:
perror("smallsh");
return (-1);
case 0:
execvp(cline[0], cline);
perror(*cline);
exit(1);
}
//code for parents
if (where == BACKGROUND)
{
printf("[process id %d]\n", pid);
return (0);
}
if (waitpid(pid, &status, 0) == -1)
return (-1);
else
return (status);
}
//smallsh.h
//*****************************************************************
#include <sys/wait.h>
#include <stdio.h>
//#include <sys/wait.h>
#define EOL 1
#define ARG 2
#define AMPERSAND 3
#define SEMICOLON 4
#define MAXARG 512
#define MAXBUF 512
#define FOREGROUND 0
#define BACKGROUND 1
//userin.c
#include "smallsh.h"
static char inpbuf[MAXBUF], tokbuf[2 * MAXBUF],
*ptr = inpbuf, *tok = tokbuf;
int inarg(char c);
int userin(char *p)
{
int c, count;
ptr = inpbuf;
tok = tokbuf;
printf("%s", p);
count = 0;
while (1)
{
if ((c = getchar()) == EOF)
return(EOF);
if (count < MAXBUF)
inpbuf[count++] = c;
if (c == '\n' && count < MAXBUF)
{
inpbuf[count] = '\0';
return count;
}
if (c == '\n')
{
printf("smallsh: input line too long\n");
count = 0;
printf("%s ", p);
}
}
}
int gettok(char **outptr)
{
int type;
*outptr = tok;
while (*ptr == ' ' || *ptr == '\t')
ptr++;
*tok++ = *ptr;
switch (*ptr++) {
case '\n':
type = EOL;
break;
case '&':
type = AMPERSAND;
break;
case ';':
type = SEMICOLON;
break;
default:
type = ARG;
while (inarg(*ptr))
*tok++ = *ptr++;
}
*tok++ = '\0';
return type;
}
//****************************************************************************************
static char special[] = { ' ', '\t', '&', ';', '\n', '\0' };
int inarg(char c)
{
char *wrk;
for (wrk = special; *wrk; wrk++)
{
if (c == *wrk)
return (0);
}
return (1);
}
来源:https://stackoverflow.com/questions/64938894/static-char-inpbufmaxbuf-tokbuf2-maxbuf-ptr-inpbuf-tok-tokbuf