Update sdcard partitioning to C++
diff --git a/Android.mk b/Android.mk
index bd4902d..3734330 100644
--- a/Android.mk
+++ b/Android.mk
@@ -29,7 +29,6 @@
     adb_install.cpp
 
 LOCAL_SRC_FILES += \
-    extra-functions.c \
     data.cpp \
     makelist.cpp \
     partition.cpp \
diff --git a/bmlutils/bmlutils.c b/bmlutils/bmlutils.c
index 6971b76..54b67f5 100644
--- a/bmlutils/bmlutils.c
+++ b/bmlutils/bmlutils.c
@@ -19,9 +19,7 @@
 
 #include <signal.h>
 #include <sys/wait.h>
-#include "../extra-functions.h"
 
-//extern int __system(const char *command);
 #define BML_UNLOCK_ALL				0x8A29		///< unlock all partition RO -> RW
 
 #ifndef BOARD_BML_BOOT
diff --git a/extra-functions.c b/extra-functions.c
deleted file mode 100644
index 3735472..0000000
--- a/extra-functions.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#include <linux/input.h>
-
-#include "bootloader.h"
-#include "common.h"
-#include "extra-functions.h"
-#include "data.h"
-#include "variables.h"
-
-void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm)
-{
-	ui_print("%s", str1);
-	ui_print("%s", str2);
-	pid_t pid = fork();
-	if (pid == 0) {
-		char *args[] = { "/sbin/sh", "-c", (char*)str3, "1>&2", NULL };
-		execv("/sbin/sh", args);
-		fprintf(stderr, str4, strerror(errno));
-		_exit(-1);
-	}
-	int status;
-	while (waitpid(pid, &status, WNOHANG) == 0) {
-		ui_print(".");
-		sleep(1);
-	}
-	ui_print("\n");
-	if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
-		ui_print("%s", str5);
-	} else {
-		ui_print("%s", str6);
-	}
-}
-
diff --git a/extra-functions.h b/extra-functions.h
deleted file mode 100644
index 082b806..0000000
--- a/extra-functions.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifndef _EXTRAFUNCTIONS_HEADER
-#define _EXTRAFUNCTIONS_HEADER
-
-#include "mincrypt/rsa.h"
-#include "minzip/Zip.h"
-
-void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);
-
-#endif // _EXTRAFUNCTIONS_HEADER
diff --git a/gui/action.cpp b/gui/action.cpp
index 4c3d88e..afa52d1 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -29,7 +29,6 @@
 #include "../common.h"
 #include "../minuitwrp/minui.h"
 #include "../recovery_ui.h"
-#include "../extra-functions.h"
 #include "../variables.h"
 #include "../twinstall.h"
 
@@ -825,6 +824,7 @@
 		if (function == "partitionsd")
 		{
 			operation_start("Partition SD Card");
+			int ret_val = 0;
 
 			if (simulate) {
 				simulate_progress_bar();
@@ -834,75 +834,11 @@
 				if (allow_partition == 0) {
 					ui_print("This device does not have a real SD Card!\nAborting!\n");
 				} else {
-					// Below seen in Koush's recovery
-					char sddevice[256];
-					char mkdir_path[255];
-#ifdef TW_EXTERNAL_STORAGE_PATH
-					TWPartition* SDCard = PartitionManager.Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
-#else
-					TWPartition* SDCard = PartitionManager.Find_Partition_By_Path("/sdcard");
-#endif
-					if (SDCard == NULL) {
-						LOGE("Unable to locate device to partition.\n");
-						operation_end(1, simulate);
-						return 0;
-					}
-					strcpy(sddevice, SDCard->Actual_Block_Device.c_str());
-					// Just need block not whole partition
-					sddevice[strlen("/dev/block/mmcblkX")] = '\0';
-
-					char es[64];
-					std::string ext_format, sd_path;
-					int ext, swap;
-					DataManager::GetValue("tw_sdext_size", ext);
-					DataManager::GetValue("tw_swap_size", swap);
-					DataManager::GetValue("tw_sdpart_file_system", ext_format);
-					sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap);
-					LOGI("\nrunning script: %s\n", es);
-					run_script("\nContinue partitioning?",
-						   "\nPartitioning sdcard : ",
-						   es,
-						   "\nunable to execute parted!\n(%s)\n",
-						   "\nOops... something went wrong!\nPlease check the recovery log!\n",
-						   "\nPartitioning complete!\n\n",
-						   "\nPartitioning aborted!\n\n", 0);
-					
-					// recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
-#ifdef TW_EXTERNAL_STORAGE_PATH
-					PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
-					DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
-					memset(mkdir_path, 0, sizeof(mkdir_path));
-					sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
-#else
-					PartitionManager.Mount_By_Path("/sdcard", 1);
-					strcpy(mkdir_path, "/sdcard/TWRP");
-#endif
-					mkdir(mkdir_path, 0777);
-					DataManager::Flush();
-#ifdef TW_EXTERNAL_STORAGE_PATH
-					DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
-					if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
-						DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
-#else
-					DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
-					if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
-						DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
-#endif
-					// This is sometimes needed to make a healthy ext4 partition
-					if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) {
-						char command[256];
-						LOGE("Fix this format command!\n");
-						//sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk);
-						ui_print("Formatting sd-ext as ext4...\n");
-						LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command);
-						system(command);
-						ui_print("DONE\n");
-					}
-
-					PartitionManager.Update_System_Details();
+					if (!PartitionManager.Partition_SDCard())
+						ret_val = 1; // failed
 				}
 			}
-			operation_end(0, simulate);
+			operation_end(ret_val, simulate);
 			return 0;
 		}
 		if (function == "installhtcdumlock")
diff --git a/partition.cpp b/partition.cpp
index 12e1f53..633011b 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -624,14 +624,6 @@
 	return false;
 }
 
-void TWPartition::Flip_Block_Device(void) {
-	string temp;
-
-	temp = Alternate_Block_Device;
-	Primary_Block_Device = Alternate_Block_Device;
-	Alternate_Block_Device = temp;
-}
-
 bool TWPartition::Is_Mounted(void) {
 	if (!Can_Be_Mounted)
 		return false;
@@ -670,6 +662,7 @@
 			LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
 		else
 			LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
+		LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
 		return false;
 	} else {
 		if (Removable)
@@ -1349,7 +1342,6 @@
 		Is_Present = true;
 		Actual_Block_Device = Primary_Block_Device;
 	} else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
-		Flip_Block_Device();
 		Actual_Block_Device = Primary_Block_Device;
 		Is_Present = true;
 	} else
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index ee199eb..98ba462 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -1684,3 +1684,147 @@
 			(*iter)->Mount(false);
 	}
 }
+
+int TWPartitionManager::Partition_SDCard(void) {
+	char mkdir_path[255], temp[255], line[512];
+	string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
+	int ext, swap, total_size = 0, fat_size;
+	FILE* fp;
+
+	ui_print("Partitioning SD Card...\n");
+#ifdef TW_EXTERNAL_STORAGE_PATH
+	TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
+#else
+	TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
+#endif
+	if (SDCard == NULL) {
+		LOGE("Unable to locate device to partition.\n");
+		return false;
+	}
+	if (!SDCard->UnMount(true))
+		return false;
+	TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
+	if (SDext != NULL) {
+		if (!SDext->UnMount(true))
+			return false;
+	}
+	system("umount \"$SWAPPATH\"");
+	Device = SDCard->Actual_Block_Device;
+	// Just use the root block device
+	Device.resize(strlen("/dev/block/mmcblkX"));
+
+	// Find the size of the block device:
+	fp = fopen("/proc/partitions", "rt");
+	if (fp == NULL) {
+		LOGE("Unable to open /proc/partitions\n");
+		return false;
+	}
+
+	while (fgets(line, sizeof(line), fp) != NULL)
+	{
+		unsigned long major, minor, blocks;
+		char device[512];
+		char tmpString[64];
+
+		if (strlen(line) < 7 || line[0] == 'm')	 continue;
+		sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
+
+		tmpdevice = "/dev/block/";
+		tmpdevice += device;
+		if (tmpdevice == Device) {
+			// Adjust block size to byte size
+			total_size = (int)(blocks * 1024ULL  / 1000000LLU);
+			break;
+		}
+	}
+	fclose(fp);
+
+	DataManager::GetValue("tw_sdext_size", ext);
+	DataManager::GetValue("tw_swap_size", swap);
+	DataManager::GetValue("tw_sdpart_file_system", ext_format);
+	fat_size = total_size - ext - swap;
+	LOGI("sd card block device is '%s', sdcard size is: %iMB, fat size: %iMB, ext size: %iMB, ext system: '%s', swap size: %iMB\n", Device.c_str(), total_size, fat_size, ext, ext_format.c_str(), swap);
+	memset(temp, 0, sizeof(temp));
+	sprintf(temp, "%i", fat_size);
+	fat_str = temp;
+	memset(temp, 0, sizeof(temp));
+	sprintf(temp, "%i", fat_size + ext);
+	ext_str = temp;
+	memset(temp, 0, sizeof(temp));
+	sprintf(temp, "%i", fat_size + ext + swap);
+	swap_str = temp;
+	if (ext + swap > total_size) {
+		LOGE("EXT + Swap size is larger than sdcard size.\n");
+		return false;
+	}
+	ui_print("Removing partition table...\n");
+	Command = "parted -s " + Device + " mklabel msdos";
+	LOGI("Command is: '%s'\n", Command.c_str());
+	if (system(Command.c_str()) != 0) {
+		LOGE("Unable to remove partition table.\n");
+		Update_System_Details();
+		return false;
+	}
+	ui_print("Creating FAT32 partition...\n");
+	Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
+	LOGI("Command is: '%s'\n", Command.c_str());
+	if (system(Command.c_str()) != 0) {
+		LOGE("Unable to create FAT32 partition.\n");
+		return false;
+	}
+	if (ext > 0) {
+		ui_print("Creating EXT partition...\n");
+		Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
+		LOGI("Command is: '%s'\n", Command.c_str());
+		if (system(Command.c_str()) != 0) {
+			LOGE("Unable to create EXT partition.\n");
+			Update_System_Details();
+			return false;
+		}
+	}
+	if (swap > 0) {
+		ui_print("Creating swap partition...\n");
+		Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
+		LOGI("Command is: '%s'\n", Command.c_str());
+		if (system(Command.c_str()) != 0) {
+			LOGE("Unable to create swap partition.\n");
+			Update_System_Details();
+			return false;
+		}
+	}
+	// recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
+#ifdef TW_EXTERNAL_STORAGE_PATH
+	Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
+	DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
+	memset(mkdir_path, 0, sizeof(mkdir_path));
+	sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
+#else
+	Mount_By_Path("/sdcard", 1);
+	strcpy(mkdir_path, "/sdcard/TWRP");
+#endif
+	mkdir(mkdir_path, 0777);
+	DataManager::Flush();
+#ifdef TW_EXTERNAL_STORAGE_PATH
+	DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
+	if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
+		DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
+#else
+	DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
+	if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
+		DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
+#endif
+	if (ext > 0) {
+		if (SDext == NULL) {
+			LOGE("Unable to locate sd-ext partition.\n");
+			return false;
+		}
+		Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
+		ui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
+		LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
+		system(Command.c_str());
+	}
+
+	Update_System_Details();
+	ui_print("Partitioning complete.\n");
+	return true;
+}
\ No newline at end of file
diff --git a/partitions.hpp b/partitions.hpp
index bb8f621..d55fec4 100644
--- a/partitions.hpp
+++ b/partitions.hpp
@@ -116,7 +116,6 @@
 	void Find_Real_Block_Device(string& Block_Device, bool Display_Error);    // Checks the block device given and follows symlinks until it gets to the real block device
 	bool Find_Partition_Size();                                               // Finds the partition size from /proc/partitions
 	unsigned long long Get_Size_Via_du(string Path, bool Display_Error);      // Uses du to get sizes
-	void Flip_Block_Device();                                                 // Flips the Block_Device and Alternate_Block_Device
 	bool Wipe_EXT23();                                                        // Formats as ext3 or ext2
 	bool Wipe_EXT4();                                                         // Formats using ext4, uses make_ext4fs when present
 	bool Wipe_FAT();                                                          // Formats as FAT except that mkdosfs from busybox usually fails so oftentimes this is actually a rm -rf wipe
@@ -182,6 +181,7 @@
 	virtual int usb_storage_enable(void);                                     // Enable USB storage mode
 	virtual int usb_storage_disable(void);                                    // Disable USB storage mode
 	virtual void Mount_All_Storage(void);                                     // Mounts all storage locations
+	virtual int Partition_SDCard(void);                                       // Repartitions the sdcard
 
 private:
 	bool Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename); // Generates an MD5 after a backup is made
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index 650a6a0..4796d06 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -116,15 +116,6 @@
 	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
-
-	#sdparted
-	include $(CLEAR_VARS)
-	LOCAL_MODULE := sdparted
-	LOCAL_MODULE_TAGS := eng
-	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
-	LOCAL_SRC_FILES := $(LOCAL_MODULE)
-	include $(BUILD_PREBUILT)
 endif
 
 ifeq ($(TW_INCLUDE_DUMLOCK), true)
diff --git a/prebuilt/sdparted b/prebuilt/sdparted
deleted file mode 100644
index 730d5dc..0000000
--- a/prebuilt/sdparted
+++ /dev/null
@@ -1,658 +0,0 @@
-#!/sbin/sh
-
-# do logging, if not excluded with -x
-LOGFILE="/data/sdparted.log"
-[ "$1" != "-x" ] && echo "$0" "$@" >> "$LOGFILE" && "$0" -x "$@" 2>&1 | tee -a "$LOGFILE" && exit
-shift
-
-ShowError() { echo ; echo " err: $1" ; echo ; exit 1 ; }
-
-ShowMessage() { echo ; echo " msg: $1" ; }
-
-ShowHelp() {
-
-cat <<DONEHELP
-
-$SCRIPTNAME v$SCRIPTREV created by $MYNAME
-
-if you use this script in your work, please give some credit. thanks.
-
-requirements: cm-recovery-v1.4
-
-usage: $SCRIPTNAME [options]
-
-
-options:
-
- --fatsize|-fs SIZE[MG]     set the size of the fat32 partition to <SIZE>.
-                            default=total sdcard size - (ext + swap)
-						
- --extsize|-es SIZE[MG]     set the size of the ext partition to <SIZE>.
-                            default=$EXTSIZE
-						
- --swapsize|-ss SIZE[MG]    set the size of the swap partition to <SIZE>.
-                            if set to 0, no swap partition will be created.
-                            default=$SWAPSIZE
-						
- --extfs|-efs TYPE          set the filesystem of ext partition to <TYPE>.
-                            valid types=ext2, ext3, ext4
-                            default=$EXTFS
-
-
- --upgradefs|-ufs TYPE      upgrades existing ext partition to <TYPE>.
-                            this operation will NOT wipe your sdcard and
-                            cannot be used with any partition creation options.
-                            valid types=ext3, ext4
-
- --downgradefs|-dfs TYPE    downgrades existing ext partition to <TYPE>.
-                            this operation will NOT wipe your sdcard and
-                            cannot be used with any partition creation options.
-                            valid types=ext2
-
-							
- --interactive|-i           interactive mode
-
- --help|-h                  display this help
-
- --printonly|-po            display sdcard information
-
- --silent|-s                do not prompt user, not even initial warning.
-
-
-examples:
- $SCRIPTNAME                     creates swap=$SWAPSIZE ext2=$EXTSIZE fat32=remaining free space
- $SCRIPTNAME -efs ext4           creates swap=$SWAPSIZE ext4=$EXTSIZE fat32=remaining free space
- $SCRIPTNAME -fs 1.5G -efs ext3  creates swap=$SWAPSIZE ext3=$EXTSIZE fat32=1536
- $SCRIPTNAME -es 256M -ss 0      creates no swap ext2=256 fat32=remaining free space
- $SCRIPTNAME -ufs ext4           upgrades ext partition to ext4
-
-DONEHELP
-
-}
-
-UserAbort() {
-	
-	WHILEEXIT=
-
-	while [ -z "$WHILEEXIT" ]
-	do
-		echo -n "do you want to continue? (Y/n) "
-		read response
-		echo
-		[ "$response" = "Y" ] || [ "$response" = "n" ] || [ "$response" = "N" ] && WHILEEXIT="$response"
-	done
-	
-	echo "$response" > /dev/null 2>&1 >>"$LOGFILE"
-	
-	[ "$response" != "Y" ]
-	
-}
-
-UnmountAll () {
-
-	# unmount all partitions so we can work with $SDPATH
-	# i'm assuming no more than 3 partitions
-	# maybe make a little more elegant later
-	echo -n "unmounting all partitions..."
-	umount "$FATPATH" > /dev/null 2>&1 >>"$LOGFILE"
-	umount "$EXTPATH" > /dev/null 2>&1 >>"$LOGFILE"
-	umount "$SWAPPATH" > /dev/null 2>&1 >>"$LOGFILE"
-	echo "done"
-	echo
-	
-}
-
-
-CheckReqs() {
-
-	echo -n "checking script requirements..."
-	# check for valid sdcard
-	[ -e $SDPATH ] || ShowError "$SDPATH does not exist!"
-	
-	# look for necessary programs
-	[ -e $CMPARTED ] || ShowError "$CMPARTED does not exist!"
-	[ -e $CMTUNE2FS ] || ShowError "$CMTUNE2FS does not exist!"
-	[ -e $CME2FSCK ] || ShowError "$CME2FSCK does not exist!"
-	
-	# verify cm-v1.4
-	PARTEDREV=`"$CMPARTED" "$SDPATH" version | grep Parted | cut -d" " -f3`
-	[ "$PARTEDREV" == "1.8.8.1.179-aef3" ] || ShowError "you are not using parted v1.8.8.1.179-aef3!"
-	echo "done"
-	echo
-	
-}
-
-CheckTableType() {
-
-	TABLETYPE=`"$CMPARTED" "$SDPATH" print | grep Table: | cut -d" " -f3`
-	
-	[ "$TABLETYPE" == "loop" -o "$TABLETYPE" == "msdos" ] && TTISOK=1 || TTISOK=0
-	[ "$TABLETYPE" == "loop" ] && TTISLOOP=1 || TTISLOOP=0
-	[ "$TABLETYPE" == "msdos" ] && TTISMSDOS=1 || TTISMOSDOS=0
-	
-}
-
-ValidateExtArg() {
-
-	FUNC_RET="nonzero"
-		
-	# validating argument
-	[ "$1" != "ext2" ] && [ "$1" != "ext3" ] && [ "$1" != "ext4" ] && FUNC_RET=
-		
-	[ -z "$FUNC_RET" ] && [ -z "$IMODE" ] && ShowError "$1 is not a valid filesystem."
-	[ -z "$FUNC_RET" ] && [ -n "$IMODE" ] && ShowMessage "$1 is not a valid filesystem."
-	
-	# return valid argument
-	[ -n "$FUNC_RET" ] && FUNC_RET="$1"
-	
-}
-
-ValidateSizeArg() {
-
-	# check for zero-length arg to protect expr length
-	[ -z "$1" ] && ShowError "zero-length argument passed to size-validator"
-		
-	SIZEMB=
-	ARGLEN=`expr length $1`
-	SIZELEN=$(($ARGLEN-1))
-	SIZEARG=`expr substr $1 1 $SIZELEN`
-	SIZEUNIT=`expr substr $1 $ARGLEN 1`
-	
-	# check if SIZEARG is an integer
-	if [ $SIZEARG -eq $SIZEARG 2> /dev/null ] ; then
-		# look for G
-		[ "$SIZEUNIT" == "G" ] && SIZEMB=$(($SIZEARG * 1024))
-		# look for M
-		[ "$SIZEUNIT" == "M" ] && SIZEMB=$SIZEARG
-		# no units on arg AND prevents using bogus size units
-		[ -z "$SIZEMB" ] && [ $SIZEUNIT -eq $SIZEUNIT 2> /dev/null ] && SIZEMB=$1
-	# check if SIZEARG is a floating point number, GB only
-	elif [ `expr index "$SIZEARG" .` != 0 ] && [ "$SIZEUNIT" == "G" ]  ; then
-		INT=`echo "$SIZEARG" | cut -d"." -f1`
-		FRAC=`echo "$SIZEARG" | cut -d"." -f2`
-		SIGDIGITS=`expr length $FRAC`
-		
-		[ -z "$INT" ] && INT=0
-		INTMB=$(($INT * 1024))
-		FRACMB=$((($FRAC * 1024) / (10**$SIGDIGITS)))
-		SIZEMB=$(($INTMB + $FRACMB))
-	# it's not a valid size
-	else
-		[ -z "$IMODE" ] && ShowError "$1 is not a valid size"
-	fi
-	
-	[ -z "$SIZEMB" ] && [ -n "$IMODE" ] && ShowMessage "$1 is not a valid size"
-	
-	# return valid argument in MB
-	FUNC_RET=$SIZEMB
-	
-}
-
-CalculatePartitions() {
-
-	# get size of sdcard in MB & do some math
-	SDSIZEMB=`"$CMPARTED" "$SDPATH" unit MB print | grep $SDPATH | cut -d" " -f3`
-	SDSIZE=${SDSIZEMB%MB}
-	[ -n "$FATSIZE" ] || FATSIZE=$(($SDSIZE - $EXTSIZE - $SWAPSIZE))
-	EXTEND=$(($FATSIZE + $EXTSIZE))
-	SWAPEND=$(($EXTEND + $SWAPSIZE))
-	
-	# check for fatsize of 0
-	[ $FATSIZE -le 0 ] && ShowError "must have a fat32 partition greater than 0MB"
-	
-	# check for zero-length sdsize...
-	# indicative of parted not reporting length 
-	# correctly b/c of error on sdcard
-	[ -z "$SDSIZE" ] && ShowError "zero-length argument passed to partition-calculator"
-	
-	# make sure we're not being asked to do the impossible
-	[ $(($FATSIZE + $EXTSIZE + $SWAPSIZE)) -gt $SDSIZE ] && [ -z "$IMODE" ] && ShowError "sum of requested partitions is greater than sdcard size"
-
-}
-
-
-UpgradeDowngradeOnly() {
-
-	if [ -n "$UEXTFSONLY" ] ; then
-		echo
-		[ -n "$CREATEPART" ] && ShowError "cannot use upgrade option when creating partitions, use -efs instead"
-		[ -n "$DEXTFSONLY" ] && ShowError "cannot upgrade AND downgrade, it just doesn't make sense"
-		echo "you have chosen to upgrade $EXTPATH to $UEXTFSONLY."
-		echo "this action will NOT delete any data from sdcard."
-		echo
-		[ -z "$SILENTRUN" ] &&	UserAbort && ShowError "script canceled by user"
-		echo
-		UpgradeExt "$UEXTFSONLY"
-		ShowCardInfo
-	elif [ -n "$DEXTFSONLY" ] ; then
-		echo
-		[ -n "$CREATEPART" ] && ShowError "cannot use downgrade option when creating partitions."
-		[ -n "$UEXTFSONLY" ] && ShowError "cannot downgrade AND upgrade, it just doesn't make sense."
-		echo "you have chosen to downgrade $EXTPATH to $DEXTFSONLY."
-		echo "this action will NOT delete any data from sdcard."
-		echo
-		[ -z "$SILENTRUN" ] &&	UserAbort && ShowError "script canceled by user"
-		echo
-		DowngradeExt "$DEXTFSONLY"
-		ShowCardInfo
-	fi
-	
-}
-
-PrepareSdCard() {
-
-	echo
-	if [ $TTISOK -eq 0 ] ; then
-		echo "partition 1 may not be aligned to cylinder boundaries."
-		echo "to continue, this must be corrected."
-	elif [ $TTISLOOP -gt 0 ] ; then
-		echo "your sdcard's partition table type is $TABLETYPE."
-		echo "to continue, partition table must be set to 'msdos'."
-	elif [ $TTISMSDOS -gt 0 ] ; then
-		# just a reminder..in a later version, 
-		# i may implement resizing of partitions, 
-		# so this will be unnecessary. but until then...
-		echo "to continue, all existing partitions must be removed."
-	else
-		# this is not good, and should never happen
-		# if it does, there is a serious problem
-		ShowError "sdcard failed table type check."
-	fi
-	
-	echo
-	echo "this action will remove all data from your sdcard."
-	echo
-	[ -z "$SILENTRUN" ] &&	UserAbort && ShowError "script canceled by user"
-	
-	[ $TTISOK -eq 0 ] && echo -n "correcting cylinder boundaries..."
-	[ $TTISLOOP -gt 0 ] && echo -n "setting partition table to msdos..."
-	[ $TTISMSDOS -gt 0 ] && echo -n "removing all partitions..."
-	
-	"$CMPARTED" -s "$SDPATH" mklabel msdos 2>&1 >>"$LOGFILE"
-	echo "done"
-	echo
-	
-}
-
-ShowActions() {
-
-	echo
-	echo "total size of sdcard=$SDSIZEMB"
-	echo
-	echo "the following actions will be performed:"
-	echo " -create $FATSIZE""MB fat32 partition"
-	[ $EXTSIZE -gt 0 ] && echo " -create $EXTSIZE""MB ext2 partition"
-	[ $SWAPSIZE -gt 0 ] && echo " -create $SWAPSIZE""MB swap partition"
-	[ "$EXTFS" != "ext2" ] && echo " -ext2 partition will be upgraded to $EXTFS"
-	echo
-	[ -z "$SILENTRUN" ] &&	UserAbort && ShowError "script canceled by user"
-	echo
-	
-}
-
-ShowCardInfo() {
-
-    echo "sddevice located in /sdcard/sddev.log"
-    echo $SDPATH > /sdcard/sddev.log
-
-	CheckTableType
-	
-	echo
-	echo "retrieving current sdcard information..."
-	
-	if [ $TTISOK -gt 0 ] ; then
-		echo
-		parted "$SDPATH" print
-		echo
-		echo "script log is located @ /data/sdparted.log"
-		exit 0
-	else
-		echo
-		echo "partition 1 may not be aligned to cylinder boundaries."
-		ShowError "cannot complete print operation."
-	fi
-	echo
-	
-}
-
-
-PartitionSdCard() {
-
-	echo "performing selected actions..."
-	echo
-	
-	if [ $FATSIZE -gt 0 ] ; then
-		echo -n "creating fat32 partition..."
-		"$CMPARTED" -s "$SDPATH" mkpartfs primary fat32 0 "$FATSIZE"MB 2>&1 >>"$LOGFILE"
-		echo "done"
-	fi
-	
-	if [ $EXTSIZE -gt 0 ] ; then
-		echo -n "creating ext2 partition..."
-		"$CMPARTED" -s "$SDPATH" mkpartfs primary ext2 "$FATSIZE"MB "$EXTEND"MB 2>&1 >>"$LOGFILE"
-		"$CMTUNE2FS" -L sd-ext "$EXTPATH" 2>&1 >>"$LOGFILE"
-		echo "done"
-	fi
-	
-	if [ $SWAPSIZE -gt 0 ] ; then
-		echo -n "creating swap partition..."
-		"$CMPARTED" -s "$SDPATH" mkpartfs primary linux-swap "$EXTEND"MB "$SWAPEND"MB 2>&1 >>"$LOGFILE"
-		echo "done"
-	fi
-	echo
-	
-}
-
-UpgradeExt() {
-
-	# check for no upgrade
-	[ "$1" == "ext2" ] && return
-	# check for ext partition
-	[ ! -e "$EXTPATH" ] && ShowError "$EXTPATH does not exist"
-
-	# have to use -m switch for this check b/c parted incorrectly
-	# reports all ext partitions as ext2 when running print <number>
-	CHECKEXTFS=`"$CMPARTED" -m "$SDPATH" print | grep ext | cut -d":" -f5`
-	[ "$CHECKEXTFS" == "$1" ] && ShowError "$EXTPATH is already $1"
-
-	# grabbed the code bits for ext3 from upgrade_fs(credit:cyanogen)
-	# check for ext2...must upgrade to ext3 first b4 ext4
-	if [ "$1" == "ext3" -o "$1" == "ext4" ] ; then
-		echo -n "adding journaling to $EXTPATH..."
-		umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
-		"$CME2FSCK" -p "$EXTPATH" 2>&1 >>"$LOGFILE"
-		"$CMTUNE2FS" -c0 -i0 -j "$EXTPATH" 2>&1 >>"$LOGFILE"
-		echo "done"
-	fi
-
-	# and got convert to ext4 from xda-forum(credit:Denkai)
-	if [ "$1" == "ext4" ] ; then
-		echo -n "converting $EXTPATH to ext4 filesystem..."
-		umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
-		"$CMTUNE2FS" -O extents,uninit_bg,dir_index "$EXTPATH" 2>&1 >>"$LOGFILE"
-		"$CME2FSCK" -fpDC0 "$EXTPATH" 2>&1 >>"$LOGFILE"
-		echo "done"
-	fi
-	echo
-	
-}
-
-DowngradeExt() {
-
-	# check for ext partition
-	[ ! -e "$EXTPATH" ] && ShowError "$EXTPATH does not exist"
-
-	# have to use print for this check b/c parted incorrectly
-	# reports all ext partitions as ext2 when running print <number>
-	CHECKEXTFS=`"$CMPARTED" -m "$SDPATH" print | grep ext | cut -d":" -f5`
-	[ "$CHECKEXTFS" == "$1" ] && ShowError "$EXTPATH is already $1"
-	
-	if [ "$CHECKEXTFS" == "ext4" -o "$1" == "ext3" ] ; then
-		# interweb says downgrading from ext4 is not possible
-		# without a backup/restore procedure.
-		# if i figure it out, i'll implement it.
-		ShowError "downgrading from ext4 is not currently supported"
-	fi
-	
-	if [ "$1" == "ext2" ] ; then
-		echo -n "removing journaling from $EXTPATH..."
-		umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
-		"$CMTUNE2FS" -O ^has_journal "$EXTPATH" 2>&1 >>"$LOGFILE"
-		"$CME2FSCK" -fp "$EXTPATH" 2>&1 >>"$LOGFILE"
-		echo "done"
-	fi
-	echo
-	
-}
-
-
-Interactive() {
-
-cat <<DONEINSTRUCT
-
-sdparted interactive mode
-
-rules:
-1. no answer means you accept default value
-2. enter '0' for no partition
-
-DONEINSTRUCT
-
-	UserAbort && ShowError "script canceled by user"
-	
-	CalculatePartitions
-	
-	GetSwapSize
-	
-	FATSIZE=
-	
-	CalculatePartitions
-	
-	GetExtSize
-	
-	GetExtType
-	
-	FATSIZE=
-	
-	CalculatePartitions
-	
-	GetFatSize
-	
-}
-
-GetSwapSize() {
-
-	SWAPTEST=
-	
-	while [ -z "$SWAPTEST" ]
-	do
-		echo
-		echo -n "swap partition size [default=$SWAPSIZE]: "
-		read SWAPRESP
-		
-		[ -z "$SWAPRESP" ] && SWAPRESP="$SWAPSIZE"
-		echo "$SWAPRESP" > /dev/null 2>&1 >>"$LOGFILE"
-		
-		ValidateSizeArg "$SWAPRESP"
-		SWAPTEST="$FUNC_RET"
-		[ -n "$SWAPTEST" ] && [ $SWAPTEST -gt $SDSIZE ] && ShowMessage "$SWAPRESP > available space($(($SDSIZE))M)." && SWAPTEST=
-	done
-	
-	SWAPSIZE=$SWAPTEST
-	
-}
-
-GetExtSize() {
-
-	EXTTEST=
-	
-	while [ -z "$EXTTEST" ]
-	do
-		echo
-		echo -n "ext partition size [default=$EXTSIZE]: "
-		read EXTRESP
-		
-		[ -z "$EXTRESP" ] && EXTRESP="$EXTSIZE"
-		echo "$EXTRESP" > /dev/null 2>&1 >>"$LOGFILE"
-		
-		ValidateSizeArg "$EXTRESP"
-		EXTTEST="$FUNC_RET"
-		
-		[ -n "$EXTTEST" ] && [ $EXTTEST -gt $(($SDSIZE - $SWAPSIZE)) ] && ShowMessage "$EXTRESP > available space($(($SDSIZE - $SWAPSIZE))M)." && EXTTEST=
-	done
-	
-	EXTSIZE=$EXTTEST
-	
-}
-
-GetExtType() {
-
-	FSTEST=
-	
-	while [ -z "$FSTEST" ]
-	do
-		echo
-		echo -n "ext partition type [default=$EXTFS]: "
-		read FSRESP
-		
-		[ -z "$FSRESP" ] && FSRESP="$EXTFS"
-		echo "$FSRESP" > /dev/null 2>&1 >>"$LOGFILE"
-		
-		ValidateExtArg "$FSRESP"
-		FSTEST="$FUNC_RET"
-	done
-	
-	EXTFS="$FSTEST"
-	
-}
-
-GetFatSize() {
-
-	FATTEST=
-	
-	while [ -z "$FATTEST" ]
-	do
-		echo
-		echo -n "fat partition size [default=$FATSIZE]: "
-		read FATRESP
-		
-		[ -z "$FATRESP" ] && FATRESP="$FATSIZE"
-		echo "$FATRESP" > /dev/null 2>&1 >>"$LOGFILE"
-		
-		ValidateSizeArg "$FATRESP"
-		FATTEST="$FUNC_RET"
-		
-		[ -n "$FATTEST" ] && [ $FATTEST -gt $FATSIZE ] && ShowMessage "$FATRESP > available space($(($SDSIZE - $SWAPSIZE - $EXTSIZE))M)." && FATTEST=
-		[ -n "$FATTEST" ] && [ $FATTEST -le 0 ] && ShowMessage "must have a fat32 partition greater than 0MB" && FATTEST=
-	done
-	
-	FATSIZE=$FATTEST
-	
-}
-
-
-SCRIPTNAME="sdparted"
-SCRIPTREV="0.6"
-MYNAME="51dusty"
-
-IMODE=
-SILENTRUN=
-CREATEPART=
-FUNC_RET=
-
-UEXTFSONLY=
-DEXTFSONLY=
-
-TTISOK=
-TTISLOOP=
-TTISMSDOS=
-
-SDSIZE=
-SDSIZEMB=
-SDINFO=$(cat /etc/fstab | grep /sdcard | awk '{print $1}')
-if [ -L "$SDINFO" ]
-then
-	SDPATH=$(ls -l $SDINFO | awk '{print $11}')
-else
-	SDPATH=$SDINFO
-fi
-# we may now have an SDPATH, let's make sure its on mmcblkX or mmcblkXp1
-CHECK_SDPATH1=$(echo $SDPATH | grep mmcblk.$)
-CHECK_SDPATH2=$(echo $SDPATH | grep mmcblk.p1$)
-if [ -z "$CHECK_SDPATH1" ]
-then
-	if [ -z "$CHECK_SDPATH2" ]
-	then
-		echo fail1
-		unset SDPATH
-	else
-		LEN=${#SDPATH}
-		BLKLEN=$(expr $LEN - 2)
-		SDPATH=${SDPATH:0:$BLKLEN}
-	fi
-fi
-
-
-FATSIZE=
-FATTYPE="fat32"
-FATPATH=$SDPATH"p1"
-
-EXTSIZE=512
-EXTFS="ext2"
-EXTPATH=$SDPATH"p2"
-EXTEND=
-
-SWAPSIZE=32
-SWAPTYPE="linux-swap"
-SWAPPATH=$SDPATH"p3"
-SWAPEND=
-
-CMPARTED="/sbin/parted"
-CMTUNE2FS="/sbin/tune2fs"
-CME2FSCK="/sbin/e2fsck"
-
-# give the output some breathing room
-echo "$SCRIPTREV" >> "$LOGFILE" 
-echo
-
-# check for arguments
-while [ $# -gt 0 ] ; do
-  case "$1" in
-
-    -h|--help) ShowHelp ; exit 0 ;;
-    
-    -fs|--fatsize) shift ; ValidateSizeArg "$1" ; FATSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
-    -es|--extsize) shift ; ValidateSizeArg "$1" ; EXTSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
-	-ss|--swapsize) shift ; ValidateSizeArg "$1" ; SWAPSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
-	-efs|--extfs) shift ; ValidateExtArg "$1" ; EXTFS="$FUNC_RET" ; CREATEPART="$1" ;;
-	
-	-ufs|--upgradefs) shift ; ValidateExtArg "$1" ; UEXTFSONLY="$FUNC_RET" ;;
-	-dfs|--downgradefs) shift ; ValidateExtArg "$1" ; DEXTFSONLY="$FUNC_RET" ;;
-	
-	-i|--interactive) IMODE="$1" ;;
-	
-	-s|--silent) SILENTRUN="$1" ;;
-	
-	-po|--printonly) ShowCardInfo ;;
-	
-	*) ShowHelp ; ShowError "unknown argument '$1'" ;;
-
-  esac
-  shift
-done
-
-# can't do silent when in interactive mode
-[ -n "$IMODE" ] && SILENTRUN=
-
-# make sure sdcard exists and all needed files are here
-CheckReqs
-
-# unmount all
-UnmountAll
-
-# upgrade only? downgrade only?
-UpgradeDowngradeOnly
-
-# check table
-CheckTableType
-
-# prep card
-PrepareSdCard
-
-# check for interactive mode
-[ -n "$IMODE" ] && Interactive
-
-# do some math
-CalculatePartitions
-
-# last chance to cancel
-ShowActions
-
-# partition card
-PartitionSdCard
-
-# upgrade fs if necessary
-UpgradeExt "$EXTFS"
-
-# say goodbye and show print output
-ShowCardInfo
diff --git a/recovery.cpp b/recovery.cpp
index 35e6116..1e1d9a9 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -50,7 +50,6 @@
 }
 
 extern "C" {
-#include "extra-functions.h"
 #include "data.h"
 #include "gui/gui.h"
 }
diff --git a/twinstall.cpp b/twinstall.cpp
index 4bc6942..86a0979 100644
--- a/twinstall.cpp
+++ b/twinstall.cpp
@@ -41,11 +41,6 @@
 #include "partitions.hpp"
 #include "twrp-functions.hpp"
 
-extern "C" {
-#include "extra-functions.h"
-int __system(const char *command);
-};
-
 extern RecoveryUI* ui;
 
 #define ASSUMED_UPDATE_BINARY_NAME  "META-INF/com/google/android/update-binary"