Exemplo n.º 1
0
ATF_TC_BODY(append_ap, tc)
{
    check_append(append_ap_aux);
}
Exemplo n.º 2
0
ATF_TC_BODY(append_fmt, tc)
{
    check_append(atf_dynstr_append_fmt);
}
Exemplo n.º 3
0
/* 
 * Do the command
 */
int do_command(char **args, int in, int out, int pipe, int block) {
    char *input_filename, *output_filename;

    // Check for redirected input
    int input = redirect_input(args, &input_filename);

    switch(input) {
    case -1:
        printf("Syntax error!\n");
        return -1;
        break;
    case 0:
        break;
    case 1:
        printf("Redirecting input from: %s\n", input_filename);
        break;
    }

    // check for append
    int append = check_append(args, &output_filename);

    // Check for redirected output
    int output = redirect_output(args, &output_filename);

    switch(append) {
    case -1:
        printf("Syntax error!\n");
        return -1;
        break;
    case 0:
        break;
    case 1:
        printf("Redirecting and appending output to: %s\n", output_filename);
        break;
    }

    switch(output) {
    case -1:
        printf("Syntax error!\n");
        return -1;
        break;
    case 0:
        break;
    case 1:
        printf("Redirecting output to: %s\n", output_filename);
        break;
    }
  
    int result;
    pid_t child_id;

    // Fork the child process
    child_id = fork();

    // Check for errors in fork()
    switch(child_id) {
    case EAGAIN:
        perror("Error EAGAIN: ");
        return child_id;
    case ENOMEM:
        perror("Error ENOMEM: ");
        return child_id;
    }

    if(child_id == 0) {

        // Set up redirection in the child process
        if(out != 1) {
            dup2(out, 1);
            close(out);
        }
        if(in != 0) {
            dup2(in, 0); 
            close(in);
        }

        if(input)
            freopen(input_filename, "r", stdin);
        if(output)
            freopen(output_filename, "w+", stdout);
        if(append)
            freopen(output_filename, "a", stdout);

        result = execvp(args[0], args);
        exit(1);
    }else {
        
        return child_id;
    }

    return result;
}