patch-src-pstree.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. --- src/pstree.c.orig 2012-02-19 23:07:14 UTC
  2. +++ src/pstree.c
  3. @@ -37,6 +37,9 @@
  4. #include <term.h>
  5. #include <termios.h>
  6. #include <langinfo.h>
  7. +#if defined __FreeBSD__ || defined __DragonFly__
  8. +#include <limits.h>
  9. +#endif
  10. #include <assert.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. @@ -609,7 +612,11 @@ static void read_proc(void)
  14. struct dirent *de;
  15. FILE *file;
  16. struct stat st;
  17. +#if defined __FreeBSD__ || defined __DragonFly__
  18. + char *path, comm[COMM_LEN + 1];
  19. +#else
  20. char *path, *comm;
  21. +#endif
  22. char *buffer;
  23. size_t buffer_size;
  24. char readbuf[BUFSIZ + 1];
  25. @@ -642,7 +649,11 @@ static void read_proc(void)
  26. if ((pid = (pid_t) atoi(de->d_name)) != 0) {
  27. if (! (path = malloc(strlen(PROC_BASE) + strlen(de->d_name) + 10)))
  28. exit(2);
  29. +#if defined __FreeBSD__ || defined __DragonFly__
  30. + sprintf(path, "%s/%d/status", PROC_BASE, pid);
  31. +#else
  32. sprintf(path, "%s/%d/stat", PROC_BASE, pid);
  33. +#endif
  34. if ((file = fopen(path, "r")) != NULL) {
  35. empty = 0;
  36. sprintf(path, "%s/%d", PROC_BASE, pid);
  37. @@ -657,6 +668,29 @@ static void read_proc(void)
  38. perror(path);
  39. exit(1);
  40. }
  41. +#if defined __FreeBSD__ || defined __DragonFly__
  42. + /* Read comm and ppid */
  43. + if (fscanf(file, "%s %*d %d", readbuf, &ppid) == 2) {
  44. + tmpptr = readbuf;
  45. + size = 0;
  46. + /* Unescape comm */
  47. + do {
  48. + if (*tmpptr == '\\')
  49. + {
  50. + char tmp = tmpptr[4];
  51. + tmpptr[4] = 0;
  52. + comm[size++] = strtol(++tmpptr, NULL, 8);
  53. + tmpptr += 2;
  54. + tmpptr[1] = tmp;
  55. + }
  56. + else
  57. + comm[size++] = *tmpptr;
  58. + } while (*++tmpptr);
  59. + comm[size] = 0;
  60. + /* Balance braces */
  61. + {
  62. + {
  63. +#else
  64. size = fread(readbuf, 1, BUFSIZ, file);
  65. if (ferror(file) == 0) {
  66. readbuf[size] = 0;
  67. @@ -711,6 +745,7 @@ static void read_proc(void)
  68. (void) closedir(taskdir);
  69. }
  70. free(taskpath);
  71. +#endif /*__FreeBSD__ || __DragonFly__ */
  72. if (!print_args)
  73. #ifdef WITH_SELINUX
  74. add_proc(comm, pid, ppid, st.st_uid, NULL, 0, 0, scontext);