1 #include <limits.h> /* for CHAR_BIT */
4 #include <string.h> /* for memset() */
6 int is_blk_free(const unsigned char *array
, unsigned int nblk
);
7 void print_blks(const unsigned char *array
, unsigned int nblks
);
8 void diep(const char *s
);
10 int main(int argc
, char *argv
[])
13 unsigned int i
, narr
, nblks
, u
;
15 /* Check argument count */
17 fprintf(stderr
, "Usage: %s nblocks\n", argv
[0]);
21 /* Get number of blocks */
22 nblks
= (unsigned int) strtoul(argv
[1], NULL
, 10);
23 u
= nblks
- CHAR_BIT
* (nblks
/ CHAR_BIT
);
25 narr
= nblks
/ CHAR_BIT
;
27 narr
= nblks
/ CHAR_BIT
+ 1;
28 printf("nblks = %u\tnarr = %u\n", nblks
, narr
);
30 /* Allocate memory for char bitmap */
35 /* Initialize all bits to 0 representing free blocks */
36 memset(array
, 0, narr
);
38 /* Print blocks' status */
39 print_blks(array
, nblks
);
41 /* Flag all blocks as used */
42 for (i
= 0; i
< narr
; i
++)
44 print_blks(array
, nblks
);
52 int is_blk_free(const unsigned char *array
, unsigned int nblk
)
56 idx
= nblk
/ CHAR_BIT
;
57 u
= nblk
- CHAR_BIT
* idx
;
58 printf("blk %u is in array %u and bitpos %u\n",
61 return ((array
[idx
] & (1 << u
)) == 0);
64 void print_blks(const unsigned char *array
, unsigned int nblks
)
68 for (i
= 0; i
< nblks
; i
++)
69 printf("blk[%04u] = %s\n",
70 i
, is_blk_free(array
, i
) ? "FREE" : "USED");
73 void diep(const char *s
)