Skip to content

Commit

Permalink
Merge pull request #5 from imrivera/master
Browse files Browse the repository at this point in the history
Fixes for large file support and long paths
  • Loading branch information
LonnyGomes committed Mar 18, 2014
2 parents e22b76d + 55aa04a commit 9f622e9
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 69 deletions.
17 changes: 8 additions & 9 deletions include/hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,19 @@ extern char EBCDIC[256];
#define MIN_COLS 70 /* screen has to be 70< cols */
#define MIN_LINES 7 /* 8 - 1 */ /* the slk crap minuses 1 line*/
#define KEY_TAB 9 /* value for the tab key */
#define FN_LEN 255

#define AlphabetSize (UCHAR_MAX +1) /* for portability */

#ifndef max
#define max(a,b) ((a) >(b) ? (a) : (b))
#endif

FILE *fpIN, *fpOUT; /* global file ptrs */
FILE *fpIN; /* global file ptr */

/* function prototypes */

/* acceptch.c */
int wacceptch(WINS *windows, off_t len, char *fpINfilename, char *fpOUTfilename);
int wacceptch(WINS *windows, off_t len);
void restoreBorder(WINS *win);
char *inputLine(WINDOW *win, int line, int col);

Expand All @@ -140,8 +139,8 @@ void outline(FILE *fp, off_t linenum);
off_t maxLoc(FILE *fp);
void print_usage();
off_t maxLines(off_t len);
int openfile(WINS *win, char *fpINfilename);
void savefile(WINS *win, char *fpINfilename, char *fpOUTfilename);
int openfile(WINS *win);
void savefile(WINS *win);
off_t hexSearch(FILE *fp, int ch[], off_t startfp, int length);
off_t gotoLine(FILE *fp, off_t currLoc, off_t gotoLoc, off_t maxlines, WINDOW *windows);
int getLocVal(off_t loc);
Expand All @@ -151,16 +150,16 @@ bool inHexList(off_t loc);
int hgetopt(int argc, char *const *argv, const char *optstring);

/* hexcurse.c */
off_t parseArgs(int argc, char *argv[], char *fpINfilename, char *fpOUTfilename);
off_t parseArgs(int argc, char *argv[]);
/*void printDebug(hexList *head, int loc);*/
int getMinimumAddressLength(off_t len);
void catchSegfault(int sig);
RETSIGTYPE catchSegfault(int sig);

/* llist.c */
hexList *deleteNode(hexList *head, off_t loc);
hexList *insertItem(hexList *head, off_t loc, int val);
int searchList(hexList *head, off_t loc);
int writeChanges(FILE *fpIN, FILE *fpOUT, char *fpINfilename, char *fpOUTfilename);
int writeChanges();
hexList *freeList(hexList *head);

/* screen.c */
Expand All @@ -170,7 +169,7 @@ void exit_err(char *err_str);
void init_screen(void);
void screen_exit(int exit_val);
void init_fkeys();
void checkScreenSize(int sig);
RETSIGTYPE checkScreenSize(int sig);
void refreshall(WINS *win);
WINDOW *drawbox(int y, int x, int height, int width);
void scrollbar(WINS *windows, int currentLine, long maxLines);
Expand Down
28 changes: 20 additions & 8 deletions src/acceptch.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ off_t LastLoc; /*last cursor location*/
bool editHex; /* flag to edit h or a*/
int SIZE_CH; /* global winch flag */

int wacceptch(WINS *win, off_t len, char *fpINfilename, char *fpOUTfilename)
int wacceptch(WINS *win, off_t len)
{
intmax_t tmp_max;

off_t count;
int row = 0, col = 0, val, tmpval, /* counters, etc. */
int col = 0, val, tmpval, /* counters, etc. */
ch[17], /* holds search string*/
eol = (BASE * 3) - 1, /* end of line pos */
save = 0, /* to save on exit */
Expand All @@ -45,7 +45,8 @@ int wacceptch(WINS *win, off_t len, char *fpINfilename, char *fpOUTfilename)
off_t cl, /* current loc in file*/
gotoLoc = 0, /* goto location */
lastLine = 0, /* line b4 LastLine */
currentLine = 0; /* current line value */
currentLine = 0, /* current line value */
row = 0;

char *gotoLocStr, /* convert to gotoLoc */
*temp,
Expand Down Expand Up @@ -390,7 +391,7 @@ int wacceptch(WINS *win, off_t len, char *fpINfilename, char *fpOUTfilename)
case KEY_F(3): /* if F3 or ^o... */


if (openfile(win, fpINfilename)) /* open file */
if (openfile(win)) /* open file */
{
if (fpIN)
{
Expand All @@ -403,14 +404,14 @@ int wacceptch(WINS *win, off_t len, char *fpINfilename, char *fpOUTfilename)

case CTRL_AND('s'): /* if F2 or ^s... */
case KEY_F(2): /* save the file */
savefile(win, fpINfilename, fpOUTfilename);
savefile(win);
break;

case CTRL_AND('f'):
case KEY_F(5):
/* SeachStr stores the last searched string into the format *\
\* "(XXXXXXX...)" with 10 being the max chars shown */
if (!strcmp(fpINfilename, ""))
if (!fpINfilename || !strcmp(fpINfilename, ""))
{ /* output prompt */
wmove(win->hex_outline, LINES-1, 1);
wclrtoeol(win->hex_outline);
Expand Down Expand Up @@ -770,8 +771,12 @@ int wacceptch(WINS *win, off_t len, char *fpINfilename, char *fpOUTfilename)
doupdate(); /* update visual */
}

free(temp);
while (stack != NULL)
popStack(&stack);

/* if user chose to save on exit, value will == 2 */
if (save == 2) savefile(win, fpINfilename, fpOUTfilename);
if (save == 2) savefile(win);

return 0; /* return */
}
Expand Down Expand Up @@ -799,15 +804,22 @@ char *inputLine(WINDOW *win, int line, int col)
int x;
unsigned long int c;
char *ch;
int allocated = 81;

noecho();

ch = (char *)malloc(81); /* allocate space */
ch = (char *)malloc(allocated); /* allocate space */

wmove(win, line, col);

for (x = 0; (c = wgetch(win)) != 10; x++)
{
if (x > 0 && x >= allocated)
{
ch = (char*)realloc(ch, x + 1);
allocated = x + 1;
}

wclrtoeol(win); /* clear line */
if (c == '\b' || c == 127) /* get backspace */
{
Expand Down
52 changes: 32 additions & 20 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,11 @@ off_t maxLines(off_t len)
* Description: opens file specified at input returning *
* non-zero if the file opened correctly *
\********************************************************/
int openfile(WINS *win, char *fpINfilename)
int openfile(WINS *win)
{
char *ch;
FILE *tmpfp; /* temp pointer */

ch = (char *) malloc(81);

wmove(win->hex_outline, LINES-1, 21); /* output prompt */
wclrtoeol(win->hex_outline);
mvwaddstr(win->hex_outline, LINES - 1, 1, "Enter file to open: ");
Expand All @@ -145,11 +143,14 @@ int openfile(WINS *win, char *fpINfilename)
{
restoreBorder(win); /* restore border */
wrefresh(win->hex_outline); /* refresh window */
free(ch);
return FALSE;
}

if (ch[0] != '\0')
strncpy(fpINfilename, ch, 80);
if (ch[0] != '\0') {
free(fpINfilename);
fpINfilename = ch;
}

if (!(tmpfp = fopen(fpINfilename, "r"))) /* if cannot open... */
{
Expand Down Expand Up @@ -184,37 +185,48 @@ int openfile(WINS *win, char *fpINfilename)
* Description: saves file specified from input, and *
* doesn't return anything *
\********************************************************/
void savefile(WINS *win, char *fpINfilename, char *fpOUTfilename)
void savefile(WINS *win)
{
char *ch; /* temp string */
ch = (char *) malloc(81); /* malloc temp string */

if (!fpINfilename)
fpINfilename = (char *) malloc(81); /* allocate if NULL */

wmove(win->hex_outline, LINES-1, 20); /* clear line and */
wclrtoeol(win->hex_outline); /* output prompt */
mvwprintw(win->hex_outline, LINES-1, 1, "Enter file to save: %s",
(strcmp(fpOUTfilename, "")) ? fpOUTfilename:fpINfilename);
(fpOUTfilename != NULL && strcmp(fpOUTfilename, "")) ? fpOUTfilename:fpINfilename);

wrefresh(win->hex_outline); /* refresh window */

ch = inputLine(win->hex_outline, LINES - 1, 21); /* get filename */

if (ch[0] != 27) /*if escape wasn't hit*/
{
if (ch[0] != '\0') /* if string exist... */
strncpy(fpOUTfilename, ch, 80); /* copy into fileout */
/* if infile... */
if (!strncmp(fpOUTfilename, fpINfilename, strlen(fpINfilename)))
bzero(fpOUTfilename, strlen(fpOUTfilename)); /* clear string */

/*write to file */
if (!writeChanges(fpIN, fpOUT, fpINfilename, fpOUTfilename))
if (ch[0] == '\0') /* same filename as input */
{
free(fpOUTfilename);
fpOUTfilename = NULL;
free(ch);
}
else /* if string exist... */
{
free(fpOUTfilename);
fpOUTfilename = ch; /* copy into fileout */
/* if infile... */
if (strcmp(fpOUTfilename, fpINfilename) == 0)
{
free(fpOUTfilename);
fpOUTfilename = NULL;
}
}

/*write to file */
if (!writeChanges())
popupWin("The file has been saved.", -1);
}

else
{
free(ch);
}

restoreBorder(win);
wnoutrefresh(win->hex_outline);
}
Expand Down
31 changes: 20 additions & 11 deletions src/hexcurse.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ int hex_win_width,
int main(int argc, char *argv[]) /* main program */
{

int x, retval = 1, val; /* counters, etc. */
off_t len; /* len need to be off_t*/
int x, retval = 1; /* counters, etc. */
off_t val, len; /* len need to be off_t*/

windows = (WINS *) calloc(1, sizeof(WINS)); /* malloc windows */
head = llalloc(); /* malloc list space */
fpINfilename = (char *) malloc(FN_LEN+1); /* allocate in and */
fpOUTfilename = (char *) malloc(FN_LEN+1); /* out file name ptrs */
fpINfilename = NULL; /* allocate in and */
fpOUTfilename = NULL; /* out file name ptrs */
printHex = TRUE; /* address format */
USE_EBCDIC = FALSE; /*use ascii by default*/

/* get cmd line args */
len = parseArgs(argc, argv, fpINfilename, fpOUTfilename);
len = parseArgs(argc, argv);
MIN_ADDR_LENGTH = getMinimumAddressLength(len);

use_env(TRUE); /* use env values */
Expand Down Expand Up @@ -128,9 +128,13 @@ int main(int argc, char *argv[]) /* main program */

mvwaddch(windows->scrollbar, 1, 0, ACS_CKBOARD);/* clear scroller */
/* get user input */
retval = wacceptch(windows, len, fpINfilename, fpOUTfilename);
retval = wacceptch(windows, len);
}

free(fpINfilename);
free(fpOUTfilename);
freeList(head);

screen_exit(0); /* end visualizations */
return retval; /* return */
}
Expand Down Expand Up @@ -166,7 +170,7 @@ void printDebug(hexList *head, long int loc)
* processes them. *
* Returns: length of file *
\********************************************************/
off_t parseArgs(int argc, char *argv[], char *fpINfilename, char *fpOUTfilename)
off_t parseArgs(int argc, char *argv[])
{
extern char *optarg; /* extern vars for */
extern int optind, /*opterr,*/ optopt; /* getopt() */
Expand All @@ -181,10 +185,12 @@ off_t parseArgs(int argc, char *argv[], char *fpINfilename, char *fpOUTfilename)
case 'a': printHex = FALSE; /* decimal addresses */
break;
/* infile */
case 'i': strncpy(fpINfilename, optarg, FN_LEN);
case 'i': free(fpINfilename);
fpINfilename = strdup(optarg);
break;
/* outfile */
case 'o': strncpy(fpOUTfilename, optarg, FN_LEN);
case 'o': free(fpOUTfilename);
fpOUTfilename = strdup(optarg);
break;

case 'r': resize = atoi(optarg); /* don't resize screen*/
Expand All @@ -205,7 +211,10 @@ off_t parseArgs(int argc, char *argv[], char *fpINfilename, char *fpOUTfilename)
argv += optind;

if (argv[0])
strncpy(fpINfilename, argv[0], FN_LEN);
{
free(fpINfilename);
fpINfilename = strdup(argv[0]);
}

if (strcmp(fpINfilename, ""))
if ((fpIN = fopen(fpINfilename, "r")) == NULL)
Expand Down Expand Up @@ -237,7 +246,7 @@ int getMinimumAddressLength(off_t len)
* report. *
* Returns: length of file *
\********************************************************/
void catchSegfault(int sig)
RETSIGTYPE catchSegfault(int sig)
{
/* Avoid unused variable warning */
UNUSED(sig);
Expand Down
Loading

0 comments on commit 9f622e9

Please sign in to comment.