fix fd leak
[rofl0r-hexedit0r.git] / hexedit.h
bloba1684ffad837289b620736e04097c73b9d0853c5
1 #ifndef HEXEDIT_H
2 #define HEXEDIT_H
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <curses.h>
13 #include <ctype.h>
14 #include <signal.h>
15 #include <sys/wait.h>
16 #include <errno.h>
17 #if HAVE_LIBGEN_H
18 #include <libgen.h>
19 #endif
20 #if HAVE_SYS_MOUNT_H
21 #include <sys/mount.h> /* for BLKGETSIZE */
22 #endif
24 void LSEEK(int fd, off_t where);
25 int LSEEK_(int fd, off_t where);
27 /*******************************************************************************/
28 /* Macros */
29 /*******************************************************************************/
30 #define BIGGEST_COPYING (1 * 1024 * 1024)
31 #define BLOCK_SEARCH_SIZE (4 * 1024)
32 #define SECTOR_SIZE ((off_t) 512)
33 #ifndef CTRL
34 #define CTRL(c) ((c) & 0x1F)
35 #endif
36 #define ALT(c) ((c) | 0xa0)
37 #define DIE(M) { fprintf(stderr, M, progName); exit(1); }
38 #define FREE(p) if (p) free(p)
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 #define MAX(a, b) ((a) > (b) ? (a) : (b))
41 #define NORMAL A_NORMAL
42 #define MARKED A_REVERSE
43 #define MODIFIED A_BOLD
44 #define ATTRPRINTW(attr, a) do { if (oldattr != (attr)) attrset(attr), oldattr = (attr); printw a; } while (0)
45 #define MAXATTRPRINTW(attr, a) do { if (oldattr & (~(attr))) attrset(attr & oldattr), oldattr &= (attr); printw a; } while (0)
46 #define PRINTW(a) ATTRPRINTW(NORMAL, a)
47 #ifndef getnstr
48 #define getnstr(str, n) wgetnstr(stdscr, str, n)
49 #endif
52 /*******************************************************************************/
53 /* Configuration parameters */
54 /*******************************************************************************/
55 typedef enum { bySector, maximized, LAST } modeType;
56 typedef struct {
57 int blocSize, lineLength, page;
58 } modeParams;
60 extern modeParams modes[LAST];
61 extern modeType mode;
62 extern int colored;
63 extern char *usage;
65 #define pressAnyKey "(press any key)"
68 /*******************************************************************************/
69 /* Pages handling */
70 /*******************************************************************************/
71 typedef struct _typePage {
72 struct _typePage *next;
74 off_t base;
75 int size;
76 unsigned char *vals;
77 } typePage;
80 /*******************************************************************************/
81 /* Global variables */
82 /*******************************************************************************/
84 extern off_t lastEditedLoc, biggestLoc, fileSize;
85 extern off_t mark_min, mark_max, mark_set;
86 extern off_t base, oldbase;
87 extern int normalSpaces, cursor, cursorOffset, hexOrAscii;
88 extern int cursor, blocSize, lineLength, colsUsed, page;
89 extern int isReadOnly, fd, nbBytes, oldcursor, oldattr, oldcursorOffset;
90 extern int sizeCopyBuffer, *bufferAttr;
91 extern char *progName, *fileName, *baseName;
92 extern unsigned char *buffer, *copyBuffer;
93 extern typePage *edited;
95 extern char *lastFindFile, *lastYankToAFile, *lastAskHexString, *lastAskAsciiString, *lastFillWithStringHexa, *lastFillWithStringAscii;
98 /*******************************************************************************/
99 /* Miscellaneous functions declaration */
100 /*******************************************************************************/
101 off_t getfilesize(void);
102 int key_to_function(int key);
103 void init(void);
104 void quit(void);
105 int tryloc(off_t loc);
106 void openFile(void);
107 void readFile(void);
108 int findFile(void);
109 int computeLineSize(void);
110 int computeCursorXCurrentPos(void);
111 int computeCursorXPos(int cursor, int hexOrAscii);
112 void updateMarked(void);
113 int ask_about_save(void);
114 int ask_about_save_and_redisplay(void);
115 void ask_about_save_and_quit(void);
116 int setTo(int c);
117 void setToChar(int i, unsigned char c);
119 /*******************************************************************************/
120 /* Pages handling functions declaration */
121 /*******************************************************************************/
122 void discardEdited(void);
123 void addToEdited(off_t base, int size, unsigned char *vals);
124 void removeFromEdited(off_t base, int size);
125 typePage *newPage(off_t base, int size);
126 void freePage(typePage *page);
129 /*******************************************************************************/
130 /* Cursor manipulation function declarations */
131 /*******************************************************************************/
132 int move_cursor(off_t delta);
133 int set_cursor(off_t loc);
134 int move_base(off_t delta);
135 int set_base(off_t loc);
137 /*******************************************************************************/
138 /* Curses functions declaration */
139 /*******************************************************************************/
140 void initCurses(void);
141 void exitCurses(void);
142 void display(void);
143 void displayLine(int offset, int max);
144 void clr_line(int line);
145 void displayCentered(char *msg, int line);
146 void displayOneLineMessage(char *msg);
147 void displayTwoLineMessage(char *msg1, char *msg2);
148 void displayMessageAndWaitForKey(char *msg);
149 int displayMessageAndGetString(char *msg, char **last, char *p, int p_size);
150 void ungetstr(char *s);
151 int get_number(off_t *i);
154 /*******************************************************************************/
155 /* Search functions declaration */
156 /*******************************************************************************/
157 void search_forward(void);
158 void search_backward(void);
161 /*******************************************************************************/
162 /* Mark functions declaration */
163 /*******************************************************************************/
164 void markRegion(off_t a, off_t b);
165 void unmarkRegion(off_t a, off_t b);
166 void markSelectedRegion(void);
167 void unmarkAll(void);
168 void markIt(int i);
169 void unmarkIt(int i);
170 void copy_region(void);
171 void yank(void);
172 void yank_to_a_file(void);
173 void fill_with_string(void);
176 /*******************************************************************************/
177 /* Small common functions declaration */
178 /*******************************************************************************/
179 int streq(const char *s1, const char *s2);
180 int strbeginswith(const char *a, const char *prefix);
181 int myceil(int a, int b);
182 off_t myfloor(off_t a, off_t b);
183 int setLowBits(int p, int val);
184 int setHighBits(int p, int val);
185 char *strconcat3(char *a, char *b, char *c);
186 int hexCharToInt(int c);
187 int not(int b);
188 char *mymemmem(char *a, int sizea, char *b, int sizeb);
189 char *mymemrmem(char *a, int sizea, char *b, int sizeb);
190 int is_file(char *name);
191 int hexStringToBinString(char *p, int *l);
193 #endif /* HEXEDIT_H */