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