home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / server.c < prev    next >
C/C++ Source or Header  |  1993-12-05  |  3KB  |  113 lines

  1. /* $Id: server.c,v 4.4.3.1 1991/11/22 04:12:21 davison Trn $
  2. **
  3. ** $Log:    server.c,v $
  4. ** Revision 4.4.3.1  1991/11/22  04:12:21  davison
  5. ** Trn Release 2.0
  6. ** 
  7. */
  8.  
  9. #include "EXTERN.h"
  10. #include "common.h"
  11. #include "threads.h"
  12.  
  13. #ifdef SERVER
  14.  
  15. #include "server.h"
  16.  
  17. char *
  18. get_server_name(errflag)
  19. int errflag;
  20. {
  21.     char *name;
  22.     extern char *getenv();
  23.  
  24.     if (SERVER_FILE[0] == '/') {
  25.     name = getserverbyfile(SERVER_FILE);
  26.     } else {
  27.     if ((name = getenv("NNTPSERVER")) == NULL)
  28.         name = SERVER_FILE;
  29.     }
  30.     if (name == NULL && errflag) {
  31.     fprintf(stderr, "Couldn't get name of news server from %s\n",
  32.         SERVER_FILE);
  33.     fprintf(stderr,
  34.         "Either fix this file, or put NNTPSERVER in your environment.\n");
  35.     }
  36.     return name;
  37. }
  38.  
  39. # ifdef XTHREAD
  40.  
  41. extern FILE *ser_rd_fp;
  42.  
  43. static    long    rawbytes = -1;    /* bytes remaining to be transfered */
  44.  
  45. /*
  46.  * rawcheck_server -- get a line of text from the server, interpreting
  47.  * it as a status message for a raw (binary) command.  Call this once
  48.  * before calling rawget_server() for the actual data transfer.
  49.  *
  50.  *    Parameters:    "string" has the buffer space for the
  51.  *            line received.
  52.  *            "size" is the size of the buffer.
  53.  *
  54.  *    Returns:    -1 on error, otherwise the length of the raw data.
  55.  *
  56.  *    Side effects:    Talks to server, changes contents of "string".
  57.  */
  58. long
  59. rawcheck_server(string, size)
  60. char    *string;
  61. int    size;
  62. {
  63.     /* try to get the status line and the status code */
  64.     if (get_server(string, size) || *string != CHAR_OK)
  65.     return rawbytes = -1;
  66.  
  67.     /* try to get the number of bytes being transfered */
  68.     if (sscanf(string, "%*d%ld", &rawbytes) != 1)
  69.     return rawbytes = -1;
  70.     return rawbytes;
  71. }
  72.  
  73. /*
  74.  * rawget_server -- read data from the server in raw format.  This call must
  75.  * follow an appropriate put_server command and a rawcheck_server call.
  76.  *
  77.  *    Parameters:    "buf" is the buffer for the data to receive.
  78.  *            "n" is the size of the buffer.
  79.  *
  80.  *    Returns:    0 on EOF, otherwise the length of the read.
  81.  *
  82.  *    Side effects:    Talks to server, changes contents of "buf".
  83.  */
  84. long
  85. rawget_server(buf, n)
  86. char    *buf;
  87. long    n;
  88. {
  89.     /* if no bytes to read, then just return EOF */
  90.     if (rawbytes < 0)
  91.     return 0;
  92.  
  93.     /* try to read some data from the server */
  94.     if (rawbytes) {
  95.     n = fread(buf, 1, n > rawbytes ? rawbytes : n, ser_rd_fp);
  96.     rawbytes -= n;
  97.     } else
  98.     n = 0;
  99.  
  100.     /* if no more left, then fetch the end-of-command signature */
  101.     if (!rawbytes) {
  102.     char buf[5];    /* "\r\n.\r\n" */
  103.  
  104.     fread(buf, 1, 5, ser_rd_fp);
  105.     rawbytes = -1;
  106.     }
  107.     return n;
  108. }
  109. # endif
  110.  
  111. #endif /* SERVER */
  112.  
  113.