Skip to content

Commit

Permalink
userland: add "uname" command
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmeh committed Jun 20, 2024
1 parent 0a2fa3c commit fafa5f1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions userland/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ BIN_TARGET_NAMES := \
sh \
sleep \
touch \
uname \
wc \
xv6-usertests
OUTDIR := ../base/bin
Expand Down
62 changes: 62 additions & 0 deletions userland/uname.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
#include <unistd.h>

static void usage(void) {
dprintf(STDERR_FILENO, "Usage: uname [-asnrvm]\n");
exit(EXIT_FAILURE);
}

int main(int argc, char* const argv[]) {
bool all = false;
bool nodename = false;
bool release = false;
bool version = false;
bool machine = false;
for (int i = 1; i < argc; ++i) {
const char* arg = argv[i];
if (arg[0] != '-' || arg[1] == '\0' || arg[2] != '\0')
usage();
switch (arg[1]) {
case 'a':
all = true;
break;
case 's':
break;
case 'n':
nodename = true;
break;
case 'r':
release = true;
break;
case 'v':
version = true;
break;
case 'm':
machine = true;
break;
default:
usage();
}
}

struct utsname buf;
if (uname(&buf) < 0) {
perror("uname");
return EXIT_FAILURE;
}

printf("%s ", buf.sysname);
if (all || nodename)
printf("%s ", buf.nodename);
if (all || release)
printf("%s ", buf.release);
if (all || version)
printf("%s ", buf.version);
if (all || machine)
printf("%s", buf.machine);
puts("");

return EXIT_SUCCESS;
}

0 comments on commit fafa5f1

Please sign in to comment.