summaryrefslogtreecommitdiff
path: root/get_rload.c
blob: 64e4a3e33f0ebde7c3759f06495850bf505016c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "xload.h"

/* Not all OS supports get_rload
   steal the STUB idea from get_load
 */
#ifndef HAVE_PROTOCOLS_RWHOD_H
#define RLOADSTUB
#endif

#ifdef RLOADSTUB
void GetRLoadPoint(
    Widget	w,		/* unused */
    XtPointer	closure,	/* unused */
    XtPointer	call_data)	/* pointer to (double) return value */

{
  *(double *)call_data = 1.0;
}
#else  /* RLOADSTUB */

#include <protocols/rwhod.h>
#ifndef _PATH_RWHODIR
#define _PATH_RWHODIR "/var/spool/rwho"
#endif

#define WHDRSIZE        ((int)(sizeof (buf) - sizeof (buf.wd_we)))

void GetRLoadPoint(
    Widget	w,		/* unused */
    XtPointer	closure,	/* unused */
    XtPointer	call_data)	/* pointer to (double) return value */
{
  int f;
  static char *fname = NULL;
  static struct whod buf;
  int cc;

  *(double *)call_data = 0.0; /* to be on the safe side */

  if (fname == NULL) {
#ifdef HAVE_ASPRINTF
    if (asprintf(&fname, "%s/whod.%s", _PATH_RWHODIR, resources.remote) < 0) {
      perror("GetRLoadPoint: asprintf() failed");
      exit(1);
    }
#else
    if ((fname = malloc(strlen(_PATH_RWHODIR)+strlen("/whod.")+strlen(resources.remote)+1)) == NULL) {
      fprintf(stderr,"GetRLoadPoint: malloc() failed\n");
      exit(1);
    }
    strcpy(fname,_PATH_RWHODIR);
    strcat(fname,"/whod.");
    strcat(fname,resources.remote);
#endif
  }
  if ((f = open(fname, O_RDONLY, 0)) < 0)
    return;

  cc = read(f, &buf, sizeof(buf));
  close(f);
  if (cc < WHDRSIZE)
    return;

  *(double *)call_data = buf.wd_loadav[0] / 100.0;
}

#endif  /* RLOADSTUB */