I have provided the following functions to get you started: • read_line(line) -
ID: 3602143 • Letter: I
Question
I have provided the following functions to get you started:
• read_line(line) - reads a line of text from input and stores it in a string.
• is_blank(str) - returns true (1) if a string contains only whitespace, false (0) otherwise.
• parse_args(cmd, argv) - takes a command line and breaks it up into a sequence of strings containing
the command and the arguments, then stores the strings in the array provided.
I have also de ned the following constants using the #define preprocessor statement:
• MAX_LINE - the maximum number of characters allowed in a single line. Use this as the size when you
declare a character array to represent the string to pass to read_line.
• MAX_ARGS - the maximum number of arguments (including the command) that may be used in a
command line. Use this as the size when you declare an array of C-strings to represent the array of
arguments to pass to parse_args.
To execute a command you must do the following:
• Use the fork function to fork o a child process. The fork function takes no arguments and returns a
value of type pid_t that can be treated like an integer value. WARNING: be very careful with using
fork in a loop. If you are not careful you will fork too many processes for your shell to handle.
• Check to see if the current process is the child or the parent by checking the return value of fork. If
the return value is 0, the current process is the child process. Otherwise, it is the parent process.
• The parent process should call the wait function with a single argument of NULL to wait for the child
to complete.
• The child process should call the execvp function with two arguments:
- The name of the command being executed (argument 0 of the array.)
- The entire array of command line arguments.
If execvp succeeds, the process will no longer be running the shell code. This means that the next line is
executed only when execvp fails. If this is the case, display an error message and terminate the child process
by calling exit(1).
Here is a sample of what running your shell might look like:
[abc@edve ~]$ ./turtle
> ls
colemanAssign5.c turtle
> ls -l
total 32
-rw-r--r-- 1 colemann staff 2146 Sep 14 16:04 colemanAssign5.c
-rwxr-xr-x 1 colemann staff 9076 Sep 14 16:05 turtle
>
> fnord
turtle: command fnord not found.
> exit
[exiting turtle shell]
________________________________________________________________________________________________________
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define MAX_LINE 256
#define MAX_ARGS 64
int main()
{
// I need the code for here...................................................................
}
/* Checks if a string contains only whitespace
* str - the string to check
* returns 1 if str contains whitespace, 0 otherwise
*/
int is_blank(const char *str)
{
int i = 0;
while(str[i] != '') {
if(!isspace(str[i++]))
return 0;
}
return 1;
}
/* Reads a line of text from the console
* line - an allocated array of characters of size MAX_LINE
* where the function can store all of the characters read.
*/
void read_line(char *line)
{
char c;
int chars_read = 0;
while((c = getchar()) != ' ' && chars_read < MAX_LINE)
line[chars_read++] = c;
line[chars_read] = '';
}
/* Breaks a command line into individual strings containing the
* command and all of its arguments
* cmd - a string containing the command line
* argv - an allocated array of strings of size MAX_ARGS where the
* command and its arguments will be stored.
*/
void parse_args(const char *cmd, char *argv[])
{
int argc = 0;
while(*cmd != '' && argc < MAX_ARGS) {
int i = 0;
while(!isspace(cmd[i]) && cmd[i] != '') i++;
argv[argc] = (char *) malloc(i + 1);
strncpy(argv[argc], cmd, i);
while(isspace(cmd[i])) i++;
cmd += i;
argc++;
}
if(argc < MAX_ARGS)
argv[argc] = NULL;
else
argv[MAX_ARGS - 1] = NULL;
}
Explanation / Answer
include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define MAX_LINE 256
#define MAX_ARGS 64
int main()
{
// I need the code for here...................................................................
}
/* Checks if a string contains only whitespace
* str - the string to check
* returns 1 if str contains whitespace, 0 otherwise
*/
int is_blank(const char *str)
{
int i = 0;
while(str[i] != '') {
if(!isspace(str[i++]))
return 0;
}
return 1;
}
/* Reads a line of text from the console
* line - an allocated array of characters of size MAX_LINE
* where the function can store all of the characters read.
*/
void read_line(char *line)
{
char c;
int chars_read = 0;
while((c = getchar()) != ' ' && chars_read < MAX_LINE)
line[chars_read++] = c;
line[chars_read] = '';
}
/* Breaks a command line into individual strings containing the
* command and all of its arguments
* cmd - a string containing the command line
* argv - an allocated array of strings of size MAX_ARGS where the
* command and its arguments will be stored.
*/
void parse_args(const char *cmd, char *argv[])
{
int argc = 0;
while(*cmd != '' && argc < MAX_ARGS) {
int i = 0;
while(!isspace(cmd[i]) && cmd[i] != '') i++;
argv[argc] = (char *) malloc(i + 1);
strncpy(argv[argc], cmd, i);
while(isspace(cmd[i])) i++;
cmd += i;
argc++;
}
if(argc < MAX_ARGS)
argv[argc] = NULL;
else
argv[MAX_ARGS - 1] = NULL;
}