home *** CD-ROM | disk | FTP | other *** search
- /***
- *fileio.c - disk editor file I/O
- *
- *Copyright (c) 1991-1994, Gregg Jennings. All wrongs reserved.
- * P O Box 200, Falmouth, MA 02541-0200
- *
- *Purpose:
- * File read/write functions.
- *
- *Notice:
- * This progam may be freely used and distributed. Any distrubution
- * with modifications must retain the above copyright statement and
- * modifications noted.
- * No pulp-publication, in whole or in part, permitted without
- * permission (magazines or books).
- *******************************************************************************/
-
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <fcntl.h>
- #include <io.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include <errno.h>
- #include <setjmp.h>
-
- #include "disked.h"
- #include "diskio.h"
- #include "mylib.h"
- #include "error.h"
-
- /*********** EXTERNAL DATA ***********/
-
- extern unsigned int byte_cnt; /* buffer byte count: append fileio find */
- extern unsigned int max_bytes; /* maximum buffer size: append fileio dparams */
- extern unsigned char *data_buf; /* data buffer: append fileio find */
- extern int exterror(void);
- extern char *harderr_list[];
-
- /*********** STATIC DATA ***********/
-
- static char ebuf[80];
- static FILE *fh;
- static int fd;
- static jmp_buf mark; /* Address for long jump to jump to */
- static int jmpret;
-
- /*
- I think I finally found a good use for setjmp() and longjump().
- All examples I have found is based on a simple floating point
- error handler.
- */
-
- static void fio_error(void);
-
- /*
- Append bytes into the databuffer. The bytes are from the
- sectorbuffer or a file.
-
- returns OK or ERROR if full
- */
-
- int append(int s, int m, int c, unsigned char *buffer, unsigned int nbytes)
- {
- register int ch;
- register int h;
- unsigned int i;
-
- for (i=0;i<nbytes;i++)
- {
- ch=buffer[i]&0xff;
- if (!isprint(ch) && !isspace(ch))
- {
- if (s) /* do we strip? */
- continue;
- if (m && ch>0x7f) /* do we mask ? */
- {
- data_buf[byte_cnt++]=(char)(ch&0x7f);
- continue;
- }
- if (c && byte_cnt<max_bytes-4) /* do we convert? */
- {
- data_buf[byte_cnt++] = '<';
- h = (ch>>4)&0x0f;
- data_buf[byte_cnt++] = (unsigned char)((h>9) ? (h+'a'-10) : (h+'0'));
- h = ch&0x0f;
- data_buf[byte_cnt++] = (unsigned char)((h>9) ? (h+'a'-10) : (h+'0'));
- data_buf[byte_cnt++] = '>';
- }
- else
- data_buf[byte_cnt++]=(char)ch;/* no mask, just put */
- }
- else
- data_buf[byte_cnt++]=(char)ch; /* no mask, just put */
- if (byte_cnt==max_bytes)
- return(ERROR);
- }
- return(1);
- }
-
- /*************************************************************************
- File Functions
- ************************************************************************/
-
- int putfile(char *filename, int xlate, int mode, int m, int s, int c)
- {
- register int ch;
- unsigned int i;
-
- error.func = "putfile";
- errno = 0;
- jmpret = setjmp(mark);
-
- if (jmpret == 0)
- {
- if ((fh=fopen(filename,"w+b"))==NULL)
- fio_error();
- for (i=0;i<byte_cnt;i++)
- {
- if (kbhit())
- break;
-
- ch=data_buf[i]&0xff;
- if (xlate && !isprint(ch) && !isspace(ch))
- {
- if (s) /* do we strip? */
- continue;
- else if (m) /* do we mask ? */
- {
- ch&=0x7F;
- if ((ch=putc(ch,fh))!=ch)
- break;
- }
- else if (c) /* do we convert? */
- {
- if (fprintf(fh,"<%02x>",ch)!=4)
- break;
- }
- }
- else if ((ch=putc(ch,fh))!=ch) /* no mask, just put */
- break;
- }
- if (fclose(fh) == -1)
- {
- fh = NULL;
- fio_error(); /* we wont re-display it */
- }
- return 1;
- }
- return -1;
- }
-
- int getfile(char *filename, int xlate, int m, int s, int c)
- {
- int i;
- unsigned char buffer[512];
-
- error.func = "getfile";
- errno = 0;
-
- jmpret = setjmp(mark);
-
- if (jmpret == 0)
- {
- if ((fd=open(filename,O_RDONLY|O_BINARY)) == -1)
- {
- if (error.num != -1)
- return -1;
- fio_error();
- }
- while ((i=read(fd,buffer,512)) > 0)
- {
- if (kbhit())
- break;
-
- if (xlate)
- append(s,m,c,buffer,i);
- else
- append(0,0,0,buffer,i);
- if (byte_cnt==max_bytes)
- return -2;
- }
- if (i == -1)
- fio_error();
- if (close(fd) == -1)
- fio_error();
- return 1;
- }
- return -1;
- }
-
- int putsectors(char *file, long start, int num)
- {
- int i;
-
- error.func = "putsects";
- errno = 0;
- jmpret = setjmp(mark);
- savesector();
-
- if (jmpret == 0)
- {
- if ((fd=open(file,O_CREAT|O_TRUNC|O_BINARY|O_RDWR,S_IWRITE|S_IREAD)) == ERROR)
- fio_error();
- log_sector = start-1;
- for (i=0; i < num; i++)
- {
- if (kbhit())
- break;
-
- if (Display)
- {
- putint(i);
- put(len(i),8);
- }
- nextsector();
- if (write(fd,sec_buf,sec_size) != (int)sec_size)
- fio_error();
- }
- close(fd);
- restoresector();
- return 1;
- }
- restoresector();
- return -1;
- }
-
- static void fio_error(void)
- {
- if (fh)
- fclose(fh);
- if (fd > 0)
- close(fd);
- if (!errno)
- errno = exterror();
- error.num = errno;
- if (errno < 16) /* sys_nerr is 37! */
- error.msg = sys_errlist[errno];
- else if (errno >= 19 && errno <= 31)
- error.msg = harderr_list[errno-19];
- else
- {
- error.msg = "DOS error code: ";
- sprintf(ebuf,"%02Xh",errno);
- error.arg = ebuf;
- }
- error.mod = "fileio";
- longjmp(mark,-1);
- }
-