Question

In: Computer Science

Create a shell in C language 1) Create an argument tokenizer then write a main() function...

Create a shell in C language

1) Create an argument tokenizer then write a main() function that prints a prompt inputed by user, accepts input, and tokenizes the input.

2) Use the argument vector to an executeCmd() function with int executeCmd(char **args);

3) The function should return a -1 if an error, or a zero otherwise. Inputing an “x” in the prompter will exit the program.

4) Write an executeCmd() function that can execute any program in the background and redirect the output of any program to a file. Any number of functions can be executed in executeCmd() such as listing the files in the current directory, listing files in long format or displaying current working directory (ls, ls -a, pwd).

5) The program should run in the background working on the function, but the prompt should be available to take new requests ($ cat prog.c &).

6) Function execBackground() should be used to determine when a job should run in the background. The function will strip the ‘&’ character from the argument vector. This function will also let the output of a running program to be redirected to a file ($ grep if prog.c > foo).

7) The maker should include two source files and one header file as well as a makefile. The main() function, the executeCmd() function and all supporting functions will be in a file program_exec.c and the function prototype for executeCmd() program_exec.h.

Solutions

Expert Solution

#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/utsname.h>
#include<pwd.h>
#define _GNU_SOURCE
int executeCmd(char *args){

        int rec;
        rec = system(args);
        if(rec < 0)
                return -1;
        else
                return 0;
}
main()
{
        int i=0, j, k=0, err;char a[50],*b;

        struct stat s;
        stat("prompt.c",&s);

        struct passwd *pwd;
        pwd=getpwuid(s.st_uid);

        struct utsname u;
        if(uname(&u)==-1);
                b=u.nodename;

        for(j=0;b[j]!='.';j++);
                b[j]='\0';
        while(1)
        {
                if(fork() == 0)
                {
                        printf("<%d.%s@%s>",++i,pwd->pw_name,u.nodename);
                        gets(a);
                        err = executeCmd(a);
                        if(err == -1){

                                perror("executeCmd:");
                                break;
                        }

                }
                else
                        wait(0);
        }
}


Related Solutions

Write a simple shell in C language and show some outputs.
Write a simple shell in C language and show some outputs.
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
Write the simple shell in C language. Please show some outputs.
Write the simple shell in C language. Please show some outputs.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X,...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X, if X is greater than 1 and X is less than 100. Put the inline function in a main program that reads X from the keyboard, calls the function, and then outputs the result. 2. Show an empty statement and detail what it does? 3. A collection of predefined functions is called a Database                    C) Subroutine                       E) None of these Library                       D) Directive 4....
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT