home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff281.lzh / MRMan / MRMan.c < prev    next >
C/C++ Source or Header  |  1989-11-20  |  3KB  |  106 lines

  1.  
  2. #include <stdio.h>
  3. #include <libraries/dos.h>
  4. #include <functions.h>
  5.  
  6. char    version[] = "\nMRMan, V1.0 - Mark Rinfret, 1989\n";
  7.  
  8. static char *viewer;
  9.  
  10. extern char *ManSearch();
  11. extern char *GetViewerName();
  12.  
  13. unsigned verbose = 0;
  14.  
  15. main(argc, argv)
  16.     int argc;
  17.     char    **argv;
  18. {
  19.     char    *manFile;
  20.     char    *dirName = NULL;
  21.     char    *subject;
  22.  
  23.     while (--argc > 0) {
  24.         ++argv;                /* Advance to next arg string. */
  25.         if (**argv != '-') break;
  26.         if (! strcmp(*argv, "-d")) {
  27.             --argc;
  28.             ++argv;
  29.             dirName = *argv;
  30.         }
  31.         else if (! strcmp(*argv, "-v")) {
  32.             verbose = 1;
  33.             Printf(version);
  34.         }
  35.         else {
  36.             Printf("Usage: MRMan [-d <directory>] <subject>\n");
  37.             goto done;
  38.         }
  39.     }
  40.     viewer = GetViewerName();
  41.     while (argc--) {
  42.         manFile = ManSearch(dirName, *argv);
  43.         if (manFile) 
  44.             ViewManFile(manFile);
  45.         else
  46.             Printf("Man file for subject '%s' not found.\n", *argv);
  47.         ++argv;
  48.     }
  49. done: ;
  50. }
  51.  
  52. ViewManFile(manFile)
  53.     char *manFile;
  54. {
  55.     static char TEMPDESC[] = "# Temp file name for compressed files (32) ->";
  56.     static char tempName[33] = "T:MRManXXXXXX";
  57.     int         c1, c2;
  58.     char        commandLine[300];
  59.     char        fileName[256];
  60.     struct FileHandle *outFile;
  61.     unsigned    retries;
  62.     
  63.     FILE    *theFile;
  64.  
  65.     theFile = fopen(manFile, "r");
  66.     if (theFile == NULL) {
  67.         Printf("MRMan: '%s' would not open!\n", manFile);
  68.     }
  69.     else {
  70.         /* Is the file compressed?  Look for magic header. */
  71.         c1 = (getc(theFile) & 0xFF);
  72.         c2 = (getc(theFile) & 0xFF);
  73.         fclose(theFile);
  74.         if ((c1 == 0x1F) && (c2 == 0x9D)) {
  75.             mktemp(tempName);
  76.             outFile = (struct FileHandle *) Open(tempName, MODE_NEWFILE);
  77.             if (! outFile) {
  78.                 Printf("MRMan: could not open output for zcat!\n");
  79.                 return;
  80.             }
  81.             if (verbose)
  82.                 Printf("Decompressing...\n");
  83.  
  84.             SyncRun("zcat", manFile, NULL, outFile);
  85.             Close(outFile);
  86. #ifdef undef
  87.             printf("Temporary name = '%s'\n", tempName);
  88. #endif
  89.             SyncRun(viewer, tempName, 0L, 0L);
  90.             /* fexecl(viewer, viewer, tempName, 0L); */
  91.             /* This next bit of messiness is an attempt to accommodate
  92.              * file viewers such as TxView, which detach and run in an
  93.              * asynchronous manner. MRMan will try for up to a minute
  94.              * to delete the temporary file created by zcat.
  95.              */
  96.             for (retries = 0; retries < 60; ++retries) {
  97.                 Delay(50L);
  98.                 if (DeleteFile(tempName)) break;
  99.             }  
  100.             if (retries >= 60)
  101.                 Printf("I couldn't delete '%s'\n", tempName);
  102.         }
  103.         else
  104.             fexecl(viewer, viewer, manFile, 0L);
  105.     }
  106. }