home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / MAIL2IND.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  7KB  |  256 lines

  1. /* MAIL2IND creates index files for all *.TXT mail files
  2.  * found in the present directory and all directories below it.
  3.  * (C) 1993 Johan. K. Reinalda, WG7J
  4.  * Freely usable for non-commercial use only !
  5.  *
  6.  * Syntax:
  7.  * ' mail2ind -d<dirname>' sets mail directory to create index files.
  8.  * Eg: mail2ind -d/nos
  9.  *
  10.  * if called with an argument not starting with - ,
  11.  * this is taken to be a file to be indexed. If this file has
  12.  * no extension, the '.txt' is assumed.
  13.  * Eg: mail2ind wg7j
  14.  *     mail2ind wg7j.txt
  15.  *
  16.  * v1.06 - fixed -d again :-(
  17.  * v1.05 - redone for new format of index file. Now call MAIL2IND.EXE
  18.  *         released with jnos 1.10x10 . Added -v option to show index file.
  19.  *         and -v for verbose mode.
  20.  * v1.04 - /spool/areas file is read for message type. (-d option also
  21.  *         works with this)
  22.  * v1.03 - added -? option for help, smtp 'Apparently-to:' handled,
  23.  * v1.02 - bug fix in -d option. (930706)
  24.  * v1.01 - bug fix, no more corrupt index file if message file is empty
  25.  *         or contains no valid messages.  (930629)
  26.  * v1.0  - Initial release, with JNOS 1.10X3 (930622)
  27.  *
  28.  */
  29.   
  30. #include <io.h>
  31. #include <fcntl.h>
  32. #include <dir.h>
  33. #include <ctype.h>
  34. #ifdef MSDOS
  35. #include <dos.h>
  36. #endif
  37. #include "global.h"
  38. #include "mailutil.h"
  39. #include "smtp.h"
  40. #include "files.h"
  41. #include "index.h"
  42.   
  43. #define VERSION "JNOS index files creator v1.06\n" \
  44. "(C) 1993 Johan. K. Reinalda, WG7J\n"
  45.   
  46. #undef fopen
  47.   
  48. char *Mailspool = "/spool/mail";
  49. char *Arealist = "/spool/areas";
  50.   
  51. int found,verbose;
  52.   
  53. void index(char *file) {
  54.     int val;
  55.     char *cp;
  56.   
  57.     found = 1;
  58.     printf("File: %s/%s\n",Mailspool,file);
  59.   
  60.     if((cp = strchr(file,'.')) != NULL)
  61.         *cp = '\0';
  62.   
  63.     if((val=IndexFile(file,verbose)) != 0)
  64.         printf("Error %d occured!\n",val);
  65.   
  66.     return;
  67. }
  68.   
  69. void show_index(char *name) {
  70.     int i,idx;
  71.     char *cp;
  72.     struct indexhdr hdr;
  73.     struct mailindex ind;
  74.     char buf[129];
  75.   
  76.     if((cp=strchr(name,'.')) != NULL)
  77.         *cp = '\0';
  78.     sprintf(buf,"%s/%s.ind",Mailspool,name);
  79.     if((idx = open(buf,READBINARY)) == -1) {
  80.         printf("Index file '%s' not found!\n",buf);
  81.         return;
  82.     }
  83.   
  84.     if(read_header(idx,&hdr) == -1){
  85.         printf("Can not read header from index file '%s'\n",buf);
  86.         return;
  87.     }
  88.   
  89.     printf("%s has %d message%s:\n\n",buf,hdr.msgs,(hdr.msgs == 1 ? "" : ""));
  90.   
  91.     memset(&ind,0,sizeof(ind));
  92.   
  93.     for(i=1;i<=hdr.msgs;i++) {
  94.         printf("Message %d\n",i);
  95.         default_index("",&ind);
  96.         if(read_index(idx,&ind) == -1) {
  97.             puts("Can not read index!");
  98.             break;
  99.         }
  100.         print_index(&ind);
  101.     }
  102.   
  103. }
  104.   
  105. int checkdir(char *path) {
  106.     char *wildcard,*newpath,*fullname;
  107.     struct ffblk ff;
  108.     int done;
  109.   
  110.     if((wildcard = malloc(129)) == NULL) {
  111.         puts("can't allocate 'wildcard'");
  112.         return -1;
  113.     }
  114.     if((newpath = malloc(129)) == NULL) {
  115.         puts("Can't allocate 'newpath'");
  116.         free(wildcard);
  117.         return -1;
  118.     }
  119.     if((fullname = malloc(129)) == NULL) {
  120.         puts("Can't allocate 'fullname'");
  121.         free(wildcard);
  122.         free(newpath);
  123.         return -1;
  124.     }
  125.   
  126.     /* First check all the files */
  127.     if(!path)
  128.         sprintf(wildcard,"%s/*.txt",Mailspool);
  129.     else
  130.         sprintf(wildcard,"%s/%s/*.txt",Mailspool,path);
  131.     done = findfirst(wildcard,&ff,0);
  132.     while(!done){
  133.         if(!path)
  134.             strcpy(fullname,ff.ff_name);
  135.         else
  136.             sprintf(fullname,"%s/%s",path,ff.ff_name);
  137.         index(fullname);
  138.         done = findnext(&ff);
  139.     }
  140.     /* Now check for sub-directories */
  141.     if(!path)
  142.         sprintf(wildcard,"%s/*.*",Mailspool);
  143.     else
  144.         sprintf(wildcard,"%s/%s/*.*",Mailspool,path);
  145.     done = findfirst(wildcard,&ff,FA_DIREC);
  146.     while(!done){
  147.         if(strcmp(ff.ff_name,".") && strcmp(ff.ff_name,"..")) {
  148.             /* Not the present or 'mother' directory, so create new path,
  149.              * and recurs into it.
  150.              */
  151.             if(!path)
  152.                 strcpy(newpath,ff.ff_name);
  153.             else
  154.                 sprintf(newpath,"%s/%s",path,ff.ff_name);
  155.             checkdir(newpath);
  156.         }
  157.         done = findnext(&ff);
  158.     }
  159.     free(wildcard);
  160.     free(newpath);
  161.     free(fullname);
  162.     return 0;
  163. }
  164.   
  165. char HelpTxt[] = "Options:\n"
  166. "<filename>   mailfile to index\n"
  167. "-d<rootdir>  set new root, same as 'NOS -d<dir>\n"
  168. "-s<filename> view index file for <filename>\n"
  169. "-v           verbose (show index before writing it)\n"
  170. "-?,-h,-H     produce this help.";
  171.   
  172. char NewMailspool[129];
  173. char NewArealist[129];
  174.   
  175. void main(int argc,char *argv[]) {
  176.     char *cp;
  177.     int i,len,onefile = 0;
  178.     char fn[129];
  179.   
  180.     puts(VERSION);
  181.   
  182.     if(argc > 1) {
  183.         for(i=1;i<argc;i++) {
  184.             dirformat(argv[1]);
  185.             if(argv[i][0] == '-') {
  186.                 /* Command line options */
  187.                 switch(tolower(argv[i][1])) {
  188.                     case '?':
  189.                     case 'h':
  190.                         puts(HelpTxt);
  191.                         return;
  192.                     case 'd':
  193.                     /* Set the directory to search and index */
  194.                         cp = &argv[i][2];
  195.                         len=strlen(cp);
  196.                         if(len) {
  197.                             if(cp[len-1] == '/' || cp[len] == '\\')
  198.                                 cp[len-1] = '\0';
  199.                         /* Add path in front */
  200.                             strcpy(NewMailspool,cp);
  201.                             strcat(NewMailspool,Mailspool);
  202.                             Mailspool = NewMailspool;
  203.                             strcpy(NewArealist,cp);
  204.                             strcat(NewArealist,Arealist);
  205.                             Arealist = NewArealist;
  206.                         }
  207.                         break;
  208.                     case 'v':
  209.                         verbose = 1;
  210.                         break;
  211.                     case 's':
  212.                         show_index(&argv[i][2]);
  213.                         return;
  214.                         break;
  215.                     default:
  216.                         printf("Invalid option %s\n\n",argv[1]);
  217.                         puts(HelpTxt);
  218.                         return;
  219.                 }
  220.             } else {
  221.                 /* Just one file to do */
  222.                 if((cp=strchr(argv[i],'.')) != NULL)
  223.                     *cp = '\0'; /* Cut off extension */
  224.                 strcpy(fn,argv[i]);
  225.                 strcat(fn,".txt");
  226.                 onefile = 1;
  227.             }
  228.         }
  229.     }
  230.   
  231.     if(onefile) {
  232.         index(fn);
  233.         if(!found)
  234.             printf("Not found: %s\n", fn);
  235.         return;
  236.     }
  237.   
  238.     if(checkdir(NULL))
  239.         puts("ERROR detected, not all index files created!\n");
  240.     if(!found)
  241.         puts("NO files found to index !\n");
  242. }
  243.   
  244. int pwait(void *event) {
  245.     return 0;
  246. }
  247.   
  248. void *
  249. mallocw(nb)
  250. unsigned nb;
  251. {
  252.     return malloc((size_t)nb);
  253. }
  254.   
  255.   
  256.