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