ftp.nice.ch/pub/next/tools/business/USDebtWatch.0.3.NIHS.bs.tar.gz#/USDebtWatch/unix-version/xdebt.c

This is xdebt.c in view mode; [Download] [Up]

/* Displays the current national debt in a window, updated once a second.
   Public domain.  By Jamie Zawinski <jwz@lucid.com>.
   cc -O -o xdebt xdebt.c -lXaw -lXmu -lXt -lX11
 */

#include <stdio.h>
#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include <X11/Label.h>

/* The US National Debt, and its rate of increase as of December 31, 1988
   (in dollars per second) according to the 1989 Survey of Current Business.
.  These values are floats because they're >42 bits.
 */
typedef double debt_t;
#define DEBT  2707284000000.0
#define DELTA 7673.015
static struct tm debt_tm = { 0, 0, 0, 31, 11, 88 };

debt_t
national_debt_at (now)
     time_t now;
{
  time_t debt_date = timelocal (&debt_tm);
  time_t seconds_since_then = now - debt_date;
  debt_t delta_since_then = DELTA * seconds_since_then;
  return DEBT + delta_since_then;
}

extern char *index ();

void
debt_to_string (debt, s)
     debt_t debt;
     char *s;
{
  int L, i = 0;
  char buf [255];
  char *b = buf;
  sprintf (b, "%lf", debt);
  b = index (b, '.') - 1;
  L = b - buf;
  s += L + (L / 3);
  *(s+1) = 0;
  i = -1;
  while (1)
    {
      if (++i == 3) i = 0, *s-- = ',';
      if (b < buf) return;
      *s-- = *b--;
    }
}


static int update;

static char *defaults[] = {
  "*Label.font:	*-times-bold-r-*-*-*-240-*-*-*-*-*-*",
  "*title:	Current U.S. National Debt",
  "*update:	1",
  NULL
};

static XrmOptionDescRec options [] = {
  { "-update",	"*update",	XrmoptionSepArg, 0 }
};


static void
get_update (dpy)	/* Easier than making a subclass... */
     Display *dpy;
{
  char *name, *class, *type, buf1[255], buf2[255];
  XrmValue value;
  XtGetApplicationNameAndClass (dpy, &name, &class);
  sprintf (buf1, "%s.update", name);
  sprintf (buf2, "%s.Update", class);
  XrmGetResource (XtDatabase (dpy), buf1, buf2, &type, &value);
  if (sscanf (value.addr, " %d ", &update) == 0 || update < 1)
    {
      fprintf (stderr, "%s: update must be a positive integer, not \"%s\"\n",
	       name, value.addr);
      update = 1;
    }
}


static void
timer (w, id)
     Widget w;
     XtIntervalId id;
{
  char buf [255];
  Arg av [10];
  int ac = 0;
  buf [0] = '$';
  debt_to_string (national_debt_at (time ((time_t *) 0)), buf + 1);
  XtSetArg (av [ac], XtNlabel, buf); ac++;
  XtSetValues (w, av, ac);
  XtAppAddTimeOut (XtWidgetToApplicationContext (w), update * 1000, timer, w);
}


void
main (argc, argv)
     int argc;
     char **argv;
{
  XtAppContext app;
  Widget shell = XtAppInitialize (&app, "XDebt", options, XtNumber (options),
				  &argc, argv, defaults, NULL, 0);
  Widget label;
  XEvent event;
  if (argc > 1)
    {
      fprintf (stderr, "%s: unknown option %s\n", argv[0], argv[1]);
      fprintf (stderr, "options: -update <seconds>\n\t -font <font>\n");
      exit (-1);
    }
  label = XtCreateManagedWidget ("label", labelWidgetClass, shell, NULL, 0);
  get_update (XtDisplay (label));
  timer (label, 0);
  XtRealizeWidget (shell);
  XtAppMainLoop (app);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.