home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XT.ZIP / RN / NORMSAVR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-21  |  3.6 KB  |  150 lines

  1. /*
  2.  * This routine implements norm.saver, the rn function that saves an
  3.  * article in normal (not mailbox) format.
  4.  *
  5.  * The Unix version would expect a command line like:
  6.  *
  7.  * <rndir>norm.saver %A %P %c %a %B %C "%b" "From %T <date>"
  8.  *
  9.  * but we expect a command like like:
  10.  *
  11.  * <rndir>/normsavr %P %c %a %B %b
  12.  *
  13.  * because DOS has a 128 character command line length limit (not
  14.  * including the command).
  15.  *
  16.  * (<rndir> is the directory that holds the rn utilities (e.g. d:/lib/uupc/news/rn)
  17.  *  <date> is the date the article was posted (as reformated by rn)
  18.  *  %A  is the full name of the current article (%P/%c/%a)
  19.  *  %a  is the current article number
  20.  *  %B  is the byte offset into the article to begin the save
  21.  *  %b  is the destination file name
  22.  *  %C  is the current newsgroup in dot form (e.g. alt.sex.bondage)
  23.  *  %c  is the current newsgroup in directory form (e.g. alt/sex/bondage)
  24.  *  %P  is the public news spool directory (e.g. d:/lib/uupc/news)
  25.  *  %T  is the poster's name as derived from Path:
  26.  * )
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <io.h>
  32. #include <conio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #ifdef __TURBOC__
  37. #include <dir.h>
  38. #else
  39. #define MAXDIR FILENAME_MAX
  40. #endif
  41.  
  42. #include "config.h"
  43. #include "timestmp.h"
  44.  
  45.  
  46. #define FROMLINE "From: "
  47. #define DATELINE "Date: "
  48.  
  49. int
  50.   main(argc, argv)
  51.    int argc;
  52.    char **argv;
  53. {
  54.  
  55.    char from_art[MAXDIR];
  56.    char *to_art;
  57.    long init_offset;
  58.    char *first_line;
  59.  
  60.    int i;
  61.    int j;
  62.  
  63.    FILE *f_art;
  64.    FILE *f_saved;
  65.    char buff[BUFSIZ];
  66.    char *from_line = NULL;
  67.    char art_date[50];        /* Better be much
  68.                               * shorter */
  69.    char *tstr;
  70.    long cur_pos;
  71.  
  72.    banner(argv);
  73.    printf("norm.saver for rn %s \n", RN_VERSION);
  74.  
  75.    *from_art = '\0';
  76.    strcat(from_art, argv[1]);
  77.    strcat(from_art, "/");
  78.    strcat(from_art, argv[2]);
  79.    strcat(from_art, "/");
  80.    strcat(from_art, argv[3]);
  81.  
  82.    sscanf(argv[4], "%ld", &init_offset);
  83.  
  84.    to_art = argv[5];
  85.  
  86. /* If this were unix we would have been passed the first line
  87.    (the From xxxx -- note, no colon) but since this is a DOS
  88.    system we will have to do the work ourselves.
  89. */
  90.  
  91.    f_art = fopen(from_art, "rb");
  92.    art_date[0] = '\0';
  93.    tstr = " ";
  94.    while (tstr != NULL)
  95.    {
  96.       tstr = fgets(buff, sizeof (buff), f_art);
  97.       if ((buff[0] == '\n') || (buff[0] == '\r') || (buff[0] == '\0') || (buff[0] == ' '))
  98.       {
  99.          break;
  100.       }
  101.       else if (strnicmp(FROMLINE, buff, strlen(FROMLINE)) == 0)
  102.       {
  103.          from_line = malloc((strlen(buff) - strlen(FROMLINE) + 1) * sizeof (char));
  104.          strcpy(from_line, &buff[strlen(FROMLINE)]);
  105.       }
  106.       else if (strnicmp(DATELINE, buff, strlen(DATELINE)) == 0)
  107.       {
  108.          strncpy(art_date, &buff[strlen(DATELINE)], 49);
  109.       }
  110.    }
  111.    if (tstr == NULL)
  112.    {
  113.       /* Something went wrong, bail out */
  114.       fclose(f_art);
  115.       return 1;
  116.    }
  117.  
  118. /* Format the first line */
  119.    tstr = from_line + strlen(from_line) - 1;
  120.    while ((*tstr == '\r') || (*tstr == '\n'))
  121.       tstr--;
  122.    tstr[1] = '\0';
  123.  
  124.    tstr = art_date + strlen(art_date) - 1;
  125.    while ((*tstr == '\r') || (*tstr == '\n'))
  126.       tstr--;
  127.    tstr[1] = '\0';
  128.  
  129.    strcpy(buff, "From ");
  130.    strcat(buff, from_line);
  131.    strcat(buff, " (");
  132.    strcat(buff, art_date);
  133.    strcat(buff, ")");
  134.  
  135.    f_saved = fopen(to_art, "ab");
  136.    fseek(f_art, init_offset, SEEK_SET);
  137.  
  138.    fprintf(f_saved, "%s\n", buff);
  139.  
  140.    while ((i = fread(buff, sizeof (char), sizeof (buff), f_art)) != 0)
  141.    {
  142.       j = fwrite(buff, sizeof (char), i, f_saved);
  143.    }
  144.  
  145.    fclose(f_art);
  146.    fclose(f_saved);
  147.  
  148.    return 0;
  149. }
  150.