Я пытаюсь воспроизвести операцию bash для канала, например:
echo test | grep test
Этот пример работает, потому что объем записи данных на стандартный вывод меньше размера буфера канала, но если я попытаюсь это сделать, то объем данных будет урезан из последнего символа, равного размеру буфера канала:
cat large_file | grep last line
В C приведен минимальный пример моего кода (надеюсь, это может помочь):
int main ()
{
int pipefd[2];
pid_t cpid;
if (pipe(pipefd) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
while (1) // While of my own bash
{
get_commandline(); // Retrieve and format of user's input
cpid = fork();
if (cpid == 0)
{
if (command_is_piped) // if the the actual command and next one are connected by a pipe
dup2(pipefd[1], STDOUT_FILENO);
else if (previous_command_is_piped) // if the the actual command and previous one are connected by a pipe
dup2(pipefd[0], STDOUT_FILENO);
exec_command(); // Execute the user's command
}
else
{
/* Wait for child process and manage other informations */
}
if (!(command_is_piped)) // if the the actual command and next one are not connected by a pipe
{
close(pipefd[0]);
close(pipefd[1]);
if (pipe(pipefd) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
}
}
}
Знаете ли вы механизм, который позволил бы мне получить все данные из стандартного вывода?