(no subject)
M
Muhammad Saad
to me
0 minutes ago
Details
#include <stdio.h>
#include <sys/types.h>
void parse(char *line, char **argv)
{
while (*line != '\0')
{
while (*line == ' ' || *line == '\t' || *line == '\n')
*line++ = '\0';
*argv++ = line;
while (*line != '\0' && *line != ' ' &&
*line != '\t' && *line != '\n')
line++;
}
*argv = '\0';
}
void execute(char **argv)
{
pid_t pid;
int status;
if ((pid = fork()) < 0)
{
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) {
if (execvp(*argv, argv) < 0)
{
printf("*** ERROR: exec failed\n");
exit(1);
}
}
else /* for the parent: */
{
while (wait(&status) != pid);
}
}
/////////////////////////////////////////////
void show_history(){
int i;
if(strcmp(hist[0]," ")==0) printf("empty\n");
else {
for (i = 0; i <10; ++i)
{
if (!strcmp(hist," ")==0) {printf(hist);
printf("\n");}
}
}
}
void keep_hist(){
int i;
for (i = 9; i > 0; --i)
{
strcpy(hist,hist[i-1]);
}
}
/////////////////////////////////////////////
vint main(void)
{
char *args[MAX_LINE/2 + 1];
int should_run = 1;
pid_t pid;
int i, next, flag;
int pos;
pos=-1;
for (i = 0; i < 10; ++i)
{
strcpy(hist," ");
}
while (should_run){
printf("osh>");
fflush(stdout);
flag=0;
char* input = (char*) malloc(sizeof(char*));
scanf ("%[^\n]%*c",input);
int j;
int s = strlen(input);
if (strcmp(input,"")==0)
{
printf("no command entered\n");
break;
}
if ((input[0]!='!') && strcmp(input,"history") ){
keep_hist();
strcpy (hist[0],input);
}
if (strcmp(input,"history")==0)
{
show_history();
}
else if (strcmp(input,"!!")==0){
printf(hist[0]);
keep_hist();
strcpy (hist[0],hist[1]);
char* temp[50];
strcpy(temp, hist[0]);
parse(temp, args, &flag);
pid = fork();
if (pid<0){
printf("ERROR in fork\n");
}
else if (pid==0)
{
if ((execvp(args[0],args)==-1)) {
printf("invalid command\n");
}
}
else {
next = 1;
if (flag==1){
next=0;
wait(NULL);
}
}
}
else if (((input[0]=='!')==1 && (input[1]=='!')== 0)){
int t;
int i;
for (i = 0; i < 10; ++i)
{
if(input[s-1]== i+'0') t = i;
}
if (strcmp(hist[t]," ")==0) {
printf("No command found\n");
}
else if (t>9) {
printf("history not recorded his far");
}
else {
char* temp[50];
strcpy(temp, hist[t]);
keep_hist();
strcpy (hist[0],temp);
parse(temp, args, &flag);
pid = fork();
if (pid<0){
printf("ERROR in fork\n");
}
else if (pid==0)
{
if ((execvp(args[0],args)==-1)) {
printf("invalid command\n");
}
}
else {
next = 1;
if (flag==1){
next=0;
wait(NULL);
}
}
}
}
else {
if(input[s-1]=='&'){
flag=1;
input[s-1]='\0';
if (input[s-2]==' ') input[s-2]='\0';
}
parse(input, args, &flag);
pid = fork();
if (pid<0){
printf("ERROR in fork\n");
}
else if (pid==0)
{
if ((execvp(args[0],args)==-1)) {
printf("invalid command\n");
}
}
else {
next = 1;
if (flag==1){
next=0;
wait(NULL);
}
}
}
}
return 0;
}
explain this code in words
(no subject) M Muhammad Saad to me 0 minutes ago Details #include #include void parse(char *line
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
(no subject) M Muhammad Saad to me 0 minutes ago Details #include #include void parse(char *line
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!