pmt: initial 2.3.0 update
Delete jni/Makefile
This commit is contained in:
273
jni/pmt.c
273
jni/pmt.c
@@ -21,19 +21,13 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* include needed libs (headers) */
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <getopt.h>
|
||||
#include <errno.h>
|
||||
#include <err.h>
|
||||
#include <sysexits.h>
|
||||
#define INC_MAIN_LIBS
|
||||
#define INC_DEBUGERS
|
||||
#define INC_STAT
|
||||
#define INC_GETOPT
|
||||
#define INC_DOCS_REQS
|
||||
|
||||
#include <pmt.h>
|
||||
#include <pmt-docs.h>
|
||||
|
||||
/* add value to variables that are added globally and are not worth */
|
||||
char* out = NULL;
|
||||
@@ -43,8 +37,8 @@ char* target_flash_file = NULL;
|
||||
char* partition_type = NULL;
|
||||
char* format_fs = NULL;
|
||||
char* bin_name = NULL;
|
||||
bool pmt_use_logical = NULL;
|
||||
bool pmt_use_cust_cxt = NULL;
|
||||
bool pmt_use_logical = false;
|
||||
bool pmt_use_cust_cxt = false;
|
||||
bool pmt_ab = false;
|
||||
bool pmt_logical = false;
|
||||
bool pmt_silent = false;
|
||||
@@ -52,10 +46,18 @@ bool pmt_flash = false;
|
||||
bool pmt_backup = false;
|
||||
bool pmt_format = false;
|
||||
bool pmt_force_mode = false;
|
||||
bool pmt_inst_on_termux = false;
|
||||
|
||||
/* variables for use in control of '-' expression */
|
||||
/* import language structs etc. */
|
||||
struct pmt_langdb_general* current = NULL;
|
||||
extern struct pmt_langdb_general en;
|
||||
extern struct pmt_langdb_general tr;
|
||||
extern const char* pmt_langdb_langs[];
|
||||
extern int pmt_langdb_total;
|
||||
|
||||
/* variable for use in control of '-' expression */
|
||||
static const char* opt_symbol = "-";
|
||||
static const char* common_symbol_rule = "When entering the attached argument of an option, an argument of another option type cannot be used. In short, the rule is: there can be no '-' at the beginning of the attached argument.";
|
||||
static char common_symbol_rule[350];
|
||||
|
||||
/**
|
||||
* He controls whether the '-' sign at
|
||||
@@ -66,8 +68,8 @@ check_optsym(const char* _Nonnull mystring)
|
||||
{
|
||||
if (strncmp(mystring, opt_symbol, 1) == 0)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "%s", common_symbol_rule);
|
||||
else exit(EX_USAGE);
|
||||
if (!pmt_force_mode) error(1, "%s", common_symbol_rule);
|
||||
else exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +85,7 @@ search_stat(const char* _Nonnull filepath, const char* _Nonnull stype)
|
||||
{
|
||||
struct stat search_stat;
|
||||
|
||||
if (stat(filepath, &search_stat) != 0) return 1;
|
||||
if (stat(filepath, &search_stat) != 0) return 0;
|
||||
|
||||
if (strcmp(stype, "dir") == 0)
|
||||
{
|
||||
@@ -112,11 +114,26 @@ search_stat(const char* _Nonnull filepath, const char* _Nonnull stype)
|
||||
/* classic main function (C binary here xd) */
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
/* check argument total */
|
||||
if (argc < 2) errx(EX_USAGE, "missing operand\nTry `%s --help' for more information.", argv[0]);
|
||||
|
||||
bin_name = argv[0];
|
||||
|
||||
/* load language */
|
||||
static char* langctrl_str;
|
||||
langctrl_str = loadlang();
|
||||
|
||||
if (strcmp(langctrl_str, "en") == 0) current = &en;
|
||||
else if (strcmp(langctrl_str, "tr") == 0) current = &tr;
|
||||
|
||||
sprintf(common_symbol_rule, "%s\n", current->common_symbol_rule);
|
||||
|
||||
if (search_sls() == 0)
|
||||
{
|
||||
if (current->welcome_ != NULL) printf("%s", current->welcome_);
|
||||
printf("%s %s %s %s.\n", current->language, current->welcome, current->by_str, current->lang_by_s);
|
||||
}
|
||||
|
||||
/* check argument total */
|
||||
if (argc < 2) error(1, "%s\n%s `%s --help' %s.", current->missing_operand, current->try_h, argv[0], current->for_more);
|
||||
|
||||
/* a structure for long arguments */
|
||||
struct option long_options[] = {
|
||||
{"logical", no_argument, 0, 'l'},
|
||||
@@ -124,6 +141,7 @@ int main(int argc, char* argv[])
|
||||
{"list", no_argument, 0, 'p'},
|
||||
{"silent", no_argument, 0, 's'},
|
||||
{"force", no_argument, 0, 'f'},
|
||||
{"set-language", required_argument, 0, 'S'},
|
||||
{"version", no_argument, 0, 'v'},
|
||||
{"help", no_argument, 0, 0},
|
||||
{"license", no_argument, 0, 'L'},
|
||||
@@ -136,11 +154,15 @@ int main(int argc, char* argv[])
|
||||
static bool wiew_version = false;
|
||||
static bool list_partitions = false;
|
||||
static bool combo_wiewers = false;
|
||||
static bool pmt_setlang = false;
|
||||
static char* langpr;
|
||||
static int search_result = 3;
|
||||
static int getvar_temp;
|
||||
static int check_getvar_temp;
|
||||
static int opt;
|
||||
|
||||
/* control for each argument */
|
||||
while ((opt = getopt_long(argc, argv, "lc:psfvL", long_options, NULL)) != -1)
|
||||
while ((opt = getopt_long(argc, argv, "lc:psfS:vL", long_options, NULL)) != -1)
|
||||
{
|
||||
/* process arguments */
|
||||
switch (opt)
|
||||
@@ -152,8 +174,8 @@ int main(int argc, char* argv[])
|
||||
if (pmt_logical) pmt_use_logical = true;
|
||||
else
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "this device does not have logical partitions!");
|
||||
else return EX_USAGE;
|
||||
if (!pmt_force_mode) error(1, "%s", current->not_logical);
|
||||
else return 1;
|
||||
}
|
||||
break;
|
||||
/* context selector option */
|
||||
@@ -162,8 +184,8 @@ int main(int argc, char* argv[])
|
||||
cust_cxt = strdup(optarg);
|
||||
if (strncmp(cust_cxt, opt_symbol, 1) == 0)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "%s", common_symbol_rule);
|
||||
else return EX_USAGE;
|
||||
if (!pmt_force_mode) error(1, "%s", common_symbol_rule);
|
||||
else return 1;
|
||||
}
|
||||
break;
|
||||
/* partition lister function */
|
||||
@@ -180,6 +202,11 @@ int main(int argc, char* argv[])
|
||||
case 's':
|
||||
pmt_silent = true;
|
||||
break;
|
||||
/* language setter option */
|
||||
case 'S':
|
||||
pmt_setlang = true;
|
||||
langpr = strdup(optarg);
|
||||
break;
|
||||
/* version info option */
|
||||
case 'v':
|
||||
wiew_version = true;
|
||||
@@ -200,33 +227,33 @@ int main(int argc, char* argv[])
|
||||
break;
|
||||
/* for invalid options */
|
||||
case '?':
|
||||
printf("Try `%s --help' for more information.\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
printf("%s `%s --help' %s\n", current->try_h, argv[0], current->for_more);
|
||||
return 1;
|
||||
break;
|
||||
default:
|
||||
printf("Usage: %s [backup] flash] [format] [-l | --logical] [-c | --context] [-D | --list] [-v | --version] [--help] [-L | --license]\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
printf("%s: %s [backup] flash] [format] [-l | --logical] [-c | --context] [-D | --list] [-v | --version] [--help] [-L | --license]\n", current->usage_head, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* stop the program if multiple viewer is used */
|
||||
if (combo_wiewers) errx(EX_USAGE, "multiple wiewers cannot be used at the same line.");
|
||||
if (combo_wiewers) error(1, "%s", current->multiple_wiewers);
|
||||
|
||||
/* controller to handle viewer */
|
||||
if (wiew_help)
|
||||
{
|
||||
help();
|
||||
return EX_OK;
|
||||
return 0;
|
||||
}
|
||||
else if (wiew_version)
|
||||
{
|
||||
version();
|
||||
return EX_OK;
|
||||
return 0;
|
||||
}
|
||||
else if (wiew_licenses)
|
||||
{
|
||||
licenses();
|
||||
return EX_OK;
|
||||
return 0;
|
||||
}
|
||||
else if (list_partitions)
|
||||
{
|
||||
@@ -234,79 +261,109 @@ int main(int argc, char* argv[])
|
||||
return listpart();
|
||||
}
|
||||
|
||||
if (pmt_setlang)
|
||||
{
|
||||
printf("%s: %s\n", argv[0], current->switching_lang);
|
||||
setlang(langpr);
|
||||
sleep(2);
|
||||
printf("%s: %s.\n", argv[0], current->please_rerun);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* detect target mode */
|
||||
static char arg1[20];
|
||||
sprintf(arg1, "%s", argv[1]);
|
||||
|
||||
if (strcmp(arg1, "backup") == 0)
|
||||
for (int argtest = 2; argtest == argc; argtest++)
|
||||
{
|
||||
if (argc == 2)
|
||||
getvar_temp = argtest;
|
||||
check_getvar_temp = getvar_temp;
|
||||
getvar_temp++;
|
||||
|
||||
if (strcmp(argv[argtest], "backup") == 0)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "expected backup argument 2, retrieved 0.");
|
||||
else return EX_USAGE;
|
||||
check_getvar_temp++;
|
||||
|
||||
if (argc < check_getvar_temp)
|
||||
{
|
||||
if (!pmt_silent) error(1, "%s 0.", current->expected_backup_arg);
|
||||
else return 1;
|
||||
}
|
||||
|
||||
target_partition = argv[getvar_temp];
|
||||
|
||||
if (argc == check_getvar_temp) out = target_partition;
|
||||
else
|
||||
{
|
||||
getvar_temp++;
|
||||
out = argv[getvar_temp];
|
||||
}
|
||||
|
||||
check_optsym(target_partition);
|
||||
check_optsym(out);
|
||||
|
||||
pmt_backup = true;
|
||||
break;
|
||||
}
|
||||
|
||||
target_partition = argv[2];
|
||||
if (argc != 4) out = target_partition;
|
||||
else out = argv[3];
|
||||
check_optsym(target_partition);
|
||||
check_optsym(out);
|
||||
|
||||
pmt_backup = true;
|
||||
}
|
||||
else if (strcmp(arg1, "flash") == 0)
|
||||
{
|
||||
if (argc == 2)
|
||||
else if (strcmp(argv[argtest], "flash") == 0)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "expected flash argument 2, retrieved 0.");
|
||||
else return EX_USAGE;
|
||||
}
|
||||
check_getvar_temp++;
|
||||
|
||||
if (argc == 3)
|
||||
if (argc < check_getvar_temp)
|
||||
{
|
||||
if (!pmt_force_mode || !pmt_silent) error(1, "%s 0.", current->expected_flash_arg);
|
||||
else return 1;
|
||||
}
|
||||
|
||||
if (argc == check_getvar_temp)
|
||||
{
|
||||
if (!pmt_force_mode || !pmt_silent) error(1, "%s 1.", current->expected_flash_arg);
|
||||
else return 1;
|
||||
}
|
||||
|
||||
target_flash_file = argv[getvar_temp];
|
||||
|
||||
getvar_temp++;
|
||||
target_partition = argv[getvar_temp];
|
||||
|
||||
check_optsym(target_flash_file);
|
||||
check_optsym(target_partition);
|
||||
|
||||
pmt_flash = true;
|
||||
break;
|
||||
}
|
||||
else if (strcmp(argv[argtest], "format") == 0)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "expected flash argument 2, retrieved 0.");
|
||||
else return EX_USAGE;
|
||||
check_getvar_temp++;
|
||||
|
||||
if (argc < check_getvar_temp)
|
||||
{
|
||||
if (!pmt_force_mode) error(1, "%s 0.", current->expected_format_arg);
|
||||
else return 1;
|
||||
}
|
||||
|
||||
if (argc == check_getvar_temp)
|
||||
{
|
||||
if (!pmt_force_mode) error(1, "%s 1.", current->expected_format_arg);
|
||||
else return 1;
|
||||
}
|
||||
|
||||
format_fs = argv[getvar_temp];
|
||||
|
||||
getvar_temp++;
|
||||
target_partition = argv[getvar_temp];
|
||||
|
||||
check_optsym(format_fs);
|
||||
check_optsym(target_partition);
|
||||
|
||||
pmt_format = true;
|
||||
break;
|
||||
}
|
||||
|
||||
target_flash_file = argv[2];
|
||||
target_partition = argv[3];
|
||||
check_optsym(target_flash_file);
|
||||
check_optsym(target_partition);
|
||||
|
||||
pmt_flash = true;
|
||||
}
|
||||
else if (strcmp(arg1, "format") == 0)
|
||||
{
|
||||
if (argc == 2)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "expected format argument 2, retrieved 0.");
|
||||
else return EX_USAGE;
|
||||
}
|
||||
|
||||
if (argc == 3)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "expected format argument 2, retrieved 1.");
|
||||
else return EX_USAGE;
|
||||
}
|
||||
|
||||
format_fs = argv[2];
|
||||
target_partition = argv[3];
|
||||
check_optsym(format_fs);
|
||||
check_optsym(target_partition);
|
||||
|
||||
pmt_format = true;
|
||||
}
|
||||
|
||||
/* target control is done */
|
||||
if (!pmt_backup && !pmt_flash && !pmt_format && !pmt_silent) errx(EX_USAGE, "missing operand.\nTry `%s --help` for more information.", argv[0]);
|
||||
else return EX_USAGE;
|
||||
|
||||
/* prevent multiple mode use */
|
||||
if (pmt_backup && pmt_flash && pmt_format)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "multi functions cannot be used in the same command.");
|
||||
else return EX_USAGE;
|
||||
}
|
||||
if (!pmt_backup && !pmt_flash && !pmt_format && !pmt_silent) error(1, "%s\n%s `%s --help` %s", argv[0], current->missing_operand, current->try_h, current->for_more);
|
||||
else return 1;
|
||||
|
||||
/* checks */
|
||||
check_root();
|
||||
@@ -316,8 +373,8 @@ int main(int argc, char* argv[])
|
||||
{
|
||||
if (strcmp(format_fs, "ext4") != 0 || strcmp(format_fs, "ext3") != 0 || strcmp(format_fs, "ext2") != 0)
|
||||
{
|
||||
if (!pmt_force_mode) errx(EX_USAGE, "formatter: unsupported filesystem: %s", format_fs);
|
||||
else return EX_USAGE;
|
||||
if (!pmt_force_mode) error(1, "%s: %s", current->unsupported_fs, format_fs);
|
||||
else return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,13 +384,13 @@ int main(int argc, char* argv[])
|
||||
|
||||
if (search_result == 1)
|
||||
{
|
||||
if (!pmt_silent) errx(EX_UNAVAILABLE, "cannot stat `%s': %s", target_flash_file, strerror(errno));
|
||||
else return EX_UNAVAILABLE;
|
||||
if (!pmt_silent) error(1, "%s `%s': %s", current->cannot_stat, target_flash_file, strerror(errno));
|
||||
else return 1;
|
||||
}
|
||||
else if (search_result == -1)
|
||||
{
|
||||
if (!pmt_silent) errx(EX_USAGE, "`%s': is a not file.", target_flash_file);
|
||||
else return EX_USAGE;
|
||||
if (!pmt_silent) error(1, "`%s': %s", target_flash_file, current->not_file);
|
||||
else return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,25 +401,25 @@ int main(int argc, char* argv[])
|
||||
|
||||
if (search_result == 1)
|
||||
{
|
||||
if (!pmt_silent) errx(EX_UNAVAILABLE, "cannot stat `%s': %s", cust_cxt, strerror(errno));
|
||||
else return EX_UNAVAILABLE;
|
||||
if (!pmt_silent) error(1, "%s `%s': %s", current->cannot_stat, cust_cxt, strerror(errno));
|
||||
else return 1;
|
||||
}
|
||||
else if (search_result == -1)
|
||||
{
|
||||
if (!pmt_silent) errx(EX_USAGE, "`%s': is a not directory.", cust_cxt);
|
||||
else return EX_USAGE;
|
||||
if (!pmt_silent) error(1, "`%s': %s", cust_cxt, current->not_dir);
|
||||
else return 1;
|
||||
}
|
||||
|
||||
if (strstr(cust_cxt, "/dev") == NULL && !pmt_force_mode) errx(EX_USAGE, ANSI_YELLOW "you're going through my wave? There's nothing about this /dev. Use force mode if you don't want this error." ANSI_RESET);
|
||||
if (strstr(cust_cxt, "/dev") == NULL && !pmt_force_mode) error(1, "%s", current->not_in_dev);
|
||||
}
|
||||
|
||||
if (target_partition == NULL)
|
||||
{
|
||||
if (!pmt_force_mode)
|
||||
{
|
||||
if (!pmt_silent) errx(EX_USAGE, "required partition name.\nTry `%s --help' for more information.\n", argv[0]);
|
||||
if (!pmt_silent) error(1, "%s\n%s `%s --help' %s", current->req_part_name, current->try_h, argv[0], current->for_more);
|
||||
}
|
||||
else return EX_USAGE;
|
||||
else return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -376,7 +433,7 @@ int main(int argc, char* argv[])
|
||||
if (pmt_backup) return pmt(1);
|
||||
else if (pmt_flash) return pmt(2);
|
||||
else if (pmt_format) return pmt(3);
|
||||
else if (!pmt_silent) errx(EX_USAGE, "no target (backup or flash).\nTry `%s --help` for more information.\n", argv[0]);
|
||||
else if (!pmt_silent) error(1, "%s\n%s `%s --help' %s", current->no_target, current->try_h, argv[0], current->for_more);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user