This is the code in it's entirety, also please don't post a solution this is for an assignment, just a point the right direction is all im looking for.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXARGS 32 /* maximum # of arguments to the program*/
/* Structure to hold input data */
struct PARAM
{
char *InputRedirect; /* file name or NULL */
char *OutputRedirect; /* file name or NULL */
int Background; /* either 0 (false) or 1 (true) */
int ArgumentCount; /* same as argc in main() */
char *ArgumentVector[MAXARGS]; /* array of strings */
};
/* define a struct named Param_t */
typedef struct PARAM Param_t;
/**
* Entry point into the program
* argc - # of arguments
* *argv[] - the arguments
*/
int main(int argc, char *argv[])
{
const char DELIMITERS[] =" \n\t";
const char EXIT[] = "exit";
char input[MAXARGS];
char *background;
char *inputRedirect;
char *outputRedirect;
char *tokens;
int index = 0; /* index into the argument vector, initialized at 0 */
/* allocate space for the struct */
Param_t *p = (Param_t *) malloc(sizeof(Param_t));
/* prompt the user */
printf("Enter input using the keyboard, when you wish to stop type exit:\n\n$$$ ");
fgets(input, MAXARGS, stdin); /* read from the terminal */
/* while you haven't read the word "exit", keep reading and tokenizing */
while( strncmp(input, EXIT, strlen(EXIT)) != 0 ) /* only compares the first 4 characters to avoid whitespace */
{
printf("input: %s\n\n", input);
tokens = strtok(input, DELIMITERS);
while(tokens != NULL)
{
background = strchr(tokens, '&');
inputRedirect = strchr(tokens, '<');
outputRedirect = strchr(tokens, '>');
/* copy argc into the structure */
p -> ArgumentCount = argc;
/* special cases */
if(inputRedirect != NULL)
p -> InputRedirect = tokens;
else
p -> InputRedirect = NULL;
if(outputRedirect != NULL)
p -> OutputRedirect = tokens;
else
p -> OutputRedirect = NULL;
if(background != NULL)
p -> Background = 1;
else
p -> Background = 0;
/* normal case */
if(inputRedirect == NULL && outputRedirect == NULL)
{
printf("%s\n", tokens);
p -> ArgumentVector[index++] = tokens;
}
/* read next token */
tokens = strtok(NULL, DELIMITERS);
}
printf("\n$$$ "); /* prompt line */
fgets(input, MAXARGS, stdin); /* read from the terminal */
}
/* print structure */
int j;
for(j = 0; j < 5; j++)
{
printf("ArgumentVector[%2d]: [%s]\n", j, p->ArgumentVector[j]);
}
return 0;
}
Thanks,
Hunter