Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tomasz Bachosz
- //NYU ABU DHABI
- //tb1747
- #define F_OK
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #define MAXLINE 1024 //Max line size
- #define MAXARGS 128 //Maximum arguments
- //Declaring functions
- void parselines(const char *cmdline, char **argv);
- void pwd();
- void cd(char *);
- void executeCommands(char **argv,char * cmd_out);
- void saveToHistory(char* str);
- void showHistory();
- void runCommandFromHistory(char* str);
- void printExport();
- void addVariableToExport(char* argv);
- int checkIfCommandExists(char* str);
- int splitInput(char input[MAXLINE],char * filename);
- //Declaring global variables
- char *globalVariables[1024];
- char *curPath;
- //Main function
- int main() {
- char input[MAXLINE];
- char *argv[128];
- //Setting first global variable
- globalVariables[0]="PATH=/bin";
- //Making sure that the history file exists
- FILE *file;
- file = fopen("history.txt", "a");
- fclose(file);
- char cmd_out[MAXLINE];
- //Getting input, saving it to history, reading the command
- while (1) {
- printf(">>" );
- char * pipe_c;
- char *temp=cmd_out;
- while(*temp){
- *temp++=' ';
- }
- fgets(input, 1024, stdin);
- saveToHistory(input);
- char *cursor=input;
- int fl=1;
- do{
- fl=1;
- if(pipe_c=strchr(cursor,'|'))
- *pipe_c++='\0';
- else fl=0;
- char fname[MAXLINE];
- if(splitInput(input,fname)){
- char content[MAXLINE];
- loadFile(fname,content);
- char * temp=cmd_out;
- temp=strchr(cmd_out,'\0');
- strcpy(temp,content);
- };
- parselines(cursor,argv);
- executeCommands(argv,cmd_out);
- if(fl)cursor=pipe_c+1;
- }while(fl);
- printf("%s",cmd_out);
- }
- return 0;
- }
- void executeCommands(char **argv,char * cmd_out){
- //Finding appropriate command and executing the function
- if (argv[0] == NULL) {
- return;
- }
- if (strcmp(argv[0], "pwd")==0) {
- pwd();
- } else if(strcmp(argv[0], "cd")==0) {
- cd(argv[1]);
- } else if(strcmp(argv[0], "export")==0) {
- if(argv[1]!=NULL){
- //Saving the argument for use.
- char* unkCommand = malloc(MAXLINE);
- strcpy(unkCommand, argv[1]);
- addVariableToExport(unkCommand);
- }else{
- printExport();
- }
- } else if(strcmp(argv[0], "history")==0) {
- showHistory(cmd_out);
- } else if(strcmp(argv[0], "exit")==0){
- exit(0);
- } else {
- //Checking if it is not a recall to history
- static char array[MAXLINE];
- char *unkCommand=array;
- strcpy(unkCommand, argv[0]);
- if(unkCommand[0]=='!'){
- //Getting the number to history
- while (*unkCommand && (*unkCommand == '!')){
- unkCommand++;
- }
- runCommandFromHistory(unkCommand);
- } else {
- //Else we check if the command is in PATH. Otherwise not found.
- char* unkCommand1 = malloc(MAXLINE);
- strcpy(unkCommand1, argv[0]);
- if(checkIfCommandExists(unkCommand1)==1){
- int fd[2];
- int fd_in[2];
- pipe(fd);
- pipe(fd_in);
- pid_t pid2 = fork();
- if(pid2==0){
- close(fd_in[0]);
- dup2(fd_in[1],STDOUT_FILENO);
- close(fd_in[1]);
- printf("%s",cmd_out);
- exit(1);
- }
- pid_t pid = fork();
- if(pid==0){
- close(fd_in[1]);
- dup2(fd_in[0],STDIN_FILENO);
- close(fd[0]);
- dup2(fd[1],1);
- close(fd[1]);
- close(fd_in[0]);
- if(execv(curPath,argv)==-1){
- printf("error with subprocess\n");
- exit(1);
- }
- exit(1);
- }
- close(fd_in[0]);
- close(fd_in[1]);
- close(fd[1]);
- waitpid(pid,NULL,0);
- waitpid(pid2,NULL,0);
- // char cmd_out[MAXLINE];
- char *temp = cmd_out;
- while(*temp!=NULL){
- *temp++='\0';
- }
- if(pid==0||pid2==0)exit(1);
- read(fd[0],cmd_out,MAXLINE);
- close(fd[0]);
- // fgets(cmd_out, MAXLINE, fd[0]);
- //printf("NO: %s",cmd_out);
- //fflush(stdout);
- //fflush(stdin);
- /*
- char * iter[128];
- parselines(cmd_out,iter);
- int i=0;
- //printf("DEBUGGING ###\n###");
- //printf("%s",iter[i]);
- while(iter[i]!=NULL){
- printf("%s\n###",iter[i]);
- i++;
- }
- */
- //CHANGES
- return;
- }
- else{
- printf("command not found\n");
- }
- }
- }
- }
- //Checking for command's existance in PATH
- int checkIfCommandExists(char* str){
- char *buf = malloc(MAXLINE); //Buffer for global Variable
- char *buf2 = malloc(MAXLINE); //Buffer for reading the path
- char *argv[128];
- char *argv2[128];
- char *delim;
- int argc;
- //First separating data from globalVariables (PATH and JAVA)
- strcpy(buf2, globalVariables[0]);
- //Setting last character to equal sign
- buf2[strlen(buf2)] = '=';
- argc = 0;
- delim = strchr(buf2, '=');
- //Replacing = to \0 and taking in between as arguments
- //Here we care only about command
- while (delim) {
- argv2[argc++] = buf2;
- *delim = '\0';
- buf2 = delim + 1;
- while (*buf2 && (*buf2 == '='))
- buf2++;
- delim = strchr(buf2, '=');
- }
- argv2[argc] = NULL;
- //Setting a flag to check whether access was granted
- int isThereAccess=0;
- //If there is no PATH in a variable we know that access won't be there
- //so we can skip
- if(argv2[1]!=NULL){
- strcpy(buf, argv2[1]);
- //Similarily like above with =, we do same algorithm with :
- buf[strlen(buf)] = ':'; // check this
- argc = 0;
- delim = strchr(buf, ':');
- while (delim) {
- argv[argc++] = buf;
- *delim = '\0';
- buf = delim + 1;
- while (*buf && (*buf == ':'))
- buf++;
- delim = strchr(buf, ':');
- }
- //Setting the last element to NULL
- argv[argc] = NULL;
- int i=0;
- //Iterating through paths and adding command to it
- //If access granted, setting the flag
- while(argv[i]!=NULL){
- strcat(argv[i],"/");
- strcat(argv[i],str);
- if(access(argv[i],F_OK)==0){
- curPath=argv[i];
- isThereAccess=1;
- break;
- }
- i++;
- }
- }
- //Returning a value according to the flag
- if(isThereAccess){
- return 1;
- }else{
- return 0;
- }
- }
- //Adding global variables
- void addVariableToExport(char* str){
- char *tempArray[128];
- char *tempVarName[128];
- static char array[MAXLINE];
- static char array2[MAXLINE];
- char *tempVarPath = array2; //Holds full command entered
- char *tempPath = array; //Will be used as a buffer pointer
- char *delim;
- int argc; //Arguments counter
- //Creating local copies
- strcpy(tempVarPath,str);
- strcpy(tempPath, str);
- //Getting rid of leading spaces, if any
- while (*tempPath && (*tempPath == ' ')){
- tempPath++;
- }
- argc = 0;
- delim = strchr(tempPath, '=');
- //Replacing = to \0 and taking in between as arguments
- while (delim) {
- tempArray[argc++] = tempPath;
- *delim = '\0';
- tempPath = delim + 1;
- while (*tempPath && (*tempPath == ' '))
- tempPath++;
- delim = strchr(tempPath, ' ');
- }
- //Now tempArray[0] is the Variable name
- //and tempPath is the path entered.
- //Setting the last element to NULL
- tempArray[argc] = NULL;
- //Setting flag to check whether element already
- //exists in local variables
- int isNewFlag = 0;
- int counter=0;
- while(globalVariables[counter]!=NULL){
- //Working on local copy of a variable
- strcpy(tempPath, globalVariables[counter]);
- tempPath[strlen(tempPath)] = '=';
- argc = 0;
- delim = strchr(tempPath, '=');
- //Replacing = to \0 and taking in between as arguments
- while (delim) {
- tempVarName[argc++] = tempPath;
- *delim = '\0';
- tempPath = delim + 1;
- while (*tempPath && (*tempPath == '='))
- tempPath++;
- delim = strchr(tempPath, '=');
- }
- //If variable from counter is same as variableName entered
- //we overwrite current variable
- if(strcmp(tempArray[0], tempVarName[0])==0){
- globalVariables[counter]=str;
- isNewFlag=1;
- break;
- }
- counter++;
- }
- if(isNewFlag==0){
- //If variable was not found in globalVariables
- //we add a new variable
- int counterx = 0;
- while(globalVariables[counterx]!=NULL)
- {
- counterx++;
- }
- globalVariables[counterx]=str;
- }
- }
- //Function to print all local variables
- void printExport(){
- int i = 0;
- while(globalVariables[i]!=NULL){
- printf("%s \n", globalVariables[i]);
- i++;
- }
- }
- //Parsing the command line
- void parselines(const char *cmdline, char **argv) {
- static char array[MAXLINE]; //Command line stored locally
- char *buf = array; //Pointer to go through the cl
- char *delim; //Delimeter
- int argc;
- //Creating a local copy
- strcpy(buf, cmdline);
- //Changing the last character to space for the algorithm
- buf[strlen(buf)-1] = ' ';
- //Deleting spaces in the beginning
- while (*buf && (*buf == ' '))
- buf++;
- argc = 0;
- delim = strchr(buf, ' ');
- //Replacing spaces to \0 and taking in between as arguments
- while (delim) {
- argv[argc++] = buf;
- *delim = '\0';
- buf = delim + 1;
- while (*buf && (*buf == ' '))
- buf++;
- delim = strchr(buf, ' ');
- }
- //Setting the last element to NULL
- argv[argc] = NULL;
- }
- //Function for pwd command
- void pwd()
- {
- char cwd[1024];
- getcwd(cwd, sizeof(cwd));
- printf("%s \n", cwd);
- }
- //Function for change directory command
- void cd(char *path) {
- chdir(path);
- }
- //Function for running command from history
- void runCommandFromHistory(char* number){
- FILE *file;
- char str[1024];
- char* filename = "history.txt";
- int counter = 1;
- char *argv[128];
- char cmd_out[MAXLINE];
- static char array[MAXLINE];
- char *newInteger = array;
- //Creating a local copy
- strcpy(newInteger, number);
- int numberCall = atoi(newInteger);
- file = fopen(filename, "r");
- if (file == NULL){
- printf("Could not open file %s",filename);
- }
- while (fgets(str, 1024, file) != NULL){
- if(counter==numberCall){
- //Executing the command under this counter number
- parselines(str, argv);
- executeCommands(argv,cmd_out);
- }
- counter++;
- }
- fclose(file);
- }
- // Function to save commands in history file
- void saveToHistory(char* str)
- {
- FILE *file;
- file = fopen("history.txt", "a");
- fprintf(file, "%s", str);
- fclose(file);
- }
- //Function to show history
- void showHistory(char *cmd_out)
- {
- FILE *file;
- char str[1024];
- char* filename = "history.txt";
- int counter = 1;
- file = fopen(filename, "r");
- if (file == NULL){
- printf("Could not open file %s",filename);
- return;
- }
- char out[MAXLINE];
- char * temp=out;
- while (fgets(str, 1024, file) != NULL){
- sprintf(temp,"%d %s\0", counter, str);
- counter++;
- temp=strchr(temp,'\0');
- }
- strcpy(cmd_out,out);
- fclose(file);
- }
- int splitInput(char input[MAXLINE], char * filename){
- char * f;
- if(!(f=strchr(input,'<')))return 0;
- *f++='\0';
- while(*f&&*f==' '){
- f++;
- }
- f=strcat(f,"\0");
- strcpy(filename,f);
- return 1;
- }
- int loadFile(char * filename, char * content){
- char * f;
- f=filename;
- while(*f&&(*f!=' '&&*f!='\n')){
- f++;
- }
- *f='\0';
- FILE* file = fopen(filename, "r");
- if (file == NULL){
- printf("Could not open file %s",filename);
- return 0;
- }
- char * temp=content;
- char str[MAXLINE];
- while (fgets(str, 1024, file) != NULL){
- temp=strcat(temp,str);
- }
- temp=strcat(temp,"\0");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement