toaruos/apps/uname.c

135 lines
2.9 KiB
C
Raw Normal View History

/**
* @brief uname - Print kernel version information
2018-08-15 04:07:33 +03:00
*
* Supports all the usual options (a,s,n,r,v,m,o)
2018-02-25 11:14:43 +03:00
*
2018-08-15 04:07:33 +03:00
* Note that o is hardcoded, which is also the situation in
* the coreutils implementation, so I don't see that being
* a problem. If you want to build this uname for Linux or
* something... you'll have to change that.
*
* @copyright
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
2018-02-25 11:14:43 +03:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#define FLAG_SYSNAME 0x01
#define FLAG_NODENAME 0x02
#define FLAG_RELEASE 0x04
#define FLAG_VERSION 0x08
#define FLAG_MACHINE 0x10
2018-08-01 04:12:27 +03:00
#define FLAG_OSNAME 0x20
2018-02-25 11:14:43 +03:00
2018-08-01 04:12:27 +03:00
#define FLAG_ALL (FLAG_SYSNAME|FLAG_NODENAME|FLAG_RELEASE|FLAG_VERSION|FLAG_MACHINE|FLAG_OSNAME)
2018-02-25 11:14:43 +03:00
#define _ITALIC "\033[3m"
#define _END "\033[0m\n"
void show_usage(int argc, char * argv[]) {
fprintf(stderr,
"uname - Print system version information.\n"
"\n"
2018-10-06 05:10:24 +03:00
"usage: %s [-asnrvmp]\n"
2018-02-25 11:14:43 +03:00
"\n"
" -a " _ITALIC "Print the standard uname string we all love" _END
" -s " _ITALIC "Print kernel name" _END
" -n " _ITALIC "Print system name" _END
" -r " _ITALIC "Print kernel version number" _END
" -v " _ITALIC "Print the extra kernel version information" _END
" -m " _ITALIC "Print the architecture name" _END
2018-08-01 04:12:27 +03:00
" -o " _ITALIC "Print operating system name" _END
2018-10-06 05:10:24 +03:00
" -p " _ITALIC "Alias to -m" _END
2018-02-25 11:14:43 +03:00
"\n", argv[0]);
exit(1);
}
int main(int argc, char * argv[]) {
struct utsname u;
int flags = 0;
int space = 0;
2018-02-25 17:05:30 +03:00
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
char *c = &argv[i][1];
while (*c) {
switch (*c) {
case 'a':
flags |= FLAG_ALL;
break;
case 's':
flags |= FLAG_SYSNAME;
break;
case 'n':
flags |= FLAG_NODENAME;
break;
case 'r':
flags |= FLAG_RELEASE;
break;
case 'v':
flags |= FLAG_VERSION;
break;
case 'm':
2018-10-06 05:10:24 +03:00
case 'p':
2018-02-25 17:05:30 +03:00
flags |= FLAG_MACHINE;
break;
2018-08-01 04:12:27 +03:00
case 'o':
flags |= FLAG_OSNAME;
break;
2018-02-25 17:05:30 +03:00
case 'h':
default:
show_usage(argc, argv);
break;
}
c++;
}
2018-02-25 11:14:43 +03:00
}
}
uname(&u);
if (!flags) {
/* By default, we just print the kernel name */
flags = FLAG_SYSNAME;
}
if (flags & FLAG_SYSNAME) {
if (space++) printf(" ");
printf("%s", u.sysname);
}
if (flags & FLAG_NODENAME) {
if (space++) printf(" ");
printf("%s", u.nodename);
}
if (flags & FLAG_RELEASE) {
if (space++) printf(" ");
printf("%s", u.release);
}
if (flags & FLAG_VERSION) {
if (space++) printf(" ");
printf("%s", u.version);
}
if (flags & FLAG_MACHINE) {
if (space++) printf(" ");
printf("%s", u.machine);
}
2018-08-01 04:12:27 +03:00
if (flags & FLAG_OSNAME) {
if (space++) printf(" ");
printf("%s", "ToaruOS");
}
2018-02-25 11:14:43 +03:00
printf("\n");
return 0;
}