Program that creates a shell and executes some shell commands

700 WordsApr 23, 20193 Pages
/* myshell.c Description: Program that creates a shell and executes some shell commands */ #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <sys/wait.h> extern char **getln(); // Function used to check the file input direction command int inputFile(char **args, char **fileIn) { int i,j; //go thorough the command line arguments for(i=0;args[i] != NULL;i++) { // check for the input direction symbol if(args[i][0] == '<') { // save the filename command if(args[i+1] != NULL) { *fileIn = args[i+1]; } /*for(j=i;args[j-1] != NULL;j++) { args[j] = args[j+2]; }*/…show more content…
= 'x')) { convert = strtol(args[i],NULL,16); printf("The number is %ld \n", convert); sum = sum + convert; } // check if it is a valid integer or not num = atoi(args[i]); if (num == 0) { //printf("Number is: %i \n", num); } else { sum = sum + num; } } } // check to see if there is arg or args command, if there is then print // number of arguments and list them on the screen if((strcmp(args[0],"arg") == 0) || strcmp(args[0],"args") == 0) { for(j=1;args[j] != NULL;j++) { count++; } printf("Output: argc = %d, ", count); printf("args = "); for(j=1;args[j] != NULL;j++) { printf("%s, ", args[j]); } printf("\n"); } // check the error messages for the addition command and print the total sum if(strcmp(args[0],"add") == 0) { if(sum == 0 && num ==0) { printf("Invalid integer/ String Error \n"); } else { printf("Output: "); for(k=1;args[k] != NULL;k++) { printf("%s + ", args[k]); } printf("= %d \n", sum); } } // check if there is a multilplication command, if there is then multiply all the // valid arguments and print the product // Accepted inputs: Only ints, Invalid Inputs: 0,Strings // Details and cases are explained in readme file int zeroCheck=0; if(strcmp(args[0],"mul") == 0) { int product=1; for(counter=1;args[counter] != NULL;counter++) {

More about Program that creates a shell and executes some shell commands

Open Document