home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / snews-20.zip / RMGROUP.C < prev    next >
C/C++ Source or Header  |  1992-08-02  |  4KB  |  130 lines

  1. /*
  2.     SNEWS 2.0
  3.  
  4.     rmgroup - remove newsgroups from the active file, and delete the files
  5.  
  6.  
  7.     Copyright (C) 1991  John McCombs, Christchurch, NEW ZEALAND
  8.                         john@ahuriri.gen.nz
  9.                         PO Box 2708, Christchurch, NEW ZEALAND
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License, version 1, as
  13.     published by the Free Software Foundation.
  14.  
  15.     This program is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU General Public License for more details.
  19.  
  20.     See the file COPYING, which contains a copy of the GNU General
  21.     Public License.
  22.  
  23.  */
  24.  
  25. #include "defs.h"
  26.  
  27. INFO my_stuff;
  28.  
  29.  
  30. /*---------------------------- main ---------------------------------------*/
  31. void main(int argc, char *argv[])
  32. {
  33.     /*
  34.      *  Delete a newsgroup.
  35.      *
  36.      *      - delete the data and index files
  37.      *      - re-write the active file, without that newsgroup, saving
  38.      *        the old one as .bak
  39.      *
  40.      *  This program could be structured to run faster, but I have tried
  41.      *  to do things so that if rmgroup croaks part way through, as little
  42.      *  damage will be done as possible.
  43.      *
  44.      */
  45.  
  46.     int    i;
  47.     char   buf[256], buf2[256], *dir, *p;
  48.     ACTIVE *gp;
  49.     FILE   *active_file, *new_active_file;
  50.  
  51.  
  52.     fprintf(stderr, "RMGROUP: (%s)\n\n", VERSION);
  53.  
  54.     if (argc > 1) {
  55.  
  56.         if (!load_stuff()) {
  57.             fprintf(stderr, "Couldn't read rc info\n");
  58.         }
  59.  
  60.         load_active_file();
  61.         close_active();
  62.  
  63.  
  64.         for (i = 1; i < argc; i++) {
  65.  
  66.             /* check that the group exists - also prevent del of 'junk' */
  67.             gp = find_news_group(argv[i]);
  68.  
  69.             if (stricmp(gp->group, "junk") != 0) {
  70.  
  71.                 /* form the directory name */
  72.                 dir = make_news_group_name(argv[i]);
  73.                 unlink(dir);
  74.                 sprintf(buf, "%s.IDX", dir);
  75.                 unlink(buf);
  76.  
  77.                 printf("rmgroup: %s: files gone... ", argv[i]);
  78.  
  79.                 /* finally remove it from the active file */
  80.                 sprintf(buf, "%sactive", my_stuff.news_dir);
  81.                 if ((active_file = fopen(buf, "rb")) == NULL) {
  82.                     fprintf(stderr, "\nrmgroup: cannot open %s\n", buf);
  83.                     exit(1);
  84.                 }
  85.  
  86.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  87.                 if ((new_active_file = fopen(buf, "wb")) == NULL) {
  88.                     fprintf(stderr, "\nrmgroup: cannot create %s\n", buf);
  89.                     exit(1);
  90.                 }
  91.  
  92.                 while (fgets(buf, 255, active_file) != NULL) {
  93.                     strcpy(buf2, buf);
  94.                     p = strtok(buf2, " \t");
  95.                     if (stricmp(argv[i], p) != 0) {
  96.                         if (fputs(buf, new_active_file) == EOF) {
  97.                             fprintf(stderr, "\nrmgroup: couldn't write new active file\n");
  98.                         }
  99.                     } else {
  100.                         printf("removed from active file\n",
  101.                                argv[i]);
  102.                     }
  103.                 }
  104.  
  105.                 fclose(active_file);
  106.                 fclose(new_active_file);
  107.  
  108.                 /* rename the active to a .bak and the .new to the active */
  109.                 sprintf(buf, "%sactive.bak", my_stuff.news_dir);
  110.                 unlink(buf);
  111.                 sprintf(buf2, "%sactive", my_stuff.news_dir);
  112.                 rename(buf2, buf);
  113.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  114.                 rename(buf, buf2);
  115.  
  116.  
  117.             } else {
  118.                 fprintf(stderr, "rmgroup: newsgroup %s not found\n", argv[i]);
  119.             }
  120.  
  121.         }
  122.  
  123.         close_active_file();
  124.  
  125.     } else {
  126.         printf("usage: rmgroup  newsgroup names....\n");
  127.     }
  128.  
  129. }
  130.