source: trunk/athena/bin/machtype/machtype_solaris.c @ 17381

Revision 17381, 3.7 KB checked in by ghudson, 23 years ago (diff)
Need <stdlib.h> for exit().
Line 
1/*
2 *  Machtype: determine machine type & display type
3 *
4 * RCS Info
5 *    $Id: machtype_solaris.c,v 1.5 2002-03-21 04:35:51 ghudson Exp $
6 */
7
8#define volatile
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <kvm.h>
13#include <nlist.h>
14#include <fcntl.h>
15#include <unistd.h>
16#include <sys/types.h>
17#include <sys/file.h>
18#include <sys/cpu.h>
19#include <ctype.h>
20#include <dirent.h>
21#include <sys/vtoc.h>
22/* OpenPROM stuff */
23#include <sys/promif.h>
24#include <sys/ddi.h>
25#include <sys/sunddi.h>
26#include <sys/ddi_impldefs.h>
27#include <sys/systeminfo.h>
28/* Frame buffer stuff */
29#include <sys/fbio.h>
30#include <errno.h>
31#include "machtype.h"
32
33void do_machtype(void)
34{
35    puts("sun4");
36}
37
38void do_cpu(int verbose)
39{
40  char            buf[BUFSIZ];
41
42  char*           cpustr;
43
44  /* read device name of the top node of the OpenPROM */
45
46  if (sysinfo(SI_PLATFORM, buf, BUFSIZ) < 0) {
47    fprintf(stderr, "Can't get CPU information\n");
48    exit(2);
49  }
50  buf[BUFSIZ-1] = '\0';
51
52  /* now, return a string identifying the CPU */
53
54  if (verbose) {
55    /* "verbose" returns the kernel information directly */
56    puts(buf);
57
58  } else {
59
60    /* skip the initial "SUNW," */
61    cpustr = strchr(buf, ',');
62    if (cpustr)
63      cpustr++;
64    else
65      cpustr = buf;
66
67    /* reformat the result to look like "SPARC/Classic" or "SPARC/5" */
68    if (! strncmp(cpustr, "SPARC", sizeof("SPARC")-1)) {
69      cpustr += sizeof("SPARC")-1;
70
71      if (! strncmp(cpustr, "station-", sizeof("station-")-1))
72        cpustr += sizeof("station-")-1;
73      else
74        if (! strcmp(cpustr, "classic")) /* backwards compat - cap classic */
75          (*cpustr) = toupper(*cpustr);
76
77      printf("SPARC/%s\n", cpustr);
78
79    } else {
80      /* if it didn't start with "SPARC", just leave it be... */
81      puts(cpustr);
82    }
83  }
84
85  return;
86}
87
88void do_dpy(int verbose)
89{
90  int count;
91  char buf[1024], *p;
92
93  count = readlink("/dev/fb", buf, sizeof(buf) - 1);
94  if (count == -1) {
95    puts(verbose ? "unknown frame buffer" : "unknown");
96    return;
97  }
98  buf[count] = 0;
99  p = buf + count;
100  while (p > buf && isdigit((unsigned char)*(p - 1)))
101    *--p = 0;
102  p = strrchr(buf, ':');
103  if (!p) {
104    puts(verbose ? "unknown frame buffer" : "unknown");
105    return;
106  }
107  printf("%s%s\n", p + 1, verbose ? " frame buffer" : "");
108}
109
110void do_disk(int verbose)
111{
112  DIR *dp;
113  struct dirent *de;
114  char path[MAXPATHLEN];
115  const char *devdir = "/dev/rdsk";
116  char *cp;
117  int fd;
118  int devlen;                   /* Length of device name, w/o partition */
119  struct vtoc vtoc;
120 
121  dp = opendir(devdir);
122  if (dp == NULL)
123    {
124      fprintf(stderr, "Cannot open %s: %s\n", devdir, strerror(errno));
125      exit(1);
126    }
127
128  while ((de = readdir(dp)) != NULL)
129    {
130      if ((!strcmp(de->d_name, ".")) || (!strcmp(de->d_name, "..")))
131        continue;
132
133      /* By convention partition (slice) 2 is the whole disk. */
134      cp = strrchr(de->d_name, 's');
135      if ((cp == NULL) || (strcmp(cp, "s2") != 0))
136        continue;
137      devlen = cp - de->d_name;         /* Get name length w/o partition */
138      sprintf(path, "%s/%s", devdir, de->d_name);
139      fd = open(path, O_RDONLY);
140      if (fd == -1)
141        continue;
142
143      if ((read_vtoc(fd, &vtoc) < 0) || (vtoc.v_sanity != VTOC_SANE))
144        {
145          close(fd);
146          continue;
147        }
148      close(fd);
149
150      if (!verbose)
151        {
152          /* Strip geometry info from the label text. */
153          cp = strchr(vtoc.v_asciilabel, ' ');
154          if (cp)
155            *cp = '\0';
156        }
157
158      printf("%.*s: %.*s\n",
159             devlen, de->d_name,
160             LEN_DKL_ASCII, vtoc.v_asciilabel);
161    }
162
163  closedir(dp);
164  return;
165}
166
167#define MEG (1024*1024)
168
169void do_memory(int verbose)
170{
171   int mem, nbpp;
172
173   nbpp = getpagesize() / 1024;
174   mem = sysconf(_SC_PHYS_PAGES);
175   if(verbose)
176      printf("%d (%d M) total\n", mem * nbpp, (mem * nbpp + 916) / 1024);
177   else
178      printf("%d\n", mem * nbpp + 916);
179   return;
180}
181
Note: See TracBrowser for help on using the repository browser.