试图获得一个基本的C shell来做一个简单的管道()

时间:2016-02-25 02:52:51

标签: c linux shell pipe piping

我正在尝试创建一个简单的c shell。我唯一能做的就是管道。我只需要一个管道(即" ls | wc -l")。通过从命令行读取参数,我无法找到有人这样做的任何内容,我们将非常感谢任何帮助。我需要帮助获得第二组参数,这些参数位于" |"之后。现在,如果我有参数" ls | wc -l"我会得到" ls"存储在args2中,但我需要" wc -l"在args3中,所以我可以分别execvp()。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#define HISTORY_COUNT 20
char *prompt = "% ";

int
main()
{
int pid;
//int child_pid;
char line[81];
char *token;
char *separator = " \t\n";
char **args;
char **args2;
char **args3;
char cmd[100];
char *hp;
char *cp;
char *ifile;
char *ofile;
int check;
int pfds[2];
int i;
int j;
int current = 0;
int p = 0;
//int check;
char *hist[HISTORY_COUNT];
//char history[90];
//typedef void (*sighandler_t) (int);

args = malloc(80 * sizeof(char *));
args2 = malloc(80 * sizeof(char *));
while (1) {
    fprintf(stderr, "%s", prompt);
    fflush(stderr);

    if (fgets(line, 80, stdin) == NULL)
        break;
    // split up the line

    i = 0;
    while (1) {
        token = strtok((i == 0) ? line : NULL, separator);
        if (token == NULL)
            break;
        args[i++] = token;
              /* build command array */

    }
    args[i] = NULL;
    // assume no redirections
    ofile = NULL;
    ifile = NULL;

    // split off the redirections
    j = 0;
    i = 0;
    while (1) {        //stackoverflow.com/questions/35569673
        cp = args[i++];
        if (cp == NULL)
            break;

        switch (*cp) {
        case '<':
            if (cp[1] == 0)
                cp = args[i++];
            else
                ++cp;
            ifile = cp;
            break;

        case '>':
            if (cp[1] == 0)
                cp = args[i++];
            else
                ++cp;
            ofile = cp;
            break;
    case '|':               //I need this case to put the commands following
                              the pipe into a variable
    if(cp[1] ==0){
       cp = args[i++];

    printf("%s\n", cp);
       if(pipe(pfds) == -1){
           perror("Broken Pipe");
           exit(1);
       }
       //args3[j++] = cp;
       p = 1;
    }
    else{ 
       ++cp;
       printf("DOES THIS HAPPEN\n");
               //

    }
       break;

        default:
            args2[j++] = cp;
    args3[cp++] = cp
            break;
        }
    }
    args2[j] = NULL;
    if (j == 0)
        continue;

    switch (pid = fork()) {
    case 0:
        // open stdin
        if (ifile != NULL) {
            int fd = open(ifile, O_RDONLY);

            if (dup2(fd, STDIN_FILENO) == -1) {
                fprintf(stderr, "dup2 failed");
            }

            close(fd);
        }


        // open stdout
        if (ofile != NULL) {
            // args[1] = NULL;
            int fd2;


            if ((fd2 = open(ofile, O_WRONLY | O_CREAT, 0644)) < 0) {
                perror("couldn't open output file.");
                exit(0);
            }

            // args+=2;
            printf("okay");
            dup2(fd2, STDOUT_FILENO);
            close(fd2);
        }


    if(p == 1){        //from stackoverflow.com/questions/2784500
    printf("Welcome to the party\n");
    close(1);
    dup(pfds[1]);
    close(pfds[0]);
    execvp(args2[0], args2);
    break;
    }


    if(strcmp(args2[0], "cd") == 0){            //cd command
        if(args2[1] == NULL){
            fprintf(stderr, "Expected argument");
        }
        else{
    //printf("Is this happening");
        check = chdir(args2[1]);
    //printf("What abuot this");
    if(check != 0){
        fprintf(stderr,"%s",prompt);

    }
        }
    break;
          }
        if (strcmp(args2[0], "history") == 0){

        int i = 0;
        int hist_num = 1;
    //printf("%d\n", current);
        do {
                if (hist[i]) {
                        printf("%4d  %s\n", hist_num, hist[i]);
                        hist_num++;
                }

                i = (i + 1) % HISTORY_COUNT;

        } while (i != current);

          break;
    }
        execvp(args2[0], args2);        /* child */
        signal(SIGINT, SIG_DFL);
        fprintf(stderr, "ERROR %s no such program\n", line);
        exit(1);
        break;

    case -1:
        /* unlikely but possible if hit a limit */
        fprintf(stderr, "ERROR can't create child process!\n");
        break;

    default:
        //printf("am I here");
    if(p==1){
        close(0);
        dup(pfds[0]);
        close(pfds[1]);
        //execvp();
    }
        wait(NULL);
        //waitpid(pid, 0, 0);
    }
}

exit(0);
}

0 个答案:

没有答案