home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume28
/
sysinfo-2.1.2
/
part03
/
sysinfo.c
< prev
Wrap
C/C++ Source or Header
|
1994-06-19
|
18KB
|
782 lines
/*
* Copyright (c) 1992-1994 Michael A. Cooper.
* This software may be freely distributed provided it is not sold for
* profit and the author is credited appropriately.
*/
#ifndef lint
static char *RCSid = "$Id: sysinfo.c,v 1.67 1994/01/12 22:56:08 mcooper Exp mcooper $";
#endif
/*
* Display System Information
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "defs.h"
/*
* Local declarations
*/
int DoPrintAll = TRUE;
int DoPrintUnknown = FALSE;
int DoPrintVersion = FALSE;
int Level = L_BRIEF;
int Terse = FALSE;
int Debug = 0;
int UseProm = 0;
int OffSetAmt = 4;
char *ShowStr = NULL;
char *LevelStr = NULL;
char *ListStr = NULL;
char *UnSupported = NULL;
char **Environ = NULL;
#if defined(OPTION_COMPAT)
/*
* Old options here for backwards compatibility
*/
int DoPrintCPU = FALSE;
int DoPrintAppArch = FALSE;
int DoPrintHostID = FALSE;
int DoPrintHostName = FALSE;
int DoPrintHostAlias = FALSE;
int DoPrintHostAddrs = FALSE;
int DoPrintKernArch = FALSE;
int DoPrintKernVer = FALSE;
int DoPrintMan = FALSE;
int DoPrintModel = FALSE;
int DoPrintMemory = FALSE;
int DoPrintVirtMem = FALSE;
int DoPrintOSName = FALSE;
int DoPrintOSVer = FALSE;
int DoPrintSerialNo = FALSE;
#endif /* OPTION_COMPAT */
/*
* Command line options table.
*/
OptionDescRec Opts[] = {
{"+all", NoArg, OptBool, __ &DoPrintAll, "1",
NULL, "Print all information"},
{"-all", NoArg, OptBool, __ &DoPrintAll, "0",
NULL, "Don't print all information"},
{"-level", SepArg, OptStr, __ &LevelStr, NULL,
"<see \"-list level\">", "Level names"},
{"-list", SepArg, OptStr, __ &ListStr, "-",
"level,show", "List information about values"},
{"-offset", SepArg, OptInt, __ &OffSetAmt, NULL,
"<amount>", "Number of spaces to offset device info"},
{"-show", SepArg, OptStr, __ &ShowStr, NULL,
"<see \"-list show\">", "Show names"},
{"+terse", NoArg, OptBool, __ &Terse, "1",
NULL, "Print info in terse format"},
{"-terse", NoArg, OptBool, __ &Terse, "0",
NULL, "Don't print info in terse format"},
{"+unknown",NoArg, OptBool, __ &DoPrintUnknown, "1",
NULL, "Print unknown devices"},
{"-unknown",NoArg, OptBool, __ &DoPrintUnknown, "0",
NULL, "Don't print unknown devices"},
{"+useprom",NoArg, OptBool, __ &UseProm, "1",
NULL, "Use PROM values"},
{"-useprom",NoArg, OptBool, __ &UseProm, "0",
NULL, "Don't use PROM values"},
{"-version",NoArg, OptBool, __ &DoPrintVersion, "1",
NULL, "Print version of this program" },
{"-debug",ArgHidden|SepArg, OptInt, __ &Debug, "1",
NULL, "Enable debugging"},
#if defined(OPTION_COMPAT)
{"+cpu", NoArg|ArgHidden,OptBool,__ &DoPrintCPU, "1" },
{"-cpu", NoArg|ArgHidden,OptBool,__ &DoPrintCPU, "0" },
{"+arch", NoArg|ArgHidden,OptBool,__ &DoPrintAppArch, "1" },
{"-arch", NoArg|ArgHidden,OptBool,__ &DoPrintAppArch, "0" },
{"+hostid", NoArg|ArgHidden,OptBool,__ &DoPrintHostID, "1" },
{"-hostid", NoArg|ArgHidden,OptBool,__ &DoPrintHostID, "0" },
{"+hostname",NoArg|ArgHidden,OptBool,__ &DoPrintHostName, "1" },
{"-hostname",NoArg|ArgHidden,OptBool,__ &DoPrintHostName, "0" },
{"+hostaliases",NoArg|ArgHidden,OptBool,__ &DoPrintHostAlias,"1" },
{"-hostaliases",NoArg|ArgHidden,OptBool,__ &DoPrintHostAlias,"0" },
{"+hostaddrs",NoArg|ArgHidden,OptBool,__ &DoPrintHostAddrs, "1" },
{"-hostaddrs",NoArg|ArgHidden,OptBool,__ &DoPrintHostAddrs, "0" },
{"+karch", NoArg|ArgHidden,OptBool,__ &DoPrintKernArch, "1" },
{"-karch", NoArg|ArgHidden,OptBool,__ &DoPrintKernArch, "0" },
{"+kernver",NoArg|ArgHidden,OptBool,__ &DoPrintKernVer, "1" },
{"-kernver",NoArg|ArgHidden,OptBool,__ &DoPrintKernVer, "0" },
{"+man", NoArg|ArgHidden,OptBool,__ &DoPrintMan, "1" },
{"-man", NoArg|ArgHidden,OptBool,__ &DoPrintMan, "0" },
{"+model", NoArg|ArgHidden,OptBool,__ &DoPrintModel, "1" },
{"-model", NoArg|ArgHidden,OptBool,__ &DoPrintModel, "0" },
{"+memory", NoArg|ArgHidden,OptBool,__ &DoPrintMemory, "1" },
{"-memory", NoArg|ArgHidden,OptBool,__ &DoPrintMemory, "0" },
{"+virtmem",NoArg|ArgHidden,OptBool,__ &DoPrintVirtMem, "1" },
{"-virtmem",NoArg|ArgHidden,OptBool,__ &DoPrintVirtMem, "0" },
{"+osname", NoArg|ArgHidden,OptBool,__ &DoPrintOSName, "1" },
{"-osname", NoArg|ArgHidden,OptBool,__ &DoPrintOSName, "0" },
{"+osvers", NoArg|ArgHidden,OptBool,__ &DoPrintOSVer, "1" },
{"-osvers", NoArg|ArgHidden,OptBool,__ &DoPrintOSVer, "0" },
{"+serial", NoArg|ArgHidden,OptBool,__ &DoPrintSerialNo, "1" },
{"-serial", NoArg|ArgHidden,OptBool,__ &DoPrintSerialNo, "0" },
#endif /* OPTION_COMPAT */
};
/*
* Option compatibility support
*/
#if defined(OPTION_COMPAT)
static void SetOptionCompat();
#define OptCom(v) v
#else
#define OptCom(v) 0
#endif
SHOWINFO ShowInfo[] = {
{ S_GEN, "general", "GENERAL INFORMATION", ShowGeneral },
{ S_GEN, "hostname", "Host Name", NULL, GetHostName,
OptCom(&DoPrintHostName) },
{ S_GEN, "hostaliases", "Host Aliases", NULL, GetHostAliases,
OptCom(&DoPrintHostAlias) },
{ S_GEN, "hostaddrs", "Host Address(es)", NULL, GetHostAddrs,
OptCom(&DoPrintHostAddrs) },
{ S_GEN, "hostid", "Host ID", NULL, GetHostID,
OptCom(&DoPrintHostID) },
{ S_GEN, "serial", "Serial Number", NULL, GetSerialNo,
OptCom(&DoPrintSerialNo) },
{ S_GEN, "man", "Manufacturer", NULL, GetMan,
OptCom(&DoPrintMan) },
{ S_GEN, "model", "System Model", NULL, GetModel,
OptCom(&DoPrintModel) },
{ S_GEN, "memory", "Main Memory", NULL, GetMemory,
OptCom(&DoPrintMemory) },
{ S_GEN, "virtmem", "Virtual Memory", NULL, GetVirtMem,
OptCom(&DoPrintVirtMem) },
{ S_GEN, "romver", "ROM Version", NULL, GetRomVer },
{ S_GEN, "cpu", "CPU Type", NULL, GetCPU,
OptCom(&DoPrintCPU) },
{ S_GEN, "numcpu", "Number of CPUs", NULL, GetNumCPU,
OptCom(&DoPrintCPU) },
{ S_GEN, "arch", "App Architecture", NULL, GetAppArch,
OptCom(&DoPrintAppArch) },
{ S_GEN, "karch", "Kernel Architecture", NULL, GetKernArch,
OptCom(&DoPrintKernArch) },
{ S_GEN, "osname", "OS Name", NULL, GetOSName,
OptCom(&DoPrintOSName) },
{ S_GEN, "osvers", "OS Version", NULL, GetOSVersion,
OptCom(&DoPrintOSVer) },
{ S_GEN, "kernver", "Kernel Version", NULL, GetKernelVersion,
OptCom(&DoPrintKernVer) },
{ S_DEV, "devices", "DEVICE INFORMATION", ShowDevices },
{ 0 },
};
/*
* Values and names of levels
*/
NAMETAB LevelNames[] = {
{ L_BRIEF, "brief" },
{ L_GENERAL, "general" },
{ L_DESC, "descriptions" },
{ L_CONFIG, "config" },
{ L_ALL, "all" },
{ L_DEBUG, "debug" },
{ 0 },
};
/*
* List table
*/
struct listtab {
char *Name;
char *Desc;
void (*Func)();
};
typedef struct listtab LISTTAB;
static void List();
static void ListLevel();
static void ListShow();
LISTTAB ListTab[] = {
{ "level", "Values for -level option", ListLevel },
{ "show", "Values for -show option", ListShow },
{ 0 },
};
/*
* Print a label and an argument
*/
static void PrintLabel(Lbl, Str)
char *Lbl;
char *Str;
{
if (!Terse)
printf("%-*s: ", 20, Lbl);
printf("%s\n", (Str && *Str) ? Str : "");
}
/*
* Print general info
*/
extern void ShowGeneral(MyInfo, SpecInfo)
SHOWINFO *MyInfo;
SHOWINFO *SpecInfo;
{
register int i;
int ShowAll = TRUE;
/*
* Do a specific info item
*/
if (SpecInfo) {
PrintLabel(SpecInfo->Label, (*SpecInfo->GetFunc)());
SpecInfo->Enable = FALSE;
return;
}
/*
* See if we are going to show all items
*/
if (!DoPrintAll)
for (i = 0; ShowInfo[i].Name; i++)
if (ShowInfo[i].Type == MyInfo->Type &&
ShowInfo[i].GetFunc && !ShowInfo[i].Enable) {
ShowAll = FALSE;
break;
}
if (MyInfo && (DoPrintAll || ShowAll || MyInfo->Enable))
printf("\n\n\t%s\n\n", MyInfo->Label);
/*
* Print each enabled item of our type
*/
for (i = 0; ShowInfo[i].Name; i++)
if ((ShowInfo[i].Type == MyInfo->Type) &&
(ShowInfo[i].Enable || MyInfo->Enable) && ShowInfo[i].GetFunc) {
PrintLabel(ShowInfo[i].Label, (*ShowInfo[i].GetFunc)());
ShowInfo[i].Enable = FALSE;
}
if (MyInfo)
MyInfo->Enable = FALSE;
}
/*
* List Show values
*/
static void ListShow()
{
register int i;
printf(
"The following values may be specified with the \"-show\" option:\n");
printf("\t%-20s %s\n", "VALUE", "DESCRIPTION");
for (i = 0; ShowInfo[i].Name; i++)
printf("\t%-20s %s%s\n",
ShowInfo[i].Name,
(ShowInfo[i].ShowFunc) ? "Show all " : "",
ShowInfo[i].Label
);
}
/*
* List Level values
*/
static void ListLevel()
{
register int i;
printf(
"The following values may be specified with the \"-level\" option:\n");
printf("\t%-20s\n", "VALUE");
for (i = 0; LevelNames[i].name; i++) {
printf("\t%s\n", LevelNames[i].name);
}
}
/*
* List list values
*/
static void ListList()
{
register int i;
printf(
"The following values may be specified with the \"-list\" option:\n");
printf("\t%-20s %s\n", "VALUE", "DESCRIPTION");
for (i = 0; ListTab[i].Name; i++) {
printf("\t%-20s %s\n",
ListTab[i].Name,
ListTab[i].Desc
);
}
}
/*
* List information about each word found in Str.
*/
static void List(Str)
char *Str;
{
register int i;
char *Word;
int Found;
if (EQ(Str, "-")) {
ListList();
return;
}
for (Word = strtok(Str, ","); Word; Word = strtok((char *)NULL, ",")) {
for (i = 0, Found = FALSE; ListTab[i].Name && !Found; i++)
if (EQ(Word, ListTab[i].Name)) {
Found = TRUE;
(*ListTab[i].Func)();
}
if (!Found) {
Error("The word \"%s\" is invalid.", Word);
ListList();
return;
}
}
}
/*
* Get argument number "arg" from "str".
*/
static char *GetArg(Str, ArgNum)
char *Str;
int ArgNum;
{
register char *p, *start = NULL;
register int c;
for (c = 1, p = Str; p && *p; ++c) {
/* set start of word */
start = p;
/* skip over word */
while (p && *p && *p != ' ' && *p != '\t' && *p != '\n')
++p;
/* is this what we want? */
if (c == ArgNum) {
if (p) *p = C_NULL;
break;
}
/* skip white space */
while (*p == ' ' || *p == '\t')
++p;
}
return(start);
}
/*
* Convert integer to ASCII
*/
char *itoa(Num)
int Num;
{
static char Buf[BUFSIZ];
(void) sprintf(Buf, "%d", Num);
return(Buf);
}
/*
* Get our hostname
*/
extern char *GetHostName()
{
static char Buf[MAXHOSTNAMLEN+1];
gethostname(Buf, sizeof(Buf));
return((Buf[0]) ? Buf : (char *) NULL);
}
/*
* Get aliases for this hostname
*
* Note that this won't work on most systems if your
* gethostbyname() call gets its info from DNS since
* DNS does not provide this funtionality.
*/
extern char *GetHostAliases()
{
static char Buf[BUFSIZ];
struct hostent *hp;
register char **pp;
char *HName;
if ((HName = GetHostName()) == NULL)
return((char *) NULL);
if ((hp = gethostbyname(HName)) == NULL) {
Error("Cannot find lookup host info for \"%s\": %s", HName, SYSERR);
return((char *) NULL);
}
for (pp = hp->h_aliases, Buf[0] = C_NULL; pp && *pp; ++pp) {
(void) strcat(Buf, *pp);
(void) strcat(Buf, " ");
}
return((Buf[0]) ? Buf : (char *) NULL);
}
/*
* Get addresses for this host
*/
extern char *GetHostAddrs()
{
static char Buf[BUFSIZ];
struct hostent *hp;
register char **pp;
char *HName;
if ((HName = GetHostName()) == NULL)
return((char *) NULL);
if ((hp = gethostbyname(HName)) == NULL) {
Error("Cannot find lookup host info for \"%s\": %s", HName, SYSERR);
return((char *) NULL);
}
for (pp = hp->h_addr_list, Buf[0] = C_NULL; pp && *pp; ++pp) {
if (hp->h_addrtype == AF_INET) {
(void) strcat(Buf, (char *) inet_ntoa(*(struct in_addr *)*pp));
(void) strcat(Buf, " ");
}
}
return((Buf[0]) ? Buf : (char *) NULL);
}
/*
* Get system model
*/
extern char *GetModel()
{
int Len;
FILE *fd;
char Buf[BUFSIZ];
char *GetModelName();
#if defined(MODEL_COMMAND)
extern char *ModelCommand[];
#endif /* MODEL_COMMAND */
/*
* Use model file if it exists.
*/
if ((fd = fopen(MODELFILE, "r")) != NULL) {
fgets(Buf, sizeof(Buf), fd);
Len = strlen(Buf);
if (Buf[Len-1] == '\n')
Buf[Len-1] = C_NULL;
return(Buf);
}
/*
* If a command to get model name has been defined, try it.
*/
#if defined(MODEL_COMMAND)
if (p = RunCmds(ModelCommand, FALSE))
return(p);
#endif /* MODEL_COMMAND */
/*
* Use a method specific to this OS
*/
return(GetModelName());
}
/*
* Get host ID
*/
extern char *GetHostID()
{
static char Buf[100];
(void) sprintf(Buf, "%08x", gethostid());
return(Buf);
}
/*
* Get system serial number
*/
extern char *GetSerialNo()
{
return(GetSerialNoStr());
}
/*
* Print error message
*/
#if defined(HAVE_VARARGS)
#include <varargs.h>
/*
* Varargs version of Error()
*/
extern void Error(va_alist)
va_dcl
{
va_list args;
char *fmt;
extern char *ProgramName;
(void) fflush(stdout);
(void) fprintf(stderr, "%s: ", ProgramName);
va_start(args);
fmt = (char *) va_arg(args, char *);
(void) vfprintf(stderr, fmt, args);
va_end(args);
(void) fprintf(stderr, "\n");
}
#else
/*
* Non-Varargs version of Error()
*/
extern void Error(fmt, a1, a2, a3, a4, a5, a6)
char *fmt;
{
extern char *ProgramName;
(void) fflush(stdout);
(void) fprintf(stderr, "%s: ", ProgramName);
(void) fprintf(stderr, fmt, a1, a2, a3, a4, a5, a6);
(void) fprintf(stderr, "\n");
}
#endif /* !HAVE_VARARGS */
/*
* Parse and set the level keyword list
*/
static int ParseLevel(Str)
char *Str;
{
register int i;
char *Word;
int Found;
/*
* Check each word in the LevelNames table
*/
for (Word = strtok(Str, ","); Word; Word = strtok((char *)NULL, ",")) {
for (i = 0, Found = FALSE; LevelNames[i].name && !Found; i++) {
if (strncasecmp(Word, LevelNames[i].name, strlen(Word)) == 0) {
Level |= LevelNames[i].value;
Found = TRUE;
}
}
if (!Found) {
Error("The word \"%s\" is not a valid verbosity level.", Word);
return(-1);
}
}
return(0);
}
/*
* Parse and set the showinfo items
*/
static int ParseShow(Str)
char *Str;
{
register int x;
char *Word;
int Found;
/*
* Check each word.
*/
for (Word = strtok(Str, ","); Word; Word = strtok((char *)NULL, ",")) {
/*
* Search the ShowInfo entries for a match.
*/
for (x = 0, Found = FALSE; !Found && ShowInfo[x].Name; x++)
if (EQ(Word, ShowInfo[x].Name)) {
ShowInfo[x].Enable = TRUE;
DoPrintAll = FALSE;
Found = TRUE;
}
if (!Found) {
Error("The word \"%s\" is not valid.", Word);
ListShow();
return(-1);
}
}
return(0);
}
/*
* Front end to calloc()
*/
char *xcalloc(nele, esize)
int nele;
int esize;
{
char *p, *calloc();
if ((p = calloc(nele, esize)) == NULL) {
Error("calloc(%d, %d) failed.", nele, esize);
exit(1);
}
return(p);
}
char *xmalloc(size)
int size;
{
char *newptr, *malloc();
if (!(newptr = malloc(size))) {
Error("malloc size %d failed: %s", size, SYSERR);
exit(1);
}
return(newptr);
}
char *xrealloc(ptr, size)
char *ptr;
int size;
{
char *newptr, *realloc();
if (!(newptr = realloc(ptr, size))) {
Error("realloc 0x%x size %d failed: %s", ptr, size, SYSERR);
exit(1);
}
return(newptr);
}
#if defined(OPTION_COMPAT)
/*
* Set option compatibility
*/
static void SetOptionCompat()
{
register int i;
/*
* For every OptCompat that's TRUE, enable the real value
*/
for (i = 0; ShowInfo[i].Name; i++)
if (ShowInfo[i].OptCompat && *ShowInfo[i].OptCompat) {
ShowInfo[i].Enable = TRUE;
/*
* These is also part of the old behavior
*/
DoPrintAll = FALSE;
Terse = TRUE;
}
}
#endif /* OPTION_COMPAT */
/*
* The beginning
*/
main(Argc, Argv, Envp)
int Argc;
char **Argv;
char **Envp;
{
register int i;
Environ = Envp;
if (ParseOptions(Opts, Num_Opts(Opts), Argc, Argv) < 0)
exit(1);
UnSupported = (Debug) ? "**UNSUPPORTED**" : (char *)NULL;
/*
* Show version info
*/
if (DoPrintVersion) {
if (!Terse)
printf("Sysinfo version ");
if (PATCHLEVEL)
printf("%s.%d\n", VERSION_STR, PATCHLEVEL);
else
printf("%s\n", VERSION_STR, PATCHLEVEL);
exit(0);
}
/*
* Do any list commands and exit
*/
if (ListStr) {
List(ListStr);
exit(0);
}
#if defined(OPTION_COMPAT)
SetOptionCompat();
#endif
/*
* Set verbosity strings
*/
if (LevelStr && ParseLevel(LevelStr))
exit(1);
if (ShowStr && ParseShow(ShowStr))
exit(1);
/*
* Run down the main topics
*/
for (i = 0; ShowInfo[i].Name; i++) {
if (!(DoPrintAll || ShowInfo[i].Enable))
continue;
if (ShowInfo[i].ShowFunc) {
/*
* The item has a show function
*/
(*ShowInfo[i].ShowFunc)(&ShowInfo[i], NULL);
} else {
register int x;
int Found;
/*
* Search for and call a search function for this type of item
*/
for (x = 0, Found = FALSE; !Found && ShowInfo[x].Name; x++)
if ((ShowInfo[x].Type == ShowInfo[i].Type) &&
ShowInfo[x].ShowFunc) {
Found = TRUE;
(*ShowInfo[x].ShowFunc)(&ShowInfo[x], &ShowInfo[i]);
}
if (!Found) {
Error("No show function exists for \"%s\".",
ShowInfo[i].Name);
exit(1);
}
}
}
exit(0);
}