esh_deinit.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "esh_deinit.h"
  2. void EShExit(int exit_code) {
  3. EShDeinit();
  4. exit(exit_code);
  5. }
  6. void EShDeinit() {
  7. EShSaveConfigFile();
  8. EShSaveHistoryFile();
  9. EShDeinitInfo();
  10. printf("\nESh: Goodbye!\n");
  11. }
  12. void EShDeinitInfo() {
  13. // TO BE IMPLEMENTED
  14. }
  15. void EShSaveConfigFile() {
  16. FILE* file = fopen(esh_info_global->config_file_path, "w");
  17. if (!file) {
  18. printf("\nESh: error, cannot open config file %s, settings may not be saved...\n", esh_info_global->config_file_path);
  19. return;
  20. }
  21. fprintf(file, "history_path=%s\n", esh_info_global->history_file_path);
  22. fprintf(file, "history_limit=%d\n", esh_info_global->history_limit);
  23. fprintf(file, "max_active_jobs=%d\n", esh_info_global->max_jobs_number);
  24. fprintf(file, "max_command_length=%d\n", esh_info_global->max_command_length);
  25. fclose(file);
  26. }
  27. void EShSaveHistoryFile() {
  28. FILE* file = fopen(esh_info_global->history_file_path, "w");
  29. if (!file) {
  30. printf("\nESh: error, cannot open history file %s, commands may not be saved...\n", esh_info_global->history_file_path);
  31. return;
  32. }
  33. for (int i = 0; i < esh_info_global->history_command_list_size; ++i) {
  34. fprintf(file, "%s\n", esh_info_global->history_command_list[i]);
  35. }
  36. fclose(file);
  37. }
  38. void EShDeinitJob(EShJob* job) {
  39. if (!job)
  40. return;
  41. if (job->stdin_fd != -1) {
  42. close(job->stdin_fd);
  43. job->stdin_fd = -1;
  44. }
  45. if (job->stdout_fd != -1) {
  46. close(job->stdout_fd);
  47. job->stdout_fd = -1;
  48. }
  49. if (job->stderr_fd != -1) {
  50. close(job->stderr_fd);
  51. job->stderr_fd = -1;
  52. }
  53. free(job->command);
  54. for (int i = 0; i < esh_info_global->max_command_tokens; ++i) {
  55. if (job->command_tokens[i]) {
  56. free(job->command_tokens[i]);
  57. }
  58. }
  59. free(job->command_tokens);
  60. }
  61. void EShFreeJobsList(EShJob* jobs_list) {
  62. for (int i = 0; i < esh_info_global->max_jobs_number; ++i) {
  63. EShDeinitJob(jobs_list + i);
  64. }
  65. free(jobs_list);
  66. }