FENIX_coreutils/uname.c

69 lines
1.4 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
2020-12-11 13:19:04 +00:00
#include <stdio.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char * argv[]) {
/* The utsname struct has to be preallocated for uname() to work. */
struct utsname * sys_name = malloc(sizeof(*sys_name));
if(sys_name == NULL) {
fprintf(stderr, "%s: insufficient memory\n", argv[0]);
return 2;
}
int stuff_printed = 0;
char c;
/*
For stuff_print:
01: -m
02: -n
04: -r
010: -s
020: -v
037: -a
*/
int stuff_print = 0;
while((c = getopt(argc, argv, "amnrsv")) != -1) {
switch(c) {
case 'm': stuff_print |= 01; break;
case 'n': stuff_print |= 02; break;
case 'r': stuff_print |= 04; break;
case 's': stuff_print |= 010; break;
case 'v': stuff_print |= 020; break;
case 'a': stuff_print |= 037; break;
}
if(c == 'a') { break; }
}
if(stuff_print == 0) {
stuff_print = 010;
}
int got_uname = 0;
got_uname = uname(sys_name);
if(got_uname == -1) {
fprintf(stderr, "%s: could not get uname\n", argv[0]);
return 1;
}
if(stuff_print & 010) {
printf("%s ", sys_name->sysname);
}
if(stuff_print & 02) {
printf("%s ", sys_name->nodename);
}
if(stuff_print & 04) {
printf("%s ", sys_name->release);
}
if(stuff_print & 020) {
printf("%s ", sys_name->version);
}
if(stuff_print & 01) {
printf("%s", sys_name->machine);
}
printf("\n");
return 0;
}