add free, uptime

This commit is contained in:
K. Lange 2018-03-15 20:01:28 +09:00 committed by Kevin Lange
parent dbae967ed2
commit 95bc33a3bc
4 changed files with 212 additions and 5 deletions

View File

@ -1,4 +1,4 @@
APPS=init hello sh ls terminal uname compositor drawlines background session kdebug cat yutani-test sysinfo hostname yutani-query env mount date echo nyancat kill ps pstree bim terminal-vga cursor-off font-server migrate
APPS=init hello sh ls terminal uname compositor drawlines background session kdebug cat yutani-test sysinfo hostname yutani-query env mount date echo nyancat kill ps pstree bim terminal-vga cursor-off font-server migrate free uptime
KERNEL_TARGET=i686-elf
KCC = $(KERNEL_TARGET)-gcc

102
apps/free.c Normal file
View File

@ -0,0 +1,102 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2015 Kevin Lange
*
* free - Show free / used / total RAM
*/
#include <stdio.h>
#include <string.h>
void show_usage(int argc, char * argv[]) {
printf(
"free - show available memory\n"
"\n"
"usage: %s [-utk?]\n"
"\n"
" -u \033[3mshow used instead of free\033[0m\n"
" -t \033[3minclude a total\033[0m\n"
" -k \033[3muse kilobytes instead of megabytes\033[0m\n"
" -? \033[3mshow this help text\033[0m\n"
"\n", argv[0]);
}
int main(int argc, char * argv[]) {
int show_used = 0;
int use_kilobytes = 0;
int show_total = 0;
if (argc > 1) {
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
char *c = &argv[i][1];
while (*c) {
switch (*c) {
case 'u':
show_used = 1;
break;
case 't':
show_total = 1;
break;
case 'k':
use_kilobytes = 1;
break;
case '?':
show_usage(argc, argv);
return 0;
}
c++;
}
}
}
}
const char * unit = "kB";
FILE * f = fopen("/proc/meminfo", "r");
if (!f) return 1;
int total, free, used;
char buf[1024] = {0};
fgets(buf, 1024, f);
char * a, * b;
a = strchr(buf, ' ');
a++;
b = strchr(a, '\n');
*b = '\0';
total = atoi(a);
fgets(buf, 1024, f);
a = strchr(buf, ' ');
a++;
b = strchr(a, '\n');
*b = '\0';
free = atoi(a);
//fscanf(f, "MemTotal: %d kB\nMemFree: %d kB\n", &total, &free);
used = total - free;
if (!use_kilobytes) {
unit = "MB";
free /= 1024;
used /= 1024;
total /= 1024;
}
if (show_used) {
printf("%d %s", used, unit);
} else {
printf("%d %s", free, unit);
}
if (show_total) {
printf(" / %d %s", total, unit);
}
printf("\n");
return 0;
}

View File

@ -47,11 +47,9 @@ int main(int argc, char * argv[]) {
sprintf(data_lines[i++], C_A "OS: " C_O "ToaruOS");
prog_lines[i] = "uname -sr";
sprintf(data_lines[i++], C_A "Kernel: " C_O);
#if 0
prog_lines[i] = "uptime -p";
sprintf(data_lines[i++], C_A "Uptime: " C_O);
//sprintf(data_lines[i++], C_A "Packages: " C_O "(hell if I know!)");
#endif
prog_lines[i] = "sh -v";
sprintf(data_lines[i++], C_A "Shell: " C_O);
prog_lines[i] = "yutani-query -r";
@ -64,10 +62,8 @@ int main(int argc, char * argv[]) {
#endif
//sprintf(data_lines[i++], C_A "CPU: " C_O "(query cpudet)");
//sprintf(data_lines[i++], C_A "GPU: " C_O "(hell if I know!)");
#if 0
prog_lines[i] = "free -ut";
sprintf(data_lines[i++], C_A "RAM: " C_O);
#endif
int j = 0;
for (unsigned int y = 0; y < gimp_image.height; y += 2) {

109
apps/uptime.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
void print_time(void) {
struct timeval now;
struct tm * timeinfo;
char clocktime[10];
gettimeofday(&now, NULL);
timeinfo = localtime((time_t *)&now.tv_sec);
strftime(clocktime, 80, "%H:%M:%S", timeinfo);
printf(" %s ", clocktime);
}
#define MINUTE (60)
#define HOUR (60 * MINUTE)
#define DAY (24 * HOUR)
void print_seconds(int seconds) {
if (seconds > DAY) {
int days = seconds / DAY;
seconds -= DAY * days;
printf("%d day%s, ", days, days != 1 ? "s" : "");
}
if (seconds > HOUR) {
int hours = seconds / HOUR;
seconds -= HOUR * hours;
int minutes = seconds / MINUTE;
printf("%2d:%02d", hours, minutes);
return;
} else if (seconds > MINUTE) {
int minutes = seconds / MINUTE;
printf("%d minute%s, ", minutes, minutes != 1 ? "s" : "");
seconds -= MINUTE * minutes;
}
printf("%2d second%s", seconds, seconds != 1 ? "s" : "");
}
void print_uptime(void) {
FILE * f = fopen("/proc/uptime", "r");
if (!f) return;
int seconds, subseconds;
char buf[1024] = {0};
fgets(buf, 1024, f);
char * dot = strchr(buf, '.');
*dot = '\0';
dot++;
dot[3] = '\0';
seconds = atoi(buf);
subseconds = atoi(dot);
//fscanf(f, "%d.%2d", &seconds, &subseconds);
printf("up ");
print_seconds(seconds);
}
void show_usage(int argc, char * argv[]) {
printf(
"uptime - display system uptime information\n"
"\n"
"usage: %s [-p]\n"
"\n"
" -p \033[3mshow just the uptime info\033[0m\n"
" -? \033[3mshow this help text\033[0m\n"
"\n", argv[0]);
}
int main(int argc, char * argv[]) {
int just_pretty_uptime = 0;
if (argc > 1) {
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
char *c = &argv[i][1];
while (*c) {
switch (*c) {
case 'p':
just_pretty_uptime = 1;
break;
case '?':
show_usage(argc, argv);
return 0;
}
c++;
}
}
}
}
if (!just_pretty_uptime)
print_time();
print_uptime();
printf("\n");
return 0;
}