home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / g0bsx / reqdir.c < prev    next >
Text File  |  1989-11-25  |  6KB  |  267 lines

  1. /*
  2.  *  REQDIR.C - Server for the W0RLI MailBox V10.xx
  3.  *
  4.  *  Copyright (C) 1989
  5.  *  Peter Meiring
  6.  *  13 Venn Crescent,
  7.  *  Hartley, Plymouth.
  8.  *  PL3 5PJ
  9.  *
  10.  *  This code may be freely used and copied for non-commercial uses.
  11.  *
  12.  *  s reqdir reqdir.exe reqdir.out H8 reqdir.in H8
  13.  *
  14.  *  See SERVER.DOC for further details.
  15.  *
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <dir.h>
  22. #include <dos.h>
  23. /*
  24.  *  Copyright storage:
  25.  */
  26.  
  27. char *copyright = "REQIR 1.05  Copyright (c) Peter Meiring, G0BSX, 1989.\n";
  28.  
  29. /*
  30.  *  Some buffer space.
  31.  */
  32.  
  33. #define ln_buf 512
  34.  
  35. char buf[ln_buf];
  36.  
  37. /*
  38.  *  End-of-message mark.
  39.  */
  40.  
  41. char *mark    = "/EX\n";
  42. char base[64];  /* storage for the root path of the files section */
  43.  
  44. /*
  45.  *  File used by this program to read initialisation Data
  46.  */
  47.  
  48. char *datfile = "\\reqxxx.dat";
  49. /*
  50.  *  File the MailBox will create, and this server will read.
  51.  */
  52.  
  53. char *infile  = "reqdir.out";
  54.  
  55. /*
  56.  *  File this server will create, and the MailBox will read.
  57.  */
  58.  
  59. char *outfile = "reqdir.in";
  60.  
  61. /* List directory to file */
  62.  
  63.  
  64. stripcr( line )
  65. char *line;
  66. {
  67.   int i;
  68.   for(i=1;i<strlen(line);i++)
  69.     if(line[i]=='\n')
  70.         line[i]='\0' ;
  71. }
  72.  
  73. touc( w )
  74. char *w ;
  75. {  int ch;
  76.    while( (ch = *w ))
  77.       *w++ = toupper(ch) ;
  78. }
  79.  
  80. main(argc, argv)
  81. int argc;
  82. char *argv[];
  83. {
  84.     FILE *in, *out;
  85.     char *st, *tail, *fname, *ptr, path[128] ;
  86.     int cntr = 0 ;
  87.     struct ffblk *fblk ;
  88. /*
  89.     int findfirst(const char *filename,
  90.     struct ffblk *ffblk, int attrib);
  91.     int findnext(struct ffblk *ffblk);
  92.  
  93.     struct ffblk {
  94.         char      ff_reserved[21];
  95.         char      ff_attrib;
  96.         unsigned  ff_ftime;
  97.         unsigned  ff_fdate;
  98.         long      ff_fsize;
  99.         char      ff_name[13];
  100.     }
  101. */
  102. /*
  103.  *   Open and read the initialisation file: reqxxx.dat in the root directory
  104.  */
  105.  
  106.   puts(copyright);
  107.  
  108.   if((in = fopen(datfile, "r")) == NULL) exit(1);
  109.  
  110.   st = fgets(buf, ln_buf, in) ;
  111.  
  112.   while (st != NULL) {
  113.  
  114. /*
  115.  *  skip all comment and blank lines
  116.  */
  117.  
  118.     while( ((*buf == ';') || (*buf == '\n')) && (st != NULL))
  119.       st = fgets(buf, ln_buf, in) ;
  120.  
  121. /*
  122.  *  Extract parameters
  123.  */
  124.     if (!strncmp(buf, "textsdir", 8)) {
  125.         ptr = strchr(buf, ' ');
  126.     while( *ptr == ' ' ) *ptr++ = '\0';
  127.         strcpy( base, ptr);
  128.         stripcr( base );
  129.     }
  130.     st = fgets(buf, ln_buf, in) ;
  131.   }
  132.  
  133.   fclose(in);
  134.  
  135. /*
  136.  *  Open the input file.
  137.  *  If not possible, exit with return code 1 to signal
  138.  *  the MailBox that the server failed.
  139.  */
  140.  
  141.   if ((in = fopen(infile, "r")) == NULL) exit(1);
  142.  
  143. /*
  144.  *  Open the output file.
  145.  *  If not possible, exit with return code 1 to signal
  146.  *  the MailBox that the server failed.
  147.  */
  148.  
  149.   if ((out = fopen(outfile, "w")) == NULL) exit(1);
  150.  
  151.   st = fgets(buf, ln_buf, in);
  152.   while (st != NULL)
  153.   {
  154.  
  155. /*
  156.  *  Read the rfc-822 header placed into the message by the MailBox.
  157.  *  The end of header is marked by a blank line.
  158.  */
  159.  
  160.     while(*buf != '\n')
  161.     {
  162.  
  163. /*
  164.  *  Find end of keyword.
  165.  *  Terminate string at end of keyword.
  166.  */
  167.  
  168.       tail = strchr(buf, ' ');
  169.       *tail++ = '\0';
  170.  
  171. /*
  172.  *  Make the new rfc-822 header.
  173.  */
  174.  
  175.       if      (!strcmp(buf, "To:"))      fprintf(out, "From: %s", tail);
  176.       else if (!strcmp(buf, "From:"))    fprintf(out, "To: %s", tail);
  177.       else if (!strcmp(buf, "Subject:")) {
  178.         fname = tail ;
  179.         if( (tail = strchr( fname, ' ')) != NULL) {
  180.            *tail++ = '\n';
  181.            *tail = '\0' ;
  182.         }
  183.         if( (*fname == '\n') || (*fname == '@') )
  184.            strcpy( fname, "*.*\n" ) ;
  185.         else if( strchr( fname, '\\') == NULL)
  186.            if((strchr(fname, '*') == NULL) && (strchr(fname, '?')==NULL))
  187.               stripcr( fname ) ;
  188.               strcat( fname, "\\*.*\n" ) ;
  189.  
  190.         touc( fname ) ;
  191.         fprintf(out, "Subject: Directory: %s", fname);
  192.       }
  193.       st = fgets(buf, ln_buf, in);
  194.     }
  195.  
  196. /*
  197.  *  Now that the rfc-822 header has been read, and a new one created,
  198.  *  copy the text of the message from the input to the output.
  199.  */
  200.     fputs(buf,out) ;
  201.     stripcr(fname) ;
  202.     cntr = 0 ;
  203.     strcpy( path, base ) ;
  204.     strcat( path, fname ) ;
  205.     touc( path ) ;
  206.     fprintf(out, "Directory: %s", path);
  207.     if( findfirst( path, fblk, FA_DIREC ) == -1 )
  208.        fprintf( out, " not found.");
  209.     else if( fblk->ff_name[0] != '.' ) {
  210.       if( (cntr++ % 3) == 0 )
  211.          fprintf( out, "\n" ) ;
  212.       else
  213.          fprintf( out, "    " ) ;
  214.       fprintf( out, "%-15s", fblk->ff_name);
  215.       if( fblk->ff_attrib == FA_DIREC)
  216.          fprintf( out, "<DIR>") ;
  217.       else
  218.          fprintf( out, "%5d", fblk->ff_fsize );
  219.        }
  220.     while( findnext( fblk ) == 0 )
  221.        if( fblk->ff_name[0] != '.' ) {
  222.       if( (cntr++ % 3) == 0 )
  223.           fprintf( out, "\n" ) ;
  224.       else
  225.           fprintf( out, "    " ) ;
  226.       fprintf( out, "%-15s", fblk->ff_name);
  227.       if( fblk->ff_attrib == FA_DIREC)
  228.          fprintf( out, "<DIR>") ;
  229.       else
  230.          fprintf( out, "%5ld", fblk->ff_fsize );
  231.      }
  232.      fprintf( out, "\n");
  233.  
  234.     while ((st != NULL) && stricmp(buf, mark))
  235.     {
  236.       st = fgets(buf, ln_buf, in);
  237.     }
  238.  
  239. /*
  240.  *  Mark the end of this message.
  241.  *  Go on to the next message.
  242.  */
  243.     fputs("\n", out);
  244.     fputs(mark, out);
  245.     if (st != NULL) st = fgets(buf, ln_buf, in);
  246.   }
  247.  
  248. /*
  249.  *  All the incoming messages have been processed,
  250.  *  and an outgoing message created for each one.
  251.  */
  252.  
  253.   fclose(in);
  254.   fclose(out);
  255.  
  256.   unlink( infile ) ;
  257.  
  258. /*
  259.  *  Exit with return code 0 to signal the MailBox that
  260.  *  the server completed it's processing normally.
  261.  *  The MailBox will then read our output file,
  262.  *  and create messages from it.
  263.  */
  264.  
  265.   exit(0);
  266. }
  267.