diff options
Diffstat (limited to 'tools/idevicename.c')
| -rw-r--r-- | tools/idevicename.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tools/idevicename.c b/tools/idevicename.c new file mode 100644 index 0000000..a991a8f --- /dev/null +++ b/tools/idevicename.c | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | /* | ||
| 2 | * idevicename.c | ||
| 3 | * Simple utility to get or set the device name | ||
| 4 | * | ||
| 5 | * Copyright (c) 2014 Nikias Bassen, All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <stdio.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <unistd.h> | ||
| 25 | #include <stdlib.h> | ||
| 26 | #include <getopt.h> | ||
| 27 | |||
| 28 | #include <libimobiledevice/libimobiledevice.h> | ||
| 29 | #include <libimobiledevice/lockdown.h> | ||
| 30 | |||
| 31 | static void print_usage() | ||
| 32 | { | ||
| 33 | printf("\nUsage: idevicename [OPTIONS] [NAME]\n"); | ||
| 34 | printf(" --udid|-u UDID use UDID to target a specific device\n"); | ||
| 35 | printf("\n"); | ||
| 36 | } | ||
| 37 | |||
| 38 | int main(int argc, char** argv) | ||
| 39 | { | ||
| 40 | int res = -1; | ||
| 41 | char* udid = NULL; | ||
| 42 | |||
| 43 | int c = 0; | ||
| 44 | int optidx = 0; | ||
| 45 | const struct option longopts[] = { | ||
| 46 | { "udid", required_argument, NULL, 'u' }, | ||
| 47 | { NULL, 0, NULL, 0} | ||
| 48 | }; | ||
| 49 | |||
| 50 | signal(SIGPIPE, SIG_IGN); | ||
| 51 | |||
| 52 | while ((c = getopt_long(argc, argv, "u:", longopts, &optidx)) != -1) { | ||
| 53 | switch (c) { | ||
| 54 | case 'u': | ||
| 55 | udid = strdup(optarg); | ||
| 56 | break; | ||
| 57 | default: | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | argc -= optind; | ||
| 63 | argv += optind; | ||
| 64 | |||
| 65 | if (argc > 1) { | ||
| 66 | print_usage(); | ||
| 67 | return -1; | ||
| 68 | } | ||
| 69 | |||
| 70 | idevice_t device = NULL; | ||
| 71 | if (idevice_new(&device, udid) != IDEVICE_E_SUCCESS) { | ||
| 72 | fprintf(stderr, "ERROR: Could not connect to device\n"); | ||
| 73 | return -1; | ||
| 74 | } | ||
| 75 | |||
| 76 | lockdownd_client_t lockdown = NULL; | ||
| 77 | lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, "idevicename"); | ||
| 78 | if (lerr != LOCKDOWN_E_SUCCESS) { | ||
| 79 | idevice_free(device); | ||
| 80 | fprintf(stderr, "ERROR: lockdown connection failed, lockdown error %d\n", lerr); | ||
| 81 | return -1; | ||
| 82 | } | ||
| 83 | |||
| 84 | plist_t node = NULL; | ||
| 85 | |||
| 86 | if (argc == 0) { | ||
| 87 | // getting device name | ||
| 88 | char* name = NULL; | ||
| 89 | lerr = lockdownd_get_value(lockdown, NULL, "DeviceName", &node); | ||
| 90 | if (node) { | ||
| 91 | plist_get_string_val(node, &name); | ||
| 92 | plist_free(node); | ||
| 93 | } | ||
| 94 | if (name) { | ||
| 95 | printf("%s\n", name); | ||
| 96 | free(name); | ||
| 97 | res = 0; | ||
| 98 | } else { | ||
| 99 | fprintf(stderr, "ERROR: Could not get device name, lockdown error %d\n", lerr); | ||
| 100 | } | ||
| 101 | } else { | ||
| 102 | // setting device name | ||
| 103 | lerr = lockdownd_set_value(lockdown, NULL, "DeviceName", plist_new_string(argv[0])); | ||
| 104 | if (lerr == LOCKDOWN_E_SUCCESS) { | ||
| 105 | printf("device name set to '%s'\n", argv[0]); | ||
| 106 | res = 0; | ||
| 107 | } else { | ||
| 108 | fprintf(stderr, "ERROR: Could not set device name, lockdown error %d\n", lerr); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | lockdownd_client_free(lockdown); | ||
| 113 | idevice_free(device); | ||
| 114 | |||
| 115 | if (udid) { | ||
| 116 | free(udid); | ||
| 117 | } | ||
| 118 | |||
| 119 | return res; | ||
| 120 | } | ||
