EnvScreen.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. htop - EnvScreen.c
  3. (C) 2015,2016 Michael Klein
  4. (C) 2016,2017 Hisham H. Muhammad
  5. (C) 2020,2021 htop dev team
  6. Released under the GNU GPLv2+, see the COPYING file
  7. in the source distribution for its full text.
  8. */
  9. #include "config.h" // IWYU pragma: keep
  10. #include "EnvScreen.h"
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "Macros.h"
  14. #include "Panel.h"
  15. #include "Platform.h"
  16. #include "ProvideCurses.h"
  17. #include "Vector.h"
  18. #include "XUtils.h"
  19. EnvScreen* EnvScreen_new(Process* process) {
  20. EnvScreen* this = xMalloc(sizeof(EnvScreen));
  21. Object_setClass(this, Class(EnvScreen));
  22. return (EnvScreen*) InfoScreen_init(&this->super, process, NULL, LINES - 2, " ");
  23. }
  24. void EnvScreen_delete(Object* this) {
  25. free(InfoScreen_done((InfoScreen*)this));
  26. }
  27. static void EnvScreen_draw(InfoScreen* this) {
  28. InfoScreen_drawTitled(this, "Environment of process %d - %s", Process_getPid(this->process), Process_getCommand(this->process));
  29. }
  30. static void EnvScreen_scan(InfoScreen* this) {
  31. Panel* panel = this->display;
  32. int idx = MAXIMUM(Panel_getSelectedIndex(panel), 0);
  33. Panel_prune(panel);
  34. char* env = Platform_getProcessEnv(Process_getPid(this->process));
  35. if (env) {
  36. for (const char* p = env; *p; p = strrchr(p, 0) + 1)
  37. InfoScreen_addLine(this, p);
  38. free(env);
  39. }
  40. else {
  41. InfoScreen_addLine(this, "Could not read process environment.");
  42. }
  43. Vector_insertionSort(this->lines);
  44. Vector_insertionSort(panel->items);
  45. Panel_setSelected(panel, idx);
  46. }
  47. const InfoScreenClass EnvScreen_class = {
  48. .super = {
  49. .extends = Class(Object),
  50. .delete = EnvScreen_delete
  51. },
  52. .scan = EnvScreen_scan,
  53. .draw = EnvScreen_draw
  54. };