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 / groups.c < prev    next >
Text File  |  1994-09-25  |  6KB  |  187 lines

  1. /* groups.c  - do some functions that deal with the active 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. #include "rnews.h"
  27. #include "mbuf.h"
  28. #include <modes.h>
  29.  
  30. static int gfd = -1;
  31. static FILE *gfp = NULL;
  32. extern int debuglvl;
  33. struct mbuf *mwrite();
  34. void putgroups();
  35.  
  36. /* Set a sleep, if the active file isn't accessable */
  37.  
  38. #define ZZZZZZZ 7
  39.  
  40. struct mbuf *getgroups()
  41. {
  42.      char lbuf[512], line[BUFSIZ];
  43.      struct groups g;
  44.      struct mbuf *mh = NULL, *mp = NULL;
  45.      char *p, *minp, *maxp;
  46.      int zcnt = 0; 
  47.      flag hasjunk = FALSE;
  48.  
  49.      /* Ok, try to open the active file, non-shareable */
  50.      for (;;)
  51.           if ((gfd = open (NGROUPS,S_IREAD|S_IWRITE|S_ISHARE)) < 0)
  52.             {
  53.                /* If this file is busy, then sleep for a bit and try again. 
  54.                   Something could be done with zcnt sometime */
  55.  
  56.                if (errno == E_SHARE)
  57.                  {
  58.                     if (zcnt == 0)
  59.                       {
  60.                          sprintf (lbuf,
  61.                                   "'%s' is busy.  I'll try again in a bit.",
  62.                                   NGROUPS);
  63.                          log (lbuf);
  64.                       }
  65.  
  66.                     zcnt++;
  67.                     sleep (ZZZZZZZ);
  68.                     continue;
  69.                  }
  70.                else
  71.                  {
  72.                     sprintf (lbuf, "Can't open '%s', because '%d'",
  73.                                    NGROUPS, errno);
  74.                     log (lbuf);
  75.                     exit (errno);
  76.                  }
  77.             }
  78.           else
  79.                break;
  80.  
  81.      /* Ok, we actually opened the active file, make a FILE pointer for it */
  82.      if ((gfp = fdopen (gfd, "r+")) == NULL)
  83.        {
  84.           sprintf (lbuf, "can't fdopen %s", NGROUPS);
  85.           log (lbuf);
  86.           exit (1);
  87.        }
  88.  
  89.      /* Read one line at a time, eating blank lines */
  90.      while (fgets (line, BUFSIZ-1, gfp) != NULL)
  91.        {
  92.           if (*line == '\n')
  93.                continue;
  94.  
  95.           for (p = line;  *p != ' ';  p++);
  96.                *p = '\0';
  97.  
  98.           minp = p + 1;
  99.  
  100.           for (p++;  *p != ' ';  p++);
  101.                *p = '\0';
  102.  
  103.           maxp = p + 1;
  104.           g.name = (char *) malloc (strlen (line) + 1);
  105.           strcpy (g.name, line);
  106.           g.min = (unsigned) atoi (minp);
  107.           g.max = (unsigned) atoi (maxp);
  108.           g.artfd = NULL;
  109.           mp = mwrite (mp, &mh, (char *) &g, sizeof (struct groups));
  110.  
  111.           /* No site should be without a junk group.  Note if this one has
  112.              one */
  113.  
  114.           if (strncmp (((struct groups *)mp->cbuf)->name, "junk", 4) == 0)
  115.                hasjunk = TRUE;
  116.  
  117.           if (debuglvl > 3)
  118.            {
  119.                sprintf (lbuf, "Newsgroup: %s, min: %d, max: %d",
  120.                               ((struct groups *)mp->cbuf)->name,
  121.                               ((struct groups *)mp->cbuf)->min,
  122.                               ((struct groups *)mp->cbuf)->max);
  123.                log (lbuf);
  124.             }
  125.        }
  126.  
  127.      /* Nope, no junk group.  Make one. */
  128.      if (hasjunk == FALSE)
  129.        {
  130.           g.name = (char *) malloc(5);
  131.           strcpy (g.name, "junk");
  132.           g.min = 1;
  133.           g.max = 0;
  134.           g.artfd = NULL;
  135.           mp = mwrite (mp, &mh, (char *) &g, sizeof (struct groups));
  136.        }
  137.  
  138.      /* Leave the file open, which should keep it locked down until later */
  139.      return (mh);
  140. }
  141.  
  142.  
  143.  
  144. /* put the active file back */
  145.  
  146. void putgroups(gh)
  147. struct mbuf *gh;
  148. {
  149.      char lbuf[512];
  150.      struct mbuf *mp;
  151.  
  152.      /* Complain, and don't do anything, if the file isn't open */
  153.  
  154.      if (gfp == NULL)
  155.        {
  156.           log ("Active file doesn't appear open");
  157.           return;
  158.        }
  159.  
  160.      /* rewind the active file and write it all out */
  161.      rewind (gfp);
  162.  
  163.      /* loop and write out the group entries */
  164.      for (mp = gh;  mp != NULL;  mp = mp->mbuf_next)
  165.        {
  166.           fprintf (gfp, "%s %d %d\n",
  167.                         ((struct groups *)mp->cbuf)->name,
  168.                         ((struct groups *)mp->cbuf)->min,
  169.                         ((struct groups *)mp->cbuf)->max);
  170.  
  171.           if (debuglvl > 3)
  172.             {
  173.                sprintf (lbuf, "ACTIVE OUT: %s %d %d",
  174.                               ((struct groups *)mp->cbuf)->name,
  175.                               ((struct groups *)mp->cbuf)->min,
  176.                               ((struct groups *)mp->cbuf)->max);
  177.                log (lbuf);
  178.             }
  179.        }
  180.  
  181.      /* Ok, close everything up, this should unlock the active file */
  182.      fclose (gfp);
  183.      close (gfd);
  184.      gfp = NULL;
  185.      gfd = -1;
  186. }
  187.