Piping program in C. Converting from user input to command line args

时间:2019-04-16 22:32:45

标签: c bash pipe fork

I was to make a program that was to implement a piping experiment in C. I did this by method of taking the command from user input but after finishing it i was told i was to take it as initial command line args which is absolutely stumping me in how to convert it so that it will take the inputs in this manor. When I do so and concatenate the args into a string and send it through it only will print out a long listing of 0's or nothing at all for the command ls /usr/bin | wc -l

CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/wait.h>

static char* args[512];
pid_t pid;
int command_pipe[2];

#define READ  0
#define WRITE 1

static int command(int input, int first, int last)
{
    int pipettes[2];

    pipe( pipettes );   
    pid = fork();


    if (pid == 0) {
        if (first == 1 && last == 0 && input == 0) {
            // First command
            dup2( pipettes[WRITE], STDOUT_FILENO );
        } else if (first == 0 && last == 0 && input != 0) {
            // Middle command
            dup2(input, STDIN_FILENO);
            dup2(pipettes[WRITE], STDOUT_FILENO);
        } else {
            // Last command
            dup2( input, STDIN_FILENO );
        }

        if (execvp( args[0], args) == -1)
            _exit(EXIT_FAILURE); 
    }

    if (input != 0) 
        close(input);


    close(pipettes[WRITE]);


    if (last == 1)
        close(pipettes[READ]);

    return pipettes[READ];
}


static void cleanup(int n)
{
    int i;
    for (i = 0; i < n; ++i) 
        wait(NULL); 
}

static int run(char* cmd, int input, int first, int last);
static char line[1024];
static int n = 0; /* number of calls to 'command' */

int main(int argc, char *argv[])
{
    while (1) {

        for(int i = 1; i < argc; i++){
            strcat(line, argv[i]);
            strcat(line, " ");
        }

        /* Read a command line */


        int input = 0;
        int first = 1;

        char* cmd = line;
        char* next = strchr(cmd, '@'); /* Find first '|' */

        while (next != NULL) {
            /* 'next' points to '|' */
            *next = '\0';
            input = run(cmd, input, first, 0);

            cmd = next + 1;
            next = strchr(cmd, '@'); /* Find next '|' */
            first = 0;
        }
        input = run(cmd, input, first, 1);
        cleanup(n);
        n = 0;
    }
    return 0;
}

static void split(char* cmd);

static int run(char* cmd, int input, int first, int last)
{
    split(cmd);
    if (args[0] != NULL) {
        if (strcmp(args[0], "exit") == 0) 
            exit(0);
        n += 1;
        return command(input, first, last);
    }
    return 0;
}

static char* skipwhite(char* s)
{
    while (isspace(*s)) ++s;
    return s;
}

static void split(char* cmd)
{
    cmd = skipwhite(cmd);
    char* next = strchr(cmd, ' ');
    int i = 0;

    while(next != NULL) {
        next[0] = '\0';
        args[i] = cmd;
        ++i;
        cmd = skipwhite(next + 1);
        next = strchr(cmd, ' ');
    }

    if (cmd[0] != '\0') {
        args[i] = cmd;
        next = strchr(cmd, '\n');
        next[0] = '\0';
        ++i; 
    }

    args[i] = NULL;
}

0 个答案:

没有答案