home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCPbb_2_1_src.lzh / UUCPBB21 / log.c < prev    next >
Text File  |  1994-09-25  |  4KB  |  161 lines

  1. /*  log.c   Routines to manage the uulog file.
  2.     Copyright (C) 1994 Brad Spencer
  3.  
  4.     This file is part of the OS-9 UUCP package, UUCPbb.
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     The author of UUCPbb, Bob Billson, can be contacted at:
  21.     bob@kc2wz.bubble.org  or  uunet!kc2wz!bob  or  by snail mail:
  22.     21 Bates Way, Westfield, NJ 07090
  23. */
  24.  
  25. #include "uucp.h"
  26.  
  27. static char logpath[200], fcaller[200];
  28. static flag  copylog = FALSE,
  29.              to_stderr = FALSE,
  30.              wval;
  31.  
  32.  
  33. /* Initalize logpath to requested log path and others */
  34.  
  35. void inizlog (caller, where)
  36. char *caller;
  37. int where;
  38. {
  39.      if (logdir == NULL)
  40.           if ((logdir = getenv ("LOGDIR")) != NULL)
  41.                logdir = strdup (logdir);
  42.           else
  43.                logdir = LOGDIR;
  44.  
  45.      if (caller == NULL  ||  *caller == '\0')
  46.           strcpy (fcaller, "unnamed");
  47.      else
  48.           strcpy (fcaller, caller);
  49.  
  50.      switch (where)
  51.        {
  52.           /* Someone did something silly, so log to LOGDIR/uulog */
  53.           default:
  54.               where = 1;
  55.               /* Fall through */
  56.  
  57.           /* Ok, log to the disk file LOGDIR/uulog */
  58.           case 1:
  59.                sprintf (logpath, "%s/uulog", logdir);
  60.                break;
  61.  
  62.           /* Ok, log to the RAMDISK and copy the contents to LOGDIR/uulog
  63.              later */
  64.  
  65.           case 2:
  66.                sprintf (logpath, "%s/uulog", RAMDISK);
  67.                copylog = TRUE;
  68.                break;
  69.  
  70.           /* Ok, log to stderr */
  71.           case 3:
  72.                strcpy (logpath, "stderr");
  73.                to_stderr = TRUE;
  74.                break;
  75.        }
  76.      wval = where;
  77.  
  78.      /* Ensure that something is in the uulog.  Might as well make it useful
  79.         information */
  80. }
  81.  
  82.  
  83.  
  84. /* Log will write a message to the end of the log, returning where, or -1
  85.    Really, varargs should be used here, but I don't know if OSK or OS-9000
  86.    has them */
  87.  
  88. int log (message)
  89. char *message;
  90. {
  91.      FILE *fd;
  92.  
  93.      /* Need a message in order to log one */
  94.      if (message == NULL  || *message == '\0')
  95.           return (wval);
  96.  
  97.      if (to_stderr)
  98.           fd = stderr;
  99.      else
  100.           if ((fd = fopen (logpath, "a")) == NULL)
  101.             {
  102.               fprintf (stderr, "log: Couldn't open '%s', because %d\n",
  103.                        logpath, errno);
  104.               return (-1);
  105.             }
  106.  
  107.      fprintf (fd,"%s %s %s\n", fcaller, gtime(), message);
  108.  
  109.      if (!to_stderr)
  110.           fclose (fd);
  111.  
  112.      return (wval);
  113. }
  114.  
  115.  
  116.  
  117. /* deinizlog will copy the log from RAMDISK, if needed */
  118.  
  119. int deinizlog()
  120. {
  121.      FILE *rfd, *lfd;
  122.      char nlog[200], bigbuf[1024], llbuf[100];
  123.      int r;
  124.  
  125.      /* Copy the file from the RAMDRIVE to LOGDIR/uulog, if asked to */
  126.      if (copylog)
  127.        {
  128.           if ((rfd = fopen (logpath, "r")) == NULL)
  129.             {
  130.                fprintf (stderr, "deinizlog: Can't open '%s', because %d\n",
  131.                         logpath, errno);
  132.                return (-1);
  133.             }
  134.  
  135.           sprintf (nlog, "%s/uulog", logdir);
  136.  
  137.           if ((lfd = fopen (nlog, "a")) == NULL)
  138.             {
  139.                fprintf (stderr, "deinizlog: Can't open '%s'\n", nlog);
  140.                return (-1);
  141.             }
  142.  
  143.           do
  144.             {
  145.                if ((r = fread (bigbuf, 1, sizeof (bigbuf)-1, rfd)) < 0)
  146.                     fputs ("deinizlog: read error\n", stderr);
  147.                else
  148.                     if (r != 0)
  149.                          fwrite (bigbuf, 1, r, lfd);
  150.             }
  151.           while (r > 0);
  152.  
  153.           fclose (rfd);
  154.           fclose (lfd);
  155.  
  156.           if (r == 0)
  157.                unlink (logpath);
  158.        }
  159.      return (r);
  160. }
  161.