-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathfile.c
More file actions
180 lines (151 loc) · 4.47 KB
/
file.c
File metadata and controls
180 lines (151 loc) · 4.47 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* 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 openDiffFile(void) {
struct stat st;
if (!is_file(difffileName)) {
fprintf(stderr, "%s: %s: Not a file.\n", progName, difffileName);
exit(1);
}
if ((difffd = open(difffileName, O_RDONLY)) == -1) {
if (page)
exitCurses();
if (difffileName[0] == '-')
DIE(usage);
fprintf(stderr, "%s: ", progName);
perror(difffileName);
exit(1);
}
if (fstat(difffd, &st) != -1 && st.st_size > 0)
diffFileSize = st.st_size;
else {
#ifdef BLKGETSIZE
unsigned long i;
if (ioctl(difffd, BLKGETSIZE, &i) == 0)
diffFileSize = (INT)i * 512;
else
#endif
diffFileSize = 0;
}
}
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();
if(difffileName && *difffileName != '\0')
readDiffFile();
}
void readDiffFile(void) {
typePage *p;
INT i;
memset(diffBuffer, 0, page * sizeof(*diffBuffer));
LSEEK(difffd, base);
diffnbBytes = read(difffd, diffBuffer, page);
}
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);
}