home *** CD-ROM | disk | FTP | other *** search
/ The CIA World Factbook 1992 / k3bimage.iso / sel / 02 / 0035 / jmodem_c.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  8.6 KB  |  143 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. #include <stdio.h>                       /* Used for NULL                   */
  14. #include <dos.h>                         /* Used for file-size              */
  15. #include <io.h>                          /* Ysed for Unix / DOS type files  */
  16. #include <fcntl.h>                       /* Used for O_BINARY, etc          */
  17. #include <string.h>                      /* Used for _strchr(), etc         */
  18. #include "jmodem.h"                      /* Used for JMODEM primatives      */
  19. /****************************************************************************/
  20. /*   Note:                                                                  */
  21. /*   I originally used fopen();, fclose();, fread();, and fwrite(); the     */
  22. /*   default C stream-I/O files. However, a bug in Microsoft's implementa-  */
  23. /*   tion results in corruption of files larger than about 512k. The bug    */
  24. /*   inserts a null into the  files every approximately 512k.               */
  25. /*   Therefore, I changed to the UNIX-style disk I/O.                       */
  26. /*                                                                          */
  27. /****************************************************************************/
  28. /* Merged from \include\sys\stat.h because improperly organized header-file */
  29. /* REQUIRES that two other files be included first! Thanks, Microsoft...    */
  30. #define S_IFMT      0170000             /* file type mask                   */
  31. #define S_IFDIR     0040000             /* directory                        */
  32. #define S_IFCHR     0020000             /* character special                */
  33. #define S_IFREG     0100000             /* regular                          */
  34. #define S_IREAD     0000400             /* read permission, owner           */
  35. #define S_IWRITE    0000200             /* write permission, owner          */
  36. #define S_IEXEC     0000100             /* execute/search permission, owner */
  37. /****************************************************************************/
  38. unsigned short file_io(command, handle, buffer, len)
  39. unsigned short command;                           /* Read/write/open, etc.  */
  40. register short *handle;                           /* File handle            */
  41. register unsigned char *buffer;                   /* Working buffer pointer */
  42. unsigned short len;                               /* Bytes to read/write    */
  43. {
  44.     struct find_t o_file;                         /* To get file size       */
  45.     char temp[65];                                /* To rename a file       */
  46.     char *dot;                                    /* Find the "."           */
  47.     switch (command)                              /* GOTO in disguise       */
  48.     {
  49. /****************************************************************************/
  50. /*      Open the file for read. If the open fails, return non-zero.         */
  51. /****************************************************************************/
  52.         case OPEN_READ:
  53.         {
  54.             screen (SCR_FIL,NULL,buffer);
  55.             if ( (*handle = open(buffer,O_RDONLY|O_BINARY)) != -1 )
  56.             {
  57.                 _dos_findfirst(buffer,_A_NORMAL, &o_file);
  58.                 syst.s_byt = o_file.size;         /* Show file size        */
  59.                 screen (SCR_FOK,&syst,NULL);      /* Show success          */
  60.                 return JM_NRM;
  61.             }
  62.         screen (SCR_FNF,NULL,NULL);               /* Show failure          */
  63.         return JM_FNF;
  64.         }
  65. /****************************************************************************/
  66. /*         Create a new file. If the file already exists, rename it.        */
  67. /****************************************************************************/
  68.        case CREATE:
  69.         {
  70.             screen (SCR_FIL,NULL,buffer);
  71.             if ( (*handle = open(buffer,O_RDONLY|O_BINARY)) != -1 )
  72.             {                                     /* If file already exists */
  73.                 close (*handle);                  /* Close the open file    */
  74.                 strcpy (temp,buffer);             /* Copy the file name     */
  75.                 dot = strchr (temp, '.');         /* Find "." in string     */
  76.                 strcpy (dot, ".OLD");             /* FILENAME.OLD           */
  77.                 if ( rename(buffer,temp) )        /* Try to rename the file */
  78.                 {
  79.                     screen (SCR_FCR,NULL,temp);   /* Tell user can't rename */
  80.                     return JM_REN;                /* Quit                   */
  81.                 }
  82.                 else
  83.                 {
  84.                    screen (SCR_FRN,NULL,temp);    /* Tell user we renamed   */
  85.                 }
  86.             }
  87. /* Note:                                                                    */
  88. /*     Microsoft's own rules of MS-DOS state that a file being CREATED is   */
  89. /*     open for WRITE (naturally, who would wish to READ from an empty      */
  90. /*     file?).                                                              */
  91. /*     Unix also provides this same logic.                                  */
  92. /*     In the following code, if I did not OR in O_RDWR, I would create a   */
  93. /*     file I can't write to!. If I did not also include the [optional]     */
  94. /*     parameters, S_IWRITE OR S_IREAD, I create a file I can't delete!!!   */
  95. /*     These are implementation bugs.                                       */
  96. /*                                                                          */
  97.             if  ( (*handle = open (              /* Open (create) new file  */
  98.                                 buffer,          /* File name               */
  99.                                 O_CREAT          /* Create new file         */
  100.                                |O_BINARY         /* Binary mode             */
  101.                                |O_RDWR ,         /* Read/write (bug??)      */
  102.                                 S_IWRITE         /* Definite bug            */
  103.                                |S_IREAD)) != -1) /* More of same bug        */
  104.             {
  105.                 screen (SCR_FOK,NULL,temp);      /* Show success            */
  106.                 return JM_NRM;
  107.             }
  108.             screen (SCR_FCR,NULL,temp);          /* Show failure            */
  109.             return JM_CRE;
  110.         }
  111. /****************************************************************************/
  112. /*        Write 'len' bytes to the file. Return the actual bytes written    */
  113. /****************************************************************************/
  114.         case WRITE:
  115.         {
  116.         return ( write ( *handle, buffer, len) );
  117.         }
  118. /****************************************************************************/
  119. /*        Read 'len' bytes from the file. Return the actual bytes read.     */
  120. /****************************************************************************/
  121.         case READ:
  122.         {
  123.         return ( read ( *handle, buffer, len) );
  124.         }
  125. /****************************************************************************/
  126. /*        Close the file. Return any error-code (ignored).                  */
  127. /****************************************************************************/
  128.         case CLOSE:
  129.         {
  130.         return (close (*handle));
  131.         }
  132. /****************************************************************************/
  133. /*        Delete the file named in 'buffer'. Return any error code.         */
  134. /****************************************************************************/
  135.         case DELETE:
  136.         {
  137.         return (remove (buffer));
  138.         }
  139.     }
  140. }
  141. /****************************************************************************/
  142. /************************ E N D  O F  M O D U L E  **************************/
  143.