diff options
author | 2022-04-30 13:31:20 +0200 | |
---|---|---|
committer | 2022-04-30 13:31:20 +0200 | |
commit | 6cb13f9e6d3939930aecf91d8e23d1896a3b92e5 (patch) | |
tree | 371e4676ac914d9eef6bb4cfc0b5b6dc6f27da4f /tools/idevicedate.c | |
parent | 3b5cad28fabb236e05b8fff82fab5098127aa2bb (diff) | |
download | libimobiledevice-6cb13f9e6d3939930aecf91d8e23d1896a3b92e5.tar.gz libimobiledevice-6cb13f9e6d3939930aecf91d8e23d1896a3b92e5.tar.bz2 |
tools: Use getopt for option parsing in all tools
Diffstat (limited to 'tools/idevicedate.c')
-rw-r--r-- | tools/idevicedate.c | 133 |
1 files changed, 70 insertions, 63 deletions
diff --git a/tools/idevicedate.c b/tools/idevicedate.c index 505ed30..d05f63e 100644 --- a/tools/idevicedate.c +++ b/tools/idevicedate.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include <stdio.h> | 28 | #include <stdio.h> |
29 | #include <stdlib.h> | 29 | #include <stdlib.h> |
30 | #include <string.h> | 30 | #include <string.h> |
31 | #include <getopt.h> | ||
31 | #include <time.h> | 32 | #include <time.h> |
32 | #if HAVE_LANGINFO_CODESET | 33 | #if HAVE_LANGINFO_CODESET |
33 | #include <langinfo.h> | 34 | #include <langinfo.h> |
@@ -49,29 +50,29 @@ | |||
49 | #endif | 50 | #endif |
50 | #endif | 51 | #endif |
51 | 52 | ||
52 | static void print_usage(int argc, char **argv) | 53 | static void print_usage(int argc, char **argv, int is_error) |
53 | { | 54 | { |
54 | char *name = NULL; | 55 | char *name = strrchr(argv[0], '/'); |
55 | 56 | fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); | |
56 | name = strrchr(argv[0], '/'); | 57 | fprintf(is_error ? stderr : stdout, |
57 | printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); | 58 | "\n" |
58 | printf("\n"); | 59 | "Display the current date or set it on a device.\n" |
59 | printf("Display the current date or set it on a device.\n"); | 60 | "\n" |
60 | printf("\n"); | 61 | "NOTE: Setting the time on iOS 6 and later is only supported\n" |
61 | printf("NOTE: Setting the time on iOS 6 and later is only supported\n"); | 62 | " in the setup wizard screens before device activation.\n" |
62 | printf(" in the setup wizard screens before device activation.\n"); | 63 | "\n" |
63 | printf("\n"); | 64 | "OPTIONS:\n" |
64 | printf("OPTIONS:\n"); | 65 | " -u, --udid UDID target specific device by UDID\n" |
65 | printf(" -u, --udid UDID\ttarget specific device by UDID\n"); | 66 | " -n, --network connect to network device\n" |
66 | printf(" -n, --network\t\tconnect to network device\n"); | 67 | " -s, --set TIMESTAMP set UTC time described by TIMESTAMP\n" |
67 | printf(" -s, --set TIMESTAMP\tset UTC time described by TIMESTAMP\n"); | 68 | " -c, --sync set time of device to current system time\n" |
68 | printf(" -c, --sync\t\tset time of device to current system time\n"); | 69 | " -d, --debug enable communication debugging\n" |
69 | printf(" -d, --debug\t\tenable communication debugging\n"); | 70 | " -h, --help prints usage information\n" |
70 | printf(" -h, --help\t\tprints usage information\n"); | 71 | " -v, --version prints version information\n" |
71 | printf(" -v, --version\t\tprints version information\n"); | 72 | "\n" |
72 | printf("\n"); | 73 | "Homepage: <" PACKAGE_URL ">\n" |
73 | printf("Homepage: <" PACKAGE_URL ">\n"); | 74 | "Bug Reports: <" PACKAGE_BUGREPORT ">\n" |
74 | printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); | 75 | ); |
75 | } | 76 | } |
76 | 77 | ||
77 | int main(int argc, char *argv[]) | 78 | int main(int argc, char *argv[]) |
@@ -80,7 +81,6 @@ int main(int argc, char *argv[]) | |||
80 | lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; | 81 | lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; |
81 | idevice_t device = NULL; | 82 | idevice_t device = NULL; |
82 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; | 83 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; |
83 | int i; | ||
84 | const char* udid = NULL; | 84 | const char* udid = NULL; |
85 | int use_network = 0; | 85 | int use_network = 0; |
86 | time_t setdate = 0; | 86 | time_t setdate = 0; |
@@ -92,65 +92,72 @@ int main(int argc, char *argv[]) | |||
92 | char buffer[80]; | 92 | char buffer[80]; |
93 | int result = 0; | 93 | int result = 0; |
94 | 94 | ||
95 | int c = 0; | ||
96 | const struct option longopts[] = { | ||
97 | { "debug", no_argument, NULL, 'd' }, | ||
98 | { "help", no_argument, NULL, 'h' }, | ||
99 | { "udid", required_argument, NULL, 'u' }, | ||
100 | { "network", no_argument, NULL, 'n' }, | ||
101 | { "version", no_argument, NULL, 'v' }, | ||
102 | { "set", required_argument, NULL, 's' }, | ||
103 | { "sync", no_argument, NULL, 'c' }, | ||
104 | { NULL, 0, NULL, 0} | ||
105 | }; | ||
106 | |||
95 | #ifndef WIN32 | 107 | #ifndef WIN32 |
96 | signal(SIGPIPE, SIG_IGN); | 108 | signal(SIGPIPE, SIG_IGN); |
97 | #endif | 109 | #endif |
98 | /* parse cmdline args */ | 110 | /* parse cmdline args */ |
99 | for (i = 1; i < argc; i++) { | 111 | while ((c = getopt_long(argc, argv, "dhu:nvs:c", longopts, NULL)) != -1) { |
100 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { | 112 | switch (c) { |
113 | case 'd': | ||
101 | idevice_set_debug_level(1); | 114 | idevice_set_debug_level(1); |
102 | continue; | 115 | break; |
103 | } | 116 | case 'u': |
104 | else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { | 117 | if (!*optarg) { |
105 | i++; | 118 | fprintf(stderr, "ERROR: UDID argument must not be empty!\n"); |
106 | if (!argv[i] || !*argv[i]) { | 119 | print_usage(argc, argv, 1); |
107 | print_usage(argc, argv); | 120 | return 2; |
108 | return 0; | ||
109 | } | 121 | } |
110 | udid = argv[i]; | 122 | udid = optarg; |
111 | continue; | 123 | break; |
112 | } | 124 | case 'n': |
113 | else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--network")) { | ||
114 | use_network = 1; | 125 | use_network = 1; |
115 | continue; | 126 | break; |
116 | } | 127 | case 'h': |
117 | else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--set")) { | 128 | print_usage(argc, argv, 0); |
118 | i++; | 129 | return 0; |
119 | if (!argv[i] || (strlen(argv[i]) <= 1)) { | 130 | case 'v': |
120 | print_usage(argc, argv); | 131 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); |
121 | return 0; | 132 | return 0; |
133 | case 's': | ||
134 | if (!*optarg) { | ||
135 | fprintf(stderr, "ERROR: set argument must not be empty!\n"); | ||
136 | print_usage(argc, argv, 1); | ||
137 | return 2; | ||
122 | } | 138 | } |
123 | setdate = atoi(argv[i]); | 139 | setdate = atoi(optarg); |
124 | if (setdate == 0) { | 140 | if (setdate == 0) { |
125 | printf("ERROR: Invalid timestamp value.\n"); | 141 | fprintf(stderr, "ERROR: Invalid timestamp value.\n"); |
126 | print_usage(argc, argv); | 142 | print_usage(argc, argv, 1); |
127 | return 0; | 143 | return 0; |
128 | } | 144 | } |
129 | continue; | 145 | break; |
130 | } | 146 | case 'c': |
131 | else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--sync")) { | ||
132 | i++; | ||
133 | /* get current time */ | 147 | /* get current time */ |
134 | setdate = time(NULL); | 148 | setdate = time(NULL); |
135 | /* convert it to local time which sets timezone/daylight variables */ | 149 | /* convert it to local time which sets timezone/daylight variables */ |
136 | tmp = localtime(&setdate); | 150 | tmp = localtime(&setdate); |
137 | /* recalculate to make it UTC */ | 151 | /* recalculate to make it UTC */ |
138 | setdate = mktime(tmp); | 152 | setdate = mktime(tmp); |
139 | continue; | 153 | break; |
140 | } | 154 | default: |
141 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { | 155 | print_usage(argc, argv, 1); |
142 | print_usage(argc, argv); | 156 | return 2; |
143 | return 0; | ||
144 | } | ||
145 | else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { | ||
146 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); | ||
147 | return 0; | ||
148 | } | ||
149 | else { | ||
150 | print_usage(argc, argv); | ||
151 | return 0; | ||
152 | } | 157 | } |
153 | } | 158 | } |
159 | argc -= optind; | ||
160 | argv += optind; | ||
154 | 161 | ||
155 | ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); | 162 | ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); |
156 | if (ret != IDEVICE_E_SUCCESS) { | 163 | if (ret != IDEVICE_E_SUCCESS) { |