Перенаправить стандартный вывод в файл и восстановить стандартный вывод с помощью функции - PullRequest
0 голосов
/ 04 октября 2019

Мне нужно создать функцию, которая временно перенаправляет стандартный вывод в имя файла «file», затем выполняет функцию f, а затем восстанавливает исходный стандартный вывод.

У меня возникли проблемыЯ понимаю, что должен использовать dup () и dup2 (), но не знаю, как их использовать.


void redirect_to (void (*f)(void), char *file)
{
 int fd = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0644);
 int old = dup(file);
 dup2(fd, old);
 f();
 close(fd);
}

Я уверен, чтоЯ делаю все неправильно, но не могу понять, как это сделать. Большое спасибо ..

Ответы [ 3 ]

0 голосов
/ 12 октября 2019

В программе есть несколько способов, в которых вы можете «перенаправить» свой вывод в файл, выполнить функцию, а затем восстановить исходный вывод на месте.

Прежде всего, вы не делаетена самом деле нужно выполнить настройку любого дескриптора файла, если вы просто переосмыслите свою задачу программирования и измените интерфейс функции для функции, которую вы передадите своей функции «перенаправления»:

RET_TYPE redirect_and_call_v1(RET_TYPE(*funct_to_be_called)(int descriptor_to_write_to), int fd_to_be_used_as_output)
{
    return funct_to_be_called(fd_to_be_used_as_output);
}

Вы увидите из этогоНапример, я немного изменил ваш API, чтобы передать функции redirect_and_call_v1 уже открытый дескриптор (потому что таким образом вы можете использовать свой собственный стандартный вывод в качестве дескриптора файла перенаправления, и вы не заставляете его бытьфайл), и я передал этот точный дескриптор в качестве параметра функции funct_to_be_called, чтобы он знал, куда что-то писать.

Во второй версии я буду уважать ваш вызывающий интерфейс, если выне может контролировать, где функция, которую вы собираетесь передать в качестве параметра, запишет в открытый дескриптор (который вы должны знать)Если вы хотите, чтобы он сохранил этот дескриптор для последующего его восстановления, а затем перенаправил его на тот дескриптор, который мы хотим использовать.

/* do error checking */
#define CHECK_ERR(var, name, exit_code) do {          \
        if((var) < 0) {                               \
            fprintf(stderr, "ERROR: " name ": %s\n",  \
                strerror(errno));                     \
            exit(exit_code);                          \
        }                                             \
    } while(0)

RET_TYPE redirect_and_call_v2(
        RET_TYPE(*funct_to_be_called)(void), 
        int fd_to_be_used_in_function_for_output, 
        int fd_funct_uses_to_write)
{
    /* save a copy descriptor, so we can overwrite the fd the one
     * function uses to write */
    int saved_old_fd = dup(fd_funct_uses_to_write);
    CHECK_ERR(saved_old_fd, "dup", 1);

    /* now, we change the descriptor the function uses to write by the one we want
     * it to be used for output */
    int res = dup2(fd_to_be_used_in_function_for_output, fd_funct_uses_to_write);
    CHECK_ERR(res, "dup2", 2);

    /* now, we call the function */
    RET_TYPE res2 = funct_to_be_called();

    /* now restore descriptors to what they where. For this, we use the saved
     * file descriptor */
    res = dup2(saved_old_func, fd_funct_uses_to_write);

    /* close the saved descriptor, as we dont need it anymore */
    close(saved_old_func);

    /* and finally, return the funct_to_be_called return value */
    return res2;
}

Последний полный пример показан ниже (репозиторий githubэто решение дано здесь ):

Файл main.c:

/* main.c --- main program for the test of temporary redirection
 * of a file descriptor.
 * fputs(3) prints to stdout, but we are going to intersperse
 * printf(3) calls, with puts(3) calls (with temporary
 * redirection) to see that the temporary redirection actually
 * works. All the output will be \n terminated, so we are sure
 * that buffers are flushed out on each call.
 *
 * Author: Luis Colorado <luiscoloradourcola@gmail.com>
 * Date: Sat Oct 12 13:28:54 EEST 2019
 * Copyright: (C) LUIS COLORADO.  All rights reserved.
 * License: BSD.
 */

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "redirect.h"

/* this is our unbuffering simulation of the puts(3) function,
 * as trouble will come if we don't consider the flushing of
 * buffers properly, and I don't want to overcomplicate things
 * with stdio buffering idiosyncracies.  Function forcibly writes
 * its output to standard output, so to make it use a different
 * descriptor, we need to redirect standard output to a different
 * place before call, and restore it later.*/
ssize_t output_function(const char *s)
{
        /* ensure all output is flushed before returning back */
        return write(1, s, strlen(s));
}

void do_usage(const char *progname)
{
        fprintf(stderr,
                "Usage: %s [ file ... ]\n"
                "where 'file' is the output file to redirect output to.\n"
                "\n"
                "The program just uses a different string to output to both,\n"
                "standard output and the indicated filename, to thow the workings\n"
                "of output redirection.  I have wrapped the call to fputs(3) in a\n"
                "function in order to show that we need to call fflush to flush\n"
                "the buffers out to the file descriptor, before returning, or the\n"
                "wrong things will be written to the output descriptors. (this is\n"
                "specially true for files, that do full buffering)\n",
                progname);
}

int main(int argc, char **argv)
{
        int i;
        if (argc == 1) {
                do_usage(argv[0]);
        } else for (i = 1; i < argc; i++) {
                char buffer[1024];

                /* text we are going to write multiple times */
                snprintf(buffer, sizeof buffer,
                        "%d: file = %s\n", i, argv[i]);

                /* we call our output function first, to see output going
                 * to it's normal output descriptor */
                output_function(buffer);

                /* create a file we are going to write message into */
                int fd_to_redirect_output = open(
                                argv[i],
                                O_WRONLY | O_CREAT | O_TRUNC,
                                0666);

                /* call our function with the message and the redirection
                 * done before the call and restored after */
                int res = redirect_and_call(output_function,
                                        fd_to_redirect_output, /* descriptor to write
                                                                                        * output to */
                                        1, /* stdout is file descriptor 1 */
                                        buffer); /* write the string to the file. */

                /* write another message to see redirection was restored
                 * properly */
                output_function("done\n");
        }
        exit(EXIT_SUCCESS);
} /* main */

Файл redirect.h:

/* redirect.h --- definitions for the module redirect.c
 * Author: Luis Colorado <luiscoloradourcola@gmail.com>
 * Date: Sat Oct 12 13:24:54 EEST 2019
 * Copyright: (C) 2019 LUIS COLORADO.  All rights reserved.
 * License: BSD.
 */
#ifndef _REDIRECT_H
#define _REDIRECT_H

/* our redirect_and_call will return int, as fputs(3) returns
 * int, and will need an extra parameter, so it can call
 * fputs, and pass it the proper parameter. */
typedef ssize_t RET_TYPE;

RET_TYPE redirect_and_call(
        RET_TYPE (*funct_to_be_called)(const char*s), 
        int fd_to_write_to,
        int fd_funct_uses_as_output,
        const char *parameter_for_the_call);

#endif /* _REDIRECT_H */

Файл redirect.c:

/* redirect.c --- function to create a temporary redirect of a
 * file descriptor in order to execute a function (passed as a
 * parameter) with that descriptor redirected so it will output
 * to the redirected desriptor, instead of to the one it used to.
 * We'll use puts(3) as an example of a function that is
 * hard wired to print into stdout (fd = 1) and to make it to
 * write to a named file instead.
 *
 * Author: Luis Colorado <luiscoloradourcola@gmail.com>
 * Date: Sat Oct 12 13:04:45 EEST 2019
 * Copyright: (C) 2019 LUIS COLORADO.  All rights reserved.
 * License: BSD.
 */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "redirect.h"

#define F(_f) __FILE__":%d:%s: " _f, __LINE__, __func__

#define CHECK_ERR(var, status, name) do {     \
                if ((var) < 0) {                      \
                        fprintf(stderr,                   \
                                F("%s: %s\n"),                \
                                name, strerror(errno));       \
                        exit(status);                     \
                }                                     \
        } while(0)

RET_TYPE redirect_and_call(
        RET_TYPE (*funct_to_be_called)(const char*s), 
        int fd_to_write_to,
        int fd_funct_uses_as_output,
        const char *parameter_for_the_call)
{
   /* save a copy descriptor, so we can overwrite the fd the one
     * function uses to write */
    int saved_old_fd = dup(
                        fd_funct_uses_as_output);
    CHECK_ERR(saved_old_fd, 1, "dup");

    /* now, we change the descriptor the function uses to write
         * by the one we want it to be used for output */
    int res = dup2(
                        fd_to_write_to,
                        fd_funct_uses_as_output);
    CHECK_ERR(res, 2, "dup2");

    /* now, we call the function */
    RET_TYPE res2 = funct_to_be_called(parameter_for_the_call);

    /* now restore descriptors to what they where. For this, we
         * use the saved file descriptor */
    res = dup2(saved_old_fd, fd_funct_uses_as_output);

    /* close the saved descriptor, as we dont need it anymore */
    close(saved_old_fd);

    /* and finally, return the funct_to_be_called return value */
    return res2;
} /* redirect_and_call */

Файл Makefile:

# Makefile --- Makefile for the test program.
# Author: Luis Colorado <luiscoloradourcola@gmail.com>
# Date: Sat Oct 12 13:43:03 EEST 2019
# Copyright: (C) 2019 LUIS COLORADO.  All rights reserved.
# License: BSD.

targets = redirect
toclean = $(targets)
RM              ?= rm -f

all: $(targets)
clean:
        $(RM) $(toclean)

redirect_objs = main.o redirect.o
toclean += $(redirect_objs)

redirect: $(redirect_deps) $(redirect_objs)
        $(CC) $(LDFLAGS) -o $@ $($@_objs) $($@_ldflags) $($@_ldlibs)

0 голосов
/ 18 октября 2019

Я наконец-то получил правильный ответ, вот как временно перенаправить стандартный вывод в файл с именем «file», затем выполнить функцию f, а затем восстановить исходный стандартный вывод:


void redirect_stdout (void (*f)(void), char *file)
{
 int fd = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0644);
 int savefd = dup(1);
 dup2(fd, 1);
 f();
 dup2(savefd, 1);
 close(fd);
 close(savefd);
}

0 голосов
/ 04 октября 2019

Ваш код близок, вот полный рабочий пример:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

void some_func() {
    printf("This gets written to file.\n");
    fflush(stdout);
}

void redirect_stdout(void (*some_func)(void), char *file) {
    printf("Preparing to redirect to file:\n");
    fflush(stdout);
    //sys has three streams (0)stdin (1)stdout (2)stderr
    //use dup to store the stdout for restoration later:
    int saved_stdout = dup(1);
    int fw = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0644);
    //checks for open failure and cleans up resources / prints errors if so
    if (fw < 0) {
        printf("Failed to open %s.\n", file);
        perror("Error: ");
        if (close(saved_stdout) != 0) {
            perror("Error: ");
        }
        return;
    }
    dup2(fw,1);    //use 1 as it is the integer assigned to stdout
    some_func();
    if (close(fw) != 0) {               //checks for bad file descriptor
        perror("Error: ");
    }
    dup2(saved_stdout, 1);
    if (close(saved_stdout) != 0) {     //checks for bad file descriptor
        perror("Error: ");
    }
}  

int main(void) {
    char filename[] = "stdout_content.txt";
    redirect_stdout(some_func, filename);
    printf("Stdout now back to terminal output.\n");
    return 0;
}

См. Справочные страницы Linux для dup, dup2, open, close и т. Д. Примечание. Сначала я попробовал это с freopen, но не смогчтобы выяснить, каким образом можно восстановить стандартный вывод на терминал, и, кажется, существуют другие методы, использующие fork () и т. д., которые более сложны. http://www.microhowto.info/howto/capture_the_output_of_a_child_process_in_c.html

...