Return non-zero status on ideviceinstaller errors

Fixes: https://github.com/libimobiledevice/ideviceinstaller/issues/96
diff --git a/src/ideviceinstaller.c b/src/ideviceinstaller.c
index f45326f..5709b58 100644
--- a/src/ideviceinstaller.c
+++ b/src/ideviceinstaller.c
@@ -657,6 +657,7 @@
 
 	if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &client, "ideviceinstaller")) {
 		fprintf(stderr, "Could not connect to lockdownd. Exiting.\n");
+		res = -1;
 		goto leave_cleanup;
 	}
 
@@ -665,6 +666,7 @@
 		  &service) != LOCKDOWN_E_SUCCESS) || !service) {
 		fprintf(stderr,
 				"Could not start com.apple.mobile.notification_proxy!\n");
+		res = -1;
 		goto leave_cleanup;
 	}
 
@@ -677,6 +679,7 @@
 
 	if (nperr != NP_E_SUCCESS) {
 		fprintf(stderr, "Could not connect to notification_proxy!\n");
+		res = -1;
 		goto leave_cleanup;
 	}
 
@@ -696,6 +699,7 @@
 		  &service) != LOCKDOWN_E_SUCCESS) || !service) {
 		fprintf(stderr,
 				"Could not start com.apple.mobile.installation_proxy!\n");
+		res = -1;
 		goto leave_cleanup;
 	}
 
@@ -708,6 +712,7 @@
 
 	if (err != INSTPROXY_E_SUCCESS) {
 		fprintf(stderr, "Could not connect to installation_proxy!\n");
+		res = -1;
 		goto leave_cleanup;
 	}
 
@@ -760,6 +765,7 @@
 			if (!apps || (plist_get_node_type(apps) != PLIST_ARRAY)) {
 				fprintf(stderr,
 						"ERROR: instproxy_browse returnd an invalid plist!\n");
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -785,6 +791,7 @@
 		instproxy_client_options_free(client_opts);
 		if (err != INSTPROXY_E_SUCCESS) {
 			fprintf(stderr, "ERROR: instproxy_browse returned %d\n", err);
+			res = -1;
 			goto leave_cleanup;
 		}
 
@@ -804,6 +811,7 @@
 		if ((lockdownd_start_service(client, "com.apple.afc", &service) !=
 			 LOCKDOWN_E_SUCCESS) || !service) {
 			fprintf(stderr, "Could not start com.apple.afc!\n");
+			res = -1;
 			goto leave_cleanup;
 		}
 
@@ -812,11 +820,13 @@
 
 		if (afc_client_new(device, service, &afc) != AFC_E_SUCCESS) {
 			fprintf(stderr, "Could not connect to AFC!\n");
+			res = -1;
 			goto leave_cleanup;
 		}
 
 		if (stat(appid, &fst) != 0) {
 			fprintf(stderr, "ERROR: stat: %s: %s\n", appid, strerror(errno));
+			res = -1;
 			goto leave_cleanup;
 		}
 
@@ -845,6 +855,7 @@
 			zf = zip_open(appid, 0, &errp);
 			if (!zf) {
 				fprintf(stderr, "ERROR: zip_open: %s: %d\n", appid, errp);
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -917,6 +928,7 @@
 								afc_file_close(afc, af);
 								zip_fclose(zfile);
 								free(dstpath);
+								res = -1;
 								goto leave_cleanup;
 							}
 						}
@@ -940,6 +952,7 @@
 
 			if (asprintf(&pkgname, "%s/%s", PKG_PATH, basename(appid)) < 0) {
 				fprintf(stderr, "ERROR: Out of memory allocating pkgname!?\n");
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -947,55 +960,59 @@
 			afc_upload_dir(afc, appid, pkgname);
 			printf("DONE.\n");
 
-                       /* extract the CFBundleIdentifier from the package */
+			/* extract the CFBundleIdentifier from the package */
 
-                       /* construct full filename to Info.plist */
-                       char *filename = (char*)malloc(strlen(appid)+10+1);
-                       strcpy(filename, appid);
-                       strcat(filename, "/Info.plist");
+			/* construct full filename to Info.plist */
+			char *filename = (char*)malloc(strlen(appid)+10+1);
+			strcpy(filename, appid);
+			strcat(filename, "/Info.plist");
 
-                       struct stat st;
-                       FILE *fp = NULL;
+			struct stat st;
+			FILE *fp = NULL;
 
-                       if (stat(filename, &st) == -1 || (fp = fopen(filename, "r")) == NULL) {
-                               fprintf(stderr, "ERROR: could not locate %s in app!\n", filename);
-                               free(filename);
-                               goto leave_cleanup;
-                       }
-                       size_t filesize = st.st_size;
-                       char *ibuf = malloc(filesize * sizeof(char));
-                       size_t amount = fread(ibuf, 1, filesize, fp);
-                       if (amount != filesize) {
-                               fprintf(stderr, "ERROR: could not read %ld bytes from %s\n", filesize, filename);
-                               free(filename);
-                               goto leave_cleanup;
-                       }
-                       fclose(fp);
-                       free(filename);
+			if (stat(filename, &st) == -1 || (fp = fopen(filename, "r")) == NULL) {
+				fprintf(stderr, "ERROR: could not locate %s in app!\n", filename);
+				free(filename);
+				res = -1;
+				goto leave_cleanup;
+			}
+			size_t filesize = st.st_size;
+			char *ibuf = malloc(filesize * sizeof(char));
+			size_t amount = fread(ibuf, 1, filesize, fp);
+			if (amount != filesize) {
+				fprintf(stderr, "ERROR: could not read %ld bytes from %s\n", filesize, filename);
+				free(filename);
+				res = -1;
+				goto leave_cleanup;
+			}
+			fclose(fp);
+			free(filename);
 
-                       plist_t info = NULL;
-                       if (memcmp(ibuf, "bplist00", 8) == 0) {
-                               plist_from_bin(ibuf, filesize, &info);
-                       } else {
-                               plist_from_xml(ibuf, filesize, &info);
-                       }
-                       free(ibuf);
+			plist_t info = NULL;
+			if (memcmp(ibuf, "bplist00", 8) == 0) {
+				plist_from_bin(ibuf, filesize, &info);
+			} else {
+				plist_from_xml(ibuf, filesize, &info);
+			}
+			free(ibuf);
 
-                       if (!info) {
-                               fprintf(stderr, "ERROR: could not parse Info.plist!\n");
-                               goto leave_cleanup;
-                       }
+			if (!info) {
+				fprintf(stderr, "ERROR: could not parse Info.plist!\n");
+				res = -1;
+				goto leave_cleanup;
+			}
 
-                       plist_t bname = plist_dict_get_item(info, "CFBundleIdentifier");
-                       if (bname) {
-                               plist_get_string_val(bname, &bundleidentifier);
-                       }
-                       plist_free(info);
-                       info = NULL;
+			plist_t bname = plist_dict_get_item(info, "CFBundleIdentifier");
+			if (bname) {
+				plist_get_string_val(bname, &bundleidentifier);
+			}
+			plist_free(info);
+			info = NULL;
 		} else {
 			zf = zip_open(appid, 0, &errp);
 			if (!zf) {
 				fprintf(stderr, "ERROR: zip_open: %s: %d\n", appid, errp);
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -1024,6 +1041,7 @@
 
 			if (zip_get_app_directory(zf, &app_directory_name)) {
 				fprintf(stderr, "Unable to locate app directory in archive!\n");
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -1039,6 +1057,7 @@
 				free(filename);
 				zip_unchange_all(zf);
 				zip_close(zf);
+				res = -1;
 				goto leave_cleanup;
 			}
 			free(filename);
@@ -1053,6 +1072,7 @@
 				fprintf(stderr, "Could not parse Info.plist!\n");
 				zip_unchange_all(zf);
 				zip_close(zf);
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -1074,12 +1094,14 @@
 				fprintf(stderr, "Could not determine value for CFBundleExecutable!\n");
 				zip_unchange_all(zf);
 				zip_close(zf);
+				res = -1;
 				goto leave_cleanup;
 			}
 
 			char *sinfname = NULL;
 			if (asprintf(&sinfname, "Payload/%s.app/SC_Info/%s.sinf", bundleexecutable, bundleexecutable) < 0) {
 				fprintf(stderr, "Out of memory!?\n");
+				res = -1;
 				goto leave_cleanup;
 			}
 			free(bundleexecutable);
@@ -1099,6 +1121,7 @@
 			pkgname = NULL;
 			if (asprintf(&pkgname, "%s/%s", PKG_PATH, bundleidentifier) < 0) {
 				fprintf(stderr, "Out of memory!?\n");
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -1162,12 +1185,14 @@
 		err = instproxy_lookup_archives(ipc, NULL, &dict);
 		if (err != INSTPROXY_E_SUCCESS) {
 			fprintf(stderr, "ERROR: lookup_archives returned %d\n", err);
+			res = -1;
 			goto leave_cleanup;
 		}
 
 		if (!dict) {
 			fprintf(stderr,
 					"ERROR: lookup_archives did not return a plist!?\n");
+			res = -1;
 			goto leave_cleanup;
 		}
 
@@ -1273,12 +1298,14 @@
 			if (stat(copy_path, &fst) != 0) {
 				fprintf(stderr, "ERROR: stat: %s: %s\n", copy_path, strerror(errno));
 				free(copy_path);
+				res = -1;
 				goto leave_cleanup;
 			}
 
 			if (!S_ISDIR(fst.st_mode)) {
 				fprintf(stderr, "ERROR: '%s' is not a directory as expected.\n", copy_path);
 				free(copy_path);
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -1290,6 +1317,7 @@
 			if ((lockdownd_start_service(client, "com.apple.afc", &service) != LOCKDOWN_E_SUCCESS) || !service) {
 				fprintf(stderr, "Could not start com.apple.afc!\n");
 				free(copy_path);
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -1298,6 +1326,7 @@
 
 			if (afc_client_new(device, service, &afc) != AFC_E_SUCCESS) {
 				fprintf(stderr, "Could not connect to AFC!\n");
+				res = -1;
 				goto leave_cleanup;
 			}
 		}
@@ -1318,6 +1347,7 @@
 			if (err_occurred) {
 				afc_client_free(afc);
 				afc = NULL;
+				res = -1;
 				goto leave_cleanup;
 			}
 			FILE *f = NULL;
@@ -1326,6 +1356,7 @@
 			char *localfile = NULL;
 			if (asprintf(&localfile, "%s/%s.ipa", copy_path, appid) < 0) {
 				fprintf(stderr, "Out of memory!?\n");
+				res = -1;
 				goto leave_cleanup;
 			}
 			free(copy_path);
@@ -1351,6 +1382,7 @@
 				fclose(f);
 				free(remotefile);
 				free(localfile);
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -1381,6 +1413,7 @@
 				fprintf(stderr, "ERROR: could not open '%s' on device for reading!\n", remotefile);
 				free(remotefile);
 				free(localfile);
+				res = -1;
 				goto leave_cleanup;
 			}
 
@@ -1430,6 +1463,7 @@
 				options = NULL;
 				if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &client, "ideviceinstaller")) {
 					fprintf(stderr, "Could not connect to lockdownd. Exiting.\n");
+					res = -1;
 					goto leave_cleanup;
 				}
 				goto run_again;