get_rload.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdio.h>
  2. #include <X11/Intrinsic.h>
  3. #include <fcntl.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include "xload.h"
  7. /* Not all OS supports get_rload
  8. steal the STUB idea from get_load
  9. */
  10. #if defined(QNX4) || defined(__CYGWIN__)
  11. #define RLOADSTUB
  12. #endif
  13. #ifdef RLOADSTUB
  14. void GetRLoadPoint(
  15. Widget w, /* unused */
  16. XtPointer closure, /* unused */
  17. XtPointer call_data) /* pointer to (double) return value */
  18. {
  19. *(double *)call_data = 1.0;
  20. }
  21. #else /* RLOADSTUB */
  22. #include <protocols/rwhod.h>
  23. #ifndef _PATH_RWHODIR
  24. #define _PATH_RWHODIR "/var/spool/rwho"
  25. #endif
  26. #define WHDRSIZE ((int)(sizeof (buf) - sizeof (buf.wd_we)))
  27. void GetRLoadPoint(
  28. Widget w, /* unused */
  29. XtPointer closure, /* unused */
  30. XtPointer call_data) /* pointer to (double) return value */
  31. {
  32. int f;
  33. static char *fname = NULL;
  34. static struct whod buf;
  35. int cc;
  36. *(double *)call_data = 0.0; /* to be on the safe side */
  37. if (fname == NULL) {
  38. if ((fname = malloc(strlen(_PATH_RWHODIR)+strlen("/whod.")+strlen(resources.remote)+1)) == NULL) {
  39. fprintf(stderr,"GetRLoadPoint: malloc() failed\n");
  40. exit(1);
  41. }
  42. strcpy(fname,_PATH_RWHODIR);
  43. strcat(fname,"/whod.");
  44. strcat(fname,resources.remote);
  45. }
  46. if ((f = open(fname, O_RDONLY, 0)) < 0)
  47. return;
  48. cc = read(f, &buf, sizeof(buf));
  49. close(f);
  50. if (cc < WHDRSIZE)
  51. return;
  52. *(double *)call_data = buf.wd_loadav[0] / 100.0;
  53. }
  54. #endif /* RLOADSTUB */