mirror of
https://github.com/thepigeongenerator/cpusetcores.git
synced 2025-12-18 06:25:46 +01:00
Compare commits
6 Commits
d44235ce0f
...
v1.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 0cf95e888f | |||
| 47b3f041bf | |||
| ed1794ffbf | |||
| 6ce633b06b | |||
| dbcf29dc82 | |||
| 53472e6fc6 |
51
src/opts.c
51
src/opts.c
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user