home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1994 September / Simtel-MSDOS-Sep1994-CD1.iso / disc1 / filedocs / simsrch2.c < prev    next >
C/C++ Source or Header  |  1994-04-20  |  9KB  |  319 lines

  1. /******************************************************************************
  2.  
  3. SIMSERCH:   This program facilitates searching through the SIMIBM.IDX file
  4.             available from the SIMTEL archives.  It supports keyword searches
  5.             within selectable fields, starting and ending date boundaries,
  6.             and user specified index files.  It can also create (or append to)
  7.             a SIMTEL request file which is suitable for e-mailing to the
  8.             SIMTEL list servers.
  9.  
  10.             This program will compile and run under Turbo-C 2.0,
  11.             UNIX ANSI C, and VAX/VMS C.
  12.  
  13.             This program is FREEWARE, and comes with NO WARRANTEES,
  14.             expressed or implied.
  15.  
  16.    Author:  Richard W. Hull <hull@skvax1.csc.ti.com>
  17.  
  18.    Acknowledgements:
  19.             Ed. Mueller and Larry Johnson for providing valuable inputs and
  20.             for insisting that I submit the results to SIMTEL.
  21.  
  22.    Usage:   simserch [?]
  23.                      [-]
  24.                      [-s[fdnx] search-string]
  25.                      [-d[se] start/end date]
  26.                      [-i index-file]
  27.                      [-r[a] simtel-request-file]
  28.  
  29.    Note:    Use "-" on VAX/VMS to list all files (including quotes),
  30.             other systems accept (-) flag w/o quotes.
  31.  
  32.    Modification History:
  33.      V1.0 Original by Richard W. Hull <hull@skvax1.csc.ti.com>
  34.      V1.1 20-Apr-94, J. Mathews  <mathews@nssdca.gsfc.nasa.gov>
  35.                                  Substituted strchr for strstr in most places,
  36.                                  added '-' flag to list all files (default
  37.                                  action w/no args shows usage),
  38.                                  and other minor modifications.
  39.  
  40. ******************************************************************************/
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45.  
  46. #ifndef __TURBOC__
  47. #include <ctype.h>
  48.  
  49. char *strlwr(arg)
  50.   char *arg;
  51. {
  52.    char *arg_save = arg;
  53.  
  54.    while( *arg )
  55.    {
  56.       *arg = tolower( *arg );
  57.       arg++;
  58.    }
  59.    return( arg_save );
  60. }
  61. #endif
  62.  
  63. #ifndef bool
  64. #define bool char
  65. #endif
  66.  
  67. void usage( errcode )
  68.    short errcode;
  69. {
  70.   if (errcode)
  71.     {
  72.       printf( "simserch: ERROR: Invalid %s\n\n",
  73.          (errcode == 1) ? "number of parameters" : "parameter");
  74.     }
  75.  
  76.     printf( "Usage:  simserch [?] [-]\n" );
  77.     printf( "                 [-s[fdnx] search-string]\n" );
  78.     printf( "                 [-i index-file-name]\n" );
  79.     printf( "                 [-d[se] start/end date]\n" );
  80.     printf( "                 [-r[a] simtel-request-file]\n" );
  81.     printf( "\n" );
  82.     printf( "  -s [f search FILE-SYSTEM\n" );
  83.     printf( "      d        DIRECTORY\n" );
  84.     printf( "      n        NAME\n" );
  85.     printf( "      x        DESCRIPTION]\n" );
  86.     printf( "     Default is to search ALL fields.\n" );
  87.     printf( "\n" );
  88.     printf( "  -d [s starting date\n" );
  89.     printf( "      e ending date]\n" );
  90.     printf( "     Default is Starting-Date\n" );
  91.     printf( "     Date format is YYMMDD.\n" );
  92.     printf( "\n" );
  93.     printf( "  -r [a append to Request File]\n" );
  94.     printf( "     Default is to Overwrite Request File.\n" );
  95.  
  96.     exit(0);
  97. }
  98.  
  99. int main(argc, argv)
  100.   int argc; char *argv[]; 
  101. {
  102. FILE  *index_file, *simreq_file;         /* input file */
  103. char  fs[10],dir[60],name[15],descr[60]; /* input variables */
  104. char  inputline[257];                    /* for initial read */
  105. int   rev,bits;                          /* input variables */
  106. long  length,date,sdate,edate;           /* input variables */
  107. char  lfs[10],ldir[60];                  /* stores last filesystem/directory */
  108. char  type;                              /* output variable for 'A' or 'B' */
  109. char  searchstr[40];                     /* string to qualify entries */
  110. bool  search_fs, search_dir,
  111.       search_name, search_desc;          /* indicates which fields to search */
  112. char  teststr[257];                      /* combo of fs,dir,name,descr */
  113. char  ndxfilnam[100];                    /* name of input index file */
  114. char  reqfilnam[100];                    /* SIMTEL request file */
  115. bool  append_to_req;                     /* Flag for opening SIMTEL req file */
  116. long  tot_files, sel_files;              /* total, selected files */
  117. long  tot_ldate, sel_ldate;              /* total, selected last date */
  118. int   i;
  119.  
  120. sdate = 0;
  121. edate = 999999;
  122. searchstr[0] = '\0';
  123. #ifdef VAX11C
  124. strcpy( ndxfilnam, "USER2:[HULL]SIMIBM.IDX" );
  125. #else
  126. strcpy( ndxfilnam, "simibm.idx" );
  127. #endif
  128. reqfilnam[0] = '\0';
  129. tot_files = sel_files = tot_ldate = sel_ldate = 0;
  130.  
  131. if (argc==1) usage(0);             /* no arguments */
  132. if (strchr(argv[1], '?' )) usage(0);
  133.  
  134. for (i=1; i < argc; i++)
  135. {
  136.   if (*argv[i] != '-') usage(2);     /* invalid param */
  137.  
  138.   /*
  139.    *      All parameters require an option argument, except for the '-' flag.
  140.    */
  141.   if (argv[i][1] != 0)
  142.     {
  143.       if (i+1 == argc)
  144.     usage(1);    /*  Error if no option argument is available */
  145.  
  146.       switch (argv[i][1]) {
  147.       case 's':
  148.     search_fs = search_dir = search_name = search_desc = 0;
  149.     strcpy( searchstr, argv[i+1] );
  150.     strlwr( searchstr );
  151.  
  152.     /* Look for mention of specific fields to search */
  153.  
  154.     if (strchr( argv[i], 'f' )) search_fs   = 1;
  155.     if (strchr( argv[i], 'd' )) search_dir  = 1;
  156.     if (strchr( argv[i], 'n' )) search_name = 1;
  157.     if (strchr( argv[i], 'x' )) search_desc = 1;
  158.  
  159.     /* If no specific fields were mentioned, search everything */
  160.  
  161.     if (!(search_fs || search_dir || search_name || search_desc))
  162.       search_fs = search_dir = search_name = search_desc = 1;
  163.     break;
  164.  
  165.       case 'd':
  166.     if (strchr( argv[i], 'e' ))
  167.       {
  168.         sscanf( argv[i+1], "%ld", &edate );
  169.         if (strchr( argv[i], 's' ))
  170.           sdate = edate;
  171.       }
  172.     else
  173.       sscanf( argv[i+1], "%ld", &sdate );    /* default date */
  174.     break;
  175.  
  176.       case 'i':
  177.     strcpy( ndxfilnam, argv[i+1] );
  178.     strlwr( ndxfilnam );
  179.     break;
  180.  
  181.       case 'r':
  182.     strcpy( reqfilnam, argv[i+1] );
  183.     strlwr( reqfilnam );
  184.     append_to_req = (argv[i][2] == 'a');
  185.     break;
  186.  
  187.       default:
  188.     usage(2);    /* invalid param */
  189.       } /* switch */
  190.  
  191.       i++;    /* increment to next argument - skip option argument */
  192.     } /* if */
  193. } /* for */
  194.  
  195. printf("SimTel MS-DOS Files Listing\n\n");
  196.  
  197. if (searchstr[0])
  198. {
  199.    printf( "Will search " );
  200.    if (search_fs)   printf( "FILE-SYSTEM " );
  201.    if (search_dir)  printf( "DIRECTORY " );
  202.    if (search_name) printf( "NAME " );
  203.    if (search_desc) printf( "DESCRIPTION " );
  204.    printf( "for: %s\n", searchstr );
  205. }
  206.  
  207. if (sdate > 0)
  208.    printf( "Starting Date: %ld\n", sdate );
  209.  
  210. if (edate < 999999)
  211. {
  212.   printf( "Ending Date:   %ld\n", edate );
  213. }
  214.  
  215. printf( "Index File to be searched is: %s\n", ndxfilnam );
  216.  
  217. if (!(index_file = fopen( ndxfilnam, "r" )))
  218. {
  219.    printf( "\nCould not open Index File: %s\n", ndxfilnam );
  220.    exit(0);
  221. }
  222.  
  223. if (reqfilnam[0])
  224. {
  225.    char *open_mode;
  226.  
  227.    if (append_to_req)
  228.    {
  229.      open_mode = "a";
  230.      printf( "Selected Files will be appended to: %s\n", reqfilnam );
  231.    }
  232.    else
  233.    {
  234.      open_mode = "w"; 
  235.      printf( "Selected Files will be written to: %s\n", reqfilnam );
  236.    }
  237.  
  238.    if (!(simreq_file = fopen( reqfilnam, open_mode )))
  239.    {
  240.       printf( "\nCould not open Request File: %s\n", reqfilnam );
  241.       exit(0);
  242.    }
  243. }
  244.  
  245. printf("\nNOTE: Type B is Binary; Type A is ASCII\n");
  246.  
  247. inputline[256] = 0;
  248.  
  249. while (fgets(inputline, 256, index_file) != NULL)  {
  250.  
  251.    sscanf(inputline,
  252.             "\"%[^\"]\",\"%[^\"]\",\"%[^\"]\",%d,%ld,%d,%ld,\"%[^\"]\"",
  253.             fs, dir, name, &rev, &length, &bits, &date, descr);
  254.  
  255.    tot_files++;
  256.    if (date > tot_ldate) tot_ldate = date;
  257.  
  258.    if (date < sdate || date > edate) continue;
  259.  
  260.    teststr[0] = '\0';
  261.    if (search_fs)
  262.    {
  263.       strcat( teststr, fs );
  264.       strcat( teststr, " " );
  265.    }
  266.    if (search_dir)
  267.    {
  268.       strcat( teststr, dir );
  269.       strcat( teststr, " " );
  270.    }
  271.    if (search_name)
  272.    {
  273.       strcat( teststr, name );
  274.       strcat( teststr, " " );
  275.    }
  276.    if (search_desc)
  277.    {
  278.       strcat( teststr, descr );
  279.       strcat( teststr, " " );
  280.    }
  281. /*
  282.    sprintf( teststr, "%s %s %s %s", fs, dir, name, descr );
  283. */
  284.    strlwr( teststr );
  285.  
  286.    if (  (searchstr[0]) && !( strstr( teststr, searchstr ) ) )
  287.       continue;
  288.  
  289.    sel_files++;
  290.    if (date > sel_ldate) sel_ldate = date;
  291.  
  292.    type = (bits == 7) ? 'A' : 'B';     /* ASCII  7-bit or Binary 8-bit */
  293.  
  294.    if (strcmp(ldir,dir) || strcmp(lfs,fs)) {  /* New Directory */
  295.       printf("\nDirectory %s%s\n",fs,dir);
  296.       printf(" Filename   Type Length   Date    Description\n");
  297.       printf("==============================================\n");
  298.       strcpy(ldir, dir);          /* Remember last directory with ldir  */
  299.       strcpy(lfs,fs);             /* Remember last file system with lfs */
  300.    }                              /* End of the New Directory routine   */
  301.  
  302.  
  303.    printf("%-12.12s  %c %7ld  %6ld  %s\n",name,type,length,date,descr);
  304.  
  305.    if (reqfilnam[0]) fprintf( simreq_file, "/PDGET %s%s%s UUENCODE\n",
  306.                                              fs, dir, name );
  307.  } /* while */
  308.  
  309. if (reqfilnam[0]) fclose( simreq_file );
  310.  
  311. printf( "\n\nNumber of Files (Total/Processed): %ld / %ld\n",
  312.         tot_files, sel_files );
  313. printf( "Last Date for Files (Total/Processed): %ld / %ld\n",
  314.         tot_ldate, sel_ldate );
  315.  
  316. return (1) ;
  317.  
  318. } /* end of main() */
  319.