Enviado em 25/11/2018 - 10:37h
//------------------------------------------------------------------- // // ARQUIVO: // redirect.c // // COMPILE: // redirect.c -o redirect -Wall // //------------------------------------------------------------------- // #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <io.h> #include <fcntl.h> //#include <windows.h> #define STRING_LEN 50000 char string [STRING_LEN+1]; int out_pipe[2]; int saved_stdout; int main (void) { saved_stdout = dup (STDOUT_FILENO); // salva stdout para no final restaurar #ifdef WIN32 if (_pipe (out_pipe, sizeof(string), O_BINARY) != 0 ) { #endif #ifdef __linux__ if (pipe(out_pipe) != 0 ) { #endif exit(1); } dup2 (out_pipe[1], STDOUT_FILENO); // REdireciona stdout para o pipe close (out_pipe[1]); // so para testar ... // system ("gcc -v"); //------------------- LINHA 42 --------------------- system ("hello"); // meu programa teste printf ("HELLO WORLD\n"); fflush (stdout); read (out_pipe[0], string, STRING_LEN); // ler o pipe ... "string" dup2 (saved_stdout, STDOUT_FILENO); // restaura stdout para teste printf("MY_STRING(\n%s)\n", string); return 0; }
#include <stdio.h> int main (void) { printf ("Hello World -- My program\n"); return 0; }
Enviado em 25/11/2018 - 12:09h
Seria melhor se em lugar de dizer simplesmente que não funciona, você dissesse também o que realmente acontece.int pipe_fds[2]; if(pipe(pipe_fds)==-1){ perror("pipe"); exit(1); } pid_t child=fork(); if(child==-1){ perror("fork"); exit(1); } if(child==0){ /* Processo filho que envia dados para o pai; logo fechar o descritor de leitura do pipe. */ /* Se ele tiver de ler do pai, você deve inverter os descritores e usar STDIN_FILENO. */ /* Se a comunicação foi bidirecional, serão necessários dois pipes! */ close(pipe_fds[0]); if(dup2(pipe_fds[1], STDOUT_FILENO)==-1){ perror("dup2 in child"); _exit(1); } close(pipe_fds[1]); execlp("gcc", "gcc", "-v", NULL); perror("execlp in child"); _exit(1); } /* Processo pai. */ /* Fecha o descritor de escrita, para que só o filho escreva nele. */ /* Se o filho tiver de ler dados enviados pelo pai, inverta os descritores. */ close(pipe_fds[1]); while(has_data(pipe_fds[0])){ read_and_use_data(pipe_fds[0]); } close(pipe_fds[0]); int child_status; waitpid(child, &child_status, 0);
FILE *from_child=popen("gcc -v", "r"); // Cuida internamente de fazer pipe(), dup2() e close()s apropriados. if(!from_child){ perror("popen"); exit(1); } while(!feof(from_child)){ read_and_use_data(from_child); } fclose(from_child); // Cuida de fazer o waitpid() internamente, mas não entrega o status de saída.
Enviado em 25/11/2018 - 12:22h
//------------------------------------------------------------------- // // ARQUIVO: // redirect.c // // COMPILE: // redirect.c -o redirect -Wall // //------------------------------------------------------------------- // #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <io.h> #include <fcntl.h> //#include <windows.h> #define STRING_LEN 50000 char string [STRING_LEN+1]; int out_pipe[2]; int saved_stdout, i; int main (void) { saved_stdout = dup (STDOUT_FILENO); // salva stdout para no final restaurar #ifdef WIN32 if (_pipe (out_pipe, sizeof(string), O_BINARY) != 0 ) { #endif #ifdef __linux__ if (pipe(out_pipe) != 0 ) { #endif exit(1); } dup2 (out_pipe[1], STDOUT_FILENO); // REdireciona stdout para o pipe close (out_pipe[1]); // so para testar ... system ("gcc -v 2>&1"); //------------------- LINHA 42 --------------------- printf ("HELLO WORLD\n"); fflush (stdout); read (out_pipe[0], string, STRING_LEN); // ler o pipe ... buffer dup2 (saved_stdout, STDOUT_FILENO); // restaura stdout para teste printf("MY_STRING(\n%s)\n", string); return 0; }
Enviado em 25/11/2018 - 18:18h
//------------------------------------------------------------------- // // ARQUIVO: // redirect.c // // COMPILE: // redirect.c -o redirect -Wall // //------------------------------------------------------------------- // #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <io.h> #include <fcntl.h> //#include <windows.h> #define STRING_LEN 5000 char string [STRING_LEN+1]; char buf [1024]; int out_pipe[2]; int saved_stdout, i; FILE *fp; int main (void) { saved_stdout = dup (STDOUT_FILENO); // salva stdout para no final restaurar #ifdef WIN32 if (_pipe (out_pipe, sizeof(string), O_BINARY) != 0 ) { #endif #ifdef __linux__ if (pipe(out_pipe) != 0 ) { #endif exit(1); } dup2 (out_pipe[1], STDOUT_FILENO); // REdireciona stdout para o pipe close (out_pipe[1]); // so para testar ... if ((fp = popen ("gcc -v 2>&1", "r")) != NULL) { while (fgets(buf, sizeof(buf), fp) != NULL) { // //------------------------------------------------------- // // aqui essa linha nem me interesaa que seja exibida ... so estou exibindo para demonstrar ... // //------------------------------------------------------- // printf ("%s", buf); // aqui poderia ja armazenar ( buf ) ... } pclose(fp); printf ("popen aberto\n"); } printf ("HELLO WORLD\n"); //--------------------------------------------------------------- // pega a quantidade de letras adicionada em: one fflush (stdout); int one = read (out_pipe[0], string, STRING_LEN); // ler o pipe ... buffer //--------------------------------------------------------------- // Mais "textos" adicionado ... ADICIONA NO INICIO DO STRING ... printf ("segunda linha\n"); printf ("MAIS UMA LINHA ... testando\n\n"); //--------------------------------------------------------------- // pega a SEGUNDA quantidade de letras adicionada em: tow fflush (stdout); // 43 letras: int tow = read (out_pipe[0], string, STRING_LEN); // ler o pipe ... buffer //--------------------------------------------------------------- dup2 (saved_stdout, STDOUT_FILENO); // restaura stdout para teste //--------------------------------------------------------------- // AGORA EXIBE O RESULTADO ... so para testes ... //--------------------------------------------------------------- printf("MY_STRING(\n%s)\n", string); printf ("\none: %d, tow: %d\n", one, tow); return 0; }
if ((fp = popen ("gcc -v 2>&1", "r")) != NULL) { }
Enviado em 26/11/2018 - 00:55h
Está redirecionando a saida de erros 'STDEER' para a saida padrão 'STDOUT'.Slackware user since ~2008 Meu canal no youtube: https://www.youtube.com/SlackJeff Meu Site: https://www.slackjeff.com.br/ Meus Programas estão aqui: https://notabug.org/jeffersonrocha
Programa IRPF - Guia de Instalação e Resolução de alguns Problemas
Criando uma Infraestrutura para uma micro Empresa
Criar entrada (menuentry) ISO no Grub
Como gerar qualquer emoji ou símbolo unicode a partir do seu teclado
Instalando o Pi-Hole versão v5.18.4 depois do lançamento da versão v6.0
Instalar o VIM 9.1 no Debian 12
Como saber o range de um IP público?
Muitas dificuldades ao instalar distro Linux em Notebook Sony Vaio PCG-6131L (VPCEA24FM)
Discussão sobre monitoramento da integridade de arquivos (6)
Problema no boot do Linux Mint Cinnamon 22 (4)
Atualização do Google Chrome do Ubuntu [RESOLVIDO] (2)
Instalei Windows 11 e não alterou o Grub do Debian (1)
Jogos baixados na Central de Aplicativos mas que não abrem (0)