home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / TEXT / UTILITY / DIGEX104.ZIP / DIGEXT.C next >
Encoding:
C/C++ Source or Header  |  1991-08-15  |  12.0 KB  |  382 lines

  1. /*
  2.     digext.c
  3.  
  4.         by
  5.             Alvin Koh
  6.             isakoh@zpcp53.att.com
  7.             27 June 1991
  8.             (with minor, approved Toad Hall Tweaks)
  9.             (and Newt Hall Tweaks (tm), too! :-) )
  10.  
  11.     For extracting digest issues from files (including the INFO-ZIP
  12.     .sma files recently posted). Files may contain more than one
  13.     digest issue. Digests issues are located using the strings defined
  14.     in the lookup table below.
  15.  
  16.     Digest issue files created have names like IZIP091.199.
  17.  
  18.     Notes:
  19.  
  20.     1.  Carriage returns (^M) are not removed from digests. For
  21.         non-MSDOS users, use flip to remove them.
  22.  
  23.     2.  If a digest issue file already exists, it is not overwritten.
  24.  
  25.     3.  Right now, digext recognizes the digests listed in the lookup
  26.         table. Add new digest info if you need to.
  27.  
  28.     4.  One added benefit of digext is that you can use it to rename
  29.         your existing digests.
  30.  
  31.     Copyright (C) 1991, Alvin Koh.  All Rights Reserved.
  32.     Permission is granted to any individual or institution to use, copy, or
  33.     redistribute this software so long as it is not sold for profit,
  34.     provided this copyright notice is retained.
  35.  
  36.     Feel free to enhance this program, but do send me a copy of
  37.     your modifications.
  38.  
  39.  
  40.     History:
  41.     ~~~~~~~~
  42.  
  43.     1.01   27 June 1991  Alvin Koh (isakoh@zpcp53.att.com)
  44.         Creation...
  45.  
  46.     1.02   28 June 1991   David Kirschbaum (kirsch@usasoc.soc.mil)
  47.         Added include files for compilation under Turbo C.
  48.  
  49.     1.03   6 July 1991   Alvin Koh (isakoh@zpcp53.att.com)
  50.         Added lookup table to support various digest types/formats.
  51.         I have only tested digext with INFO-ZIP and UNIX-WIZARDS digests
  52.         so far, as these are the 2 digests I subscribe to currently.
  53.         Please send me your bug reports, enhancements and new formats.
  54.  
  55.     1.04   10 July 1991   Alvin Koh (isakoh@zpcp53.att.com)
  56.         Changed the various *unwieldy* sscanf formats to short and
  57.         more manageable ones. Volume and Issue variable changed from
  58.         char[80] to int.
  59.  
  60.     1.04a  13 August 1991   Cave Newt (roelofs@amelia.nas.nasa.gov)
  61.         Tweaks for VMS/VAXC compilation (didn't like void main; return 
  62.         status must be zero or else interpreted strangely; added wildcard
  63.         code).  Tested a little bit on INFO-ZIP digests.
  64.  
  65. */
  66.  
  67. #include <stdio.h>
  68. #include <string.h>
  69.  
  70. #define VERSION         "1.04a"
  71. #define DATE            "13 August 1991"
  72.  
  73. #ifdef __STDC__
  74. #include <process.h>        /* exit() */
  75. #include <io.h>             /* access() */
  76. #endif
  77.  
  78. #ifdef VMS
  79. #  include <rms.h>
  80. #  include <ssdef.h>
  81. #  include <descrip.h>
  82.    typedef struct direct {
  83.        int d_wild;        /* flag for wildcard vs. non-wild */
  84.        struct FAB fab;
  85.        struct NAM nam;
  86.        char d_qualwildname[NAM$C_MAXRSS + 1];
  87.        char d_name[NAM$C_MAXRSS + 1];
  88.    } DIR;
  89.    int _vms_findfirst(char *, DIR *);
  90.    int _vms_findnext(DIR *);
  91. #endif /* VMS */
  92.  
  93. typedef struct INFO_TABLE_T INFO_TABLE;
  94. struct INFO_TABLE_T {
  95.     char *DIGEST_BEGIN;     /* start of digest */
  96.     char *DIGEST_END;       /* end of digest */
  97.     char *DIGEST_FMT;       /* format of digest name */
  98.     char *DIGEST_SCAN;      /* scanf format to extract volume/issue number */
  99. } info_table[] = {
  100.  
  101.     "INFO-ZIP Digest",                 /* tested by Alvin Koh */
  102.     "End of INFO-ZIP Digest",
  103.     "IZIP%03d.%03d",
  104.     "%*[^V] %*s %d %*c %*s %d",
  105.  
  106.     "Info-IBMPC Digest",               /* tested by Alvin Koh */
  107.     "End of Info-IBMPC Digest",
  108.     "IIBM%03d.%03d",
  109.     "%*[^V] %*s %d %*c %*s %d",
  110.  
  111.     "INFO-C Digest",
  112.     "End of Info-C Digest",
  113.     "IC%03d.%03d",
  114.     "%*[^V] %*s %d %*c %*s %d",
  115.  
  116.     "INFO-WIZARD Digest",
  117.     "End of Info-WIZARD Digest",
  118.     "IWIZ%03d.%03d",
  119.     "%*[^V] %*s %d %*c %*s %d",
  120.  
  121.     "Info-Kermit Digest",              /* tested by Alvin Koh */
  122.     "End of Info-Kermit Digest",
  123.     "IKER%03d.%03d",
  124.     "%*[^V] %*s %d %*c %*s %d",
  125.  
  126.     "UNIX-WIZARDS Digest",             /* tested by Alvin Koh */
  127.     "End of UNIX-WIZARDS Digest",
  128.     "UWIZ%03d.%03d",
  129.     "%*[^V] %*c %d %*c %d",
  130.  
  131.     "VIRUS-L Digest",                  /* tested by Alvin Koh */
  132.     "End of VIRUS-L Digest",
  133.     "VL%03d.%03d",
  134.     "VIRUS-L %*[^V] %*s %d %*c %*s %d",
  135.  
  136.     /* add new info after this line */
  137.  
  138.     /* add new info before this line */
  139.  
  140.     NULL, NULL, NULL, NULL
  141. };
  142.  
  143. #ifdef VAXC
  144. int main(int argc, char *argv[])
  145. #else /* !VAXC */
  146. void main(argc, argv)
  147. int argc;
  148. char *argv[];
  149. #endif /* ?VAXC */
  150. {
  151.     FILE *fi;               /* digest source descriptor */
  152.     FILE *fo;               /* digest file descriptor */
  153.     char s[BUFSIZ];         /* line of text from digest source */
  154.     int Volume;             /* digest volume number */
  155.     int Issue;              /* digest issue number */
  156.     char digest[80];        /* name of digest file */
  157.     int in_digest = 0;      /* 'in digest' flag */
  158.     int i;
  159.     INFO_TABLE *it;         /* ptr for searching info table */
  160.     char digest_id[80];     /* storage for digest name */
  161.     char keyword[80];       /* use for locate 'Digest' keyword */
  162. #ifdef VMS
  163.     DIR d;                  /* struct direct for VMS wildcards */
  164. #endif /* VMS */
  165.  
  166.     if (argc == 1) {
  167.         printf("digext - The Digest Extractor v%s  (%s)\n", VERSION, DATE);
  168.         printf("(C)Copyright 1991, Alvin Koh. All Rights Reserved\n\n");
  169.         printf("Usage: digext file [ file ... ]\n");
  170.         exit(0);
  171.     }
  172.  
  173.     for (i = 1; i < argc; i++) {
  174.  
  175.         /* open raw digest source */
  176. #ifdef VMS
  177.         _vms_findfirst(argv[i], &d);
  178.         while (_vms_findnext(&d) == 0) {
  179.         if ((fi = fopen(d.d_name, "r")) == NULL) {
  180. #else /* !VMS */
  181.         if ((fi = fopen(argv[i], "r")) == NULL) {
  182. #endif /* ?VMS */
  183.             printf("Can't open %s\n", argv[i]);
  184.             continue;
  185.         }
  186.  
  187. #ifdef VMS
  188.         printf("Processing %s ...\n", d.d_name);
  189. #else /* !VMS */
  190.         printf("Processing %s...\n", argv[i]);
  191. #endif /* ?VMS */
  192.  
  193.         /* look for start of digest issue */
  194.         while (fgets(s, BUFSIZ, fi) != NULL) {
  195.  
  196.             /* found it */
  197.             if (!in_digest) {
  198.  
  199.                 /* look for 'Digest' keyword */
  200.                 sscanf(s, "%s %s", digest_id, keyword);
  201.  
  202.                 if (strcmp(keyword, "Digest") != 0)
  203.                     continue;
  204.  
  205.         /* clear keyword to prevent re-loop */
  206.         keyword[0] = '\0';
  207.  
  208.                 /* look up info table */
  209.                 it = info_table;
  210.                 while (it->DIGEST_BEGIN &&
  211.                         strncmp(it->DIGEST_BEGIN, digest_id, strlen(digest_id)))
  212.                     it++;
  213.  
  214.                 if (it->DIGEST_BEGIN == NULL) {
  215.                     printf("No info available for %s Digest\n", digest_id);
  216.                     continue;
  217.                 }
  218.  
  219.                 /* scan for volume/issue number */
  220.                 sscanf(s, it->DIGEST_SCAN, &Volume, &Issue);
  221.  
  222.                 /* form name of digest issue */
  223.                 sprintf(digest, it->DIGEST_FMT, Volume, Issue);
  224.  
  225.                 /* if already exists, skip */
  226.                 if (access(digest, 00) == 0) {
  227.                     printf("\t%s already exists\n", digest);
  228.                     continue;
  229.                 }
  230.                 printf("\tExtracting %s Digest Volume %d Issue %d\n", digest_id, Volume, Issue);
  231.  
  232.                 /* create issue file */
  233.                 if ((fo = fopen(digest, "w")) == NULL) {
  234.                     printf("Can't create %s\n", digest);
  235. #ifdef VMS
  236.                     exit(0);   /* VMS interprets exit statuses annoyingly */
  237. #else /* !VMS */
  238.                     exit(1);
  239. #endif /* ?VMS */
  240.                 }
  241.  
  242.                 /* set 'in digest' flag */
  243.                 in_digest = 1;
  244.             }
  245.  
  246.             /* found end of digest */
  247.             if (in_digest && !strncmp(s, it->DIGEST_END, strlen(it->DIGEST_END))) {
  248.                 /* reset flag */
  249.                 in_digest = 0;
  250.  
  251.                 /* print ending lines */
  252.                 fprintf(fo, "%s", s);
  253.  
  254.                 if (fgets(s, BUFSIZ, fi) != NULL)
  255.                     fprintf(fo, "%s\n", s);
  256.  
  257.                 /* close digest */
  258.                 fclose(fo);
  259.             }
  260.  
  261.             /* inside digest, copy lines */
  262.             if (in_digest)
  263.                 fprintf(fo, "%s", s);
  264.         }
  265.  
  266.         /* close digest source */
  267. #ifdef VMS
  268.         fclose(fi);
  269.         } /* end while (_vms_findnext has more files) */
  270. #else /* !VMS */
  271.         fclose(fi);
  272. #endif /* ?VMS */
  273.     }
  274.  
  275.     printf("Done\n");
  276.     exit(0);
  277. #ifdef VAXC
  278.     return(0);   /* to satisfy the compiler warning about void return type */
  279. #endif
  280. }
  281.  
  282.  
  283.  
  284. #ifdef VMS
  285.  
  286. /*---------------------------------------------------------------------------
  287.  
  288.     _vms_findfirst() and _vms_findnext(), based on public-domain DECUS C
  289.     fwild() and fnext() routines (originally written by Martin Minow, poss-
  290.     ibly modified by Jerry Leichter for bintnxvms.c), were written by Greg
  291.     Roelofs on or around 2 March 1991 and are still in the public domain.
  292.     Routines approximate the behavior of MS-DOS (MSC and Turbo C) findfirst
  293.     and findnext functions.  Most recent modifications, 13 August 1991.
  294.  
  295.   ---------------------------------------------------------------------------*/
  296.  
  297. int _vms_findfirst( char *wildname, DIR *d )
  298. /*
  299.  * Do wildcard setup
  300.  */
  301. {
  302. #ifdef DEBUG
  303.     printf("  findfirst working on DIR ptr %u\n", d);
  304. #endif
  305.     /* set up the FAB and NAM blocks. */
  306.     d->fab = cc$rms_fab;           /* initialize fab */
  307.     d->nam = cc$rms_nam;           /* initialize nam */
  308.  
  309.     d->fab.fab$l_nam = &d->nam;           /* fab -> nam */
  310.     d->fab.fab$l_fna = wildname;       /* argument wild name */
  311.     d->fab.fab$b_fns = strlen(wildname);   /* length */
  312.  
  313.     d->nam.nam$l_esa = d->d_qualwildname;  /* qualified wild name */
  314.     d->nam.nam$b_ess = NAM$C_MAXRSS;       /* max length */
  315.     d->nam.nam$l_rsa = d->d_name;       /* matching file name */
  316.     d->nam.nam$b_rss = NAM$C_MAXRSS;       /* max length */
  317.  
  318.     /* parse the file name */
  319.     if (sys$parse(&d->fab) != RMS$_NORMAL)
  320.         return -1;
  321.  
  322.     /* have qualified wild name (i.e., disk:[dir.subdir]*.*); null-terminate
  323.      * and set wild-flag */
  324.     d->d_qualwildname[d->nam.nam$b_esl] = '\0';
  325.     d->d_wild = (d->nam.nam$l_fnb & NAM$M_WILDCARD)? 1 : 0;   /* not used... */
  326. #ifdef DEBUG
  327.     printf("  incoming wildname:  %s\n", wildname);
  328.     printf("  qualified wildname:  %s\n", d->d_qualwildname);
  329. #endif
  330.  
  331.     return 0;   /* OK */
  332. }
  333.  
  334.  
  335. int _vms_findnext( DIR *d )
  336. /*
  337.  * Match the next valid file.  Return 0 if successful, -1 if failed.
  338.  */
  339. {
  340.     register int errorcode;
  341.  
  342.  
  343. #ifdef DEBUG
  344.     printf("  findnext working on DIR ptr %u\n  %s\n  %s\n", d, 
  345.       d->d_qualwildname, d->d_name);
  346.     printf("    d->d_qualwildname = %u, d->nam.nam$l_esa = %u\n",
  347.       d->d_qualwildname, d->fab.fab$l_nam->nam$l_esa);
  348.     printf("    d->d_name = %u, d->nam.nam$l_rsa = %u\n", d->d_name, 
  349.       d->fab.fab$l_nam->nam$l_rsa);
  350. #endif
  351.     for (;;) {
  352.         d->fab.fab$w_ifi = 0;    /* internal file index:  what does this do? */
  353.  
  354.         /* get next match to possible wildcard */
  355.         if ((errorcode = sys$search(&d->fab)) == RMS$_NORMAL) {
  356.             d->d_name[d->nam.nam$b_rsl] = '\0';   /* null terminate */
  357. #ifdef DEBUG
  358.             printf("  normal:  %s\n", d->d_name);
  359. #endif
  360.             return 0;   /* OK */
  361.         } else if (errorcode == RMS$_PRV) {
  362.             /* sys$search() found something, but we don't have privileges
  363.              * to open it.  Look for another.  (Never seems to occur.) */
  364. #ifdef DEBUG
  365.             d->d_name[d->nam.nam$b_rsl] = '\0';   /* null terminate */
  366.             printf("  protected:  %s\n", d->d_name);
  367. #endif
  368.             continue;
  369.         } else {
  370.             /* can't find a file--should be RMS$_NMF */
  371. #ifdef DEBUG
  372.             d->d_name[d->nam.nam$b_rsl] = '\0';   /* null terminate */
  373.             printf("  error/no more files:  %s , code = %d\n",
  374.               d->d_name, errorcode);
  375. #endif
  376.             return -1;
  377.         }
  378.     }
  379. } /* end function _vms_findnext() */
  380.  
  381. #endif /* VMS */
  382.