Question: need the pseudo code turned into a workable program as specified in th
ID: 3822619 • Letter: Q
Question
Question: need the pseudo code turned into a workable program as specified in the following pseudo code.
Given Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define MAXLINE 80
#define MAXARGS 20
#define MAX_PATH_LENGTH 50
#define TRUE 1
/* function prototypes */
int parseline(char *cmdline, char **argv);
/* ----------------------------------------------------------------- */
/* The main program starts here */
/* ----------------------------------------------------------------- */
int main(void)
{
char cmdline[MAXLINE];
char *argv[MAXARGS];
int argc;
int status;
pid_t pid;
/* Loop forever to wait and process commands */
while (TRUE) {
/* Print your shell name: csc60mshell (m for mini shell) */
printf("cscc60mshell> ");
/* Read the command line */
fgets(cmdline, MAXLINE, stdin);
/* Call parseline to build argc/argv */
//code here
/* If user hits enter key without a command, continue to loop
* again at the beginning */
/* Hint: if argc is zero, no command declared */
/* Hint: look up for the keyword "continue" in C */
//code here
/* Handle build-in command: exit, pwd, or cd */
/* Put the rest of your code here */
//code here
} /* end of the while */
} /* end of main */
/* ----------------------------------------------------------------- */
/* parseline */
/* ----------------------------------------------------------------- */
/* parse input line into argc/argv format */
int parseline(char *cmdline, char **argv)
{
int count = 0;
char *separator = " "; /* Includes space, Enter, Tab */
/* strtok searches for the characters listed in separator */
argv[count] = strtok(cmdline, separator);
while ((argv[count] != NULL) && (count+1 < MAXARGS)) {
argv[++count] = strtok((char *) 0, separator);
}
return count;
}
Explanation / Answer
Here is Your Completd Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define MAXLINE 80
#define MAXARGS 20
#define MAX_PATH_LENGTH 50
#define TRUE 1
int parseline(char *cmdline, char **argv);
int main(void)
{
char cmdline[MAXLINE];
char *argv[MAXARGS];
int argc;
int status;
pid_t pid;
while (TRUE) {
int childPid;
int count;
char *cmdline;
printf("cscc60mshell> ");
fgets(cmdline, MAXLINE, stdin);
argc = parseline(*cmdline, **argv);
printf("Argc = %s ", argc);
for(count = 0; count < argc; count++){
printf("Argv %i = %s ", count, argv[count]);
if(argv[0] == 0){
printf("No Command Given");
continue;
}
// printf("%i" ,argv[count]);
int strcmp(const char *s1, const char *s2);
if(strcmp(argv[0], "exit") == 0){
return(EXIT_SUCCESS);
}else if(strcmp(argv[0], "pwd") == 0){
char c[MAX_PATH_LENGTH];
getcwd(c, MAX_PATH_LENGTH);
fprintf(stdout, " Current Directory: %s ", c);
continue;
}
else if(strcmp(argv[0], "cd")== 0){
char *dir, check;
if(argc == 1){
dir = getenv("Home");
}else{
dir = argv[1];
check = chdir(dir);
if(check != 0){
fprintf(stderr, "No Command Inputted!");
}
}
}
}
return 0;
}}
int parseline(char *cmdline, char **argv)
{
int count = 0;
char *separator = " "; /* Includes space, Enter, Tab */
/* strtok searches for the characters listed in separator */
argv[count] = strtok(cmdline, separator);
while ((argv[count] != NULL) && (count+1 < MAXARGS)) {
argv[++count] = strtok((char *) 0, separator);
}
return count;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.