From 117f2e2cec5c84cc8d544bb210cc0a0688019e5e Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 11 Feb 2025 20:01:28 -0600 Subject: [PATCH] Progress: Continue working on completing the remove program. Utilize the recently added `f_id_t`, `f_gid_t`, and `f_uid_t` types and the respective function to reduce the amount of code for user and group ID processing. --- sources/c/program/kevux/tools/remove/main/common.c | 155 +++++++++------------ sources/c/program/kevux/tools/remove/main/common.h | 43 +++++- .../program/kevux/tools/remove/main/common/print.c | 6 +- .../program/kevux/tools/remove/main/common/print.h | 4 +- .../program/kevux/tools/remove/main/common/type.c | 4 +- .../c/program/kevux/tools/remove/main/convert.c | 62 ++------- .../c/program/kevux/tools/remove/main/convert.h | 38 +---- 7 files changed, 128 insertions(+), 184 deletions(-) diff --git a/sources/c/program/kevux/tools/remove/main/common.c b/sources/c/program/kevux/tools/remove/main/common.c index 14578b6..a6791d5 100644 --- a/sources/c/program/kevux/tools/remove/main/common.c +++ b/sources/c/program/kevux/tools/remove/main/common.c @@ -332,51 +332,8 @@ extern "C" { } } - if (main->program.parameters.array[kt_remove_parameter_group_e].result & f_console_result_found_e) { - if (main->program.parameters.array[kt_remove_parameter_group_e].result & f_console_result_value_e) { - total_arguments = main->program.parameters.array[kt_remove_parameter_group_e].values.used; - - if (main->program.parameters.array[kt_remove_parameter_group_e].locations.used != total_arguments) { - main->setting.state.status = F_status_set_error(F_parameter); - - kt_remove_print_error_parameter_missing_value_requires_amount(&main->program.error, f_console_symbol_long_normal_s, kt_remove_long_group_s, 1); - - return; - } - - main->setting.state.status = f_memory_array_increase_by(total_arguments, sizeof(uint32_t), (void **) &main->setting.groups.array, &main->setting.groups.used, &main->setting.groups.size); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_memory_array_increase_by)); - - return; - } - - for (i = 0; i < total_arguments; ++i) { - - index = main->program.parameters.array[kt_remove_parameter_group_e].values.array[i]; - - main->setting.groups.array[main->setting.groups.used] = kt_remove_get_id_group(main, main->program.parameters.arguments.array[index]); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(kt_remove_get_id_group)); - - return; - } - - ++main->setting.groups.used; - } // for - - main->setting.flag |= kt_remove_main_flag_group_d | kt_remove_main_flag_option_used_d; - } - else { - main->setting.state.status = F_status_set_error(F_parameter); - - kt_remove_print_error_parameter_missing_value_requires_amount(&main->program.error, f_console_symbol_long_normal_s, kt_remove_long_group_s, 1); - - return; - } - } + kt_remove_setting_load_id(main, &main->program.parameters.array[kt_remove_parameter_group_e], &main->setting.groups, kt_remove_long_group_s, kt_remove_main_flag_group_d); + if (F_status_is_error(main->setting.state.status)) return; if (main->program.parameters.array[kt_remove_parameter_mode_e].result & f_console_result_found_e) { if (main->program.parameters.array[kt_remove_parameter_mode_e].result & f_console_result_value_e) { @@ -465,51 +422,8 @@ extern "C" { } } - if (main->program.parameters.array[kt_remove_parameter_user_e].result & f_console_result_found_e) { - if (main->program.parameters.array[kt_remove_parameter_user_e].result & f_console_result_value_e) { - total_arguments = main->program.parameters.array[kt_remove_parameter_user_e].values.used; - - if (main->program.parameters.array[kt_remove_parameter_user_e].locations.used != total_arguments) { - main->setting.state.status = F_status_set_error(F_parameter); - - kt_remove_print_error_parameter_missing_value_requires_amount(&main->program.error, f_console_symbol_long_normal_s, kt_remove_long_user_s, 1); - - return; - } - - main->setting.state.status = f_memory_array_increase_by(total_arguments, sizeof(uint32_t), (void **) &main->setting.users.array, &main->setting.users.used, &main->setting.users.size); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_memory_array_increase_by)); - - return; - } - - for (i = 0; i < total_arguments; ++i) { - - index = main->program.parameters.array[kt_remove_parameter_user_e].values.array[i]; - - main->setting.users.array[main->setting.users.used] = kt_remove_get_id_user(main, main->program.parameters.arguments.array[index]); - - if (F_status_is_error(main->setting.state.status)) { - kt_remove_print_error(&main->program.error, macro_kt_remove_f(kt_remove_get_id_user)); - - return; - } - - ++main->setting.users.used; - } // for - - main->setting.flag |= kt_remove_main_flag_user_d | kt_remove_main_flag_option_used_d; - } - else { - main->setting.state.status = F_status_set_error(F_parameter); - - kt_remove_print_error_parameter_missing_value_requires_amount(&main->program.error, f_console_symbol_long_normal_s, kt_remove_long_user_s, 1); - - return; - } - } + kt_remove_setting_load_id(main, &main->program.parameters.array[kt_remove_parameter_user_e], &main->setting.users, kt_remove_long_user_s, kt_remove_main_flag_user_d); + if (F_status_is_error(main->setting.state.status)) return; if (main->program.parameters.array[kt_remove_parameter_prompt_e].result & f_console_result_found_e) { if (main->program.parameters.array[kt_remove_parameter_prompt_e].result & f_console_result_value_e) { @@ -602,6 +516,67 @@ extern "C" { } #endif // _di_kt_remove_setting_load_ +#ifndef _di_kt_remove_setting_load_id_ + void kt_remove_setting_load_id(kt_remove_main_t * const main, f_console_parameter_t * const parameter, f_ids_t * const ids, const f_string_static_t name, const uint64_t flag) { + + if (!main) return; + + if (!parameter || !ids) { + main->setting.state.status = F_status_set_error(F_parameter); + + kt_remove_print_error(&main->program.error, macro_kt_remove_f(kt_remove_setting_load)); + + return; + } + + if (parameter->result & f_console_result_found_e) { + if (parameter->result & f_console_result_value_e) { + const f_number_unsigned_t total_arguments = parameter->values.used; + + if (parameter->locations.used != total_arguments) { + main->setting.state.status = F_status_set_error(F_parameter); + + kt_remove_print_error_parameter_missing_value_requires_amount(&main->program.error, f_console_symbol_long_normal_s, name, 1); + + return; + } + + main->setting.state.status = f_memory_array_increase_by(total_arguments, sizeof(f_id_t), (void **) &ids->array, &ids->used, &ids->size); + + if (F_status_is_error(main->setting.state.status)) { + kt_remove_print_error(&main->program.error, macro_kt_remove_f(f_memory_array_increase_by)); + + return; + } + + for (f_number_unsigned_t i = 0; i < total_arguments; ++i) { + + ids->array[ids->used] = kt_remove_get_id(main, main->program.parameters.arguments.array[parameter->values.array[i]], F_false); + + if (F_status_is_error(main->setting.state.status)) { + kt_remove_print_error(&main->program.error, macro_kt_remove_f(kt_remove_get_id)); + + return; + } + + ++ids->used; + } // for + + main->setting.flag |= flag | kt_remove_main_flag_option_used_d; + } + else { + main->setting.state.status = F_status_set_error(F_parameter); + + kt_remove_print_error_parameter_missing_value_requires_amount(&main->program.error, f_console_symbol_long_normal_s, name, 1); + + return; + } + } + + main->setting.state.status = F_okay; + } +#endif // _di_kt_remove_setting_load_id_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/sources/c/program/kevux/tools/remove/main/common.h b/sources/c/program/kevux/tools/remove/main/common.h index 64ba7a6..71bed6b 100644 --- a/sources/c/program/kevux/tools/remove/main/common.h +++ b/sources/c/program/kevux/tools/remove/main/common.h @@ -21,8 +21,6 @@ extern "C" { * * This prints error messages as appropriate. * - * If either main or setting is NULL, then this immediately returns without doing anything. - * * @param arguments * The parameters passed to the process (often referred to as command line arguments). * @param main @@ -40,16 +38,53 @@ extern "C" { * * Errors (with error bit) from: f_console_parameter_process(). * Errors (with error bit) from: f_file_stream_open(). - * Errors (with error bit) from: f_string_dynamics_resize(). + * Errors (with error bit) from: f_memory_array_increase_by(). * * @see f_console_parameter_process() * @see f_file_stream_open() - * @see f_string_dynamics_resize() + * @see f_memory_array_increase_by() */ #ifndef _di_kt_remove_setting_load_ extern void kt_remove_setting_load(const f_console_arguments_t arguments, kt_remove_main_t * const main); #endif // _di_kt_remove_setting_load_ +/** + * Perform the standard program setting load process, specifically for the UID and GID. + * + * This prints error messages as appropriate. + * + * @param main + * The main program and settings data. + * + * The setting.flag has kt_remove_flag_option_used_d forcibly cleared on the start of this function. + * + * Must not be NULL. + * + * This alters setting.status: + * F_okay on success. + * + * F_parameter (with error bit) on parameter error. + * + * Errors (with error bit) from: f_memory_array_increase_by(). + * Errors (with error bit) from: kt_remove_get_id(). + * @param parameter + * The console parameter, which should represent either the group parameter or the user parameter. + * + * Must not be NULL. + * @param ids + * The array of group IDs or user IDs. + * + * Must not be NULL. + * @param flag + * The flags to assign when the group or user is loaded. + * + * @see f_memory_array_increase_by() + * @see kt_remove_get_id() + */ +#ifndef _di_kt_remove_setting_load_id_ + extern void kt_remove_setting_load_id(kt_remove_main_t * const main, f_console_parameter_t * const parameter, f_ids_t * const ids, const f_string_static_t name, const uint64_t flag); +#endif // _di_kt_remove_setting_load_id_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/sources/c/program/kevux/tools/remove/main/common/print.c b/sources/c/program/kevux/tools/remove/main/common/print.c index 4233727..01e496c 100644 --- a/sources/c/program/kevux/tools/remove/main/common/print.c +++ b/sources/c/program/kevux/tools/remove/main/common/print.c @@ -27,8 +27,10 @@ extern "C" { "fll_program_parameter_process_verbosity", "kt_remove_convert_date_relative", "kt_remove_convert_timezone", - "kt_remove_get_id_group", - "kt_remove_get_id_user", + "kt_remove_dates_resize", + "kt_remove_get_id", + "kt_remove_modes_resize", + "kt_remove_setting_load", }; #endif // _di_kt_remove_f_a_ diff --git a/sources/c/program/kevux/tools/remove/main/common/print.h b/sources/c/program/kevux/tools/remove/main/common/print.h index 5c2c009..1611454 100644 --- a/sources/c/program/kevux/tools/remove/main/common/print.h +++ b/sources/c/program/kevux/tools/remove/main/common/print.h @@ -61,9 +61,9 @@ extern "C" { kt_remove_f_kt_remove_convert_date_relative_e, kt_remove_f_kt_remove_convert_timezone_e, kt_remove_f_kt_remove_dates_resize_e, - kt_remove_f_kt_remove_get_id_group_e, - kt_remove_f_kt_remove_get_id_user_e, + kt_remove_f_kt_remove_get_id_e, kt_remove_f_kt_remove_modes_resize_e, + kt_remove_f_kt_remove_setting_load_e, }; // enum #endif // _di_kt_remove_f_e_ diff --git a/sources/c/program/kevux/tools/remove/main/common/type.c b/sources/c/program/kevux/tools/remove/main/common/type.c index af691fc..050040b 100644 --- a/sources/c/program/kevux/tools/remove/main/common/type.c +++ b/sources/c/program/kevux/tools/remove/main/common/type.c @@ -41,8 +41,8 @@ extern "C" { f_memory_array_resize(0, sizeof(kt_remove_date_t), (void **) &setting->updated.array, &setting->updated.used, &setting->updated.size); f_memory_array_resize(0, sizeof(kt_remove_mode_t), (void **) &setting->modes.array, &setting->modes.used, &setting->modes.size); - f_memory_array_resize(0, sizeof(uint32_t), (void **) &setting->groups.array, &setting->groups.used, &setting->groups.size); - f_memory_array_resize(0, sizeof(uint32_t), (void **) &setting->users.array, &setting->users.used, &setting->users.size); + f_memory_array_resize(0, sizeof(f_gid_t), (void **) &setting->groups.array, &setting->groups.used, &setting->groups.size); + f_memory_array_resize(0, sizeof(f_uid_t), (void **) &setting->users.array, &setting->users.used, &setting->users.size); } #endif // _di_kt_remove_setting_delete_ diff --git a/sources/c/program/kevux/tools/remove/main/convert.c b/sources/c/program/kevux/tools/remove/main/convert.c index f5f4bfc..07d00bd 100644 --- a/sources/c/program/kevux/tools/remove/main/convert.c +++ b/sources/c/program/kevux/tools/remove/main/convert.c @@ -425,8 +425,8 @@ extern "C" { } #endif // _di_kt_remove_convert_date_relative_ -#ifndef _di_kt_remove_get_id_group_ - uint32_t kt_remove_get_id_group(kt_remove_main_t * const main, const f_string_static_t buffer) { +#ifndef _di_kt_remove_get_id_ + f_id_t kt_remove_get_id(kt_remove_main_t * const main, const f_string_static_t buffer, const uint8_t is_user) { if (!main) return 0; @@ -441,9 +441,11 @@ extern "C" { main->setting.state.status = f_rip_dynamic_nulless(buffer, &main->cache.buffer); if (F_status_is_error_not(main->setting.state.status)) { - gid_t gid = 0; + f_id_t id = 0; - main->setting.state.status = f_account_group_id_by_name(main->cache.buffer, &gid); + main->setting.state.status = is_user + ? f_account_id_by_name(main->cache.buffer, &id) + : f_account_group_id_by_name(main->cache.buffer, &id); if (F_status_is_error_not(main->setting.state.status)) { if (F_status_set_fine(main->setting.state.status) == F_exist_not) { @@ -452,7 +454,7 @@ extern "C" { else { main->setting.state.status = F_okay; - return (uint32_t) gid; + return id; } } } @@ -464,58 +466,12 @@ extern "C" { else { main->setting.state.status = F_okay; - return (uint32_t) number; + return (f_id_t) number; } return 0; } -#endif // _di_kt_remove_get_id_group_ - -#ifndef _di_kt_remove_get_id_user_ - uint32_t kt_remove_get_id_user(kt_remove_main_t * const main, const f_string_static_t buffer) { - - if (!main) return 0; - - f_number_unsigned_t number = 0; - - main->setting.state.status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, buffer, &number); - - if (F_status_is_error(main->setting.state.status)) { - if (F_status_set_fine(main->setting.state.status) == F_number) { - main->cache.buffer.used = 0; - - main->setting.state.status = f_rip_dynamic_nulless(buffer, &main->cache.buffer); - - if (F_status_is_error_not(main->setting.state.status)) { - f_uid_t uid = f_uid_t_initialize; - - main->setting.state.status = f_account_id_by_name(main->cache.buffer, &uid); - - if (F_status_is_error_not(main->setting.state.status)) { - if (F_status_set_fine(main->setting.state.status) == F_exist_not) { - main->setting.state.status = F_status_set_error(F_exist_not); - } - else { - main->setting.state.status = F_okay; - - return (uint32_t) uid; - } - } - } - } - } - else if (number > F_type_size_32_unsigned_d) { - main->setting.state.status = F_status_set_error(F_number_too_large); - } - else { - main->setting.state.status = F_okay; - - return (uint32_t) number; - } - - return 0; - } -#endif // _di_kt_remove_get_id_user_ +#endif // _di_kt_remove_get_id_ #ifndef _di_kt_remove_get_mode_ mode_t kt_remove_get_mode(kt_remove_main_t * const main, const f_string_static_t buffer) { diff --git a/sources/c/program/kevux/tools/remove/main/convert.h b/sources/c/program/kevux/tools/remove/main/convert.h index 217e394..1e41ea7 100644 --- a/sources/c/program/kevux/tools/remove/main/convert.h +++ b/sources/c/program/kevux/tools/remove/main/convert.h @@ -113,7 +113,7 @@ extern "C" { #endif // _di_kt_remove_convert_date_relative_ /** - * Convert the string from a string representation of an ID or a group name into the numeric representation of that ID or group name. + * Convert the string from a string representation of an ID or a user/group name into the numeric representation of that ID or user/group name. * * @param main * The main program and settings data. @@ -130,41 +130,17 @@ extern "C" { * Errors (with error bit) from: fl_conversion_dynamic_to_unsigned_detect(). * @param buffer * A string containing group name or ID. + * @param is_user + * F_true to designate that this is getting a user name or ID. + * F_false to designate that this is getting a group name or ID. * * @see f_account_group_id_by_name() * @see f_rip_dynamic_nulless() * @see fl_conversion_dynamic_to_unsigned_detect() */ -#ifndef _di_kt_remove_get_id_group_ - extern uint32_t kt_remove_get_id_group(kt_remove_main_t * const main, const f_string_static_t buffer); -#endif // _di_kt_remove_get_id_group_ - -/** - * Convert the string from a string representation of an ID or a user name into the numeric representation of that ID or user name. - * - * @param main - * The main program and settings data. - * - * Must not be NULL. - * - * This alters main.setting.state.status: - * F_okay on success. - * - * F_number_too_large (with error bit) if the given ID is too large. - * - * Errors (with error bit) from: f_account_group_id_by_name(). - * Errors (with error bit) from: f_rip_dynamic_nulless(). - * Errors (with error bit) from: fl_conversion_dynamic_to_unsigned_detect(). - * @param buffer - * A string containing group name or ID. - * - * @see f_account_id_by_name() - * @see f_rip_dynamic_nulless() - * @see fl_conversion_dynamic_to_unsigned_detect() - */ -#ifndef _di_kt_remove_get_id_user_ - extern uint32_t kt_remove_get_id_user(kt_remove_main_t * const main, const f_string_static_t buffer); -#endif // _di_kt_remove_get_id_user_ +#ifndef _di_kt_remove_get_id_ + extern f_id_t kt_remove_get_id(kt_remove_main_t * const main, const f_string_static_t buffer, const uint8_t is_user); +#endif // _di_kt_remove_get_id_ /** * Convert the string from a string representation of a mode into the numeric representation of that mode. -- 1.8.3.1