diff options
Diffstat (limited to 'tools/idevicename.c')
-rw-r--r-- | tools/idevicename.c | 86 |
1 files changed, 55 insertions, 31 deletions
diff --git a/tools/idevicename.c b/tools/idevicename.c index ef226f7..69b76f6 100644 --- a/tools/idevicename.c +++ b/tools/idevicename.c @@ -23,60 +23,84 @@ #include <config.h> #endif +#define TOOL_NAME "idevicename" + #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <getopt.h> +#ifndef WIN32 +#include <signal.h> +#endif #include <libimobiledevice/libimobiledevice.h> #include <libimobiledevice/lockdown.h> -static void print_usage(void) +static void print_usage(int argc, char** argv, int is_error) { - printf("Usage: idevicename [OPTIONS] [NAME]\n"); - printf("Display the device name or set it to NAME if specified.\n"); - printf("\n"); - printf(" -d, --debug\t\tenable communication debugging\n"); - printf(" -u, --udid UDID\ttarget specific device by UDID\n"); - printf(" -h, --help\t\tprint usage information\n"); - printf("\n"); - printf("Homepage: <" PACKAGE_URL ">\n"); + char *name = strrchr(argv[0], '/'); + fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] [NAME]\n", (name ? name + 1: argv[0])); + fprintf(is_error ? stderr : stdout, + "\n" + "Display the device name or set it to NAME if specified.\n" + "\n" + "OPTIONS:\n" + " -u, --udid UDID target specific device by UDID\n" + " -n, --network connect to network device\n" + " -d, --debug enable communication debugging\n" + " -h, --help print usage information\n" + " -v, --version print version information\n" + "\n" + "Homepage: <" PACKAGE_URL ">\n" + "Bug Reports: <" PACKAGE_BUGREPORT ">\n" + ); } int main(int argc, char** argv) { - int res = -1; - char* udid = NULL; - int c = 0; - int optidx = 0; const struct option longopts[] = { - { "udid", required_argument, NULL, 'u' }, - { "help", no_argument, NULL, 'h' }, + { "udid", required_argument, NULL, 'u' }, + { "network", no_argument, NULL, 'n' }, + { "debug", no_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'v' }, { NULL, 0, NULL, 0} }; + int res = -1; + const char* udid = NULL; + int use_network = 0; + +#ifndef WIN32 + signal(SIGPIPE, SIG_IGN); +#endif - while ((c = getopt_long(argc, argv, "du:h", longopts, &optidx)) != -1) { + while ((c = getopt_long(argc, argv, "du:hnv", longopts, NULL)) != -1) { switch (c) { case 'u': if (!*optarg) { fprintf(stderr, "ERROR: UDID must not be empty!\n"); - print_usage(); + print_usage(argc, argv, 1); exit(2); } - free(udid); - udid = strdup(optarg); + udid = optarg; + break; + case 'n': + use_network = 1; break; case 'h': - print_usage(); + print_usage(argc, argv, 0); return 0; case 'd': idevice_set_debug_level(1); break; + case 'v': + printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); + return 0; default: - print_usage(); - return -1; + print_usage(argc, argv, 1); + return 2; } } @@ -84,18 +108,22 @@ int main(int argc, char** argv) argv += optind; if (argc > 1) { - print_usage(); - return -1; + print_usage(argc, argv, 1); + return 2; } idevice_t device = NULL; - if (idevice_new(&device, udid) != IDEVICE_E_SUCCESS) { - fprintf(stderr, "ERROR: Could not connect to device\n"); + if (idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX) != IDEVICE_E_SUCCESS) { + if (udid) { + fprintf(stderr, "ERROR: No device found with udid %s.\n", udid); + } else { + fprintf(stderr, "ERROR: No device found.\n"); + } return -1; } lockdownd_client_t lockdown = NULL; - lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, "idevicename"); + lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, TOOL_NAME); if (lerr != LOCKDOWN_E_SUCCESS) { idevice_free(device); fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", lerr); @@ -127,9 +155,5 @@ int main(int argc, char** argv) lockdownd_client_free(lockdown); idevice_free(device); - if (udid) { - free(udid); - } - return res; } |