The following is my code that is supposed to act as a replacement shell for Linux:
Code:
#include "g5shell.h"
/* Program buffers and work pointers */
static char inpbuf[MAXBUF], tokbuf[2*MAXBUF], *ptr = inpbuf, *tok = tokbuf;
/* ---- USERIN: Print prompt and read a line ---- */
int userin(char *p)
{
int c, count;
ptr = inpbuf;
tok = tokbuf;
printf("%s", p); // Display prompt
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'){ // Restart if line too long
printf("G5-Shell Error: Input line too long.\n");
count = 0;
printf("%s", p);}
}
}
/* ---- GETTOK: Get token and place into tokbuf ---- */
int gettok(char **outptr)
{
int type;
*outptr = tok; // Set the outptr string to tok
while(*ptr == ' ' || *ptr == '\t') ptr++; // Strip white space from the buffer containing the tokens
*tok++ = *ptr; // Set token pointer to the 1st token in the buffer
switch(*ptr++){ // Set the type variable depending
case '\n': // on the token in the buffer
type = EOL;
break;
case '&':
type = AMPERSAND;
break;
case ';':
type = SEMICOLON;
break;
default:
type = ARG;
while(inarg(*ptr)) // Keep reading valid ordinary chars
*tok++ = *ptr++;
}
*tok++ = '\0';
return type;
}
/* List of special chars for inarg() */
static char special [] = {' ','\t','&',';','\n','\0'};
/* ---- INARG: Check for special chars ---- */
int inarg(char c)
{
char *wrk;
for(wrk=special; *wrk; wrk++){
if(c == *wrk) return(0);}
return(1);
}
/* ---- PROCLINE: Process input line ---- */
int procline(void)
{
char *arg[MAXARG+1]; // Pointer array for runcommand
int toktype; // Type of token in command
int narg; // Number of args so far
int type; // FOREGROUND or BACKGROUND
narg = 0;
for(;;){ // Infinite loop
switch(toktype=gettok(&arg[narg])){ // Take action according to token types
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;
}
}
}
/* ---- RUNCOMMAND: Execute command with optional wait ---- */
int runcommand(char **cline, int where)
{
pid_t pid;
int status;
switch(pid = fork()){
case -1: perror("G5-Shell");
return(-1);
case 0: execvp(*cline, cline);
perror(*cline);
exit(1);
}
if(where == BACKGROUND){ // If BACKGROUND process, then print pid and exit
printf("[Process id %d]\n", pid);
return(0);}
if(waitpid(pid, &status, 0) == -1) return; // Wait until process pid exits
else return(status);
}
/* Prompt */
char *prompt = "Command> ";
/* ---- MAIN: Main function ---- */
main()
{
char cmd;
execl("/bin/clear", "clear", (char *)0); // clear screen
printf("Welcome to Group 5 Shell System.\n");
while(userin(prompt) != EOF) procline();
}
After trying to compile I get the following 2 errors (faulty lines are highlighted on the code):
shell.c:62: warning: type mismatch with previous implicit declaration
shell.c:50: warning: previous implicit declaration of `inarg'
I can't figure out why this is happening. Any help is appreciated.