From: John L. <mov...@us...> - 2001-09-27 17:10:51
|
Update of /cvsroot/oprofile/oprofile/gui In directory usw-pr-cvs1:/tmp/cvs-serv20447/gui Modified Files: oprof_start_util.cpp Log Message: avoid temp files Index: oprof_start_util.cpp =================================================================== RCS file: /cvsroot/oprofile/oprofile/gui/oprof_start_util.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- oprof_start_util.cpp 2001/09/27 16:30:41 1.5 +++ oprof_start_util.cpp 2001/09/27 17:10:46 1.6 @@ -57,43 +57,6 @@ return user_dir; } -/** - * fill_from_fd - output fd to a stream - * @stream: the stream to output to - * @fd: the fd to read from - * - * Read from @fd until a read would block and write - * the output to @stream. - */ -static void fill_from_fd(std::ostream & stream, fd_t fd) -{ - // FIXME: there might be a better way than this; suggestions welcome - fd_t fd_out; - char templ[] = "/tmp/op_XXXXXX"; - char buf[4096]; - - if ((fd_out = mkstemp(templ) == -1)) { - stream.set(ios::failbit); - return; - } - - ssize_t count; - fcntl(fd, F_SETFL, O_NONBLOCK); - while ((count = read(fd, buf, 4096)) != -1) - write(fd_out, buf, count); - - close(fd_out); - - std::ifstream in(templ); - std::string str; - - while (getline(in, str)) - stream << str; - - unlink(templ); -} - - static int exec_command(std::string const & cmd, std::ostream & out, std::ostream & err, std::vector<std::string> args) { @@ -139,8 +102,19 @@ int ret; waitpid(pid, &ret, 0); - fill_from_fd(out, pstdout[0]); - fill_from_fd(err, pstderr[0]); + /// don't hang when data ends + fcntl(pstdout[0], F_SETFL, O_NONBLOCK); + fcntl(pstderr[0], F_SETFL, O_NONBLOCK); + + // attach is non-standard, so we need GNU C++ + std::ifstream outf(pstdout[0]); + std::ifstream errf(pstderr[0]); + std::string str; + + while (std::getline(outf, str)) + out << str; + while (std::getline(errf, str)) + err << str; return WEXITSTATUS(ret); } |