Package: mktorrent Version: 1.0-4 Tags: patch Another common feature that mktorrent(1) is lacking is the support for writing the resulting Bencode'd data to stdout. The issue is three-fold:
• ‘-’ has no special meaning for ‘--output=’, and thus using ‘--output=-’ results in a file named ‘-’ being created in the current working directory; (unless such file exists, that is); • alternatively, ‘--output=/dev/stdout’ (or similar) fails because of the unconditional use of O_CREAT | O_EXCL (as I've noted in another report); • finally, most of the diagnostics (excluding mainly actual errors) is directed to stdout, although it (arguably) makes sense to send them to stderr instead. Please thus consider the two patches MIME'd. (Note, however, that the resulting code may benefit from an additional isatty () check.) For debian/changelog, I'd suggest the following bit: * Allow output to stdout with "--output=-" (Closes: #XXX). Note that all the diagnostics (including --verbose) now goes to stderr unconditionally. TIA. PS. Also to note is that as long as the use of mktorrent(1) from Shell code is considered, writing copyright information upon every invocation doesn't seem like a particularly clever idea, either. Perhaps it should be dropped altogether. Or, there could be --version… -- FSF associate member #7257
Author: Ivan Shmakov <oneing...@gmail.com> Description: Direct all diagnostics to stderr (where they belong) Index: mktorrent-1.0/hash.c =================================================================== --- mktorrent-1.0.orig/hash.c 2009-08-25 18:53:22.000000000 +0000 +++ mktorrent-1.0/hash.c 2013-01-31 21:21:39.024885149 +0000 @@ -91,8 +91,7 @@ f->path, strerror(errno)); exit(EXIT_FAILURE); } - printf("Hashing %s.\n", f->path); - fflush(stdout); + fprintf(stderr, "Hashing %s.\n", f->path); /* fill the read buffer with the contents of the file and append the SHA1 hash of it to the hash string when the buffer is full. Index: mktorrent-1.0/hash_pthreads.c =================================================================== --- mktorrent-1.0.orig/hash_pthreads.c 2009-08-25 18:53:22.000000000 +0000 +++ mktorrent-1.0/hash_pthreads.c 2013-01-31 21:22:17.359138783 +0000 @@ -177,8 +177,8 @@ while (1) { /* print progress and flush the buffer immediately */ - printf("\rHashed %u of %u pieces.", q->pieces_hashed, q->pieces); - fflush(stdout); + fprintf(stderr, "\rHashed %u of %u pieces.", + q->pieces_hashed, q->pieces); /* now sleep for PROGRESS_PERIOD microseconds */ usleep(PROGRESS_PERIOD); } @@ -369,7 +369,8 @@ free_buffers(&q); /* ok, let the user know we're done too */ - printf("\rHashed %u of %u pieces.\n", q.pieces_hashed, q.pieces); + fprintf(stderr, "\rHashed %u of %u pieces.\n", + q.pieces_hashed, q.pieces); return hash_string; } Index: mktorrent-1.0/init.c =================================================================== --- mktorrent-1.0.orig/init.c 2013-01-31 21:21:38.872892073 +0000 +++ mktorrent-1.0/init.c 2013-01-31 21:23:03.789023590 +0000 @@ -237,7 +237,7 @@ } if (m->verbose) - printf("Adding %s\n", path); + fprintf(stderr, "Adding %s\n", path); /* count the total size of the files */ m->size += sb->st_size; @@ -327,7 +327,7 @@ { if (list == 0) { /* an empty list passed; return early */ - puts(" (no announce URI's given)"); + fputs(" (no announce URI's given)", stderr); return; } @@ -336,9 +336,9 @@ for (n = 1; list; list = list->next, n++) { slist_t *l = list->l; - printf(" %u : %s\n", n, l->s); + fprintf(stderr, " %u : %s\n", n, l->s); for (l = l->next; l; l = l->next) - printf(" %s\n", l->s); + fprintf(stderr, " %s\n", l->s); } } @@ -347,16 +347,16 @@ */ static void print_web_seed_list(slist_t *list) { - printf(" Web Seed URL: "); + fputs(" Web Seed URL: ", stderr); if (list == NULL) { - printf("none\n"); + fputs("none\n", stderr); return; } - printf("%s\n", list->s); + fprintf(stderr, "%s\n", list->s); for (list = list->next; list; list = list->next) - printf(" %s\n", list->s); + fprintf(stderr, " %s\n", list->s); } /* @@ -364,30 +364,32 @@ */ static void dump_options(metafile_t *m) { - printf("Options:\n" - " Announce URLs:\n"); + fprintf(stderr, + "Options:\n" + " Announce URLs:\n"); print_announce_list(m->announce_list); - printf(" Torrent name: %s\n" - " Metafile: %s\n" - " Piece length: %u\n" - " Be verbose: yes\n", - m->torrent_name, m->metainfo_file_path, m->piece_length); + fprintf(stderr, + " Torrent name: %s\n" + " Metafile: %s\n" + " Piece length: %u\n" + " Be verbose: yes\n", + m->torrent_name, m->metainfo_file_path, m->piece_length); - printf(" Write date: "); + fprintf(stderr, " Write date: "); if (m->no_creation_date) - printf("no\n"); + fprintf(stderr, "no\n"); else - printf("yes\n"); + fprintf(stderr, "yes\n"); print_web_seed_list(m->web_seed_list); - printf(" Comment: "); + fprintf(stderr, " Comment: "); if (m->comment == NULL) - printf("none\n\n"); + fprintf(stderr, "none\n\n"); else - printf("\"%s\"\n\n", m->comment); + fprintf("\"%s\"\n\n", m->comment); } /* @@ -573,7 +575,8 @@ /* now print the size and piece count if we should be verbose */ if (m->verbose) - printf("\n%" PRIoff " bytes in all.\n" + fprintf(stderr, + "\n%" PRIoff " bytes in all.\n" "That's %u pieces of %u bytes each.\n\n", m->size, m->pieces, m->piece_length); } Index: mktorrent-1.0/main.c =================================================================== --- mktorrent-1.0.orig/main.c 2013-01-31 21:19:35.646505664 +0000 +++ mktorrent-1.0/main.c 2013-01-31 21:23:03.789023590 +0000 @@ -157,7 +157,8 @@ }; /* print who we are */ - printf("mktorrent " VERSION " (c) 2007, 2009 Emil Renner Berthing\n\n"); + fputs("mktorrent " VERSION " (c) 2007, 2009 Emil Renner Berthing\n\n", + stderr); /* process options */ init(&m, argc, argv); Index: mktorrent-1.0/output.c =================================================================== --- mktorrent-1.0.orig/output.c 2013-01-31 21:21:38.872892073 +0000 +++ mktorrent-1.0/output.c 2013-01-31 21:21:39.028884966 +0000 @@ -121,8 +121,7 @@ EXPORT void write_metainfo(FILE *f, metafile_t *m, unsigned char *hash_string) { /* let the user know we've started writing the metainfo file */ - printf("Writing metainfo file... "); - fflush(stdout); + fputs("Writing metainfo file... ", stderr); /* every metainfo file is one big dictonary and the first entry is the announce URL */ @@ -187,6 +186,5 @@ fprintf(f, "e"); /* let the user know we're done already */ - printf("done.\n"); - fflush(stdout); + fputs("done.\n", stderr); }
Author: Ivan Shmakov <oneing...@gmail.com> Description: Allow output to stdout via "--output=-". Index: mktorrent-1.0/main.c =================================================================== --- mktorrent-1.0.orig/main.c 2013-01-31 21:04:52.290797059 +0000 +++ mktorrent-1.0/main.c 2013-01-31 21:05:28.869127921 +0000 @@ -92,6 +92,11 @@ */ static FILE *open_file(const char *path) { + if (strcmp ("-", path) == 0) { + /* just return stdout for --output=- */ + return stdout; + } + int fd; /* file descriptor */ FILE *f; /* file stream */ @@ -120,6 +125,11 @@ */ static void close_file(FILE *f) { + if (f == stdout) { + /* do not attempt to fclose (stdout) */ + return; + } + /* close the metainfo file */ if (fclose(f)) { fprintf(stderr, "Error closing stream: %s\n", Index: mktorrent-1.0/init.c =================================================================== --- mktorrent-1.0.orig/init.c 2013-01-31 21:05:28.829129746 +0000 +++ mktorrent-1.0/init.c 2013-01-31 21:05:28.869127921 +0000 @@ -74,6 +74,11 @@ return that */ if (m->metainfo_file_path && *m->metainfo_file_path == DIRSEP_CHAR) return; + /* return early if path is "-" (meaning stdout), either */ + if (m->metainfo_file_path + && strcmp ("-", m->metainfo_file_path) == 0) { + return; + } /* first get the current working directory using getcwd is a bit of a PITA */