home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / news / nntp / nntplink3.1.0 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-25  |  3.7 KB  |  214 lines

  1. #include "conf.h"
  2. #include <fcntl.h>
  3. #ifdef HAVE_STRING_H
  4. #include <string.h>
  5. #else
  6. #include <strings.h>
  7. #endif
  8. #include <errno.h>
  9. #ifdef FAKESYSLOG
  10. #include "fsyslog.h"
  11. #else
  12. #include <syslog.h>
  13. #endif
  14. #ifdef USE_VARARGS
  15. #include <varargs.h>
  16. #endif
  17. #include <values.h>
  18. #include "readline.h"
  19. #include "nntplink.h"
  20.  
  21.  
  22. extern Boolean Debug;
  23. extern FILE *Debugfp;
  24. extern long Entry_sleep;
  25. extern long Idle_time;
  26. extern int Input_from;
  27. extern Boolean Log_close;
  28. extern Boolean One_shot;
  29. extern int Prog_pid;
  30. extern int Stdin_fd_flags;
  31.  
  32. extern void close_connection();
  33. extern char *emalloc();
  34. extern void log();
  35. extern void log_stats();
  36. extern void update_batchfile();
  37. extern void write_link_datafile();
  38.  
  39. extern void exit();
  40.  
  41. void my_exit();
  42.  
  43. #ifdef USE_VARARGS
  44. void
  45.   log(va_alist)
  46. va_dcl
  47. {
  48.     va_list ap;
  49.     char *fname, *format;
  50.     char *buf;
  51.     int priority;
  52.  
  53.     va_start(ap);
  54.     priority = va_arg(ap, int);
  55.     fname = va_arg(ap, char *);
  56.     format = va_arg(ap, char *);
  57.     format += 2;
  58.  
  59.     if (Debug) {
  60.     if (fputs(fname, Debugfp) == EOF) {
  61.         fprintf(stderr, "log: fputs failed - %s\n", errmsg(errno));
  62.         my_exit(FAIL);
  63.     }
  64.     if (vfprintf(Debugfp, format, ap) == EOF) {
  65.         fprintf(stderr, "log: vfprintf failed - %s\n", errmsg(errno));
  66.         my_exit(FAIL);
  67.     }
  68.     fflush(Debugfp);
  69.     } else {
  70.     buf = emalloc(1024);
  71.     vsprintf(buf, format, ap);
  72.     syslog(priority, "%s", buf);
  73.     free(buf);
  74.     }
  75.  
  76.     va_end(ap);
  77.     return;
  78. }
  79.  
  80. void
  81.   fail(va_alist)
  82. va_dcl
  83. {
  84.     va_list ap;
  85.     char *fname, *format;
  86.     char *buf;
  87.  
  88.     va_start(ap);
  89.     fname = va_arg(ap, char *);
  90.     format = va_arg(ap, char *);
  91.     format += 2;
  92.  
  93.     if (Debug) {
  94.     if (fputs(fname, Debugfp) == EOF) {
  95.         fprintf(stderr, "fail: fputs failed - %s\n", errmsg(errno));
  96.         my_exit(FAIL);
  97.     }
  98.     if (vfprintf(Debugfp, format, ap) == EOF) {
  99.         fprintf(stderr, "fail: vfprintf failed - %s\n", errmsg(errno));
  100.         my_exit(FAIL);
  101.     }
  102.     fflush(Debugfp);
  103.     } else {
  104.     buf = emalloc(1024);
  105.     vsprintf(buf, format, ap);
  106.     syslog(LOG_ERR, "%s", buf);
  107.     free(buf);
  108.     }
  109.     va_end(ap);
  110.     my_exit(FAIL);
  111. }
  112.  
  113. #else /* !USE_VARARGS */
  114.  
  115. /*VARARGS*/
  116. void
  117.   log(priority, fname, format, x1, x2, x3, x4, x5, x6, x7, x8)
  118. int priority;
  119. char *fname;
  120. char *format, *x1, *x2, *x3, *x4, *x5, *x6, *x7, *x8;
  121. {
  122.     if (Debug) {
  123.  
  124.     if (fprintf(Debugfp, format, fname, x1, x2, x3, x4, x5, x6, x7, x8) == EOF) {
  125.         fprintf(stderr, "log: fprintf failed - %s\n", errmsg(errno));
  126.         my_exit(FAIL);
  127.     }
  128.     fflush(Debugfp);
  129.  
  130.     } else
  131.       syslog(priority, format, "", x1, x2, x3, x4, x5, x6, x7, x8);
  132.  
  133.     return;
  134. }
  135.  
  136.  
  137. /*VARARGS*/
  138. void
  139.   fail(fname, format, x1, x2, x3, x4, x5, x6, x7, x8)
  140. char *fname;
  141. char *format, *x1, *x2, *x3, *x4, *x5, *x6, *x7, *x8;
  142. {
  143.  
  144.     log(LOG_ERR, fname, format, x1, x2, x3, x4, x5, x6, x7, x8);
  145.  
  146.     my_exit(FAIL);
  147. }
  148. #endif /* !USE_VARARGS */
  149.  
  150.  
  151. void
  152.   abort_nntplink()
  153. {
  154.     static char *fname = "abort_nntplink: ";
  155.  
  156.     dlog(LOG_DEBUG, fname, "%sexiting\n");
  157.  
  158.     if (!(Input_from & FLG_BATCHFILE) && !One_shot)
  159.       update_batchfile();
  160.  
  161.     if (Host.connected)
  162.       close_connection(!SEND_QUIT_MSG);
  163.     else if (!One_shot)
  164.       write_link_datafile(-999);
  165.  
  166.     if (!Log_close)
  167.       log_stats();
  168.  
  169.     my_exit(0);
  170. }
  171.  
  172.  
  173. void
  174. my_exit(status)
  175. int status;
  176. {
  177.   if (Input_from & FLG_STDIN)
  178.     fcntl(fb_fileno(Stdin), F_SETFL, Stdin_fd_flags);
  179.   exit(status);
  180. }
  181.  
  182.  
  183. char *
  184. itoa(num)
  185.      int num;
  186. {
  187.     static char *numbers = "0123456789";
  188.     static char *neg = "-";
  189.     register char *bp, *negative = neg + 1;
  190.     char *buf = emalloc(MAXDIGS);
  191.  
  192.     if (num == 0)
  193.       return "0";
  194.     else if (num > MAXINT)
  195.       return NULL;
  196.     
  197.     *(bp = buf + MAXDIGS) = '\0';
  198.  
  199.     if ((long)num < 0) {
  200.     negative--;
  201.     num = -num;
  202.     }
  203.  
  204.     do {
  205.     *--bp = *(numbers + (num % 10));
  206.     num /= 10;
  207.     } while (num);
  208.  
  209.     if (*negative)
  210.       *--bp = *negative;
  211.  
  212.     return bp;
  213. }
  214.