LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Creating a linux shell (http://www.linuxquestions.org/questions/programming-9/creating-a-linux-shell-4175437693/)

leo1925 11-18-2012 11:49 AM

Creating a linux shell
 
Hello to all.
I am doing a project and i have hit a wall in the programming of the shell, i am trying to add the capability to run processes in the background but i can't think of a way to do it.
Can anyone help?

Here is what i have done so far:
Code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>        // Mainly for strtok
#include <unistd.h>
#include <errno.h>

#define MAX_ARGS 4

char * PATH = "MyShell$ ";

char *path; //for the command path

typedef struct{
        char *cmd_path;        //path of command
        int argc;        //number of args for the cmd
        char *argv[MAX_ARGS];
        //char cmd_suffix;
}commandStruct;

void runCommand(commandStruct *command1);

void cdr(int argc, char *argv[]);

int main() {
       
        system("clear");

        while(1) {
                int flag = 0; //gia tin entoli cd
                char buffer[100], *input;
                pid_t child;
                int childstatus, status;        //For Fork
       
                char *cmd;                        //For strtok

                char *command;                        //The command
                int cmdlen;

                commandStruct *command1 = (commandStruct*) malloc(sizeof(commandStruct));       
                command1->argc = 1;
               
                printf("%s", PATH);
                fflush(stdout);
                input=fgets(buffer, 100, stdin);
               
               
                while(input[0]=='\n')
                {
                    printf("%s", PATH);
                    fflush(stdout);
                    input = fgets(buffer, 256, stdin);
                }
               
               

                cmdlen = strlen(input);                //Take out new line char
                if (input[cmdlen-1] == '\n') {        //
                        input[cmdlen-1] = '\0';        //
                }                                //

                //Get first token (the command) from input
                      cmd = strtok(input, " ");
                command = cmd;
               
             
                if (strcmp(command, "exit") == 0){
                    break;
                }
               
             
                command1->cmd_path = command;
                command1->argv[0] = command1->cmd_path;
       
                //Loop thru until whole command is tokenized
                while (1)
                {
                              //Check command has no more than 4 args
                        if (command1->argc > 4) {
                                printf("\nError! Too many command arguements!\n");
                                break;
                        }

                        //Extract next part of command
                        cmd = strtok(NULL, " ");

                        //Check that there is nothing else to extract
                        if (cmd == NULL)
                        {
                                //printf("Command Tokenised");
                                break;
                        }

                        command1->argv[command1->argc] = cmd;        //put arg into arg array
                        command1->argc = command1->argc+1;        //increase count of args
                               
                }

                printf("Number of args: %d\n", command1->argc);
       
                if(strcmp(command, "cd") == 0){
                                cdr(command1->argc, command1->argv);
                                flag=1;
                }
                if(flag==0){
                    childstatus = fork();
                    if (childstatus != 0) { //if parent process is running
                        wait(&status);
                }
                else {
                /* This area is child process */
                        runCommand(command1);
                        return(0);
                }
                }

        }
        return 0;
        exit(0);
}

void cdr(int argc, char *argv[]){
   
    if(argc==1){
        const char* home=getenv("HOME");
        chdir(home);
        return;
    }
    int result=chdir(argv[1]);
    if (result!=0){
        switch(result){
            case EACCES: perror("Permission denied");
            break;
            case EIO:        perror("An input output error occured");
            break;
            case ENAMETOOLONG: perror("Path is to long");
            break;
            case ENOTDIR: perror("A component of path not a directory");
            break;
            case ENOENT: perror("No such file or directory"); printf("enoent\n");
            break;
            default: printf("Couldn't change directory to %s\n", argv[1] );
        }
  }
}

void runCommand(commandStruct *command1) {
       
        printf("Command: %s\n", command1->cmd_path);
        int i=0;
        i=execvp(command1->cmd_path, command1->argv);
        if (i==-1){
            printf("Λανθασμένη ή μη υποστηριζόμενη εντολή\n");
        }

}


Sergei Steshenko 11-18-2012 06:30 PM

Quote:

Originally Posted by leo1925 (Post 4832084)
Hello to all.
... i am trying to add the capability to run processes in the background but i can't think of a way to do it.
...

https://duckduckgo.com/?q=fork+manpage
https://duckduckgo.com/?q=exec+manpage
https://duckduckgo.com/?q=fork+exec+tutorial
...
http://linux.softpedia.com/get/Progr...mon-7353.shtml

sundialsvcs 11-19-2012 07:03 AM

I sincerely suggest that you should ask your instructor and/or your classmates, so that you can find the solution to this problem yourself. If this is not a homework assignment and you are still "truly stuck," e.g. after thoroughly searching even this site, and C/C++ programmng tutorials online, then please ask again.

$ goodluck &


All times are GMT -5. The time now is 11:16 AM.