Major `cat` improvements
[lab.git] / uname.c
blob8ba5a73b8b8df884a82e1a06772ee69de90498d5
1 /* `uname.c` - return system name
2 Copyright (c) 2022, Alan Potteiger
3 See `LICENSE` for copyright and license details */
5 #define _POSIX_C_SOURCE 200809L
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <sys/utsname.h>
11 static const char *usage = {
12 "usage: uname [-amnrsv]\n"
15 int
16 main(int argc, char *argv[])
18 struct utsname name;
19 int m, n, r, s, v;
20 char ch;
22 m = n = r = s = v = 0;
24 while ((ch = getopt(argc, argv, "amnrsv")) != -1) {
25 switch (ch) {
26 case 'a':
27 m = n = r = s = v = 1;
28 break;
29 case 'm':
30 m = 1;
31 break;
32 case 'n':
33 n = 1;
34 break;
35 case 'r':
36 r = 1;
37 break;
38 case 's':
39 s = 1;
40 break;
41 case 'v':
42 v = 1;
43 break;
44 case '?':
45 default:
46 fputs(usage, stderr);
47 return 1;
51 if (uname(&name) == -1) {
52 perror("uname");
53 return 1;
56 if (!(m || n || r || s || v))
57 s = 1;
59 if (m && n && r && s && v) {
60 printf(
61 "%s %s %s %s %s\n",
62 name.sysname, name.nodename, name.release,
63 name.version, name.machine
66 return 0;
69 /* I think this could be done better or more efficiently. I'm tired
70 as hell though. I'll revisit this at some point */
71 if (s)
72 printf("%s ", name.sysname);
73 if (n)
74 printf("%s ", name.nodename);
75 if (r)
76 printf("%s ", name.release);
77 if (v)
78 printf("%s ", name.version);
79 if (m)
80 printf("%s ", name.machine);
82 puts("");
84 return 0;