home *** CD-ROM | disk | FTP | other *** search
/ Oracle Video Server 3.0.3.1 / OVS_3031_NT.iso / win32 / sqlnet / net23 / client / filep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-15  |  4.6 KB  |  192 lines

  1. /*
  2.   $Header: /netrcs/RCS/oracle/network/tns/tnsapi/RCS/filep.c,v 1.3 1995/09/13 06:52:58 yzheng Exp $
  3. */
  4.  
  5. /*
  6.  * Routines to open/close/read/write local file
  7.  * For binary transmission, we use the UNIX open/read/write system calls
  8.  * For ascii transmission, we use the UNIX standard i/o routine 
  9.  * fopen/getc/putc
  10.  */
  11.  
  12. #ifndef TFTPDEF
  13. # include "tftpdef.h"
  14. #endif
  15.  
  16. static int lastcr = 0;  /* 1 if last char was a carriage-return */
  17. static int nextchar = 0;
  18.  
  19. FILE *file_open(fname, mode, initblknum)
  20. char *fname;
  21. char *mode;
  22. int initblknum;
  23. {
  24.   register FILE *fp;
  25.  
  26.   if (strcmp(fname, "-") == 0)
  27.     fp  = stdout;
  28.   else if ((fp = fopen(fname, mode)) == NULL)
  29.            return ((FILE *)0);
  30.  
  31.   nextblknum = initblknum;  /* for first data or ACK packet */
  32.   lastcr  = 0;              /* for file_write()             */
  33.   nextchar = -1;            /* for file_read()              */
  34.  
  35.   DEBUG2("file_open: opened %s, mode = %s", fname, mode);
  36.   return(fp);
  37. }
  38.  
  39. void file_close(fp)
  40. FILE *fp;
  41. {
  42.   if (lastcr)
  43.     err_dump("final character was a CR");
  44.   if (nextchar >= 0)
  45.     err_dump("nextchar >= 0");
  46.  
  47.   if (fp  == stdout)
  48.     return;
  49.   else if (fclose(fp) == EOF)
  50.     err_dump("fclose error");
  51. }
  52.  
  53.  
  54. /*
  55.  * Read data from local file.
  56.  * Here we handle any conversion between the file's mode on the local system
  57.  * and the network mode
  58.  *
  59.  * Return the number of bytes read (between 1 and maxnbytes, inclusive) 
  60.  * or 0 on EOF
  61.  */
  62. int file_read(fp, ptr, maxnbytes, mode)
  63. FILE *fp;
  64. char *ptr;
  65. int maxnbytes;
  66. int mode;
  67. {
  68.   register int c, count;
  69.  
  70.   if (mode == MODE_BINARY) {
  71. #ifndef WIN32
  72.     count = read(fileno(fp), ptr, maxnbytes);
  73. #else
  74.     count = _read(fileno(fp), ptr, maxnbytes);
  75. #endif
  76.     if (count < 0)
  77.       err_dump("read error from local file");
  78.     return (count);
  79.   }
  80.   else if (mode == MODE_ASCII)
  81.   {
  82.     /*
  83.      * For files that are transferred in netascii, we must perform the reverse
  84.      * conversions that file_write() does. Note that we have to use the
  85.      * global "nextchar" to remember if the next character to output is a 
  86.      * linefeed or a null, since the second byte of a 2-byte sequence may not 
  87.      * fit in the current buffer, and may have to go as the first byte of 
  88.      * the next buffer (i.e., we have to remember this fact from one call
  89.      * to the next)
  90.      */
  91.     for (count = 0; count < maxnbytes; count++)
  92.     {
  93.       if (nextchar >=0 )
  94.       {
  95.         *ptr++ = nextchar;
  96.         nextchar = -1; 
  97.         continue;
  98.       }
  99.       c = getc(fp);
  100.  
  101.       if (c == EOF) /* EOF return means eof or error */
  102.       {
  103.         if (ferror(fp))
  104.           err_dump("read error from getc on local file");
  105.         return(count);
  106.       }
  107.       else if (c == '\n')
  108.       {
  109.         c = '\r';        /* newline -> CR, LF */
  110.         nextchar = '\n';
  111.       }
  112.       else if (c == '\r')
  113.       {
  114.         nextchar = '\0';         /* CR -> CR, NULL */
  115.       }
  116.       else
  117.         nextchar = -1;
  118.       
  119.       *ptr++ = c;
  120.     }
  121.     return(count);
  122.   }
  123.   else 
  124.     err_dump("unknown Mode value");
  125. }
  126.  
  127.  
  128. /*
  129.  * Write data to the local file.
  130.  * Here we handle any conversion between the mode of the file on the network
  131.  * and the local system's conversion.
  132.  */
  133. void file_write(fp, ptr, nbytes, mode)
  134. FILE *fp;
  135. char *ptr;
  136. int nbytes;
  137. int mode;
  138. {
  139.   register int c, i;
  140.  
  141.   if (mode == MODE_BINARY)
  142.   {
  143. #ifndef WIN32
  144.     i = write(fileno(fp), ptr, nbytes);
  145. #else
  146.     i = _write(fileno(fp), ptr, nbytes);
  147. #endif
  148.     if (i != nbytes)
  149.       err_dump("write error to local file, i = %d", i);
  150.  
  151.   }
  152.   else if (mode == MODE_ASCII)
  153.   {
  154.     /*
  155.      * For files that are transferred in netascii, we must perform the 
  156.      * following conversion
  157.      * CR, LF   -> newline = '\n'
  158.      * CR, NULL -> CR      = '\r'
  159.      * CR, anything else -> undefined
  160.      *
  161.      * Note that we have to use the global "lastcr" to remember if the last
  162.      * character was a CR, LF or not, since if the last character of a buffer
  163.      * is a CR, we have to remember that when we are called for the next 
  164.      * buffer.
  165.      */
  166.  
  167.     for (i = 0; i < nbytes; i++)
  168.     {
  169.       c = *ptr++;
  170.       if (lastcr)
  171.       {
  172.         if (c == '\n')
  173.           c = '\n';
  174.         else if ( c == '\0')
  175.           c = '\r';
  176.         else 
  177.           err_dump("CR followed by 0x%02x", c);
  178.         lastcr = 0;
  179.       }
  180.       else if ( c == '\r')
  181.       {
  182.         lastcr = 1;
  183.         continue;      /* get next char */
  184.       }
  185.       if (putc(c, fp) == EOF)
  186.         err_dump("write error to local file");
  187.     }
  188.   }
  189.   else
  190.     err_dump("Unknown MODE value");
  191. }
  192.