Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
956 B
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

int main (int argc, char *argv[]){
	char s[20];
	while(strcmp("exit", s) != 0){ //same
		printf("$");
		scanf("%s", s);
		
		char *token = strtok(s, " ");
		int a = 0; //activation variable to see if command or argument
		while(token != NULL){
			if(strcmp(token, "ls") == 0){
				char *args[2];
				args[0] = "/bin/ls";
				args[1] = NULL;
				
				int rc = fork();
				if(rc<0){printf("Fork failed");}
				else if(rc==0){
					execv(args[0], args);
					exit(1);
				}
				else{
					int w = wait(NULL); printf("Success!\n");
				}
				break;
			}
			if(a == 0){
				if(strcmp(token, "sleep") == 0){a = 1;}
				else if(strcmp(token, "echo") == 0){a = 2;}
			}
			if(a == 1){
				printf("Sleep: variable.");
				a = 0;
			}
			if(a == 2){
				printf("Echo: variable.");
				a = 0;
			}
			token = strtok(NULL, " ");
		}
		
		
	}
}