home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 406_01 / disked25 / source / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-13  |  6.1 KB  |  252 lines

  1. /***
  2. *fileio.c - disk editor file I/O
  3. *
  4. *Copyright (c) 1991-1994, Gregg Jennings.  All wrongs reserved.
  5. *   P O Box 200, Falmouth, MA 02541-0200
  6. *
  7. *Purpose:
  8. *   File read/write functions.
  9. *
  10. *Notice:
  11. *   This progam may be freely used and distributed.  Any distrubution
  12. *   with modifications must retain the above copyright statement and
  13. *   modifications noted.
  14. *   No pulp-publication, in whole or in part, permitted without
  15. *   permission (magazines or books).
  16. *******************************************************************************/
  17.  
  18. #include <stdio.h>
  19. #include <conio.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. #include <fcntl.h>
  23. #include <io.h>
  24. #include <sys\types.h>
  25. #include <sys\stat.h>
  26. #include <errno.h>
  27. #include <setjmp.h>
  28.  
  29. #include "disked.h"
  30. #include "diskio.h"
  31. #include "mylib.h"
  32. #include "error.h"
  33.  
  34. /*********** EXTERNAL DATA ***********/
  35.  
  36. extern unsigned int byte_cnt;      /* buffer byte count: append fileio find */
  37. extern unsigned int max_bytes;     /* maximum buffer size: append fileio dparams */
  38. extern unsigned char *data_buf;    /* data buffer: append fileio find */
  39. extern int exterror(void);
  40. extern char *harderr_list[];
  41.  
  42. /*********** STATIC DATA ***********/
  43.  
  44. static char ebuf[80];
  45. static FILE *fh;
  46. static int fd;
  47. static jmp_buf mark;             /* Address for long jump to jump to */
  48. static int jmpret;
  49.  
  50. /*
  51.    I think I finally found a good use for setjmp() and longjump().
  52.    All examples I have found is based on a simple floating point
  53.    error handler.
  54. */
  55.  
  56. static void fio_error(void);
  57.  
  58. /*
  59.    Append bytes into the databuffer.  The bytes are from the
  60.    sectorbuffer or a file.
  61.  
  62.    returns OK or ERROR if full
  63. */
  64.  
  65. int append(int s, int m, int c, unsigned char *buffer, unsigned int nbytes)
  66. {
  67. register int ch;
  68. register int h;
  69. unsigned int i;
  70.  
  71.    for (i=0;i<nbytes;i++)
  72.    {
  73.       ch=buffer[i]&0xff;
  74.       if (!isprint(ch) && !isspace(ch))
  75.       {
  76.          if (s)                           /* do we strip? */
  77.             continue;
  78.          if (m && ch>0x7f)                /* do we mask ? */
  79.          {
  80.             data_buf[byte_cnt++]=(char)(ch&0x7f);
  81.             continue;
  82.          }
  83.          if (c && byte_cnt<max_bytes-4)   /* do we convert? */
  84.          {
  85.             data_buf[byte_cnt++] = '<';
  86.             h = (ch>>4)&0x0f;
  87.             data_buf[byte_cnt++] = (unsigned char)((h>9) ? (h+'a'-10) : (h+'0'));
  88.             h = ch&0x0f;
  89.             data_buf[byte_cnt++] = (unsigned char)((h>9) ? (h+'a'-10) : (h+'0'));
  90.             data_buf[byte_cnt++] = '>';
  91.          }
  92.          else
  93.             data_buf[byte_cnt++]=(char)ch;/* no mask, just put */
  94.       }
  95.       else
  96.          data_buf[byte_cnt++]=(char)ch;   /* no mask, just put */
  97.       if (byte_cnt==max_bytes)
  98.          return(ERROR);
  99.    }
  100.    return(1);
  101. }
  102.  
  103. /*************************************************************************
  104.                               File Functions
  105.  ************************************************************************/
  106.  
  107. int putfile(char *filename, int xlate, int mode, int m, int s, int c)
  108. {
  109. register int ch;
  110. unsigned int i;
  111.  
  112.    error.func = "putfile";
  113.    errno = 0;
  114.    jmpret = setjmp(mark);
  115.  
  116.    if (jmpret == 0)
  117.    {
  118.       if ((fh=fopen(filename,"w+b"))==NULL)
  119.          fio_error();
  120.       for (i=0;i<byte_cnt;i++)
  121.       {
  122.          if (kbhit())
  123.             break;
  124.  
  125.          ch=data_buf[i]&0xff;
  126.          if (xlate && !isprint(ch) && !isspace(ch))
  127.          {
  128.             if (s)                  /* do we strip? */
  129.                continue;
  130.             else if (m)             /* do we mask ? */
  131.             {
  132.                ch&=0x7F;
  133.                if ((ch=putc(ch,fh))!=ch)
  134.                   break;
  135.             }
  136.             else if (c)             /* do we convert? */
  137.             {
  138.                if (fprintf(fh,"<%02x>",ch)!=4)
  139.                   break;
  140.             }
  141.          }
  142.          else if ((ch=putc(ch,fh))!=ch)   /* no mask, just put */
  143.             break;
  144.       }
  145.       if (fclose(fh) == -1)
  146.       {
  147.          fh = NULL;
  148.          fio_error();               /* we wont re-display it */
  149.       }
  150.       return 1;
  151.    }
  152.    return -1;
  153. }
  154.  
  155. int getfile(char *filename, int xlate, int m, int s, int c)
  156. {
  157. int i;
  158. unsigned char buffer[512];
  159.  
  160.    error.func = "getfile";
  161.    errno = 0;
  162.  
  163.    jmpret = setjmp(mark);
  164.  
  165.    if (jmpret == 0)
  166.    {
  167.       if ((fd=open(filename,O_RDONLY|O_BINARY)) == -1)
  168.       {
  169.          if (error.num != -1)
  170.             return -1;
  171.          fio_error();
  172.       }
  173.       while ((i=read(fd,buffer,512)) > 0)
  174.       {
  175.          if (kbhit())
  176.             break;
  177.  
  178.          if (xlate)
  179.             append(s,m,c,buffer,i);
  180.          else
  181.             append(0,0,0,buffer,i);
  182.          if (byte_cnt==max_bytes)
  183.             return -2;
  184.       }
  185.       if (i == -1)
  186.          fio_error();
  187.       if (close(fd) == -1)
  188.          fio_error();
  189.       return 1;
  190.    }
  191.    return -1;
  192. }
  193.  
  194. int putsectors(char *file, long start, int num)
  195. {
  196. int i;
  197.  
  198.    error.func = "putsects";
  199.    errno = 0;
  200.    jmpret = setjmp(mark);
  201.    savesector();
  202.  
  203.    if (jmpret == 0)
  204.    {
  205.       if ((fd=open(file,O_CREAT|O_TRUNC|O_BINARY|O_RDWR,S_IWRITE|S_IREAD)) == ERROR)
  206.          fio_error();
  207.       log_sector = start-1;
  208.       for (i=0; i < num; i++)
  209.       {
  210.          if (kbhit())
  211.             break;
  212.  
  213.          if (Display)
  214.          {
  215.             putint(i);
  216.             put(len(i),8);
  217.          }
  218.          nextsector();
  219.          if (write(fd,sec_buf,sec_size) != (int)sec_size)
  220.             fio_error();
  221.       }
  222.       close(fd);
  223.       restoresector();
  224.       return 1;
  225.    }
  226.    restoresector();
  227.    return -1;
  228. }
  229.  
  230. static void fio_error(void)
  231. {
  232.    if (fh)
  233.       fclose(fh);
  234.    if (fd > 0)
  235.       close(fd);
  236.    if (!errno)
  237.       errno = exterror();
  238.    error.num = errno;
  239.    if (errno < 16)                     /* sys_nerr is 37! */
  240.       error.msg = sys_errlist[errno];
  241.    else if (errno >= 19 && errno <= 31)
  242.       error.msg = harderr_list[errno-19];
  243.    else
  244.    {
  245.       error.msg = "DOS error code: ";
  246.       sprintf(ebuf,"%02Xh",errno);
  247.       error.arg = ebuf;
  248.    }
  249.    error.mod = "fileio";
  250.    longjmp(mark,-1);
  251. }
  252.