int main() { char *line; char **cmd; uneditedLine = (char*)malloc(256 * sizeof(char)); while (1) { my_setup(); my_prompt(); line = my_read(); strcpy(uneditedLine, line); if (line == 0) return 0; if (line[0] == '\0') { free(line); continue; } cmd = my_parse(line); my_execute(cmd); my_clean(line, cmd); } free(uneditedLine); return 0; }
void check_command(char **cmd){ if (strcmp(cmd[0], "echo") == 0) Echo(cmd); else if (strcmp(cmd[0], "exit") == 0) Exit(cmd); else my_execute(cmd); }
double Etime(char** cmd) { Remove_first(cmd); // remove "etime" in cmd int i; char *ifIR=NULL, *ifOR=NULL; for(i=0; cmd[i]!=NULL;i++) { ifOR = strchr(cmd[i], '>'); if( ifOR!=NULL ) break; // if cmd contains > } for(i=0; cmd[i]!=NULL;i++) { ifIR = strchr(cmd[i], '<'); if( ifIR!=NULL ) break;// if cmd contains < } struct timeval start, finish, diff; gettimeofday(&start, 0); if(cmd[0] != NULL) { if( ifOR!=NULL ) output_red(cmd); else if( ifIR!=NULL ) input_red(cmd); else if((strcmp(cmd[0], "echo") == 0)||(strcmp(cmd[0], "exit") == 0)) check_command(cmd); else my_execute(cmd); } gettimeofday(&finish, 0); time_substract(&diff, &start, &finish); double duration = (double)diff.tv_sec + ((double)diff.tv_usec/1000000.0); printf("Elapsed Time: %.9fs\n",duration); return duration; }
void execute_built_in(char **cmd) { int i; struct timeval start, end; pid_t pid; FILE *file; DIR *dir; char str1[256] = "/proc/"; char *str2 = "/limits"; char str3[256]; char pidstr[9]; int status; int result; char *same; char *lastOccur; char *nextSpace, *nextTab, *endofstr; int len; if (strcmp(cmd[0], "exit") == 0) { printf("Exiting Shell...\n"); exit(1); } else if (strcmp(cmd[0], "cd") == 0) { /* if more than one argument is present, trigger error */ if (cmd[2] != 0) { printf("cd: Too many arguments.\n"); return; } /* if no arguments, treat as if $HOME is the argument */ if (cmd[1] == 0) cmd[1] = getenv("HOME"); result = chdir(cmd[1]); if (result == 0) /* successful directory change */ setenv("PWD", cmd[1], 1); else /* directory doesn't exist */ printf("%s: No such file or directory.\n", cmd[1]); } else if (strcmp(cmd[0], "echo") == 0) { i = 1; while(cmd[i] != 0) { /* print argument if it isn't an external command */ if (!is_external(cmd[i])) printf("%s ", cmd[i]); /* if external, find original statement and print that echo ls will print "ls" instead of "/bin/ls" echo /bin/ls will print "/bin/ls" */ else { same = strstr(uneditedLine, cmd[i]); /* if cmd matches thing in uneditedLine, print cmd[i] and change uneditedline */ if (same != NULL) { /* find next whitespace character or '\0'*/ nextSpace = strchr(same, ' '); nextTab = strchr(same, '\t'); endofstr = strchr(same, '\0'); if (nextSpace != NULL) { if (nextTab != NULL && nextTab > nextSpace) nextSpace = nextTab; } else if (nextTab != NULL) nextSpace = nextTab; else nextSpace = endofstr; len = 0; while (nextSpace != same) { nextSpace--; len++; } printf("%s ", cmd[i]); strncpy(same, "-", len); } /* else if it doesn't match, but cmd is an external command */ else if ((dir = opendir(cmd[i])) == 0) { lastOccur = strrchr(cmd[i], '/'); strcpy(str3, lastOccur + 1); printf("%s ", str3); } /* else if the cmd[i] is an expanded env variable */ else { closedir(dir); printf("%s ", cmd[i]); } } i++; } printf("\n"); } else if (strcmp(cmd[0], "etime") == 0) { if (cmd[1] == 0) { printf("Usage: etime requires an argument.\n"); return; } /* remove etime from cmd */ for (i = 1; cmd[i] != 0; i++) { cmd[i - 1] = cmd[i]; } cmd[i - 1] = NULL; gettimeofday(&start, NULL); my_execute(cmd); gettimeofday(&end, NULL); printf("Elapsed Time: %.6fs\n", (double)((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)) / 1000000.0); } else if (strcmp(cmd[0], "limits") == 0) { if (cmd[1] == 0) { printf("Usage: limits requires an argument.\n"); return; } /* remove limits from cmd */ for (i = 1; cmd[i] != 0; i++) { cmd[i - 1] = cmd[i]; } cmd[i - 1] = NULL; /* child */ if ((pid = fork()) == 0) { my_execute(cmd); sprintf(pidstr, "%d", i); strcat(str1, pidstr); strcat(str1, str2); file = fopen(str1, "r"); if (file != NULL) { i = 1; while (fgets(str1, sizeof str1, file) != NULL) { if (i == 3) printf("\n%s", str1); if (i == 8) strcpy(str3, str1); if (i == 9) printf("%s%s", str1, str3); if (i == 13) printf("%s", str1); i = i + 1; } fclose(file); } exit(1); } /* parent */ else { i = pid; waitpid(pid, &status, 0); } } }
static void tcpServerWorkTask(TWORK *targ) /*int sFd, char *address, unsigned short port) */ { int ret; TREQUEST clientRequest; /* request/message from client */ int nRead; /* number of bytes read */ char message[REQUEST_MSG_SIZE]; int len, oldstdout; #ifdef Linux prctl(PR_SET_NAME,"tcp_server_work"); #endif if( (nRead = recv(targ->newFd, (char *) &clientRequest, sizeof (TREQUEST), 0)) > 0 ) { /* convert integers from network byte order */ clientRequest.msgLen = ntohl(clientRequest.msgLen); clientRequest.reply = ntohl(clientRequest.reply); /* printf ("MESSAGE (nRead=%d, Address>%s<, port=%d): Executing >%s<\n", nRead, targ->address, targ->port, clientRequest.message); */ strcpy(message, clientRequest.message); /* store it to be used later for debugging */ strcpy(current_message, message); /* try Executing the message (each component must provide codaExecute() function */ /*do not print: message may contains bad characters, it will be checked inside codaExecute printf("Executing >%s< (len=%d)\n",message,strlen(message));*/ fflush(stdout); oldstdout = dup(STDOUT_FILENO); /*save stdout*/ dup2(targ->newFd,STDOUT_FILENO); /*redirect stdout*/ /*close(targ->newFd);*/ /* check if message makes sence */ my_execute(message); dup2(oldstdout, STDOUT_FILENO); /*restore stdout*/ ret = close(oldstdout); /* close server socket connection */ if(ret<0) perror("close oldstdout: "); } else if(nRead == 0) { printf("connection closed, exit thread\n"); } else { perror("ERROR (recv)"); } /*free(targ->address);-stuck here !!!*/ /* free malloc from inet_ntoa() */ ret = close(targ->newFd); /* close server socket connection */ if(ret<0) perror("close targ->newFd: "); request_in_progress = 0; /* terminate calling thread */ pthread_exit(NULL); }