0% found this document useful (0 votes)
43 views

Cs Assignment

This C program runs bash commands from a user prompt. It uses the fork and exec system calls to execute commands as child processes. The program takes user input, parses it into arguments, prepends "/bin/" or "/usr/bin/" to the first argument, and executes the command using execv. If that fails, it tries executing with "/usr/bin/" prepended instead. It continues taking commands in a loop until the user enters "exit".

Uploaded by

api-289515699
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Cs Assignment

This C program runs bash commands from a user prompt. It uses the fork and exec system calls to execute commands as child processes. The program takes user input, parses it into arguments, prepends "/bin/" or "/usr/bin/" to the first argument, and executes the command using execv. If that fails, it tries executing with "/usr/bin/" prepended instead. It continues taking commands in a loop until the user enters "exit".

Uploaded by

api-289515699
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

/* Matthew Daniel

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";

// for first variable in array to be passed to execv


char * path = (char*) malloc(maxLineSize * sizeof(char));
char * usrPath = (char*) malloc(maxLineSize * sizeof(char));

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);

// Copy into identical array


for (i = 0; i < argc; i++)
{
usrArgs[i] = args[i];
}
// Exit condition
if (strcmp (args[0], exitStr) == 0)
{
break;
}

// Then change the first argument to /bin/... or /usr/bin/...


strcat(path, args[0]);
args[0] = path;
strcat(usrPath, usrArgs[0]);
usrArgs[0] = usrPath;

// set last arg to null for exec command


args[(argc)] = NULL;
usrArgs[(argc)] = NULL;

if ((childPID = fork()) == -1)


{
perror("Fork Failed\n");
exit(1);
}
else if (childPID == 0)
{
j = (execv (args[0], args) );
if (j == -1)
{
if (execv(usrArgs[0], usrArgs) < 0)
{
perror("Command Failed!\n");
exit(1);
}
}
}
sleep(1);

hasExecuted = 1;
}
return 0;
}

// Function from CS 270, assignment one


int makearg(char *s, char ***args)
{
int i = 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;
}

You might also like