summaryrefslogtreecommitdiffstats
path: root/tools/ideviceenterrecovery.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2022-04-30 13:31:20 +0200
committerGravatar Nikias Bassen2022-04-30 13:31:20 +0200
commit6cb13f9e6d3939930aecf91d8e23d1896a3b92e5 (patch)
tree371e4676ac914d9eef6bb4cfc0b5b6dc6f27da4f /tools/ideviceenterrecovery.c
parent3b5cad28fabb236e05b8fff82fab5098127aa2bb (diff)
downloadlibimobiledevice-6cb13f9e6d3939930aecf91d8e23d1896a3b92e5.tar.gz
libimobiledevice-6cb13f9e6d3939930aecf91d8e23d1896a3b92e5.tar.bz2
tools: Use getopt for option parsing in all tools
Diffstat (limited to 'tools/ideviceenterrecovery.c')
-rw-r--r--tools/ideviceenterrecovery.c71
1 files changed, 41 insertions, 30 deletions
diff --git a/tools/ideviceenterrecovery.c b/tools/ideviceenterrecovery.c
index 0cc9936..29cc5c9 100644
--- a/tools/ideviceenterrecovery.c
+++ b/tools/ideviceenterrecovery.c
@@ -27,8 +27,9 @@
27 27
28#include <stdio.h> 28#include <stdio.h>
29#include <string.h> 29#include <string.h>
30#include <errno.h>
31#include <stdlib.h> 30#include <stdlib.h>
31#include <getopt.h>
32#include <errno.h>
32#ifndef WIN32 33#ifndef WIN32
33#include <signal.h> 34#include <signal.h>
34#endif 35#endif
@@ -36,22 +37,22 @@
36#include <libimobiledevice/libimobiledevice.h> 37#include <libimobiledevice/libimobiledevice.h>
37#include <libimobiledevice/lockdown.h> 38#include <libimobiledevice/lockdown.h>
38 39
39static void print_usage(int argc, char **argv) 40static void print_usage(int argc, char **argv, int is_error)
40{ 41{
41 char *name = NULL; 42 char *name = strrchr(argv[0], '/');
42 43 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0]));
43 name = strrchr(argv[0], '/'); 44 fprintf(is_error ? stderr : stdout,
44 printf("Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0])); 45 "\n"
45 printf("\n"); 46 "Makes a device with the supplied UDID enter recovery mode immediately.\n"
46 printf("Makes a device with the supplied UDID enter recovery mode immediately.\n"); 47 "\n"
47 printf("\n"); 48 "OPTIONS:\n"
48 printf("OPTIONS:\n"); 49 " -d, --debug enable communication debugging\n"
49 printf(" -d, --debug\t\tenable communication debugging\n"); 50 " -h, --help prints usage information\n"
50 printf(" -h, --help\t\tprints usage information\n"); 51 " -v, --version prints version information\n"
51 printf(" -v, --version\t\tprints version information\n"); 52 "\n"
52 printf("\n"); 53 "Homepage: <" PACKAGE_URL ">\n"
53 printf("Homepage: <" PACKAGE_URL ">\n"); 54 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
54 printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); 55 );
55} 56}
56 57
57int main(int argc, char *argv[]) 58int main(int argc, char *argv[])
@@ -60,34 +61,44 @@ int main(int argc, char *argv[])
60 lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; 61 lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
61 idevice_t device = NULL; 62 idevice_t device = NULL;
62 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; 63 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
63 int i;
64 const char* udid = NULL; 64 const char* udid = NULL;
65 int c = 0;
66 const struct option longopts[] = {
67 { "debug", no_argument, NULL, 'd' },
68 { "help", no_argument, NULL, 'h' },
69 { "version", no_argument, NULL, 'v' },
70 { NULL, 0, NULL, 0}
71 };
65 72
66#ifndef WIN32 73#ifndef WIN32
67 signal(SIGPIPE, SIG_IGN); 74 signal(SIGPIPE, SIG_IGN);
68#endif 75#endif
69 /* parse cmdline args */ 76 /* parse cmdline args */
70 for (i = 1; i < argc; i++) { 77 while ((c = getopt_long(argc, argv, "dhv", longopts, NULL)) != -1) {
71 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { 78 switch (c) {
79 case 'd':
72 idevice_set_debug_level(1); 80 idevice_set_debug_level(1);
73 continue; 81 break;
74 } 82 case 'h':
75 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { 83 print_usage(argc, argv, 0);
76 print_usage(argc, argv);
77 return 0; 84 return 0;
78 } 85 case 'v':
79 else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
80 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); 86 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
81 return 0; 87 return 0;
88 default:
89 print_usage(argc, argv, 1);
90 return 2;
82 } 91 }
83 } 92 }
93 argc -= optind;
94 argv += optind;
84 95
85 i--; 96 if (!argv[0]) {
86 if (argc < 2 || !argv[i] || !*argv[i]) { 97 fprintf(stderr, "ERROR: No UDID specified\n");
87 print_usage(argc, argv); 98 print_usage(argc+optind, argv-optind, 1);
88 return 0; 99 return 2;
89 } 100 }
90 udid = argv[i]; 101 udid = argv[0];
91 102
92 ret = idevice_new(&device, udid); 103 ret = idevice_new(&device, udid);
93 if (ret != IDEVICE_E_SUCCESS) { 104 if (ret != IDEVICE_E_SUCCESS) {