Start 1.33.0 development cycle
[oweals/busybox.git] / miscutils / hexedit.c
index 8334181c70980b0da78956cb45d27348836c261f..898d773766d14e9c1fd964935d3ed6b7c183e0c7 100644 (file)
@@ -4,7 +4,7 @@
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 //config:config HEXEDIT
-//config:      bool "hexedit"
+//config:      bool "hexedit (21 kb)"
 //config:      default y
 //config:      help
 //config:      Edit file in hexadecimal.
@@ -153,7 +153,8 @@ static void redraw(unsigned cursor)
                i++;
        }
 
-       printf(ESC"[%u;%uH", 1 + cursor / 16, 1 + pos + (cursor & 0xf) * 3);
+       G.row = cursor / 16;
+       printf(ESC"[%u;%uH", 1 + G.row, 1 + pos + (cursor & 0xf) * 3);
 }
 
 static void redraw_cur_line(void)
@@ -192,7 +193,7 @@ static int remap(unsigned cur_pos)
        );
        if (G.baseaddr == MAP_FAILED) {
                restore_term();
-               bb_perror_msg_and_die("mmap");
+               bb_simple_perror_msg_and_die("mmap");
        }
 
        G.current_byte = G.baseaddr + cur_pos;
@@ -367,6 +368,8 @@ int hexedit_main(int argc UNUSED_PARAM, char **argv)
                                if (G.current_byte > G.eof_byte) {
                                        /* _after_ eof - don't allow this */
                                        G.current_byte -= 16;
+                                       if (G.current_byte < G.baseaddr)
+                                               move_mapping_lower();
                                        break;
                                }
                        }
@@ -432,28 +435,27 @@ int hexedit_main(int argc UNUSED_PARAM, char **argv)
                                printf(ESC"[999;1H" CLEAR_TILL_EOL); /* go to last line */
                                if (read_line_input(NULL, "Go to (dec,0Xhex,0oct): ", buf, sizeof(buf)) > 0) {
                                        off_t t;
-                                       unsigned pgmask;
+                                       unsigned cursor;
 
                                        t = bb_strtoull(buf, NULL, 0);
                                        if (t >= G.size)
                                                t = G.size - 1;
-                                       pgmask = G_pagesize - 1;
-                                       cnt = t & pgmask;
-                                       t = t & ~(off_t)pgmask;
+                                       cursor = t & (G_pagesize - 1);
+                                       t -= cursor;
                                        if (t < 0)
-                                               cnt = t = 0;
-                                       if (t != 0 && cnt < 0x1ff) {
+                                               cursor = t = 0;
+                                       if (t != 0 && cursor < 0x1ff) {
                                                /* very close to end of page, possibly to EOF */
                                                /* move one page lower */
                                                t -= G_pagesize;
-                                               cnt += G_pagesize;
+                                               cursor += G_pagesize;
                                        }
                                        G.offset = t;
-                                       remap(cnt);
-                                       redraw(cnt);
+                                       remap(cursor);
+                                       redraw(cursor);
                                        break;
                                }
-                               /* EOF/error on input: fall through to exiting */
+                               /* ^C/EOF/error: fall through to exiting */
                        }
                case CTRL('X'):
                        restore_term();