refactor(userspace/coreutils): improve uname command

The IEEE Std 1003.1-2024 specifies this for output:

    By default, the output shall be a single line of the following form:

    "%s\n", <sysname>

    If the -a option is specified, the output shall be a single line of the following form:

    "%s %s %s %s %s\n", <sysname>, <nodename>, <release>,
        <version>, <machine>

    Additional implementation-defined symbols may be written; all such symbols shall be written at the end of the line of output before the <newline>.

    If options are specified to select different combinations of the symbols, only those symbols shall be written, in the order shown above for the -a option. If a symbol is not selected for writing, its corresponding trailing <blank> characters also shall not be written.

Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
EnderIce2 2025-03-11 15:33:08 +00:00
parent 1d7a9edd46
commit 8a6910bf04
No known key found for this signature in database
GPG Key ID: 2EE20AF089811A5A
2 changed files with 60 additions and 39 deletions

View File

@ -15,11 +15,12 @@
along with Fennix Core Utilities. If not, see <https://www.gnu.org/licenses/>.
*/
#include <coreutils.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <coreutils.h>
void PrintHelp()
{

View File

@ -15,11 +15,30 @@
along with Fennix Core Utilities. If not, see <https://www.gnu.org/licenses/>.
*/
#include <coreutils.h>
#include <stdio.h>
#include <sys/utsname.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <sys/utsname.h>
typedef union
{
struct
{
uint8_t KernelName : 1;
uint8_t NodeName : 1;
uint8_t KernelRelease : 1;
uint8_t KernelVersion : 1;
uint8_t Machine : 1;
uint8_t Processor : 1;
uint8_t HardwarePlatform : 1;
uint8_t OperatingSystem : 1;
};
uint8_t raw;
} UnameFlags;
const char *GetOperatingSystemName(const char *sysname)
{
@ -118,6 +137,7 @@ void PrintUsage()
printf(" -i, --hardware-platform display the hardware platform (non-portable)\n");
printf(" -o, --operating-system display the operating system\n");
printf(" --help show this help message and exit\n");
printf(" --version output version information and exit\n");
}
int main(int argc, char *argv[])
@ -129,48 +149,45 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
bool print_all = false;
bool print_kernel_name = false;
bool print_nodename = false;
bool print_kernel_release = false;
bool print_kernel_version = false;
bool print_machine = false;
bool print_processor = false;
bool print_hardware_platform = false;
bool print_operating_system = false;
UnameFlags flags = {0};
if (argc == 1)
print_kernel_name = true;
flags.KernelName = 1;
else
{
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0)
{
print_all = true;
flags.raw = 0xFF;
break;
}
else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--kernel-name") == 0)
print_kernel_name = true;
flags.KernelName = 1;
else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--nodename") == 0)
print_nodename = true;
flags.NodeName = 1;
else if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--kernel-release") == 0)
print_kernel_release = true;
flags.KernelRelease = 1;
else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--kernel-version") == 0)
print_kernel_version = true;
flags.KernelVersion = 1;
else if (strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--machine") == 0)
print_machine = true;
flags.Machine = 1;
else if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--processor") == 0)
print_processor = true;
flags.Processor = 1;
else if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--hardware-platform") == 0)
print_hardware_platform = true;
flags.HardwarePlatform = 1;
else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--operating-system") == 0)
print_operating_system = true;
flags.OperatingSystem = 1;
else if (strcmp(argv[i], "--help") == 0)
{
PrintUsage();
exit(EXIT_SUCCESS);
}
else if (strcmp(argv[i], "--version") == 0)
{
PRINTF_VERSION;
exit(EXIT_SUCCESS);
}
else
{
fprintf(stderr, "uname: invalid option -- '%s'\n", argv[i]);
@ -180,22 +197,25 @@ int main(int argc, char *argv[])
}
}
if (print_all || print_kernel_name)
printf("%s ", buffer.sysname);
if (print_all || print_nodename)
printf("%s ", buffer.nodename);
if (print_all || print_kernel_release)
printf("%s ", buffer.release);
if (print_all || print_kernel_version)
printf("%s ", buffer.version);
if (print_all || print_machine)
printf("%s ", buffer.machine);
if (print_all || print_processor)
printf("%s ", GetProcessorType(buffer.machine));
if (print_all || print_hardware_platform)
printf("%s ", GetHardwarePlatform(buffer.machine));
if (print_all || print_operating_system)
printf("%s ", GetOperatingSystemName(buffer.sysname));
printf("\n");
bool first = true;
#define PRINT_IF(flag, value) \
if (flags.flag) \
{ \
if (!first) \
putchar(' '); \
printf("%s", value); \
first = false; \
}
PRINT_IF(KernelName, buffer.sysname);
PRINT_IF(NodeName, buffer.nodename);
PRINT_IF(KernelRelease, buffer.release);
PRINT_IF(KernelVersion, buffer.version);
PRINT_IF(Machine, buffer.machine);
PRINT_IF(Processor, GetProcessorType(buffer.machine));
PRINT_IF(HardwarePlatform, GetHardwarePlatform(buffer.machine));
PRINT_IF(OperatingSystem, GetOperatingSystemName(buffer.sysname));
putchar('\n');
return 0;
}