Compare commits

6 Commits

Author SHA1 Message Date
0cf95e888f write documentation about getoptions 2025-08-20 13:27:40 +02:00
47b3f041bf remove branch 2025-08-20 13:25:22 +02:00
ed1794ffbf improve number parsing in opts.c 2025-08-20 13:18:45 +02:00
6ce633b06b fix opts.c to use correct integer types 2025-08-20 13:18:27 +02:00
dbcf29dc82 remove duplicate checks and put everything on a single line. 2025-08-20 12:49:27 +02:00
53472e6fc6 add help option 2025-08-20 12:48:56 +02:00
2 changed files with 34 additions and 24 deletions

View File

@@ -1,44 +1,49 @@
#include "opts.h"
#include <errno.h>
#include <getopt.h>
#include <stdint.h>
#include <stdlib.h>
#include "error.h"
#include "util/intdef.h"
uint8_t getoptions(int32_t argc, char *const *argv, int32_t *ncpus) {
u8 getoptions(int argc, char *const *argv, int *ncpus) {
*ncpus = -1;
uint8_t opts = 0;
char opt;
while ((opt = getopt(argc, argv, "lavi")) != -1) {
while ((opt = getopt(argc, argv, "lavih")) != -1) {
switch (opt) {
case 'l':
if (opts & OPT_LIST_CORES) fatal("-%c has already been set", 'l');
opts |= OPT_LIST_CORES;
break;
case 'a':
if (opts & OPT_SET_ALL) fatal("-%c has already been set", 'a');
opts |= OPT_SET_ALL;
break;
case 'v':
if (opts & OPT_VERBOSE) fatal("-%c has already been set", 'v');
opts |= OPT_VERBOSE | OPT_LIST_CORES;
break;
case 'i':
if (opts & OPT_INVERT) fatal("-%c has already been set", 'i');
opts |= OPT_INVERT;
break;
case 'l': opts |= OPT_LIST_CORES; break;
case 'a': opts |= OPT_SET_ALL; break;
case 'v': opts |= OPT_VERBOSE | OPT_LIST_CORES; break;
case 'i': opts |= OPT_INVERT; break;
case '?':
fatal("usage: %s [number] [-l] [-a] [-v] [-i]", argv[0]);
printf("specify -h for help\n");
exit(EXIT_FAILURE);
case 'h':
printf(
"%s: a tool for turning on/off CPU threads\n"
"USAGE: %s [integer] [-l] [-a] [-v] [-i] [-h]\n"
"ARGUMENTS:\n"
"\t-l : List cores after executing the command. May be specified without integer.\n"
"\t-a : Writes to all cores, regardless of their state.\n"
"\t-v : Print the cores that are written. (implies -l)\n"
"\t-i : Instead of the given number.\n"
"\t-h : Shows this dialogue.\n",
argv[0], argv[0]);
exit(EXIT_SUCCESS);
}
}
if (optind < argc) {
char *end;
char *num = argv[optind];
*ncpus = atoi(num);
} else if (opts & OPT_LIST_CORES) {
*ncpus = -1;
} else
*ncpus = strtol(num, &end, 0);
if (errno || end == num || *end != '\0')
fatal("failed to parse numeric value from string '%s'", num);
} else if (!(opts & OPT_LIST_CORES))
fatal("must execute with either a number or [-l]");
return opts;

View File

@@ -1,6 +1,8 @@
#pragma once
#include <stdint.h>
#include "util/intdef.h"
enum opt {
OPT_LIST_CORES = 1, // option that lists the total amount of cores that are set
OPT_SET_ALL = 2, // option to set all cores, regardless of their assumed state
@@ -8,4 +10,7 @@ enum opt {
OPT_INVERT = 8, // 'num' now represents the amount of cores to disable
};
uint8_t getoptions(int32_t, char *const *, int32_t *);
/* acquires the options given by the user through arguments.
* returns the bitmap of these options.
* `ncpus` is set to `-1`, or the amount of CPUs that the user requested to enable.*/
u8 getoptions(int argc, char *const *argv, int *ncpus);