1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/* hexedit -- Hexadecimal Editor for Binary Files
Copyright (C) 1998 Pixel (Pascal Rigaux)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
#include "hexedit.h"
void openFile(void)
{
struct stat st;
if (!is_file(fileName)) {
fprintf(stderr, "%s: %s: Not a file.\n", progName, fileName);
exit(1);
}
/* edited should be cleaned here (assert(edited == NULL)) */
if ((fd = open(fileName, O_RDWR)) == -1) {
isReadOnly = TRUE;
if ((fd = open(fileName, O_RDONLY)) == -1) {
if (page) exitCurses();
if (fileName[0] == '-') DIE(usage);
fprintf(stderr, "%s: ", progName);
perror(fileName);
exit(1);
}
} else isReadOnly = FALSE;
baseName = basename(fileName);
mark_set = FALSE;
lastEditedLoc = base = cursor = cursorOffset = 0;
/* check file size- doesn't work on devices. I considered implementing a conquer-and-divide algorithm, but it would unpleasant on tape drives and such. */
if (fstat(fd, &st) != -1 && st.st_size > 0)
fileSize = st.st_size;
else {
#ifdef BLKGETSIZE
unsigned long i;
if (ioctl(fd, BLKGETSIZE, &i) == 0)
fileSize = (INT) i * 512;
else
#endif
fileSize = 0;
}
biggestLoc = fileSize;
}
void readFile(void)
{
typePage *p;
INT i;
memset(buffer, 0, page * sizeof(*buffer));
LSEEK(fd, base);
nbBytes = read(fd, buffer, page);
if (nbBytes < 0)
nbBytes = 0;
else if (nbBytes && base + nbBytes > biggestLoc)
biggestLoc = base + nbBytes;
memset(bufferAttr, A_NORMAL, page * sizeof(*bufferAttr));
for (p = edited; p; p = p->next) {
for (i = MAX(base, p->base); i < MIN(p->base + p->size, base + page); i++) {
if (buffer[i - base] != p->vals[i - p->base]) {
buffer[i - base] = p->vals[i - p->base];
bufferAttr[i - base] |= MODIFIED;
}
}
if (p->base + p->size > base + nbBytes) { /* Check for modifications past EOF */
for(; p->base + p->size > base + nbBytes && nbBytes < page; nbBytes++)
bufferAttr[nbBytes] |= MODIFIED;
}
}
if (mark_set) markSelectedRegion();
}
int findFile(void)
{
char *p, tmp[BLOCK_SEARCH_SIZE];
p = lastFindFile ? strdup(lastFindFile) : NULL;
if (!displayMessageAndGetString("File name: ", &p, tmp, sizeof(tmp))) return FALSE;
if (!is_file(tmp)) return FALSE;
FREE(lastFindFile); lastFindFile = fileName;
fileName = p;
return TRUE;
}
INT getfilesize(void)
{
return MAX(lastEditedLoc, biggestLoc);
}
/* tryloc:
* returns TRUE if loc is an apropriate location to place the cursor
* assumes the file won't shrink.
* returns TRUE if the cursor is one past EOF to allow appending
*/
int tryloc(INT loc)
{
char c;
if (loc < 0)
return FALSE;
if (loc <= lastEditedLoc)
return TRUE;
if (loc <= biggestLoc)
return TRUE;
if (LSEEK_(fd, loc - 1) != -1 && /* don't have to worry about loc - 1 < 0 */
read(fd, &c, 1) == 1) {
biggestLoc = loc;
return TRUE;
}
return FALSE;
}
int is_file(char *name)
{
struct stat st;
return stat(name, &st) != -1 && !S_ISDIR(st.st_mode);
}
|