Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIZE 257
- int main(){
- //string for user input to be read to
- char *line;
- //string for line to be copied to, minus the null char
- char input[257];
- //array of strings to be us:q
- //used for execvp
- char *cmd[257];
- //search string for strtok
- char *search = " ";
- //for waitpid
- int status;
- line = malloc (SIZE+1);
- pid_t pid;
- //repeat infinitely
- while(1){
- printf("cmd: ");
- fgets(line, SIZE+1,stdin);
- //get char count of user input
- int i=0;
- while(line[i] != 0){
- i++;
- }
- puts(line);
- //copy line to input, removing null character from end
- strncpy(input,line,i-1);
- //free(line);
- puts(input);
- cmd[0] = strtok(input, " ");
- //if the inputed command is quit, quit
- if (strncmp(cmd[0],"quit",4)==0){
- puts("bye");
- free(line);
- exit(1);
- }
- //tokenize input into cmd
- i=1;
- while(cmd[i] = strtok(NULL, " ")){
- puts("success");
- puts(cmd[i]);
- i++;
- }
- pid = fork();
- if(pid < 0){
- perror("error");
- }
- else if(pid > 0){
- waitpid(-1, &status, 0);
- }
- else{
- if(execvp(cmd[0],cmd)<0){
- perror("exec failed");
- }
- }
- }
- puts("Time to quit");
- return 0;
- }
Add Comment
Please, Sign In to add comment