LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2012, 11:49 AM   #1
leo1925
LQ Newbie
 
Registered: Nov 2012
Posts: 1

Rep: Reputation: Disabled
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");
        }

}
 
Old 11-18-2012, 06:30 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by leo1925 View Post
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

Last edited by Sergei Steshenko; 11-18-2012 at 06:32 PM.
 
Old 11-19-2012, 07:03 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,636
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
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 &
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a linux shell with C CragStar Programming 2 10-02-2002 09:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration