12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include "esh_deinit.h"
- void EShExit(int exit_code) {
- EShDeinit();
- exit(exit_code);
- }
- void EShDeinit() {
- EShSaveConfigFile();
- EShSaveHistoryFile();
- EShDeinitInfo();
- printf("\nESh: Goodbye!\n");
- }
- void EShDeinitInfo() {
- // TO BE IMPLEMENTED
- }
- void EShSaveConfigFile() {
- FILE* file = fopen(esh_info_global->config_file_path, "w");
- if (!file) {
- printf("\nESh: error, cannot open config file %s, settings may not be saved...\n", esh_info_global->config_file_path);
- return;
- }
- fprintf(file, "history_path=%s\n", esh_info_global->history_file_path);
- fprintf(file, "history_limit=%d\n", esh_info_global->history_limit);
- fprintf(file, "max_active_jobs=%d\n", esh_info_global->max_jobs_number);
- fprintf(file, "max_command_length=%d\n", esh_info_global->max_command_length);
- fclose(file);
- }
- void EShSaveHistoryFile() {
- FILE* file = fopen(esh_info_global->history_file_path, "w");
- if (!file) {
- printf("\nESh: error, cannot open history file %s, commands may not be saved...\n", esh_info_global->history_file_path);
- return;
- }
- for (int i = 0; i < esh_info_global->history_command_list_size; ++i) {
- fprintf(file, "%s\n", esh_info_global->history_command_list[i]);
- }
- fclose(file);
- }
- void EShDeinitJob(EShJob* job) {
- if (!job)
- return;
- if (job->stdin_fd != -1) {
- close(job->stdin_fd);
- job->stdin_fd = -1;
- }
- if (job->stdout_fd != -1) {
- close(job->stdout_fd);
- job->stdout_fd = -1;
- }
-
- if (job->stderr_fd != -1) {
- close(job->stderr_fd);
- job->stderr_fd = -1;
- }
- free(job->command);
- for (int i = 0; i < esh_info_global->max_command_tokens; ++i) {
- if (job->command_tokens[i]) {
- free(job->command_tokens[i]);
- }
- }
- free(job->command_tokens);
- }
- void EShFreeJobsList(EShJob* jobs_list) {
- for (int i = 0; i < esh_info_global->max_jobs_number; ++i) {
- EShDeinitJob(jobs_list + i);
- }
- free(jobs_list);
- }
|