formatting, add freebsd support

This commit is contained in:
bch 2018-12-26 18:46:04 +00:00
parent 06227deb08
commit 3b3d39c18c
3 changed files with 139 additions and 121 deletions

View File

@ -1,8 +1,23 @@
OS = FreeBSD
PROG= xmem
VERSION= 1.26
LDFLAGS+= -L/usr/X11R6/lib -lX11 -lXt -lXaw -lXmu
CFLAGS+= -Wall -I/usr/X11R6/include/
OBJECTS= xmem.o get_mem.o MemStripChart.o
FreeBSD_lflag= -lkvm
LDFLAGS+= -L/usr/X11R6/lib -L/usr/local/lib/ -lX11 -lXt -lXaw -lXmu
LDFLAGS+= $($(OS)_lflag)
CFLAGS+= -Wall -I/usr/X11R6/include/ -I/usr/local/include/
OBJECTS= xmem.o MemStripChart.o
FreeBSD_stub = get_mem_fbsd.o
OpenBSD_stub = get_mem.o
OBJECTS += $($(OS)_stub)
all: xmem
xmem.full:
echo full
xmem.debug:
echo full
xmem: $(OBJECTS)
$(CC) -o xmem $(OBJECTS) $(LDFLAGS)
@ -33,6 +48,5 @@ install:
install -c -o root -g bin -m 644 xmem.bit ${PREFIX}/share/${PROG}/xmem.bit
install -c -o root -g bin -m 644 copyright ${PREFIX}/share/${PROG}/copyright
all: xmem
.include <bsd.prog.mk>

View File

@ -1,6 +1,7 @@
/* $Id: get_mem.c,v 1.4 2017/02/26 16:03:48 bch Exp $
/* $Id: get_mem.c,v 1.5 2018/12/26 18:46:04 bch Exp $
*
* Adapted: get memory usage on OpenBSD
* Author: Christian Barthel <bch@vcs.onfire.org>
* Author: Christian Barthel <bch@online.de>
*
* get memory usage, from get_load.c derived
*
@ -9,6 +10,7 @@
* Modified for more recent kernels Helmut Geyer Oct. 1996
*/
#include <sys/param.h> /* DEV_BSIZE MAXCOMLEN PZERO */
#include <sys/sysctl.h>
#include <sys/swap.h>

232
xmem.c
View File

@ -1,7 +1,7 @@
/*
* xmem - display memory/swap usage utility for X
*
* Updated by Christian Barthel <bch@onfire.org>
* Updated by Christian Barthel <bch@online.de>
*
* Copyright 1989 Massachusetts Institute of Technology
*
@ -47,19 +47,19 @@ typedef struct _XLoadResources {
*/
static XrmOptionDescRec options_mem[] = {
{"-scale", "*mem.minScale", XrmoptionSepArg, NULL},
{"-update", "*mem.update", XrmoptionSepArg, NULL},
{"-hl", "*mem.highlight", XrmoptionSepArg, NULL},
{"-highlight", "*mem.highlight", XrmoptionSepArg, NULL},
{"-codecolor", "*mem.codecolor", XrmoptionSepArg, NULL},
{"-cachedcolor", "*mem.cachedcolor", XrmoptionSepArg, NULL},
{"-buffercolor", "*mem.buffercolor", XrmoptionSepArg, NULL},
{"-freecolor", "*mem.freecolor", XrmoptionSepArg, NULL},
{"-swapcolor", "*mem.swapcolor", XrmoptionSepArg, NULL},
{"-jumpscroll", "*mem.jumpScroll", XrmoptionSepArg, NULL},
{"-label", "*label.label", XrmoptionSepArg, NULL},
{"-nolabel", "*showLabel", XrmoptionNoArg, "False"},
{"-lights", "*useLights", XrmoptionNoArg, "True"},
{"-scale", "*mem.minScale", XrmoptionSepArg, NULL},
{"-update", "*mem.update", XrmoptionSepArg, NULL},
{"-hl", "*mem.highlight", XrmoptionSepArg, NULL},
{"-highlight", "*mem.highlight", XrmoptionSepArg, NULL},
{"-codecolor", "*mem.codecolor", XrmoptionSepArg, NULL},
{"-cachedcolor", "*mem.cachedcolor", XrmoptionSepArg, NULL},
{"-buffercolor", "*mem.buffercolor", XrmoptionSepArg, NULL},
{"-freecolor", "*mem.freecolor", XrmoptionSepArg, NULL},
{"-swapcolor", "*mem.swapcolor", XrmoptionSepArg, NULL},
{"-jumpscroll", "*mem.jumpScroll", XrmoptionSepArg, NULL},
{"-label", "*label.label", XrmoptionSepArg, NULL},
{"-nolabel", "*showLabel", XrmoptionNoArg, "False"},
{"-lights", "*useLights", XrmoptionNoArg, "True"},
};
/*
@ -71,7 +71,7 @@ static XrmOptionDescRec options_mem[] = {
static XtResource my_resources_mem[] = {
{"showLabel", XtCBoolean, XtRBoolean, sizeof(Boolean),
Offset(show_label), XtRImmediate, (XtPointer) TRUE},
Offset(show_label), XtRImmediate, (XtPointer) TRUE},
};
#undef Offset
@ -79,7 +79,7 @@ static XtResource my_resources_mem[] = {
static XLoadResources resources;
static XtActionsRec xload_actions[] = {
{ "quit", quit },
{ "quit", quit },
};
static Atom wm_delete_window;
@ -92,38 +92,38 @@ void usage(char *progname)
fprintf (stderr, "usage: %s [-options ...]\n\n", progname);
fprintf (stderr, "where options include:\n");
fprintf (stderr,
" -display dpy X server on which to display\n");
" -display dpy X server on which to display\n");
fprintf (stderr,
" -geometry geom size and location of window\n");
" -geometry geom size and location of window\n");
fprintf (stderr,
" -fn font font to use in label\n");
" -fn font font to use in label\n");
fprintf (stderr,
" -update seconds interval between updates\n");
" -update seconds interval between updates\n");
fprintf (stderr,
" -label string annotation text\n");
" -label string annotation text\n");
fprintf (stderr,
" -bg color background color\n");
" -bg color background color\n");
fprintf (stderr,
" -fg color text color\n");
" -fg color text color\n");
fprintf (stderr,
" -hl color scale color\n");
" -hl color scale color\n");
fprintf (stderr,
" -codecolor color used code and stack memory color\n");
" -codecolor color used code and stack memory color\n");
fprintf (stderr,
" -cachedcolor color used cached memory color\n");
" -cachedcolor color used cached memory color\n");
fprintf (stderr,
" -buffercolor color used buffer memory color\n");
" -buffercolor color used buffer memory color\n");
fprintf (stderr,
" -freecolor color used free memory color\n");
" -freecolor color used free memory color\n");
fprintf (stderr,
" -swapcolor color used swap memory color\n");
" -swapcolor color used swap memory color\n");
fprintf (stderr,
" -nolabel removes the label from above the chart.\n");
" -nolabel removes the label from above the chart.\n");
fprintf (stderr,
" -jumpscroll value number of pixels to scroll on overflow\n");
" -jumpscroll value number of pixels to scroll on overflow\n");
fprintf (stderr,
"The reference line refers to the avaible installed ram\n");
"The reference line refers to the avaible installed ram\n");
fprintf (stderr, "\n");
exit(1);
@ -131,94 +131,96 @@ void usage(char *progname)
int main(int argc, char **argv)
{
XtAppContext app_con;
Widget toplevel, load, pane, label_wid, load_parent;
Arg args[1];
Pixmap icon_pixmap = None;
char *label, host[256];
/* For security reasons, we reset our uid/gid after doing the necessary
system initialization and before calling any X routines. */
if (setgid(getgid()) == -1)
errx(1, "%s: setgid failed: %s\n", argv[0], strerror(errno));
if (setuid(getuid()) == -1)
errx(1, "%s: setuid failed: %s\n", argv[0], strerror(errno));
toplevel = XtAppInitialize(&app_con, "XMem", options_mem, XtNumber(options_mem),
&argc, argv, NULL, NULL, (Cardinal) 0);
if (argc != 1) usage(argv[0]);
XtGetApplicationResources( toplevel, (XtPointer) &resources,
my_resources_mem, XtNumber(my_resources_mem),
NULL, (Cardinal) 0);
/*
* This is a hack so that f.delete will do something useful in this
* single-window application.
*/
XtAppAddActions (app_con, xload_actions, XtNumber(xload_actions));
XtOverrideTranslations(toplevel,
XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));
XtSetArg (args[0], XtNiconPixmap, &icon_pixmap);
XtGetValues(toplevel, args, ONE);
if (icon_pixmap == None) {
XtSetArg(args[0], XtNiconPixmap,
XCreateBitmapFromData(XtDisplay(toplevel),
XtScreen(toplevel)->root,
(char *)xload_bits,
xload_width, xload_height));
XtSetValues (toplevel, args, ONE);
}
if (resources.show_label) {
pane = XtCreateManagedWidget ("paned", panedWidgetClass,
toplevel, NULL, ZERO);
label_wid = XtCreateManagedWidget ("label", labelWidgetClass,
pane, NULL, ZERO);
XtSetArg (args[0], XtNlabel, &label);
XtGetValues(label_wid, args, ONE);
if ( strcmp("label", label) == 0 ) {
(void) XmuGetHostname (host, 255);
XtSetArg (args[0], XtNlabel, host);
XtSetValues (label_wid, args, ONE);
}
load_parent = pane;
}
else
load_parent = toplevel;
load = XtCreateManagedWidget ("mem", memStripChartWidgetClass,
load_parent, NULL, ZERO);
XtAddCallback(load, XtNgetValue, (void*)GetMemLoadPoint, NULL);
XtRealizeWidget (toplevel);
wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW",
False);
(void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
&wm_delete_window, 1);
XtAppContext app_con;
Widget toplevel, load, pane, label_wid, load_parent;
Arg args[1];
Pixmap icon_pixmap = None;
char *label, host[256];
/* For security reasons, we reset our uid/gid after doing the necessary
system initialization and before calling any X routines. */
if (setgid(getgid()) == -1)
errx(1, "%s: setgid failed: %s\n", argv[0], strerror(errno));
if (setuid(getuid()) == -1)
errx(1, "%s: setuid failed: %s\n", argv[0], strerror(errno));
toplevel = XtAppInitialize(&app_con, "XMem", options_mem, XtNumber(options_mem),
&argc, argv, NULL, NULL, (Cardinal) 0);
if (argc != 1) usage(argv[0]);
XtGetApplicationResources( toplevel, (XtPointer) &resources,
my_resources_mem, XtNumber(my_resources_mem),
NULL, (Cardinal) 0);
/*
* This is a hack so that f.delete will do something useful in this
* single-window application.
*/
XtAppAddActions (app_con, xload_actions, XtNumber(xload_actions));
XtOverrideTranslations(toplevel,
XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));
XtSetArg (args[0], XtNiconPixmap, &icon_pixmap);
XtGetValues(toplevel, args, ONE);
if (icon_pixmap == None) {
XtSetArg(args[0], XtNiconPixmap,
XCreateBitmapFromData(XtDisplay(toplevel),
XtScreen(toplevel)->root,
(char *)xload_bits,
xload_width, xload_height));
XtSetValues (toplevel, args, ONE);
}
if (resources.show_label) {
pane = XtCreateManagedWidget ("paned", panedWidgetClass,
toplevel, NULL, ZERO);
label_wid = XtCreateManagedWidget ("label", labelWidgetClass,
pane, NULL, ZERO);
XtSetArg (args[0], XtNlabel, &label);
XtGetValues(label_wid, args, ONE);
if ( strcmp("label", label) == 0 ) {
(void) XmuGetHostname (host, 255);
XtSetArg (args[0], XtNlabel, host);
XtSetValues (label_wid, args, ONE);
}
load_parent = pane;
}
else
load_parent = toplevel;
load = XtCreateManagedWidget ("mem", memStripChartWidgetClass,
load_parent, NULL, ZERO);
XtAddCallback(load, XtNgetValue, (void*)GetMemLoadPoint, NULL);
XtRealizeWidget (toplevel);
wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW",
False);
(void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
&wm_delete_window, 1);
if (pledge("ps vminfo stdio", NULL) == -1)
errx(1, "pledge failed: %s", strerror(errno));
#if __OpenBSD__
if (pledge("ps vminfo stdio", NULL) == -1)
errx(1, "pledge failed: %s", strerror(errno));
#endif
XtAppMainLoop(app_con);
return 0;
XtAppMainLoop(app_con);
return 0;
}
static void quit (Widget w, XEvent *event, String *params, Cardinal *num_params)
{
if (event->type == ClientMessage &&
event->xclient.data.l[0] != wm_delete_window) {
XBell (XtDisplay(w), 0);
return;
}
XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
exit (0);
if (event->type == ClientMessage &&
event->xclient.data.l[0] != wm_delete_window) {
XBell (XtDisplay(w), 0);
return;
}
XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
exit (0);
}