cpuset_list_compute.c revision 70259d6e7ec2569972fb663ea85f959c4f2b4f81
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <signal.h>
6#include <dirent.h>
7#include <sys/types.h>
8#include <pwd.h>
9#include <err.h>
10
11#include "../cpuset_lib/common.h"
12#include "../cpuset_lib/bitmask.h"
13
14#define MAX_STRING_SIZE	400
15#define MAX_NBITS	1024
16
17#define USAGE  ("Usage : %s [-a|s] list1 [list2]\n"			\
18		"\t-a|s   list1 add/subtract list2."			\
19		"[default: -a]\n"					\
20		"\t-h     Help.\n")
21int add_or_subtract;
22int convert;
23
24static void usage(char *prog_name, int status)
25{
26	FILE *output = NULL;
27
28	if (prog_name == NULL)
29		prog_name = "cpu-usage";
30
31	if (status)
32		output = stderr;
33	else
34		output = stdout;
35
36	fprintf(output, USAGE, prog_name);
37	exit(status);
38}
39
40static void checkopt(int argc, char **argv)
41{
42	char c = '\0';
43	int optc = 0;
44
45	while ((c = getopt(argc, argv, "ash")) != -1) {
46		switch (c) {
47		case 'a':
48			add_or_subtract = 0;
49			optc++;
50			break;
51		case 's':
52			add_or_subtract = 1;
53			optc++;
54			break;
55		case 'h':   /* help */
56			usage(argv[0], 0);
57			break;
58		default:
59			usage(argv[0], 1);
60			break;
61		}
62	}
63
64	if (optc == 2)
65		OPT_COLLIDING(argv[0], 'a', 's');
66
67	if (argc == optc + 1) {
68		fprintf(stderr, "%s: missing the argument list1.\n",
69			argv[0]);
70		usage(argv[0], 1);
71	} else if (argc == optc + 2)
72		convert = 1;
73}
74
75int main(int argc, char **argv)
76{
77	struct bitmask *mask1 = NULL, *mask2 = NULL, *mask3 = NULL;
78	char buff[MAX_STRING_SIZE];
79
80	checkopt(argc, argv);
81
82	mask1 = bitmask_alloc(MAX_NBITS);
83	if (mask1 == NULL)
84		err(EXIT_FAILURE, "alloc memory space for bitmask1 failed.");
85
86	mask2 = bitmask_alloc(MAX_NBITS);
87	if (mask2 == NULL)
88		err(EXIT_FAILURE, "alloc memory space for bitmask2 failed.");
89
90	mask3 = bitmask_alloc(MAX_NBITS);
91	if (mask3 == NULL)
92		err(EXIT_FAILURE, "alloc memory space for bitmask3 failed.");
93
94	if (bitmask_parselist(argv[argc - 2 + convert], mask1) != 0)
95		err(EXIT_FAILURE, "parse list1 string failed.");
96
97	if (convert) {
98		bitmask_displaylist(buff, MAX_STRING_SIZE, mask1);
99		printf("%s\n", buff);
100		return 0;
101	}
102
103	if (bitmask_parselist(argv[argc - 1], mask2) != 0)
104		err(EXIT_FAILURE, "parse list2 string failed.");
105
106	if (add_or_subtract)
107		bitmask_andnot(mask3, mask1, mask2);
108	else
109		bitmask_or(mask3, mask1, mask2);
110
111	bitmask_displaylist(buff, MAX_STRING_SIZE, mask3);
112	printf("%s\n", buff);
113
114	return 0;
115}
116