mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-05-28 15:34:31 +00:00
refactor(userspace/coreutils): change code style
Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
parent
a43fac0c2d
commit
3feb4e72aa
@ -22,32 +22,32 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
typedef struct alias_t
|
||||
typedef struct AliasData
|
||||
{
|
||||
char *name;
|
||||
char *value;
|
||||
struct alias_t *next;
|
||||
} alias_t;
|
||||
struct AliasData *next;
|
||||
} AliasData;
|
||||
|
||||
alias_t *aliases = NULL;
|
||||
AliasData *Aliases = NULL;
|
||||
|
||||
void FreeAliases()
|
||||
{
|
||||
alias_t *current = aliases;
|
||||
AliasData *current = Aliases;
|
||||
while (current != NULL)
|
||||
{
|
||||
alias_t *next = current->next;
|
||||
AliasData *next = current->next;
|
||||
free(current->name);
|
||||
free(current->value);
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
aliases = NULL;
|
||||
Aliases = NULL;
|
||||
}
|
||||
|
||||
alias_t *FindAlias(const char *name)
|
||||
AliasData *FindAlias(const char *name)
|
||||
{
|
||||
alias_t *current = aliases;
|
||||
AliasData *current = Aliases;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (strcmp(current->name, name) == 0)
|
||||
@ -59,7 +59,7 @@ alias_t *FindAlias(const char *name)
|
||||
|
||||
void AddAlias(const char *name, const char *value)
|
||||
{
|
||||
alias_t *existing = FindAlias(name);
|
||||
AliasData *existing = FindAlias(name);
|
||||
if (existing)
|
||||
{
|
||||
free(existing->value);
|
||||
@ -67,11 +67,11 @@ void AddAlias(const char *name, const char *value)
|
||||
return;
|
||||
}
|
||||
|
||||
alias_t *new_alias = malloc(sizeof(alias_t));
|
||||
AliasData *new_alias = malloc(sizeof(AliasData));
|
||||
new_alias->name = strdup(name);
|
||||
new_alias->value = strdup(value);
|
||||
new_alias->next = aliases;
|
||||
aliases = new_alias;
|
||||
new_alias->next = Aliases;
|
||||
Aliases = new_alias;
|
||||
}
|
||||
|
||||
int ReadAliases(const char *filename)
|
||||
@ -92,8 +92,8 @@ int ReadAliases(const char *filename)
|
||||
char *eq = strchr(line, '=');
|
||||
if (!eq)
|
||||
continue;
|
||||
*eq = '\0';
|
||||
|
||||
*eq = '\0';
|
||||
AddAlias(line, eq + 1);
|
||||
}
|
||||
|
||||
@ -111,7 +111,7 @@ int WriteAliases(const char *filename)
|
||||
return -1;
|
||||
}
|
||||
|
||||
alias_t *current = aliases;
|
||||
AliasData *current = Aliases;
|
||||
while (current)
|
||||
{
|
||||
fprintf(file, "%s=%s\n", current->name, current->value);
|
||||
@ -173,7 +173,7 @@ int main(int argc, char *argv[])
|
||||
int status = 0;
|
||||
if (argc == 1)
|
||||
{
|
||||
alias_t *current = aliases;
|
||||
AliasData *current = Aliases;
|
||||
while (current)
|
||||
{
|
||||
char *q = QuoteValue(current->value);
|
||||
@ -202,7 +202,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
else
|
||||
{
|
||||
alias_t *a = FindAlias(arg);
|
||||
AliasData *a = FindAlias(arg);
|
||||
if (a)
|
||||
{
|
||||
char *q = QuoteValue(a->value);
|
||||
|
@ -41,29 +41,28 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
if (argc == 1)
|
||||
printf("%s\n", buffer.machine);
|
||||
else
|
||||
{
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (strcmp(argv[i], "--help") == 0)
|
||||
{
|
||||
PrintUsage();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else if (strcmp(argv[1], "--version") == 0)
|
||||
{
|
||||
PRINTF_VERSION;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "uname: invalid option -- '%s'\n", argv[i]);
|
||||
PrintUsage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
printf("%s\n", buffer.machine);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (strcmp(argv[i], "--help") == 0)
|
||||
{
|
||||
PrintUsage();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else if (strcmp(argv[1], "--version") == 0)
|
||||
{
|
||||
PRINTF_VERSION;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "uname: invalid option -- '%s'\n", argv[i]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ void PrintHelp()
|
||||
printf(" \\v vertical tab\n");
|
||||
}
|
||||
|
||||
static void PrintEscaped(const char *str)
|
||||
void PrintEscaped(const char *str)
|
||||
{
|
||||
while (*str)
|
||||
{
|
||||
@ -89,9 +89,8 @@ static void PrintEscaped(const char *str)
|
||||
{
|
||||
int octal = 0;
|
||||
for (int i = 0; i < 3 && *str >= '0' && *str <= '7'; i++, str++)
|
||||
{
|
||||
octal = octal * 8 + (*str - '0');
|
||||
}
|
||||
|
||||
putchar(octal);
|
||||
str--;
|
||||
break;
|
||||
@ -122,9 +121,7 @@ static void PrintEscaped(const char *str)
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
putchar(*str);
|
||||
}
|
||||
str++;
|
||||
}
|
||||
}
|
||||
@ -132,8 +129,8 @@ static void PrintEscaped(const char *str)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool newline = true;
|
||||
bool interpret_escapes = false;
|
||||
int arg_start = 1;
|
||||
bool interpretEscapes = false;
|
||||
int argStart = 1;
|
||||
|
||||
if (argc == 2)
|
||||
{
|
||||
@ -160,25 +157,23 @@ int main(int argc, char *argv[])
|
||||
if (argv[i][j] == 'n')
|
||||
newline = false;
|
||||
else if (argv[i][j] == 'e')
|
||||
interpret_escapes = true;
|
||||
interpretEscapes = true;
|
||||
else if (argv[i][j] == 'E')
|
||||
interpret_escapes = false;
|
||||
interpretEscapes = false;
|
||||
else
|
||||
goto print_args;
|
||||
goto printArgs;
|
||||
}
|
||||
arg_start++;
|
||||
argStart++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print_args:
|
||||
for (int i = arg_start; i < argc; i++)
|
||||
printArgs:
|
||||
for (int i = argStart; i < argc; i++)
|
||||
{
|
||||
if (interpret_escapes)
|
||||
if (interpretEscapes)
|
||||
PrintEscaped(argv[i]);
|
||||
else
|
||||
fputs(argv[i], stdout);
|
||||
|
@ -22,7 +22,7 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
static int TestFile(const char *path, char mode)
|
||||
int TestFile(const char *path, char mode)
|
||||
{
|
||||
struct stat st;
|
||||
if (stat(path, &st) != 0)
|
||||
@ -62,7 +62,7 @@ static int TestFile(const char *path, char mode)
|
||||
}
|
||||
}
|
||||
|
||||
static int TestString(const char *s1, const char *op, const char *s2)
|
||||
int TestString(const char *s1, const char *op, const char *s2)
|
||||
{
|
||||
if (!strcmp(op, "="))
|
||||
return strcmp(s1, s2) == 0 ? 0 : 1;
|
||||
@ -75,7 +75,7 @@ static int TestString(const char *s1, const char *op, const char *s2)
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int TestInteger(const char *n1, const char *op, const char *n2)
|
||||
int TestInteger(const char *n1, const char *op, const char *n2)
|
||||
{
|
||||
int i1 = atoi(n1), i2 = atoi(n2);
|
||||
if (!strcmp(op, "-eq"))
|
||||
@ -97,8 +97,8 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
char *base = strrchr(argv[0], '/');
|
||||
base = base ? base + 1 : argv[0];
|
||||
int isBracketForm = (strcmp(base, "[") == 0);
|
||||
|
||||
int isBracketForm = (strcmp(base, "[") == 0);
|
||||
if (isBracketForm)
|
||||
{
|
||||
if (argc < 2 || strcmp(argv[argc - 1], "]") != 0)
|
||||
|
@ -28,59 +28,61 @@ 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 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)
|
||||
const char *GetOperatingSystemName(const char *systemName)
|
||||
{
|
||||
if (strcmp(sysname, "Fennix") == 0)
|
||||
if (strcmp(systemName, "Fennix") == 0)
|
||||
return "Fennix";
|
||||
if (strncmp(sysname, "Linux", 5) == 0)
|
||||
if (strncmp(systemName, "Linux", 5) == 0)
|
||||
return "GNU/Linux";
|
||||
if (strncmp(sysname, "Darwin", 6) == 0)
|
||||
if (strncmp(systemName, "Darwin", 6) == 0)
|
||||
return "macOS";
|
||||
if (strncmp(sysname, "FreeBSD", 7) == 0)
|
||||
if (strncmp(systemName, "FreeBSD", 7) == 0)
|
||||
return "FreeBSD";
|
||||
if (strncmp(sysname, "NetBSD", 6) == 0)
|
||||
if (strncmp(systemName, "NetBSD", 6) == 0)
|
||||
return "NetBSD";
|
||||
if (strncmp(sysname, "OpenBSD", 7) == 0)
|
||||
if (strncmp(systemName, "OpenBSD", 7) == 0)
|
||||
return "OpenBSD";
|
||||
if (strncmp(sysname, "DragonFly", 9) == 0)
|
||||
if (strncmp(systemName, "DragonFly", 9) == 0)
|
||||
return "DragonFly BSD";
|
||||
if (strncmp(sysname, "SunOS", 5) == 0)
|
||||
if (strncmp(systemName, "SunOS", 5) == 0)
|
||||
return "SunOS";
|
||||
if (strncmp(sysname, "AIX", 3) == 0)
|
||||
if (strncmp(systemName, "AIX", 3) == 0)
|
||||
return "AIX";
|
||||
if (strncmp(sysname, "HP-UX", 5) == 0)
|
||||
if (strncmp(systemName, "HP-UX", 5) == 0)
|
||||
return "HP-UX";
|
||||
if (strncmp(sysname, "GNU", 3) == 0)
|
||||
if (strncmp(systemName, "GNU", 3) == 0)
|
||||
return "GNU";
|
||||
if (strncmp(sysname, "Minix", 5) == 0)
|
||||
if (strncmp(systemName, "Minix", 5) == 0)
|
||||
return "Minix";
|
||||
if (strncmp(sysname, "QNX", 3) == 0)
|
||||
if (strncmp(systemName, "QNX", 3) == 0)
|
||||
return "QNX";
|
||||
if (strncmp(sysname, "Haiku", 5) == 0)
|
||||
if (strncmp(systemName, "Haiku", 5) == 0)
|
||||
return "Haiku";
|
||||
if (strncmp(sysname, "OS/2", 4) == 0)
|
||||
if (strncmp(systemName, "OS/2", 4) == 0)
|
||||
return "OS/2";
|
||||
|
||||
return sysname;
|
||||
return systemName;
|
||||
}
|
||||
|
||||
const char *GetProcessorType(const char *machine)
|
||||
{
|
||||
if (strcmp(machine, "x86_64") == 0)
|
||||
return "x86_64";
|
||||
if (strcmp(machine, "i686") == 0 || strcmp(machine, "i386") == 0)
|
||||
if (strcmp(machine, "i386") == 0)
|
||||
return "i386";
|
||||
if (strcmp(machine, "i686") == 0)
|
||||
return "i686";
|
||||
if (strncmp(machine, "arm", 3) == 0)
|
||||
return "arm";
|
||||
@ -149,88 +151,88 @@ int main(int argc, char *argv[])
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
UnameFlags flags = {0};
|
||||
|
||||
if (argc == 1)
|
||||
flags.KernelName = 1;
|
||||
else
|
||||
{
|
||||
for (int i = 1; i < argc; i++)
|
||||
printf("%s\n", buffer.sysname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
UnameFlags flags = {0};
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0)
|
||||
{
|
||||
if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0)
|
||||
flags.raw = 0xFF;
|
||||
break;
|
||||
}
|
||||
else if (argv[i][0] == '-' && argv[i][1] != '\0')
|
||||
{
|
||||
for (size_t j = 1; j < strlen(argv[i]); j++)
|
||||
{
|
||||
flags.raw = 0xFF;
|
||||
break;
|
||||
}
|
||||
else if (argv[i][0] == '-' && argv[i][1] != '\0')
|
||||
{
|
||||
for (size_t j = 1; j < strlen(argv[i]); j++)
|
||||
switch (argv[i][j])
|
||||
{
|
||||
switch (argv[i][j])
|
||||
{
|
||||
case 's':
|
||||
flags.KernelName = 1;
|
||||
break;
|
||||
case 'n':
|
||||
flags.NodeName = 1;
|
||||
break;
|
||||
case 'r':
|
||||
flags.KernelRelease = 1;
|
||||
break;
|
||||
case 'v':
|
||||
flags.KernelVersion = 1;
|
||||
break;
|
||||
case 'm':
|
||||
flags.Machine = 1;
|
||||
break;
|
||||
case 'p':
|
||||
flags.Processor = 1;
|
||||
break;
|
||||
case 'i':
|
||||
flags.HardwarePlatform = 1;
|
||||
break;
|
||||
case 'o':
|
||||
flags.OperatingSystem = 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "uname: invalid option -- '%c'\n", argv[i][j]);
|
||||
PrintUsage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
case 's':
|
||||
flags.kernelName = 1;
|
||||
break;
|
||||
case 'n':
|
||||
flags.nodeName = 1;
|
||||
break;
|
||||
case 'r':
|
||||
flags.kernelRelease = 1;
|
||||
break;
|
||||
case 'v':
|
||||
flags.kernelVersion = 1;
|
||||
break;
|
||||
case 'm':
|
||||
flags.machine = 1;
|
||||
break;
|
||||
case 'p':
|
||||
flags.processor = 1;
|
||||
break;
|
||||
case 'i':
|
||||
flags.hardwarePlatform = 1;
|
||||
break;
|
||||
case 'o':
|
||||
flags.operatingSystem = 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "uname: invalid option -- '%c'\n", argv[i][j]);
|
||||
PrintUsage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else if (strcmp(argv[i], "--kernel-name") == 0)
|
||||
flags.KernelName = 1;
|
||||
else if (strcmp(argv[i], "--nodename") == 0)
|
||||
flags.NodeName = 1;
|
||||
else if (strcmp(argv[i], "--kernel-release") == 0)
|
||||
flags.KernelRelease = 1;
|
||||
else if (strcmp(argv[i], "--kernel-version") == 0)
|
||||
flags.KernelVersion = 1;
|
||||
else if (strcmp(argv[i], "--machine") == 0)
|
||||
flags.Machine = 1;
|
||||
else if (strcmp(argv[i], "--processor") == 0)
|
||||
flags.Processor = 1;
|
||||
else if (strcmp(argv[i], "--hardware-platform") == 0)
|
||||
flags.HardwarePlatform = 1;
|
||||
else if (strcmp(argv[i], "--operating-system") == 0)
|
||||
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]);
|
||||
PrintUsage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else if (strcmp(argv[i], "--kernel-name") == 0)
|
||||
flags.kernelName = 1;
|
||||
else if (strcmp(argv[i], "--nodename") == 0)
|
||||
flags.nodeName = 1;
|
||||
else if (strcmp(argv[i], "--kernel-release") == 0)
|
||||
flags.kernelRelease = 1;
|
||||
else if (strcmp(argv[i], "--kernel-version") == 0)
|
||||
flags.kernelVersion = 1;
|
||||
else if (strcmp(argv[i], "--machine") == 0)
|
||||
flags.machine = 1;
|
||||
else if (strcmp(argv[i], "--processor") == 0)
|
||||
flags.processor = 1;
|
||||
else if (strcmp(argv[i], "--hardware-platform") == 0)
|
||||
flags.hardwarePlatform = 1;
|
||||
else if (strcmp(argv[i], "--operating-system") == 0)
|
||||
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]);
|
||||
PrintUsage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -244,14 +246,14 @@ int main(int argc, char *argv[])
|
||||
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));
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user