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

  1. /*
  2.     SNEWS 2.0
  3.  
  4.     addgroup - add a new newsgroup
  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 <io.h>
  26. #include "defs.h"
  27.  
  28. INFO my_stuff;
  29.  
  30.  
  31. /*---------------------------- main ---------------------------------------*/
  32. void main(int argc, char *argv[])
  33. {
  34.     /*
  35.      *  Add newsgroups.  All that has to be done is stick the new
  36.      *  group in the active file, after checking that is doesn't
  37.      *  already exist.  It aint pretty, but it works.
  38.      */
  39.  
  40.     int    i;
  41.     char   buf[256], buf2[256], *p, txt_name[20];
  42.     int    added;
  43.     time_t t;
  44.     ACTIVE *gp;
  45.     FILE   *tmp;
  46.     FILE   *active_file, *new_active_file;
  47.  
  48.  
  49.     fprintf(stderr, "ADDGROUP: (%s)\n\n", VERSION);
  50.  
  51.     if (argc > 1) {
  52.  
  53.         if (!load_stuff()) {
  54.             fprintf(stderr, "Couldn't read rc info\n");
  55.         }
  56.  
  57.         /*
  58.          *  Check for active file.  If there is none:
  59.          *    - create it with a single entry 'junk'.
  60.          *    - ensure that the newsbase directory exists
  61.          *    - create an empty junk text file and index file
  62.          */
  63.         sprintf(buf, "%sactive", my_stuff.news_dir);
  64.         if (access(buf, 0) != 0) {
  65.             active_file = fopen(buf, "wb");
  66.             fprintf(active_file, "junk 00000000 00000000 00000000 y\n");
  67.             fclose(active_file);
  68.         }
  69.  
  70.         /* ensure that the news base file exists - just try to create it */
  71.         sprintf(buf, "%snewsbase", my_stuff.news_dir);
  72.         mkdir(buf);
  73.         sprintf(buf, "%snewsbase\\00000000", my_stuff.news_dir);
  74.         if (access(buf, 0) != 0) {
  75.             if ((tmp = fopen(buf, "wb")) == NULL) {
  76.                 fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  77.                 exit(1);
  78.             }
  79.             fclose(tmp);
  80.         }
  81.         sprintf(buf, "%snewsbase\\00000000.idx", my_stuff.news_dir);
  82.         if (access(buf, 0) != 0) {
  83.             if ((tmp = fopen(buf, "wb")) == NULL) {
  84.                 fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  85.                 exit(1);
  86.             }
  87.             fclose(tmp);
  88.         }
  89.  
  90.         load_active_file();
  91.         close_active();
  92.  
  93.  
  94.         for (i = 1; i < argc; i++) {
  95.  
  96.             /* check that the group doesn't already exist */
  97.             gp = find_news_group(argv[i]);
  98.  
  99.             if ((stricmp(argv[i], "junk") != 0) &&
  100.               (stricmp(gp->group, "junk") == 0)) {
  101.  
  102.                 /* open the active file */
  103.                 sprintf(buf, "%sactive", my_stuff.news_dir);
  104.                 active_file = fopen(buf, "rb");
  105.  
  106.                 /* create new one */
  107.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  108.                 if ((new_active_file = fopen(buf, "wb")) == NULL) {
  109.                     fprintf(stderr, "addgroup: cannot create %s\n", buf);
  110.                     exit(1);
  111.                 }
  112.  
  113.                 added = FALSE;
  114.                 if (active_file != NULL) {
  115.  
  116.                     /* delay ensures names are different - ahem */
  117.                     delay(1000);
  118.                     sprintf(txt_name, "%ld", time(&t));
  119.                     if (strlen(txt_name) > 8) {
  120.                         strcpy(txt_name, txt_name+(strlen(txt_name)-8));
  121.                     }
  122.  
  123.                     /*
  124.                      *  Check newbase name is not already used.  If not
  125.                      *  create an empty newsbase and index files.
  126.                      */
  127.                     sprintf(buf, "%snewsbase\\%s", my_stuff.news_dir, txt_name);
  128.                     if (access(buf, 0) != 0) {
  129.  
  130.                         if ((tmp = fopen(buf, "wb")) == NULL) {
  131.                             fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  132.                             exit(1);
  133.                         }
  134.                         fclose(tmp);
  135.                         sprintf(buf, "%snewsbase\\%s.idx", my_stuff.news_dir, txt_name);
  136.                         if ((tmp = fopen(buf, "wb")) == NULL) {
  137.                             fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  138.                             exit(1);
  139.                         }
  140.                         fclose(tmp);
  141.  
  142.                     } else {
  143.                         fprintf(stderr, "addgroup: newsbase name collision - try again\n");
  144.                         fclose(new_active_file);
  145.                         unlink(buf);
  146.                         exit(1);
  147.                     }
  148.  
  149.                     while (fgets(buf, 255, active_file) != NULL) {
  150.  
  151.                         strcpy(buf2, buf);
  152.                         p = strtok(buf2, " \t");
  153.  
  154.                         /* the active file entry */
  155.                         if ((stricmp(argv[i], p) < 0) && !added) {
  156.                             fprintf(new_active_file, "%s %s 00000000 00000000 y\n",
  157.                                 argv[i], txt_name);
  158.                             added = TRUE;
  159.                         }
  160.  
  161.                         if (fputs(buf, new_active_file) == EOF) {
  162.                             fprintf(stderr, "addgroup: couldn't write new active file\n");
  163.                         }
  164.  
  165.                     }
  166.                 }
  167.  
  168.                 if (!added) {
  169.                     fprintf(new_active_file, "%s %s 00000000 00000000 y\n",
  170.                                 argv[i], txt_name);
  171.                 }
  172.  
  173.                 fclose(active_file);
  174.                 fclose(new_active_file);
  175.  
  176.                 sprintf(buf, "%sactive.bak", my_stuff.news_dir);
  177.                 unlink(buf);
  178.                 sprintf(buf2, "%sactive", my_stuff.news_dir);
  179.                 rename(buf2, buf);
  180.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  181.                 rename(buf, buf2);
  182.  
  183.  
  184.             } else {
  185.                 fprintf(stderr, "addgroup: newsgroup %s already exists\n", argv[i]);
  186.             }
  187.  
  188.         }
  189.  
  190.         close_active_file();
  191.  
  192.     } else {
  193.         printf("usage: addgroup  newsgroup names....\n");
  194.     }
  195.  
  196. }
  197.