From 730f81ebe905d6954eaff99589d1f7aed8d0e9f7 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 25 Mar 2010 00:50:23 +0000 Subject: [PATCH] never write binary data to stdout on windows --- examples/make_torrent.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/examples/make_torrent.cpp b/examples/make_torrent.cpp index 1744e609e..e957912b2 100644 --- a/examples/make_torrent.cpp +++ b/examples/make_torrent.cpp @@ -74,6 +74,10 @@ void print_usage() " than bytes will be piece-aligned\n" "-s bytes specifies a piece size for the torrent\n" " This has to be a multiple of 16 kiB\n" + "-o file specifies the output filename of the torrent file\n" + " If this is not specified, the torrent file is\n" + " printed to the standard out, except on windows\n" + " where the filename defaults to a.torrent\n" , stderr); } @@ -99,6 +103,13 @@ int main(int argc, char* argv[]) int piece_size = 0; int flags = 0; + std::string outfile; +#ifdef TORRENT_WINDOWS + // don't ever write binary data to the console on windows + // it will just be interpreted as text and corrupted + outfile = "a.torrent"; +#endif + for (int i = 2; i < argc; ++i) { if (argv[i][0] != '-') @@ -129,6 +140,10 @@ int main(int argc, char* argv[]) case 'm': flags |= create_torrent::merkle; break; + case 'o': + ++i; + outfile = argv[i]; + break; default: print_usage(); return 1; @@ -170,7 +185,13 @@ int main(int argc, char* argv[]) // create the torrent and print it to stdout std::vector torrent; bencode(back_inserter(torrent), t.generate()); - fwrite(&torrent[0], 1, torrent.size(), stdout); + FILE* output = stdout; + if (!outfile.empty()) + output = fopen(outfile.c_str(), "wb+"); + fwrite(&torrent[0], 1, torrent.size(), output); + + if (output != stdout) + fclose(output); #ifndef BOOST_NO_EXCEPTIONS }