static void showUsage (void) { tr_getopt_usage (MY_NAME, getUsage (), options); exit (0); }
int main (int argc, char * argv[]) { char * out2 = NULL; tr_metainfo_builder * b = NULL; tr_logSetLevel (TR_LOG_ERROR); if (parseCommandLine (argc, (const char**)argv)) return EXIT_FAILURE; if (showVersion) { fprintf (stderr, MY_NAME" "LONG_VERSION_STRING"\n"); return EXIT_SUCCESS; } if (!infile) { fprintf (stderr, "ERROR: No input file or directory specified.\n"); tr_getopt_usage (MY_NAME, getUsage (), options); fprintf (stderr, "\n"); return EXIT_FAILURE; } if (outfile == NULL) { char * base = tr_basename (infile); char * end = tr_strdup_printf ("%s.torrent", base); char * cwd = tr_getcwd (); outfile = out2 = tr_buildPath (cwd, end, NULL); tr_free (cwd); tr_free (end); tr_free (base); } if (!trackerCount) { if (isPrivate) { fprintf (stderr, "ERROR: no trackers specified for a private torrent\n"); return EXIT_FAILURE; } else { printf ("WARNING: no trackers specified\n"); } } printf ("Creating torrent \"%s\" ...", outfile); fflush (stdout); b = tr_metaInfoBuilderCreate (infile); if (piecesize_kib != 0) tr_metaInfoBuilderSetPieceSize (b, piecesize_kib * KiB); tr_makeMetaInfo (b, outfile, trackers, trackerCount, comment, isPrivate); while (!b->isDone) { tr_wait_msec (500); putc ('.', stdout); fflush (stdout); } putc (' ', stdout); switch (b->result) { case TR_MAKEMETA_OK: printf ("done!"); break; case TR_MAKEMETA_URL: printf ("bad announce URL: \"%s\"", b->errfile); break; case TR_MAKEMETA_IO_READ: printf ("error reading \"%s\": %s", b->errfile, tr_strerror (b->my_errno)); break; case TR_MAKEMETA_IO_WRITE: printf ("error writing \"%s\": %s", b->errfile, tr_strerror (b->my_errno)); break; case TR_MAKEMETA_CANCELLED: printf ("cancelled"); break; } putc ('\n', stdout); tr_metaInfoBuilderFree (b); tr_free (out2); return EXIT_SUCCESS; }
int tr_getopt( const char * usage, int argc, const char ** argv, const tr_option * opts, const char ** setme_optarg ) { int i; const char * arg = NULL; const tr_option * o = NULL; *setme_optarg = NULL; /* handle the builtin 'help' option */ for( i = 1; i < argc; ++i ) { if( !strcmp( argv[i], "-h" ) || !strcmp( argv[i], "--help" ) ) { tr_getopt_usage( argv[0], usage, opts ); exit( 0 ); } } /* out of options? */ if( argc == 1 || tr_optind >= argc ) return TR_OPT_DONE; o = findOption( opts, argv[tr_optind], &arg ); if( !o ) { /* let the user know we got an unknown option... */ *setme_optarg = argv[tr_optind++]; return TR_OPT_UNK; } if( !o->has_arg ) { /* no argument needed for this option, so we're done */ if( arg ) return TR_OPT_ERR; *setme_optarg = NULL; ++tr_optind; return o->val; } /* option needed an argument, and it was embedded in this string */ if( arg ) { *setme_optarg = arg; ++tr_optind; return o->val; } /* throw an error if the option needed an argument but didn't get one */ if( ++tr_optind >= argc ) return TR_OPT_ERR; if( findOption( opts, argv[tr_optind], NULL ) ) return TR_OPT_ERR; *setme_optarg = argv[tr_optind++]; return o->val; }
int main (int argc, char * argv[]) { int i; int changedCount = 0; files = tr_new0 (const char*, argc); tr_setMessageLevel (TR_MSG_ERR); if (parseCommandLine (argc, (const char**)argv)) return EXIT_FAILURE; if (showVersion) { fprintf (stderr, MY_NAME" "LONG_VERSION_STRING"\n"); return EXIT_SUCCESS; } if (fileCount < 1) { fprintf (stderr, "ERROR: No torrent files specified.\n"); tr_getopt_usage (MY_NAME, getUsage (), options); fprintf (stderr, "\n"); return EXIT_FAILURE; } if (!add && !deleteme && !replace[0]) { fprintf (stderr, "ERROR: Must specify -a, -d or -r\n"); tr_getopt_usage (MY_NAME, getUsage (), options); fprintf (stderr, "\n"); return EXIT_FAILURE; } for (i=0; i<fileCount; ++i) { tr_benc top; bool changed = false; const char * filename = files[i]; printf ("%s\n", filename); if (tr_bencLoadFile (&top, TR_FMT_BENC, filename)) { printf ("\tError reading file\n"); continue; } if (deleteme != NULL) changed |= removeURL (&top, deleteme); if (add != NULL) changed = addURL (&top, add); if (replace[0] && replace[1]) changed |= replaceURL (&top, replace[0], replace[1]); if (changed) { ++changedCount; tr_bencToFile (&top, TR_FMT_BENC, filename); } tr_bencFree (&top); } printf ("Changed %d files\n", changedCount); tr_free (files); return EXIT_SUCCESS; }
int tr_main (int argc, char * argv[]) { int err; tr_info inf; tr_ctor * ctor; tr_logSetLevel (TR_LOG_ERROR); tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); tr_formatter_size_init (DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR); if (parseCommandLine (argc, (const char* const *)argv)) return EXIT_FAILURE; if (showVersion) { fprintf (stderr, MY_NAME" "LONG_VERSION_STRING"\n"); return EXIT_SUCCESS; } /* make sure the user specified a filename */ if (!filename) { fprintf (stderr, "ERROR: No .torrent file specified.\n"); tr_getopt_usage (MY_NAME, getUsage (), options); fprintf (stderr, "\n"); return EXIT_FAILURE; } /* try to parse the .torrent file */ ctor = tr_ctorNew (NULL); tr_ctorSetMetainfoFromFile (ctor, filename); err = tr_torrentParse (ctor, &inf); tr_ctorFree (ctor); if (err) { fprintf (stderr, "Error parsing .torrent file \"%s\"\n", filename); return EXIT_FAILURE; } if (magnetFlag) { doShowMagnet (&inf); } else { printf ("Name: %s\n", inf.name); printf ("File: %s\n", filename); printf ("\n"); fflush (stdout); if (scrapeFlag) doScrape (&inf); else showInfo (&inf); } /* cleanup */ putc ('\n', stdout); tr_metainfoFree (&inf); return EXIT_SUCCESS; }
int main (int argc, char * argv[]) { int i; int changedCount = 0; #ifdef _WIN32 tr_win32_make_args_utf8 (&argc, &argv); #endif files = tr_new0 (const char*, argc); tr_logSetLevel (TR_LOG_ERROR); if (parseCommandLine (argc, (const char**)argv)) return EXIT_FAILURE; if (showVersion) { fprintf (stderr, MY_NAME" "LONG_VERSION_STRING"\n"); return EXIT_SUCCESS; } if (fileCount < 1) { fprintf (stderr, "ERROR: No torrent files specified.\n"); tr_getopt_usage (MY_NAME, getUsage (), options); fprintf (stderr, "\n"); return EXIT_FAILURE; } if (!add && !deleteme && !replace[0]) { fprintf (stderr, "ERROR: Must specify -a, -d or -r\n"); tr_getopt_usage (MY_NAME, getUsage (), options); fprintf (stderr, "\n"); return EXIT_FAILURE; } for (i=0; i<fileCount; ++i) { tr_variant top; bool changed = false; const char * filename = files[i]; tr_error * error = NULL; printf ("%s\n", filename); if (!tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename, &error)) { printf ("\tError reading file: %s\n", error->message); tr_error_free (error); continue; } if (deleteme != NULL) changed |= removeURL (&top, deleteme); if (add != NULL) changed = addURL (&top, add); if (replace[0] && replace[1]) changed |= replaceURL (&top, replace[0], replace[1]); if (changed) { ++changedCount; tr_variantToFile (&top, TR_VARIANT_FMT_BENC, filename); } tr_variantFree (&top); } printf ("Changed %d files\n", changedCount); tr_free (files); return EXIT_SUCCESS; }