]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - ext2ed/blockbitmap_com.c
ext2fs: nls: support UTF-8 11.0 with NFKD normalization
[thirdparty/e2fsprogs.git] / ext2ed / blockbitmap_com.c
CommitLineData
583a1ce5
TT
1/*
2
3/usr/src/ext2ed/blockbitmap_com.c
4
5A part of the extended file system 2 disk editor.
6
7-------------------------
8Handles the block bitmap.
9-------------------------
10
11This file implements the commands which are specific to the blockbitmap type.
12
13First written on: July 5 1995
14
15Copyright (C) 1995 Gadi Oxman
16
17*/
18
d1154eb4 19#include "config.h"
583a1ce5
TT
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "ext2ed.h"
25
26/*
27
28The functions in this file use the flobal structure block_bitmap_info. This structure contains the current
29position in the bitmap.
30
31*/
32
33void type_ext2_block_bitmap___entry (char *command_line)
34
35/*
36
37This function changes the current entry in the bitmap. It just changes the entry_num variable in block_bitmap_info
38and dispatches a show command to show the new entry.
39
40*/
41
42{
43 unsigned long entry_num;
44 char *ptr,buffer [80];
efc6f628
TT
45
46
47
583a1ce5
TT
48 ptr=parse_word (command_line,buffer); /* Get the requested entry */
49 if (*ptr==0) {
50 wprintw (command_win,"Error - No argument specified\n");
51 refresh_command_win (); return;
52 }
53 ptr=parse_word (ptr,buffer);
54
55 entry_num=atol (buffer);
56
efc6f628 57
583a1ce5
TT
58 if (entry_num >= file_system_info.super_block.s_blocks_per_group) { /* Check if it is a valid entry number */
59
60 wprintw (command_win,"Error - Entry number out of bounds\n");
61 refresh_command_win ();return;
62 }
efc6f628
TT
63
64
65
583a1ce5
TT
66 block_bitmap_info.entry_num=entry_num; /* If it is, just change entry_num and */
67 strcpy (buffer,"show");dispatch (buffer); /* dispatch a show command */
68}
69
70void type_ext2_block_bitmap___next (char *command_line)
71
72/*
73
74This function passes to the next entry in the bitmap. We just call the above entry command.
75
76*/
77
78{
79 long entry_offset=1;
80 char *ptr,buffer [80];
efc6f628 81
583a1ce5
TT
82 ptr=parse_word (command_line,buffer);
83 if (*ptr!=0) {
84 ptr=parse_word (ptr,buffer);
85 entry_offset=atol (buffer);
86 }
87
88 sprintf (buffer,"entry %ld",block_bitmap_info.entry_num+entry_offset);
89 dispatch (buffer);
90}
91
92void type_ext2_block_bitmap___prev (char *command_line)
93
94{
95 long entry_offset=1;
96 char *ptr,buffer [80];
efc6f628 97
583a1ce5
TT
98 ptr=parse_word (command_line,buffer);
99 if (*ptr!=0) {
100 ptr=parse_word (ptr,buffer);
101 entry_offset=atol (buffer);
102 }
103
104 sprintf (buffer,"entry %ld",block_bitmap_info.entry_num-entry_offset);
105 dispatch (buffer);
106}
107
108void type_ext2_block_bitmap___allocate (char *command_line)
109
110/*
111
112This function starts allocating block from the current position. Allocating involves setting the correct bits
113in the bitmap. This function is a vector version of allocate_block below - We just run on the blocks that
114we need to allocate, and call allocate_block for each one.
115
116*/
117
118{
119 long entry_num,num=1;
120 char *ptr,buffer [80];
efc6f628 121
583a1ce5
TT
122 ptr=parse_word (command_line,buffer); /* Get the number of blocks to allocate */
123 if (*ptr!=0) {
124 ptr=parse_word (ptr,buffer);
125 num=atol (buffer);
126 }
efc6f628 127
583a1ce5
TT
128 entry_num=block_bitmap_info.entry_num;
129 /* Check for limits */
130 if (num > file_system_info.super_block.s_blocks_per_group-entry_num) {
efc6f628
TT
131 wprintw (command_win,"Error - There aren't that much blocks in the group\n");
132 refresh_command_win ();return;
583a1ce5 133 }
efc6f628 134
583a1ce5
TT
135 while (num) { /* And call allocate_block */
136 allocate_block (entry_num); /* for each block */
137 num--;entry_num++;
138 }
efc6f628 139
583a1ce5
TT
140 dispatch ("show"); /* Show the result */
141}
142
143void type_ext2_block_bitmap___deallocate (char *command_line)
144
145/* This is the opposite of the above function - We call deallocate_block instead of allocate_block */
146
147{
148 long entry_num,num=1;
149 char *ptr,buffer [80];
efc6f628 150
583a1ce5
TT
151 ptr=parse_word (command_line,buffer);
152 if (*ptr!=0) {
153 ptr=parse_word (ptr,buffer);
154 num=atol (buffer);
155 }
efc6f628 156
583a1ce5
TT
157 entry_num=block_bitmap_info.entry_num;
158 if (num > file_system_info.super_block.s_blocks_per_group-entry_num) {
efc6f628
TT
159 wprintw (command_win,"Error - There aren't that much blocks in the group\n");
160 refresh_command_win ();return;
583a1ce5 161 }
efc6f628 162
583a1ce5
TT
163 while (num) {
164 deallocate_block (entry_num);
165 num--;entry_num++;
166 }
efc6f628 167
583a1ce5
TT
168 dispatch ("show");
169}
170
171
172void allocate_block (long entry_num)
173
174/* In this function we convert the bit number into the right byte and inner bit positions. */
175
176{
177 unsigned char bit_mask=1;
178 int byte_offset,j;
efc6f628 179
583a1ce5
TT
180 byte_offset=entry_num/8; /* Find the correct byte - entry_num/8 */
181 /* The position inside the byte is entry_num %8 */
182 for (j=0;j<entry_num%8;j++)
183 bit_mask*=2; /* Generate the or mask - 1 at the right place */
184 type_data.u.buffer [byte_offset] |= bit_mask; /* And apply it */
185}
186
187void deallocate_block (long entry_num)
188
189/* This is the opposite of allocate_block above. We use an and mask instead of an or mask. */
190
191{
192 unsigned char bit_mask=1;
193 int byte_offset,j;
efc6f628 194
583a1ce5
TT
195 byte_offset=entry_num/8;
196 for (j=0;j<entry_num%8;j++)
197 bit_mask*=2;
198 bit_mask^=0xff;
efc6f628 199
583a1ce5
TT
200 type_data.u.buffer [byte_offset] &= bit_mask;
201}
202
203void type_ext2_block_bitmap___show (char *command_line)
204
205/*
206
207We show the bitmap as a series of bits, grouped at 8-bit intervals. We display 8 such groups on each line.
208The current position (as known from block_bitmap_info.entry_num) is highlighted.
209
210*/
211
212{
213 int i,j;
214 unsigned char *ptr;
215 unsigned long block_num,entry_num;
216
217 ptr=type_data.u.buffer;
218 show_pad_info.line=0;show_pad_info.max_line=-1;
efc6f628 219
583a1ce5
TT
220 wmove (show_pad,0,0);
221 for (i=0,entry_num=0;i<file_system_info.super_block.s_blocks_per_group/8;i++,ptr++) {
222 for (j=1;j<=128;j*=2) { /* j contains the and bit mask */
223 if (entry_num==block_bitmap_info.entry_num) { /* Highlight the current entry */
224 wattrset (show_pad,A_REVERSE);
225 show_pad_info.line=show_pad_info.max_line-show_pad_info.display_lines/2;
226 }
227
228 if ((*ptr) & j) /* Apply the mask */
229 wprintw (show_pad,"1");
230 else
231 wprintw (show_pad,"0");
232
233 if (entry_num==block_bitmap_info.entry_num)
234 wattrset (show_pad,A_NORMAL);
235
236 entry_num++; /* Pass to the next entry */
237 }
238 wprintw (show_pad," ");
239 if (i%8==7) { /* Display 8 groups in a row */
240 wprintw (show_pad,"\n");
241 show_pad_info.max_line++;
242 }
243 }
efc6f628
TT
244
245 refresh_show_pad ();
583a1ce5
TT
246 show_info (); /* Show the usual information */
247
248 /* Show the group number */
249 wmove (show_win,1,0);
250 wprintw (show_win,"Block bitmap of block group %ld\n",block_bitmap_info.group_num);
251 /* Show the block number */
efc6f628 252
583a1ce5 253 block_num=block_bitmap_info.entry_num+block_bitmap_info.group_num*file_system_info.super_block.s_blocks_per_group;
efc6f628 254 block_num+=file_system_info.super_block.s_first_data_block;
583a1ce5
TT
255
256 wprintw (show_win,"Status of block %ld - ",block_num); /* and the allocation status */
257 ptr=type_data.u.buffer+block_bitmap_info.entry_num/8;
258 j=1;
259 for (i=block_bitmap_info.entry_num % 8;i>0;i--)
260 j*=2;
efc6f628 261 if ((*ptr) & j)
583a1ce5
TT
262 wprintw (show_win,"Allocated\n");
263 else
264 wprintw (show_win,"Free\n");
265 refresh_show_win ();
266}