/*
- * Extended `who' command, version 1.11.
+ * Extended `who' command, version 1.12.
*
- * (c) 1996--2003 Martin Mares <mj@ucw.cz>
+ * (c) 1996--2010 Martin Mares <mj@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU General Public License. See file COPYING in any of the GNU packages.
}
static void
-dispsys(void)
+line1(void)
{
FILE *f;
char line[256];
- char *z, *w;
- w = NULL;
+ char *z;
+ int num_cpus = 0;
+
if (f = fopen("/proc/version", "r"))
{
fgets(line, 256, f);
printf(line);
fclose(f);
}
+
+ if (gethostname(line, sizeof(line)) >= 0)
+ printf(" (%s)", line);
+
if (f = fopen("/proc/cpuinfo", "r"))
{
- char a[32], b[32], c[32];
- strcpy(a, "???");
- b[0] = 0;
- c[0] = 0;
- for(;;)
+ while (fgets(line, 256, f))
{
- fgets(line, 256, f);
- if (feof(f))
- break;
- if (!strncmp(line, "cpu\t\t: ", 7))
- {
- z = line+7;
- w = a;
- }
- else if (!strncmp(line, "cpu family\t: ",13))
- {
- z = line+13; strcpy(z + 1, "86\n");
- w = a;
- }
- else if (!strncmp(line, "model\t\t: ", 9))
- {
- z = line+9;
- w = b;
- }
- else if (!strncmp(line, "model name\t: ", 13))
- {
- z = line+13;
- if (!strncmp(z, "AMD-K6", 6))
- {
- z[3] = ' ';
- if ((w = strstr(z, "tm w/ multimedia")))
- strcpy(w, "-1\n");
- else if ((w = strstr(z, "(tm) 3D")))
- strcpy(w, "-2\n");
- }
- w = b;
- }
- else if (!strncmp(line, "mask\t\t: ", 8))
- {
- z = line+8;
- w = c;
- }
- else
- z = NULL;
- if (z)
- {
- line[strlen(line)-1] = 0;
- strcpy(w, z);
- if (!strcmp(w, "Unknown"))
- w[0] = 0;
- }
+ if (!strncmp(line, "processor\t", 9))
+ num_cpus++;
}
- printf(" on %s", *b ? b : a);
- if (*c)
- printf("/%s", c);
+ printf(" [%d CPU%s]", num_cpus, (num_cpus != 1 ? "s" : ""));
fclose(f);
}
+
if (f = fopen("/proc/uptime", "r"))
{
int p,q,p1,q1;
printf(" up ");
puttime(p);
printf(" run ");
- puttime(p-q);
+ puttime(num_cpus*p - q);
fclose(f);
}
+
{
- struct tm *tm;
- char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
now = time(NULL);
- tm = localtime(&now);
- printf(" on %s %02d-%02d-%02d %02d.%02d:%02d", days[tm->tm_wday], tm->tm_mday, tm->tm_mon+1,
- tm->tm_year%100, tm->tm_hour, tm->tm_min, tm->tm_sec);
- printf(" (%u UE)\n", (unsigned int) now);
+ struct tm *tm = localtime(&now);
+ strftime(line, sizeof(line), "%a %Y-%m-%d %H.%M:%S", tm);
+ printf(" on %s\n", line);
}
+}
+
+static void
+line2(void)
+{
+ FILE *f;
+ char line[256];
+ char *z;
+
if (f = fopen("/proc/loadavg", "r"))
{
int i = 3;
fgets(line, 256, f);
- *strchr(line, '\n') = 0;
- z = line-1;
+ z = strchr(line, '\n');
+ if (z)
+ *z = 0;
+ z = line;
while (i-- && z)
{
- *z = '/';
+ if (z > line)
+ *z = '/';
z = strchr(z, ' ');
}
if (z)
printf("LAV=%s", line);
fclose(f);
}
+
if (f = fopen("/proc/meminfo", "r"))
{
- int free, buffers, stotal, sfree;
+ int free, buffers, cached, stotal, sfree;
free = buffers = stotal = sfree = 0;
while (fgets(line, 256, f))
{
sscanf(line+8, "%d", &free);
else if (!strncmp(line, "Buffers:", 8))
sscanf(line+8, "%d", &buffers);
+ else if (!strncmp(line, "Cached:", 7))
+ sscanf(line+7, "%d", &cached);
else if (!strncmp(line, "SwapTotal:", 10))
sscanf(line+10, "%d", &stotal);
else if (!strncmp(line, "SwapFree:", 9))
sscanf(line+9, "%d", &sfree);
}
printf(", free ");
- memory(1024*(free+buffers));
+ memory(1024*(free+buffers+cached));
printf(" of RAM");
if (stotal != sfree)
{
printf(" of swap");
}
}
- printf(", %d user%s.\n", usercnt, usercnt == 1 ? "" : "s");
+ printf(", %d user%s\n", usercnt, usercnt == 1 ? "" : "s");
}
static void
putchar(' ');
puttime((unsigned)u->total_time/(unsigned)HZ);
putchar(' ');
- for (c = u->proc->cmd_line; *c; c++)
+ for (c = (unsigned char *) u->proc->cmd_line; *c; c++)
{
if (*c < 0x20 || (*c >= 0x7f && *c < 0xa0))
putchar(' ');
int main(void)
{
readutmp();
- dispsys();
+ line1();
+ line2();
readproc();
solve();
sort();