diff options
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/hexedit.c | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/miscutils/hexedit.c b/miscutils/hexedit.c index eaf4ba57b..bdb7683f2 100644 --- a/miscutils/hexedit.c +++ b/miscutils/hexedit.c | |||
| @@ -159,7 +159,8 @@ static void redraw_cur_line(void) | |||
| 159 | ); | 159 | ); |
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | static void remap(unsigned cur_pos) | 162 | /* if remappers return 0, no change was done */ |
| 163 | static int remap(unsigned cur_pos) | ||
| 163 | { | 164 | { |
| 164 | if (G.baseaddr) | 165 | if (G.baseaddr) |
| 165 | munmap(G.baseaddr, G_mapsize); | 166 | munmap(G.baseaddr, G_mapsize); |
| @@ -184,14 +185,15 @@ static void remap(unsigned cur_pos) | |||
| 184 | /* we do have a mapped byte which is past eof */ | 185 | /* we do have a mapped byte which is past eof */ |
| 185 | G.eof_byte = G.baseaddr + (G.size - G.offset); | 186 | G.eof_byte = G.baseaddr + (G.size - G.offset); |
| 186 | } | 187 | } |
| 188 | return 1; | ||
| 187 | } | 189 | } |
| 188 | static void move_mapping_further(void) | 190 | static int move_mapping_further(void) |
| 189 | { | 191 | { |
| 190 | unsigned pos; | 192 | unsigned pos; |
| 191 | unsigned pagesize; | 193 | unsigned pagesize; |
| 192 | 194 | ||
| 193 | if ((G.size - G.offset) < G_mapsize) | 195 | if ((G.size - G.offset) < G_mapsize) |
| 194 | return; /* can't move mapping even further, it's at the end already */ | 196 | return 0; /* can't move mapping even further, it's at the end already */ |
| 195 | 197 | ||
| 196 | pagesize = getpagesize(); /* constant on most arches */ | 198 | pagesize = getpagesize(); /* constant on most arches */ |
| 197 | pos = G.current_byte - G.baseaddr; | 199 | pos = G.current_byte - G.baseaddr; |
| @@ -205,16 +207,17 @@ static void move_mapping_further(void) | |||
| 205 | } | 207 | } |
| 206 | pos -= pagesize; | 208 | pos -= pagesize; |
| 207 | } while (pos >= pagesize); | 209 | } while (pos >= pagesize); |
| 208 | remap(pos); | 210 | return remap(pos); |
| 209 | } | 211 | } |
| 212 | return 0; | ||
| 210 | } | 213 | } |
| 211 | static void move_mapping_lower(void) | 214 | static int move_mapping_lower(void) |
| 212 | { | 215 | { |
| 213 | unsigned pos; | 216 | unsigned pos; |
| 214 | unsigned pagesize; | 217 | unsigned pagesize; |
| 215 | 218 | ||
| 216 | if (G.offset == 0) | 219 | if (G.offset == 0) |
| 217 | return; /* we are at 0 already */ | 220 | return 0; /* we are at 0 already */ |
| 218 | 221 | ||
| 219 | pagesize = getpagesize(); /* constant on most arches */ | 222 | pagesize = getpagesize(); /* constant on most arches */ |
| 220 | pos = G.current_byte - G.baseaddr; | 223 | pos = G.current_byte - G.baseaddr; |
| @@ -229,7 +232,7 @@ static void move_mapping_lower(void) | |||
| 229 | } | 232 | } |
| 230 | pos -= pagesize; | 233 | pos -= pagesize; |
| 231 | 234 | ||
| 232 | remap(pos); | 235 | return remap(pos); |
| 233 | } | 236 | } |
| 234 | 237 | ||
| 235 | //usage:#define hexedit_trivial_usage | 238 | //usage:#define hexedit_trivial_usage |
| @@ -292,9 +295,8 @@ int hexedit_main(int argc UNUSED_PARAM, char **argv) | |||
| 292 | case '0': case '1': case '2': case '3': case '4': | 295 | case '0': case '1': case '2': case '3': case '4': |
| 293 | case '5': case '6': case '7': case '8': case '9': | 296 | case '5': case '6': case '7': case '8': case '9': |
| 294 | if (G.current_byte == G.eof_byte) { | 297 | if (G.current_byte == G.eof_byte) { |
| 295 | move_mapping_further(); | 298 | if (!move_mapping_further()) { |
| 296 | if (G.current_byte == G.eof_byte) { | 299 | /* already at EOF; extend the file */ |
| 297 | /* extend the file */ | ||
| 298 | if (++G.size <= 0 /* overflow? */ | 300 | if (++G.size <= 0 /* overflow? */ |
| 299 | || ftruncate(G.fd, G.size) != 0 /* error extending? (e.g. block dev) */ | 301 | || ftruncate(G.fd, G.size) != 0 /* error extending? (e.g. block dev) */ |
| 300 | ) { | 302 | ) { |
| @@ -343,7 +345,7 @@ int hexedit_main(int argc UNUSED_PARAM, char **argv) | |||
| 343 | if (G.current_byte >= G.eof_byte) { | 345 | if (G.current_byte >= G.eof_byte) { |
| 344 | move_mapping_further(); | 346 | move_mapping_further(); |
| 345 | if (G.current_byte > G.eof_byte) { | 347 | if (G.current_byte > G.eof_byte) { |
| 346 | /* eof - don't allow going past it */ | 348 | /* _after_ eof - don't allow this */ |
| 347 | G.current_byte -= 16; | 349 | G.current_byte -= 16; |
| 348 | break; | 350 | break; |
| 349 | } | 351 | } |
| @@ -368,8 +370,7 @@ int hexedit_main(int argc UNUSED_PARAM, char **argv) | |||
| 368 | if ((0xf & (uintptr_t)G.current_byte) == 0) { | 370 | if ((0xf & (uintptr_t)G.current_byte) == 0) { |
| 369 | /* leftmost pos, wrap to prev line */ | 371 | /* leftmost pos, wrap to prev line */ |
| 370 | if (G.current_byte == G.baseaddr) { | 372 | if (G.current_byte == G.baseaddr) { |
| 371 | move_mapping_lower(); | 373 | if (!move_mapping_lower()) |
| 372 | if (G.current_byte == G.baseaddr) | ||
| 373 | break; /* first line, don't do anything */ | 374 | break; /* first line, don't do anything */ |
| 374 | } | 375 | } |
| 375 | G.half = 1; | 376 | G.half = 1; |
| @@ -386,9 +387,8 @@ int hexedit_main(int argc UNUSED_PARAM, char **argv) | |||
| 386 | case KEYCODE_UP: | 387 | case KEYCODE_UP: |
| 387 | k_up: | 388 | k_up: |
| 388 | if ((G.current_byte - G.baseaddr) < 16) { | 389 | if ((G.current_byte - G.baseaddr) < 16) { |
| 389 | move_mapping_lower(); | 390 | if (!move_mapping_lower()) |
| 390 | if ((G.current_byte - G.baseaddr) < 16) | 391 | break; /* already at 0, stop */ |
| 391 | break; | ||
| 392 | } | 392 | } |
| 393 | G.current_byte -= 16; | 393 | G.current_byte -= 16; |
| 394 | up: | 394 | up: |
