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	fprintf(stderr, USAGE, prog_name);
27	exit(1);
28}
29
30static void checkopt(int argc, char **argv)
31{
32	char c = '\0';
33	int optc = 0;
34
35	while ((c = getopt(argc, argv, "ahs")) != -1) {
36		switch (c) {
37		case 'a':
38			add_or_subtract = 0;
39			optc++;
40			break;
41		case 'h':	/* help */
42			usage(argv[0], 0);
43			break;
44		case 's':
45			add_or_subtract = 1;
46			optc++;
47			break;
48		default:
49			usage(argv[0], 1);
50			break;
51		}
52	}
53
54	if (optc == 2)
55		OPT_COLLIDING(argv[0], 'a', 's');
56
57	if (argc == optc + 1) {
58		fprintf(stderr, "%s: missing the argument list1.\n", argv[0]);
59		usage(argv[0], 1);
60	} else if (argc == optc + 2)
61		convert = 1;
62}
63
64int main(int argc, char **argv)
65{
66	struct bitmask *mask1 = NULL, *mask2 = NULL, *mask3 = NULL;
67	char buff[MAX_STRING_SIZE];
68
69	checkopt(argc, argv);
70
71	mask1 = bitmask_alloc(MAX_NBITS);
72	if (mask1 == NULL)
73		err(EXIT_FAILURE, "alloc memory space for bitmask1 failed.");
74
75	mask2 = bitmask_alloc(MAX_NBITS);
76	if (mask2 == NULL)
77		err(EXIT_FAILURE, "alloc memory space for bitmask2 failed.");
78
79	mask3 = bitmask_alloc(MAX_NBITS);
80	if (mask3 == NULL)
81		err(EXIT_FAILURE, "alloc memory space for bitmask3 failed.");
82
83	if (bitmask_parselist(argv[argc - 2 + convert], mask1) != 0)
84		err(EXIT_FAILURE, "parse list1 string failed.");
85
86	if (convert) {
87		bitmask_displaylist(buff, MAX_STRING_SIZE, mask1);
88		printf("%s\n", buff);
89		exit(0);
90	}
91
92	if (bitmask_parselist(argv[argc - 1], mask2) != 0)
93		err(EXIT_FAILURE, "parse list2 string failed.");
94
95	if (add_or_subtract)
96		bitmask_andnot(mask3, mask1, mask2);
97	else
98		bitmask_or(mask3, mask1, mask2);
99
100	bitmask_displaylist(buff, MAX_STRING_SIZE, mask3);
101	printf("%s\n", buff);
102
103	return 0;
104}
105