6 void add_pages(char* buffer
, int sr
, int er
, int npages
) {
8 for (i
= sr
; i
<= er
&& i
< npages
&& i
> 0; ++i
) {
13 void add_page(char* buffer
, int sr
, int npages
) {
14 if (sr
> 0 && sr
< npages
) {
19 void apply_range(char* buffer
, int npages
, const char* range
) {
31 memset(buffer
, 1, npages
); /* no pages. */
33 while ((state
& end
) == 0) {
35 case '0': case '1': case '2': case '3': case '4':
36 case '5': case '6': case '7': case '8': case '9':
37 ++range
; /* case [:digit:] */
41 if ((state
& brange
) != 0) {
42 sscanf(t
, "%d-", &sr
);
50 --range
; /* no break */
53 if ((state
& brange
) != 0) {
54 sscanf(t
, "%d,", &sr
);
55 add_page(buffer
, sr
, npages
);
56 } else if ((state
& erange
) != 0) {
59 sscanf(t
, "%d-", &er
);
60 add_pages(buffer
, sr
, er
, npages
);
72 int main(int argc
, char** argv
) {
81 fprintf(stderr
, "%s nofpages [e | o] [r] [range]\n", argv
[0]);
85 sscanf(argv
[1], "%d", &npages
);
87 fprintf(stderr
, "Number of pages invalid.\n");
91 for (i
= 2; i
< argc
; ++i
) {
92 if (strcmp(argv
[i
], "r") == 0) {
94 } else if (strcmp(argv
[i
], "e") == 0) {
96 } else if (strcmp(argv
[i
], "o") == 0) {
102 if (e
== 1 && o
== 1) {
109 /* if buffer[i] is zero, i is in the range. */
110 char* buffer
= calloc(1, npages
);
111 if (buffer
== NULL
) {
112 fprintf(stderr
, "Sorry, internal error.\n");
115 apply_range(buffer
, npages
, range
);
117 for (i
= 1; i
< npages
; i
+= 2) {
122 for (i
= 2; i
< npages
; i
+= 2) {
126 buffer
[0] = 1; /* page zero does not exist. */
128 for (i
= 0; i
< npages
; ++i
) {
129 if (buffer
[i
] == 0) printf("%d ", i
);
132 for (i
=npages
; i
> 0; ) {
134 if (buffer
[i
] == 0) printf("%d ", i
);