get_rload.c revision 48e69166
1fd0c672fSmrg/* $XFree86: xc/programs/xload/get_rload.c,v 1.4 2002/01/07 20:38:31 dawes Exp $ */
2fd0c672fSmrg
3fd0c672fSmrg#include <stdio.h>
4fd0c672fSmrg#include <X11/Intrinsic.h>
5fd0c672fSmrg#include <fcntl.h>
6fd0c672fSmrg#include <stdlib.h>
7fd0c672fSmrg#include <unistd.h>
8fd0c672fSmrg#include "xload.h"
9fd0c672fSmrg
10fd0c672fSmrg/* Not all OS supports get_rload
11fd0c672fSmrg   steal the STUB idea from get_load
12fd0c672fSmrg */
13fd0c672fSmrg#if defined(QNX4) || defined(__CYGWIN__)
14fd0c672fSmrg#define RLOADSTUB
15fd0c672fSmrg#endif
16fd0c672fSmrg
17fd0c672fSmrg#ifdef RLOADSTUB
1848e69166Smrgvoid GetRLoadPoint(
1948e69166Smrg    Widget	w,		/* unused */
2048e69166Smrg    XtPointer	closure,	/* unused */
2148e69166Smrg    XtPointer	call_data)	/* pointer to (double) return value */
22fd0c672fSmrg
23fd0c672fSmrg{
24fd0c672fSmrg  *(double *)call_data = 1.0;
25fd0c672fSmrg}
26fd0c672fSmrg#else  /* RLOADSTUB */
27fd0c672fSmrg
28fd0c672fSmrg#include <protocols/rwhod.h>
29fd0c672fSmrg#ifndef _PATH_RWHODIR
30fd0c672fSmrg#define _PATH_RWHODIR "/var/spool/rwho"
31fd0c672fSmrg#endif
32fd0c672fSmrg
33fd0c672fSmrg#define WHDRSIZE        ((int)(sizeof (buf) - sizeof (buf.wd_we)))
34fd0c672fSmrg
3548e69166Smrgvoid GetRLoadPoint(
3648e69166Smrg    Widget	w,		/* unused */
3748e69166Smrg    XtPointer	closure,	/* unused */
3848e69166Smrg    XtPointer	call_data)	/* pointer to (double) return value */
39fd0c672fSmrg{
40fd0c672fSmrg  int f;
41fd0c672fSmrg  static char *fname = NULL;
42fd0c672fSmrg  static struct whod buf;
43fd0c672fSmrg  int cc;
44fd0c672fSmrg
45fd0c672fSmrg  *(double *)call_data = 0.0; /* to be on the safe side */
46fd0c672fSmrg
47fd0c672fSmrg  if (fname == NULL) {
48fd0c672fSmrg    if ((fname = malloc(strlen(_PATH_RWHODIR)+strlen("/whod.")+strlen(resources.remote)+1)) == NULL) {
49fd0c672fSmrg      fprintf(stderr,"GetRLoadPoint: malloc() failed\n");
50fd0c672fSmrg      exit(1);
51fd0c672fSmrg    }
52fd0c672fSmrg    strcpy(fname,_PATH_RWHODIR);
53fd0c672fSmrg    strcat(fname,"/whod.");
54fd0c672fSmrg    strcat(fname,resources.remote);
55fd0c672fSmrg  }
56fd0c672fSmrg  if ((f = open(fname, O_RDONLY, 0)) < 0)
57fd0c672fSmrg    return;
58fd0c672fSmrg
59fd0c672fSmrg  cc = read(f, &buf, sizeof(buf));
60fd0c672fSmrg  close(f);
61fd0c672fSmrg  if (cc < WHDRSIZE)
62fd0c672fSmrg    return;
63fd0c672fSmrg
64fd0c672fSmrg  *(double *)call_data = buf.wd_loadav[0] / 100.0;
65fd0c672fSmrg}
66fd0c672fSmrg
67fd0c672fSmrg#endif  /* RLOADSTUB */
68