home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / comm / jmodem.zip / JMODEM_C.C < prev    next >
Text File  |  1990-06-20  |  8KB  |  142 lines

  1. /****************************************************************************/
  2. /*   FILE JMODEM_C.C                                                        */
  3. /*   Created 11-JAN-1990                   Richard B. Johnson               */
  4. /*                                         405 Broughton Drive              */
  5. /*                                         Beverly, Massachusetts 01915     */
  6. /*                                         BBS (508) 922-3166               */
  7. /*                                                                          */
  8. /*   File I/O                                                               */
  9. /*   file_io();                                                             */
  10. /*   All of the file I/O is called from this one block to ease portability  */
  11. /*                                                                          */
  12. /****************************************************************************/
  13. #define FILE_IO                          /* For variable length param list  */
  14. #include <stdio.h>                       /* Used for NULL                   */
  15. #include <dos.h>                         /* Used for file-size              */
  16. #include <io.h>                          /* Ysed for Unix / DOS type files  */
  17. #include <fcntl.h>                       /* Used for O_BINARY, etc          */
  18. #include <string.h>                      /* Used for _strchr(), etc         */
  19. #include "jmodem.h"                      /* Used for JMODEM primatives      */
  20. #if !defined (TURBOC)
  21.     #include <sys\types.h>               /* Used to make <sys\stat.h> work  */
  22. #endif
  23. #include <sys\stat.h>                    /* Used for Unix style file I/O    */
  24. #if defined (TURBOC)
  25.     #include <dir.h>                     /* struct for ffblk                */
  26.     #define find_t ffblk
  27.     #define _A_NORMAL 0
  28. #endif
  29.  
  30. #define FCREATE ( O_CREAT | O_BINARY | O_RDWR)   /* Create new file         */
  31. #define FACCESS ( S_IWRITE | S_IREAD )           /* File access mode        */
  32. #define FOPENRD ( O_RDONLY | O_BINARY)           /* Open for read only      */
  33. #define FAIL -1                                  /* Open close create fail  */
  34. unsigned char old[]=".OLD";                      /* For renaming files      */
  35.  
  36. /****************************************************************************/
  37. unsigned short file_io(command, handle, buffer, len)
  38. unsigned short command;                          /* Read/write/open, etc.   */
  39. register short *handle;                          /* File handle             */
  40. register unsigned char *buffer;                  /* Working buffer pointer  */
  41. unsigned short len;                              /* Bytes to read/write     */
  42. {
  43.     struct find_t o_file;                        /* To get file size        */
  44.     unsigned char temp[65];                      /* To rename a file        */
  45.     unsigned char *dot;                          /* Find the "."            */
  46.     switch (command)                             /* GOTO in disguise        */
  47.     {
  48. /****************************************************************************/
  49. /*      Open the file for read. If the open fails, return non-zero.         */
  50. /****************************************************************************/
  51.         case OPEN_READ:
  52.         {
  53.             syst.s_txt = buffer;
  54.             screen (SCR_FIL,&syst);
  55.             if ( (*handle = open(buffer,FOPENRD) ) != FAIL )
  56.             {
  57. #if defined (TURBOC)
  58.                 findfirst(buffer,&o_file,_A_NORMAL);
  59.                 syst.s_byt = o_file.ff_fsize;
  60. #else
  61.                 _dos_findfirst(buffer,_A_NORMAL, &o_file);
  62.                 syst.s_byt = o_file.size;        /* Show file size          */
  63. #endif
  64.                 screen (SCR_FOK,&syst);          /* Show success            */
  65.                 return JM_NRM;
  66.             }
  67.         screen (SCR_FNF);                        /* Show failure            */
  68.         return JM_FNF;
  69.         }
  70. /****************************************************************************/
  71. /*         Create a new file. If the file already exists, rename it.        */
  72. /****************************************************************************/
  73.        case CREATE:
  74.         {
  75.             syst.s_txt = buffer;                 /* Buffer has filename     */
  76.             screen (SCR_FIL,&syst);              /* Write "opening file"    */
  77.             if ( (*handle = open  (              /* See if it exists        */
  78.                             buffer ,             /* Filename                */
  79.                             FOPENRD )            /* Open for read only      */
  80.                             ) != FAIL )
  81.             {                                    /* If file already exists  */
  82.                 close (*handle);                 /* Close the open file     */
  83.                 strcpy (temp,buffer);            /* Copy the file name      */
  84.                 dot = strchr (temp, '.');        /* Find "." in string      */
  85.                 if (dot)                         /* If there was a dot      */
  86.                     strcpy (dot, old);           /* Substitute ".OLD"       */
  87.                 else
  88.                     strcat (temp, old);          /* Append "filename.OLD"   */
  89.                 syst.s_txt = temp;
  90.                 if ( rename(buffer,temp) )       /* Try to rename the file  */
  91.                 {
  92.                     screen (SCR_FCR,&syst);      /* Tell user can't rename  */
  93.                     return JM_REN;               /* Quit                    */
  94.                 }
  95.                 screen (SCR_FRN,&syst);          /* Tell user we renamed    */
  96.             }
  97.             if  ( (*handle = open (              /* Open (create) new file  */
  98.                              buffer ,            /* File name in buffer     */
  99.                              FCREATE ,           /* Create new file         */
  100.                              FACCESS )           /* Define access mode      */
  101.                              ) != FAIL)          /* If successfull          */
  102.             {
  103.                 screen (SCR_FOK,NULL);           /* Show success            */
  104.                 return JM_NRM;
  105.             }
  106.             screen (SCR_FCR);                    /* Show failure            */
  107.             return JM_CRE;
  108.         }
  109. /****************************************************************************/
  110. /*        Write 'len' bytes to the file. Return the actual bytes written    */
  111. /****************************************************************************/
  112.         case WRITE:
  113.         {
  114.         return ( write ( *handle, buffer, len) );
  115.         }
  116. /****************************************************************************/
  117. /*        Read 'len' bytes from the file. Return the actual bytes read.     */
  118. /****************************************************************************/
  119.         case READ:
  120.         {
  121.         return ( read ( *handle, buffer, len) );
  122.         }
  123. /****************************************************************************/
  124. /*        Close the file. Return any error-code (ignored).                  */
  125. /****************************************************************************/
  126.         case CLOSE:
  127.         {
  128.         return (close (*handle));
  129.         }
  130. /****************************************************************************/
  131. /*        Delete the file named in 'buffer'. Return any error code.         */
  132. /****************************************************************************/
  133.         case DELETE:
  134.         {
  135.         return (remove (buffer));
  136.         }
  137.     }
  138.     return JM_ABT;                                          /* Bad function */
  139. }
  140. /****************************************************************************/
  141. /************************ E N D  O F  M O D U L E  **************************/
  142.