home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher+1.2b4 / gopherd / special.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-09  |  4.6 KB  |  197 lines

  1. /********************************************************************
  2.  * lindner
  3.  * 3.3
  4.  * 1993/04/09 15:00:19
  5.  * /home/mudhoney/GopherSrc/CVS/gopher+/gopherd/special.c,v
  6.  * Exp
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: special.c
  14.  * routines to deal with special types of files, compressed, scripts, etc.
  15.  *********************************************************************
  16.  * Revision History:
  17.  * special.c,v
  18.  * Revision 3.3  1993/04/09  15:00:19  lindner
  19.  * Fixes for ask shell scripts, ensure that they're run in the current
  20.  * directory of the script.
  21.  *
  22.  * Revision 3.2  1993/03/24  20:24:23  lindner
  23.  * Addition for compressed file support rfopenz()
  24.  *
  25.  * Revision 3.1.1.1  1993/02/11  18:02:53  lindner
  26.  * Gopher+1.2beta release
  27.  *
  28.  * Revision 1.3  1993/02/09  22:15:36  lindner
  29.  * additions for askfile
  30.  *
  31.  * Revision 1.2  1993/01/30  23:57:44  lindner
  32.  * Additions for ASK block support.
  33.  *
  34.  * Revision 1.1  1992/12/10  23:13:27  lindner
  35.  * gopher 1.1 release
  36.  *
  37.  *
  38.  *********************************************************************/
  39.  
  40. #include "gopherd.h"
  41.  
  42.  
  43. /* Check to see if this file needs special treatment before heading
  44.  * back to the client... We will check for:
  45.  *    Compressed files    if so, zcat first
  46.  *    Shellscript        if so, "do it"
  47.  *                              (add ask block params if exists..)
  48.  * Note: it would be somewhat non-portable to check of a binary
  49.  *  (we'd need to check for so many different magic numbers; the
  50.  *  shell script designation should be sufficient, since the script
  51.  *  can call an executable anyway
  52.  * Recognized elsewhere:
  53.  *    .snd            needs special processing on client
  54.  *    uuencoded        needs special processing on client
  55.  * Other filetypes we could check for:
  56.  *    GIF        ->    Bring up GIF previewer
  57.  *    Postscript    ->    Bring up Postscript previewer
  58.  */
  59.  
  60. static int ispipe;
  61.  
  62. FILE *
  63. specialfile(fp, pathname)
  64.   FILE *fp;
  65.   char *pathname;
  66. {
  67.      FILE *pp;
  68.      char buf[256], s[256], *cp;
  69.      long i;
  70.      
  71.      ispipe = 0;
  72.      
  73.      /* Keep track of where we are */
  74.      i = ftell(fp);
  75.      rewind(fp);
  76.      
  77.      /* Grab the first line or 254 bytes, and rewind */
  78.      if (fgets(s, 255, fp) == NULL)
  79.       return (FILE *)0;
  80.  
  81.      fseek(fp, i, 0);
  82.      
  83.      /* Compressed? */
  84.      if (iscompressed(s)) {
  85.       if (dochroot)
  86.            sprintf(buf, "%s \"%s\"\n", ZCATCMD, pathname);
  87.       else
  88.            sprintf(buf, "%s \"%s/%s\"\n", ZCATCMD, Data_Dir, pathname);
  89.  
  90.       if (! (pp = popen(buf, "r")))
  91.            return (FILE *)0;
  92.       ispipe = 1;
  93.       return pp;
  94.      }
  95.  
  96.      /* Script? */
  97.      if (isshellscript(s)) {
  98.       s[strlen(s)-1] = '\0';
  99.       if (dochroot)
  100.            sprintf(buf, "\"%s\" %s", pathname, (EXECargs == NULL) ? "" : EXECargs);
  101.       else
  102.            sprintf(buf, "\"%s/%s\" %s", Data_Dir, pathname, (EXECargs == NULL) ? "" : EXECargs);
  103.  
  104.       if (ASKfile != NULL) {
  105.            strcat(buf, " < ");
  106.            strcat(buf, ASKfile);
  107.       }
  108.  
  109.       /*
  110.        * Okay, let's change our working directory for the benefit of
  111.        * shell script writers everywhere :-)
  112.        */
  113.  
  114.       strcpy(s, pathname);
  115.       cp = strrchr(s, '/');
  116.       if (cp != NULL) {
  117.            *cp = '\0';
  118.            ;
  119.       } else
  120.            strcpy(s, "/");
  121.       
  122.       rchdir(s);
  123.       
  124.       if (DEBUG)
  125.            fprintf(stderr, "Executing %s\n", buf);
  126.  
  127.       if (! (pp = popen(buf, "r")))
  128.            return (FILE *)0;
  129.       ispipe = 1;
  130.       
  131.       if (DEBUG)
  132.            fprintf(stderr, "Zcat/popen is okay\n");
  133.       
  134.  
  135.       return pp;
  136.      }
  137.  
  138.      return (FILE *)0;
  139. }
  140.  
  141.  
  142. int
  143. iscompressed(s)
  144.   char *s;
  145. {
  146.      /* Magic number stuff for compressed file */
  147.      if ( (s[0] & 0xff ) == 0x1f && ( s[1] & 0xff ) == 0x9d)
  148.       return 1;
  149.      else
  150.       return 0;
  151. }
  152.  
  153. int
  154. isshellscript(s)
  155.   char *s;
  156. {
  157.      if (! strncmp(s, "#!/", 3))
  158.       return 1;
  159.      else
  160.       return 0;
  161. }
  162.  
  163.  
  164. int
  165. Specialclose(fp)
  166.   FILE *fp;
  167. {
  168.      if (ASKfile != NULL)
  169.       unlink(ASKfile);
  170.      if (ispipe)
  171.       return(pclose(fp));
  172.      else
  173.       return(fclose(fp));
  174. }
  175.  
  176. /* Pandora extension: Try opening a .Z file if regular file not found
  177.    or vica versa */
  178. FILE *
  179. rfopenz( filename, type )
  180.   char *filename, *type;
  181. {
  182.      FILE *TheFile;
  183.      if (( TheFile = rfopen( filename,type)) == NULL ) {
  184.       /* Couldnt open the file as is , try the other*/
  185.       if (strcmp(filename + strlen(filename) -2, ".Z") == 0)
  186.            /* Tried compressed, so try regular next */
  187.            filename[strlen(filename) - 2] = '\0';
  188.       else /* Must have been a regular file , try compressed */
  189.            strcat(filename,".Z");
  190.       if (DEBUG)
  191.            fprintf(stderr, "Trying alternative %s", filename);
  192.       return (rfopen( filename,type));
  193.      }
  194.      else return(TheFile);
  195. }
  196.  
  197.