simple shell

myshell.c

#include  "myshell.h"

int main()
{
    printf("My Shell. Enjoy it\n");
    while (1) {
            Command *cmd; 
                cmd=(Command *)malloc(sizeof(Command)); 
        /* Print the command prompt */
        printf("%s $> ", getenv("PWD"));
       
        /* Read a command line */
        if (!fgets(cmd->line, 1024, stdin) )  
            return 0;

        run(cmd);
        wait(NULL);
    }
    return 0;
}

#include  "myshell.h"

int run(Command* cmd)
{
    split(cmd->line);
    redirect(cmd);
   
    if (args[0] != NULL) {
        if (strcmp(args[0], "exit") == 0) {
            exit(0);
        }
        if (strcmp(args[0], "pwd") == 0) {
            do_pwd();
            return;
        }
       
        pid = fork();
        if (pid == 0) {   
            if( cmd->is_stdin_red){ 
                int input_fd = open(cmd->in_red_file, O_RDONLY); 
                dup2(input_fd, 0);   
            } 
            if(cmd->is_stdout_red){ 
                int output_fd = open(cmd->out_red_file, O_WRONLY | O_CREAT, 0644); 
                dup2(output_fd, 1);   
            } 
           
            if (execvp( cmd->args[0], cmd->args) == -1)
                _exit(EXIT_FAILURE); // If child fails
        }
    }
    return 0;
}
void redirect(Command *cmd)
{
    int i = 0 , j = 0;
    while(args[i] != NULL){
            if (*args[i] == '<' && args[i + 1] != NULL ){
                cmd->is_stdin_red = 1; 
                cmd->in_red_file = args[i + 1];
                i += 2;
            } else {
                if (*args[i]== '>' && args[i + 1] != NULL){               
                    cmd->is_stdout_red = 1;  
                    cmd->out_red_file = args[i + 1];
                    i+= 2;
                }
                else{
                    cmd->args[j] = args[i];
                    ++j;
                    ++i;
                }
            }
    }
    cmd->args[j+1] = NULL;
}

void split(char* line)
{
    line = skipwhite(line);
    char* next = strchr(line, ' ');
    int i = 0;
 
    while(next != NULL) {
        next[0] = '\0';
        args[i] = line;
        ++i;
        line = skipwhite(next + 1);
        next = strchr(line, ' ');
    }
 
    if (line[0] != '\0') {
        args[i] = line;
        next = strchr(line, '\n');
        next[0] = '\0';
        ++i;
    }
 
    args[i] = NULL;
}
char* skipwhite(char* s)
{
    while (isspace(*s)) ++s;
    return s;
}

void do_pwd()
{
    char *buf;
    if((buf=getcwd(NULL,0))) {
        printf("%s\n",buf);
    } else
        printf("Error\n");
}

myshell.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
 
#include <sys/types.h>
#include <sys/wait.h>

#define MAX_LINE_LEN 256
#define CURRENT_PATH_LEN 256
#define MAX_ARGS_LEN 80

typedef struct shell_command
{
    char  line[MAX_LINE_LEN];
    char *cmd;
    char *args[MAX_ARGS_LEN];
    int is_stdin_red, is_stdout_red; 
    char *in_red_file, *out_red_file;
} Command;

int run(Command *cmd);
void split(char *line);
void redirect(Command *cmd);
char* skipwhite(char *s);
void do_pwd();

char* args[MAX_ARGS_LEN];
int pid;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
 
#include <sys/types.h>
#include <sys/wait.h>

#define MAX_LINE_LEN 256 
#define CURRENT_PATH_LEN 256 
#define MAX_ARGS_LEN 80

typedef struct shell_command
{
	char  line[MAX_LINE_LEN];
	char *cmd;
	char *args[MAX_ARGS_LEN];
	int is_stdin_red, is_stdout_red;  
	char *in_red_file, *out_red_file; 
} Command;

int run(Command *cmd);
void split(char *line);
void redirect(Command *cmd);
char* skipwhite(char *s);
void do_pwd();

char* args[MAX_ARGS_LEN];
int pid;

#include  "myshell.h"

int main()
{
	printf("My Shell. Enjoy it\n");
	while (1) {
			Command *cmd;  
				cmd=(Command *)malloc(sizeof(Command));  
		/* Print the command prompt */
		printf("%s $> ", getenv("PWD"));
		
		/* Read a command line */
		if (!fgets(cmd->line, 1024, stdin) )   
            return 0;

		run(cmd);
		wait(NULL); 
	}
	return 0;
}
#include  "myshell.h"

int run(Command* cmd)
{
	split(cmd->line);
	redirect(cmd);
	
	if (args[0] != NULL) {
		if (strcmp(args[0], "exit") == 0) {
			exit(0);
		}
		if (strcmp(args[0], "pwd") == 0) {
			do_pwd();
			return;
		}
		
		pid = fork();
		if (pid == 0) {	
			if( cmd->is_stdin_red){  
                int input_fd = open(cmd->in_red_file, O_RDONLY);  
                dup2(input_fd, 0);    
            }  
            if(cmd->is_stdout_red){  
                int output_fd = open(cmd->out_red_file, O_WRONLY | O_CREAT, 0644);  
                dup2(output_fd, 1);    
            }  
            
			if (execvp( cmd->args[0], cmd->args) == -1)
				_exit(EXIT_FAILURE); // If child fails
		}
	}
	return 0;
}
void redirect(Command *cmd)
{
	int i = 0 , j = 0;
	while(args[i] != NULL){
			if (*args[i] == '<' && args[i + 1] != NULL ){
				cmd->is_stdin_red = 1;  
				cmd->in_red_file = args[i + 1];
				i += 2;
			} else {
				if (*args[i]== '>' && args[i + 1] != NULL){				
					cmd->is_stdout_red = 1;   
					cmd->out_red_file = args[i + 1]; 
					i+= 2;
				} 
				else{
					cmd->args[j] = args[i];
					++j;
					++i;
				}
			} 
	}
	cmd->args[j+1] = NULL;
}

void split(char* line)
{
	line = skipwhite(line);
	char* next = strchr(line, ' ');
	int i = 0;
 
	while(next != NULL) {
		next[0] = '\0';
		args[i] = line;
		++i;
		line = skipwhite(next + 1);
		next = strchr(line, ' ');
	}
 
	if (line[0] != '\0') {
		args[i] = line;
		next = strchr(line, '\n');
		next[0] = '\0';
		++i; 
	}
 
	args[i] = NULL;
}
char* skipwhite(char* s)
{
	while (isspace(*s)) ++s;
	return s;
}

void do_pwd()
{
	char *buf;
	if((buf=getcwd(NULL,0))) {
		printf("%s\n",buf);
	} else 
		printf("Error\n");
}

猜你喜欢

转载自summerli.iteye.com/blog/2054337