home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / sm-smtp / netio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  1.4 KB  |  80 lines

  1. #ifndef lint
  2. static char *sccsid = "@(#)netio.c    1.7 87/07/31";
  3. #endif lint
  4.  
  5. #include "smtp.h"
  6. #include <setjmp.h>
  7.  
  8. #ifdef NOBOMB
  9. #define bomb exit
  10. #endif
  11.  
  12. char *strcpy(), *strncat();
  13.  
  14. int hooting = 0;        /* true if not server */
  15.  
  16. int
  17. tgets(line, size, fi)        /* fgets from TCP */
  18. char *line;
  19. int size;
  20. FILE *fi;
  21. {
  22.     register char *cr;
  23.  
  24.     *line = 0;
  25.     if (fgets(line, size, fi) == NULL)
  26.         return -1;
  27.     if (ferror(fi)) {
  28.         perror("error reading from smtp");
  29.         bomb(E_IOERR);
  30.     }
  31.  
  32.     /* convert \r\n -> \n */
  33.     cr = line + strlen(line) - 2;
  34.     if (cr >= line && *cr == '\r' && *(cr+1) == '\n') {    /* CRLF there? */
  35.         *cr++ = '\n';
  36.         *cr = 0;
  37.     } else                /* no CRLF present */
  38.         cr += 2;        /* point at NUL byte */
  39.  
  40. #ifdef HOOTING
  41.     if (hooting) (void) printf("<<< %s", line);
  42. #endif
  43.     if (feof(fi)) {
  44.         perror("read eof from smtp");
  45.         bomb(E_IOERR);
  46.     }
  47.     return cr - line;
  48. }
  49.  
  50. int
  51. tputs(line, fo)            /* fputs to TCP */
  52. char *line;
  53. FILE *fo;
  54. {
  55.     char buf[MAXSTR];
  56.     register char *nl;
  57.     extern int debug;
  58.  
  59.     (void) strcpy(buf, line);
  60. #ifdef HOOTING
  61.     if (hooting) (void) printf(">>> %s", buf);
  62. #endif
  63.     /* replace terminating \n by \r\n */
  64.     nl = buf + strlen(buf) - 1;        /* presumably \n */
  65.     if (nl >= buf && *nl=='\n') {        /* if it is ... */
  66.         *nl++ = '\r';
  67.         *nl++ = '\n';
  68.         *nl = 0;
  69.     } else
  70.         printf("unterminated line: <%s>\n", buf);
  71.  
  72.     (void) fputs(buf, fo);
  73.     (void) fflush(fo);
  74.     if (ferror(fo)) {
  75.         (void) perror("error writing to smtp");
  76.         bomb(E_IOERR);
  77.     }
  78.     return 0;
  79. }
  80.