get_rload.c revision fd0c672f
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
18fd0c672fSmrgvoid GetRLoadPoint( w, closure, call_data )
19fd0c672fSmrg     Widget   w;              /* unused */
20fd0c672fSmrg     XtPointer  closure;        /* unused */
21fd0c672fSmrg     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
33fd0c672fSmrgtypedef struct _XLoadResources {
34fd0c672fSmrg  Boolean show_label;
35fd0c672fSmrg  Boolean use_lights;
36fd0c672fSmrg  String remote;
37fd0c672fSmrg} XLoadResources;
38fd0c672fSmrg
39fd0c672fSmrgextern XLoadResources resources ;
40fd0c672fSmrg
41fd0c672fSmrg#define WHDRSIZE        ((int)(sizeof (buf) - sizeof (buf.wd_we)))
42fd0c672fSmrg
43fd0c672fSmrgvoid GetRLoadPoint( w, closure, call_data )
44fd0c672fSmrg     Widget   w;              /* unused */
45fd0c672fSmrg     XtPointer  closure;        /* unused */
46fd0c672fSmrg     XtPointer  call_data;      /* pointer to (double) return value */
47fd0c672fSmrg
48fd0c672fSmrg{
49fd0c672fSmrg  int f;
50fd0c672fSmrg  static char *fname = NULL;
51fd0c672fSmrg  static struct whod buf;
52fd0c672fSmrg  int cc;
53fd0c672fSmrg
54fd0c672fSmrg  *(double *)call_data = 0.0; /* to be on the safe side */
55fd0c672fSmrg
56fd0c672fSmrg  if (fname == NULL) {
57fd0c672fSmrg    if ((fname = malloc(strlen(_PATH_RWHODIR)+strlen("/whod.")+strlen(resources.remote)+1)) == NULL) {
58fd0c672fSmrg      fprintf(stderr,"GetRLoadPoint: malloc() failed\n");
59fd0c672fSmrg      exit(1);
60fd0c672fSmrg    }
61fd0c672fSmrg    strcpy(fname,_PATH_RWHODIR);
62fd0c672fSmrg    strcat(fname,"/whod.");
63fd0c672fSmrg    strcat(fname,resources.remote);
64fd0c672fSmrg  }
65fd0c672fSmrg  if ((f = open(fname, O_RDONLY, 0)) < 0)
66fd0c672fSmrg    return;
67fd0c672fSmrg
68fd0c672fSmrg  cc = read(f, &buf, sizeof(buf));
69fd0c672fSmrg  close(f);
70fd0c672fSmrg  if (cc < WHDRSIZE)
71fd0c672fSmrg    return;
72fd0c672fSmrg
73fd0c672fSmrg  *(double *)call_data = buf.wd_loadav[0] / 100.0;
74fd0c672fSmrg}
75fd0c672fSmrg
76fd0c672fSmrg#endif  /* RLOADSTUB */
77