Cs Assignment
Cs Assignment
CS 240
Programming Assignment 2
CS240-Ass2.c
A program that runs bash commands
with the prompt "?: "
02/20/2015
*/
#include
#include
#include
#include
#include
#include
<sys/types.h>
<sys/wait.h>
<unistd.h>
<stdio.h>
<stdlib.h>
<string.h>
int main()
{
pid_t childPID;
int makearg(char *s, char ***args);
char * str = NULL;
const int maxLineSize = 256;
int argc;
char ** args;
char ** usrArgs;
int i = 0;
int j = 0;
char inputChar;
int hasExecuted;
int maxStrLen = 0;
const char exitStr[10] = "exit";
while (1)
{
// set counter to zero, reset the paths to their original base values
i = 0;
strcpy(path, "/bin/");
strcpy(usrPath, "/usr/bin/");
// reset 2D arrays and 1D array
args = NULL;
args = (char**) malloc(maxLineSize * sizeof(char));
usrArgs = NULL;
usrArgs = (char**) malloc(maxLineSize * sizeof(char));
str = NULL;
str = (char*) malloc(maxLineSize * sizeof(char));
// print prompt
printf("?: ");
// Use getchar() to read in the line
do
{
// If newline only is read
if (str[0] == '\n')
{
i = 0;
}
inputChar = getchar();
str[i] = inputChar;
i++;
} while (inputChar != '\n' || str[0] == '\n');
maxStrLen = i;
// Call to parse arguments into array
argc = makearg(str, &args);
hasExecuted = 1;
}
return 0;
}
int j = 0;
int argc = 0;
int flag = 0;
(*args)[j] = s;
while (*s != '\0')
{
// Handles any extra whitespace
while (*s == ' ' || *s == '\t' || *s == '\n')
{
*s = '\0';
*s++;
flag = 1;
}
// If there was a space character
if (flag == 1)
{
i++;
argc++;
j++;
(*args)[j] = s;
}
*s++;
i++;
flag = 0;
}
// Error message if no arguments were entered.
if ((***args) == '\0')
{
printf("Error: No arguments entered\n");
argc = -1;
}
return argc;
}